IBM’s Robocode: A Platform for Learning AI

By ai-depot | January 5, 2003

This platform is not only a fun tool for learning how to program in Java, but is also a powerful environment for testing out AI concepts. This article is an introduction to the platform and how it
can be used for AI programming.

Written by Nick Loadholtes.

IBM’s Robocode: A Platform for Learning AI

With the release of its Robocode platform, IBM has presented developers with a platform that not only allows them to learn (or refine) their skills in the Java programming language, but also a platform which provides an arena for testing artificial intelligence (AI) concepts.

The basic concept behind Robocode is that there is an arena where software robots (in the form of little tanks) can battle against each other. Each robot is given the same equipment (a gun, a radar, the ability to move, etc.) and has the same constraints (ammunition, energy, the heat of the barrel) but how the resources is used is up to the programmer who actually codes the robot. This framework allows developers to quickly code and test out ideas in a very standard manner. When a developer feels the robot has reach a good stage (i.e. it is able to win lots of battles against other robots), he or she can submit the robot code to compete in on-line competitions.

Java for Robocode

When competing in a battle, the robots are autonomous in that they do not receive input from the user. They react to the environment around them in the way they were programmed. For example, when a moving robot hits a wall, the class that controls the robot will be notified. If the developer has chosen to handle this situation, the appropriate method will be called and the robot will take action (such as going in reverse and turning). This environment is perfect for trying out AI concepts, it allows a developer to quickly try out ideas without having to worry excessively about the testing environment.

The coding of a robot is done in Java. Developers who have not coded in Java before should not be scared off by this, in fact the Robocode platform was designed to teach basic programming to neophyte developers. There are tutorials and sample code to show all stages of robot development, from the beginning steps (including how to compile a robots) to more advanced techniques for more experienced developers who would like to learn Java in a more in-depth manner. Additionally there is a way to program teams of robots, however that is beyond the scope of this article.

The basic “skeleton” of a robot is provided by the class Robot. (More advanced features are available by using the base class of AdvancedRobot. This article will focus on the basic base class of Robot for clarity.) Figure 1 shows a screenshot of the Robocode editor in action. It shows what is seen when first creating a robot. As you can see there is a method called onScannedRobot that, for this robot, will fire the gun when it spots another robot. The method that comes after that one is called onHitByBullet and for this robot it will turn left some amount (based on the direction it was hit from).

Robocode Simulation

These two methods are examples of methods a programmer would implement to handle events (such as being shot, or hitting a wall). The API documentation for Robocode (which as a side note is very helpful and well done) details all of the possible “events” that the robot can be made aware of. Within the methods implemented to handle these “events” is where the real magic can happen. Or in the event of a robot that doesn’t win a battle, not happen.

For example, if a robot gets shot but does not have a method to handle that situation (by either fleeing or turning to attack the attacker, etc.) then that robot is in danger of being killed quickly. Likewise, if a robot has a poor scanning strategy it may fail to see other robots that it could attack. That would give those robots a chance to attack it. Figure 2 shows a screen shot of a battle in action. The red robot (labeled “FirstRobot”) is one that I coded. It has poor movement code, so when it turns to attack it usually overshoots the target. As a result, my robot does not always win.

Figure 3 is a screenshot of the summary that is given after a battle. In this case, the battle was 10 rounds long with 4 robots competing. There are several categories where the robots are scored. These scores are combined to come up an overall score which is then used to rank the robots. If you look at the 3rd place finisher the “RamFire” robot, you will notice that it scored a large number of points in the “Ram Dmg*2″ column. This column is the points that are awarded to a robot for ramming another robot (intentionally or not). The RamFire robot is designed to run into the robots it is attacking, and as a result it has 368 points there.

Robot Statistics

The RamBot is a perfect example of an AI strategy: Scan for a new target, when the target is found, move towards it while shooting. A very simple strategy for attacking. However, looking at the code for RamFire we see that there is only an attack strategy, and not a defense strategy. This is why the RamFire robot never survived for very long (its best was making into the final three robots), it is lopsided in its approach to the battle.

And there are many, many, many approaches to battling in the Robocode arena. From looking at Figure 3, you can see that my robot, njl.FirstRobot, didn’t fare too well. FirstRobot is a mish-mash of ideas. (The source for FirstRobot is available at the end of the article.) In the beginning it was much like the other robots it was battling against, but over time I made changes (such as trying a state-machine approach). At this time the changes obviously need to be tweaked, as my robot is doing ok (it is usually one of the top 3 survivors per round), but it could do a lot better (its low bullet damage score shows that the logic for firing and aiming the gun needs to be improved).

And that is the beauty of the Robocode platform. The ability to quickly code an idea into an autonomous robot, the ability to quickly and painlessly test out the idea, and the ability to make changes and repeat quickly and easily. For someone who is just getting started in AI programming, Robocode is a great way to make a lot of progress in a short amount of time. The statistics provided at the end of a battle combined with the ability to run the battles quickly (by minimizing the window) allows a developer to not only try out an idea, but to gather data and metrics that can be used to evaluate the performance of the robot.

On IBM’s Robocode website there are links to all things Robocode on the web, including the source files to robots that have won competitions in on-line battles. These robots are available for download and can be studied to learn what makes them tick. These robots are a valuable resource as they allow you to get a jump-start on your robot development.

In summary, the Robocode platform (which is a free download from IBM’s Robocode website) is an ideal tool to use for AI programming in addition to learning the basics of Java programming.

IBM Robocode website
The Robocode Repository
The Robocode Rumble Site

Written by Nick Loadholtes.

Tags: none
Category: review |

One Response to “IBM’s Robocode: A Platform for Learning AI”

  1. Robocode | CS-SIIT Says:
    February 18th, 2008 at 9:55 am

    […] http://ai-depot.com/articles/ibms-robocode-a-platform-for-learning-ai/ […]

Comments