Menu

Team of robots : sharing data

Help
Dorian
2014-04-03
2014-04-06
  • Dorian

    Dorian - 2014-04-03

    Hi, I'm new to Robocode,

    I've been trying out stuff related to team battles.
    I have one leader and 4 robots (no droids), the 5 of them are actually born from the same class.
    The leader still is the one in control : it gives order to the other ones, but if it dies, it can give the leadership to another robot.

    Since they are from the same class, I thought they could share data in static fields, but it appears it doesn't work. Seems like static fields only allow a robot to keep data through the different rounds of a battle.

    Is there a way to have fields shared by different bots?
    I know I can share data through messages (broadcastMessage), but, for example, if I want my leader to keep data in a class "FieldMonitor", when it dies, another robot will receive leadership and the instance of FieldMonitor, so it will be able to keep updating it.
    However, next round, leadership will go back to my robotnumber 1 (the one with 200 energy), but everything that was writtent in my FieldMonitor by my subtitute leader will be lost, won't it?

    I guessed my only option was to write my data in a file (an XML should be a good solution), but reading / writing in a file could be a problem as it will make my code heavier and might change my bot's division (nano bot, big bot, mega bot, etc...), so any other solution could come in handy.

    Thanks in advance !
    (PS : I hope I made myself clear, I'm not an english native speaker !)

     
  • Flemming N. Larsen

    Hi Dorian,

    Static data fields are cleaned (set to null, false, 0 depending on the field type) by Robocode between each round. This is done in order to prevent memory leaks, e.g. when robots uses large data sets with circular references, which causes memory leaks and severe performance problems, e.g. with RoboRumble when running thousands of battles.

    I recommend that the current 'master' robot is responsible for saving the necessary fields using the RobocodeFileOutputStream which you can create based on the a File you get from getDataFile(String fileName).
    The 'master' needs to save the content of your crucial static fields in the end of a round.

    I am not sure, but I guess very small Robocode bots like NanoBot do not save data between rounds as they'll get too big. Saving data between rounds is typically done by more advanced bots (MegaBots).

    I recommend that you ask this question on the RoboWiki where lots of experienced Robocoders share tips and tricks, code snippets, strategies etc. If no discussion exists about sharing data between robots, then you could start a new one. :-)

     

    Last edit: Flemming N. Larsen 2014-04-03
  • Flemming N. Larsen

    Just a quick follow-up. It seems that another Robocoder has already written an article about the topic here. The discussion page for this article is even more interesting. ;-)

     
  • Dorian

    Dorian - 2014-04-03

    Thanks for your answer,

    I didn't know about RobocodeFileOutputStream wich might come in handy :)
    I guess that's what I'll need to use here.

    I'm gonna look around the pages you gave me though.

    However, I did check that static fields keep their values between rounds (but not battles, of course). That's what is said on your links too.
    They also explain why and how static fields are purposedly not shared between bots of the same class, wich was quite an interesting thing to look up as I just learned a new way around a problem I encountered on an unrelated project :)

     

    Last edit: Dorian 2014-04-03
    • Flemming N. Larsen

      Yes, the articles from the RoboWiki er right. :-)
      Robocode is cleaning static fields by removing the entire class (as an object) from the Robocode ClassLoader (between battles).
      Static fields must be cleaned in order to removed the class from the class loader. If the class is not removed successfully, it will cause a memory leak.

       

      Last edit: Flemming N. Larsen 2014-04-04
  • Dorian

    Dorian - 2014-04-06

    Hmmmm, still working on my team of robots, I noticed that in the "onDeath(DeathEvent e)" function, we can't seem to use sendMessage or broadcastMessage anymore.

    Seems like dead bots tell no tales !

    Is there any mean for my bot to send data just before dying?

    I would prefer not to take any chance with something like "if(energy < [low_number])", because failing to foresee the death of my leader (i.e. if it takes more damage [low_number] in one turn) would basically mean loosing the round, and raising the low number would make my team less efficient on hard fought battles.

    Would something like sending the Data every single turn to the robot with the most energy in case my leader could die be reasonnable ? (The data is a serrializable Object including other Objects and Arrays for a total of something like 40 fields of java standard data types. I don't think that's a relevant info though)

     
  • Flemming N. Larsen

    You are right. When a robot dies in Robocode, it is dead and can't help its team mates.

    One way to handle your situation is to let all robots in the team broadcast their data in the start of the turn with data like x and y coordinate etc. If the other robots does not receive this information, they could consider the robot dead - as the robot did not broadcast its data. :-)

     

    Last edit: Flemming N. Larsen 2014-04-06

Log in to post a comment.