• 沒有找到結果。

How to Enter the Contest

在文檔中 Webots User Guide (頁 127-132)

Figure 8.4: First prize: a Khepera II robot and a Webots PRO package.

8.4 How to Enter the Contest

If you are willing to challenge the other competitors of the contest, here is the detailed procedure on how to enter the ALife contest. You will need either a Windows or a Linux machine to program your robot controller.

8.4.1 Obtaining the software

All the software for running the contest may be obtained free of charge.

• The Webots software to be used for the contest is available from the Webots download page3. This is an evaluation version of Webots which contains all the necessary material to develop a robot controller for the contest, except the Java environment. Follow the instructions on the Webots download page to install the Webots package.

• The Java 2 Standard Edition (J2SE) Software Development Kit (SDK) may be downloaded from Sun web site4for free. Please use the version 1.4 of the SDK. Follow the instructions from Sun to install the SDK.

3http://www.cyberbotics.com/products/webots/download.html

4http://java.sun.com/j2se/1.4/download.html

128 CHAPTER 8. ALIFE CONTEST

8.4.2 Running the software

Launch Webots and open the world namedalife.wbt. Click on therunto start the simulation.

You will see two robots moving around in the world. Each robot is controlled by a Java program named respectively ALife0and ALife1 located in the Webotscontrollers directory. You may enter their directory and have a look a the source code of the programs.

8.4.3 Creating your own robot controller

The simplest way to create your own robot controller is to start from the existing ALife0 or ALife1controllers.

Installation

It is safer and cleaner to install a local copy of the material you will need to modify while developing your intelligent controller. Here is how to proceed:

1. Create a working directory which you will store all your developments. Let’s call this directory my alife. It may be in your Linux home directory or in your Windows My Documentsdirectory or somewhere else.

2. Enter this directory and create two subdirectories calledcontrollersandworlds. 3. Copy the filealife.wbtfrom the Webotsworldsdirectory to your ownworldsyou just

created. Copy also the thealifedirectory and all its contents from the Webots worlds directory to your ownworlds directory. You may replace the imagesAlife0.png and Alife1.pngin the alifedirectory by your own custom images. These images are ac-tually texture flags associated to the robots. Their size must be 64x64 pixels with 24 or 32 bits depth. They should not represent a green rectangle, possibly faking the face of a charger and hence confusing the opponent. If a flag appears to be a charger fake, it will be removed.

4. Copy the wholeALife0directory from the Webotscontrollersdirectory to your own controllers directory you just created. Repeat this with theALife1 directory. This way you could modify the example controllers without loosing the original files.

5. In order to indicate Webots where the files are, launch Webots, go to theFile menu and select the Preferences... menu item to open the Preferences window. Select the Files and paths tab. Set alife.wbt as the Default world and indicate the absolute path to yourmy alifedirectory, which may be/home/myname/my alife on Linux orC:\My Documents\my alifeon Windows.

From there, you can modify the source code of the controllers in yourcontrollersdirectory, recompile them and test them with Webots.

8.4. HOW TO ENTER THE CONTEST 129 Modifying and Compiling your controller

If you know a little bit of Java, it won’t be difficult to understand the source code of theALife0 andALife1controllers, which are stored respectively in theALife0.javaandALife1.java. You may use any standard Java objects provided with the Java SDK. The documentation for the Controller class is actually the same as for the C programming interface, since all the methods of theControllerclass are similar to the C functions of the Controller API described in the Webots Reference Manual, except for one function,robot livewhich is useless in Java.

Before modifying a controller, it is recommended to try to compile the copy of the original controllers.

To compile theALife0controller, just go to theALife0directory and type the following on the command line:

javac -classpath "C:\Program Files\Webots\lib\Controller.jar;." ALife0.java on Windows.

javac -classpath "/usr/local/webots/lib/Controller.jar:." ALife0.javaon Linux.

If everything goes well, it should produce a newALife0.classfile that will be used by Webots next time you launch it (or reload thealife.wbtworld).

Now, you can start developing! Edit the ALife0.java, add lines of code, methods, objects.

You may also create other files for other objects that will be used by the ALife0 class. Test your controller in Webots to see if it performs well and improve it as long as you think it is necessary.

8.4.4 Submitting your controller to the ALife contest

Once you think you have a good, working controller for your robot, you can submit it to the on-line contest. In order to proceed, you will have to find a name for your robot. Let’s say ”MyBot”

(but please, choose another name). Copy yourALife0.javato a file namedMyBot.java. Edit this new file and replace the line:

public abstract class ALife0 extends Controller { by:

public abstract class MyBot extends Controller {

Save the modified file and compile it using a similar command line as seen previously. You should get a MyBot.class file that you could not test, but that will behave the same way as ALife0.class.

Register to the contest from the main contest web page5, providing ”MyBot” as the name of the robot. Then, upload all the necessary files in your MyBot directory. This includes the following:

5http.//www.cyberbotics.com/contest

130 CHAPTER 8. ALIFE CONTEST

• MyBot.classfile and possibly some other.classfiles corresponding to other java ob-jects you created (it is useless to upload theALife0.classfile)

• A text file named description.txt of about 10 lines that may include some HTML tags, like hyperlinks.

• A PNG image namedflag.pngthat will be used as a texture to decorate your robot, so that you can recognize it from the webcam. This image should be a 64x64 pixels with a bit depth of 24 or 32. It should not represent a green rectangle, trying to fake the face of a charger, otherwise it will be cancelled.

That’s it. Once this material uploaded, your robot will automatically enter the competition with an initial score of 10. A contest supervisor program will use you controller to run matches and update your score and position in the hall of fame. You can check regularly the contest web site to see how your robot performs.

8.4.5 Analysing the performance and improving your competing controller

Match movies

During each round, a number of match movies are generated and stored in theresultsdirectory of the contest home page. These files can be played back with Webots. Just download them and save them in thealife playbackdirectory which lies in the Webotscontrollersdirectory.

Rename the file to match.dat (overwriting the existing match.dat file) and open the world namedalife playback.wbtwith Webots. You should then see the match playback running.

To know who was the winner in a.datfile, just look at the two bottom lines of the file. If the last line ends with 0, then the first robot wins (i.e., its name is displayed on the first line of the file). Otherwise the second robot wins.

Debug and error log

In order to debug your program, or at least to understand what went wrong or right during a round match, you can save data into a log file. This will help you developing your controller, especially on Windows where the DOS console closes immediately after a controller crashes and doesn’t let you read the printed messages in this console. Moreover, it may also be useful to do it during the contest matches running on the match server to understand exactly how your controller behaved during a contest match. Your log file can be retrieved from the match server after the round completed as a zipped file.

To proceed, you first need to create such a log file and then log useful information using the printlnstatement:

8.4. HOW TO ENTER THE CONTEST 131 import java.io.*;

...

PrintStream log;

FileOutputStream file;

try {

file = new FileOutputStream("log.txt");

log = new PrintStream(file);

} catch (Exception e) { } ...

log.println("My estimated coords: ("+x+","+y+") my state="+state);

...

log.println("My energy level: "+energy);

...

log.close();

file.close();

...

During each round, for each competitor using this log file facility, a log file called log.zipis stored in the controller directory of thecompetitorsdirectory of the contest home page. This file is the compressed version of yourlog.txtfile. It contains all the debug messages produced by your controller along the different matches of the last round. Please note that this log file will be visible by all the other competitors, so be cautious and don’t reveal your secret algorithms.

Also useful, in the resultsdirectory, a file called errors.zip contains the error log of the last round, which may be useful to detect if your controller crashed, producing a java exception.

Note that these files are erased at the beginning of each new round and replaced by new ones corresponding to the new round.

Robot memory

It may be useful for your robot to store some data corresponding to knowledge aquired across the different matches. Such data should be saved regularly during a normal run or, if you prefer, just when the controller energy reaches a small value (like below 3), that is the match is about to complete. The data can be in turn re-read by the controller when it starts up a new match, to refresh its memory. Here is how to implement it:

import java.io.*;

// to create/write into the file Random r = new Random();

try {

DataOutputStream s;

s=new DataOutputStream(new FileOutputStream("memory.dat"));

s.writeInt(100); // save 100 int

132 CHAPTER 8. ALIFE CONTEST for (int i=0; i<100;i++) s.writeInt(r.nextInt(100));

// you should rather save some useful info // here instead of random garbage!

} catch (Exception e) {

e.printStackTrace(System.out);

}

// to read from that file try {

DataInputStream s =

new DataInputStream(new FileInputStream("memory.dat"));

int t = s.readInt(); // read the size of the data int[] a = new int[t];

for(int i=0; i<t; i++) a[i] = s.readInt(); // read back my garbage for (int i=0; i<t; i++) System.out.print(a[i]+"\t" );

} catch (Exception e) {

e.printStackTrace(System.out);

}

Thememory.datfile of each competitor is also made available for download to all competitors on the contest web site. This file is stored at the same place as thelog.zipfile, that is, within the controller directory of thecompetitorsdirectory on the contest web site.

在文檔中 Webots User Guide (頁 127-132)

相關文件