1 | /* HeuristicLab
|
---|
2 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
3 | *
|
---|
4 | * This file is part of HeuristicLab.
|
---|
5 | *
|
---|
6 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation, either version 3 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | import robocode.control.*;
|
---|
21 | import robocode.control.events.*;
|
---|
22 |
|
---|
23 | //
|
---|
24 | // Add this file to the HL bin folder. Compile it with the following command:
|
---|
25 | // javac -cp c:\robocode\;robocode.jar BattleRunner.java
|
---|
26 | //
|
---|
27 | public class BattleRunner {
|
---|
28 | static String[] defaultRobots = new String[] {
|
---|
29 | "Evaluation.BestSolution*",
|
---|
30 | "sample.Corners",
|
---|
31 | "sample.Crazy",
|
---|
32 | "sample.Fire",
|
---|
33 | "sample.TrackFire",
|
---|
34 | "sample.Walls",
|
---|
35 | "sample.SpinBot",
|
---|
36 | "sample.SittingDuck"
|
---|
37 | };
|
---|
38 | public static String player = "Evaluation.output*";
|
---|
39 | public static int score = 0;
|
---|
40 |
|
---|
41 | //
|
---|
42 | // This program requires at least 1 argument: the name of the robot.
|
---|
43 | // The second argument is the path to the robocode installation. If not give, c:\robocode is assumed.
|
---|
44 | // The third argument is optional. If it is true, robocode is shown, otherwise it is hidden.
|
---|
45 | //
|
---|
46 | public static void main(String[] args) {
|
---|
47 | String roboCodePath = "C:\robocode";
|
---|
48 | Boolean visible = false;
|
---|
49 | String bots = "";
|
---|
50 | int numberOfRounds = 3;
|
---|
51 | String[] robots = new String[defaultRobots.length];
|
---|
52 | robots = defaultRobots.clone();
|
---|
53 |
|
---|
54 | if (args.length == 1)
|
---|
55 | {
|
---|
56 | robots[0] = player = args[0];
|
---|
57 | }
|
---|
58 | else if (args.length == 2)
|
---|
59 | {
|
---|
60 | robots[0] = player = args[0];
|
---|
61 | roboCodePath = args[1];
|
---|
62 | }
|
---|
63 | else if (args.length == 3)
|
---|
64 | {
|
---|
65 | robots[0] = player = args[0];
|
---|
66 | roboCodePath = args[1];
|
---|
67 | visible = Boolean.valueOf(args[2]);
|
---|
68 | }
|
---|
69 | else
|
---|
70 | {
|
---|
71 | System.exit(-1);
|
---|
72 | }
|
---|
73 |
|
---|
74 | RobocodeEngine.setLogMessagesEnabled(false);
|
---|
75 | RobocodeEngine engine = new RobocodeEngine(new java.io.File(roboCodePath));
|
---|
76 | engine.setVisible(visible);
|
---|
77 | engine.addBattleListener(new BattleObserver());
|
---|
78 |
|
---|
79 | // setup the battle specification
|
---|
80 | for (String s : robots)
|
---|
81 | bots += s + ", ";
|
---|
82 | bots = bots.substring(0, bots.length() - 1);
|
---|
83 |
|
---|
84 | BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600);
|
---|
85 | RobotSpecification[] selectedRobots = engine.getLocalRepository(bots);
|
---|
86 |
|
---|
87 | for (int i = 1; i < selectedRobots.length; i++) {
|
---|
88 | BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield,
|
---|
89 | new RobotSpecification[] { selectedRobots[0], selectedRobots[i] });
|
---|
90 |
|
---|
91 | // run our specified battle and wait till the battle finishes
|
---|
92 | engine.runBattle(battleSpec, true);
|
---|
93 | }
|
---|
94 | engine.close();
|
---|
95 |
|
---|
96 | // print out result which is then parsed by HeuristicLab
|
---|
97 | System.out.println(score / (robots.length - 1));
|
---|
98 | System.exit(0);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | //
|
---|
103 | // The battle listener for handling the battle event we are interested in.
|
---|
104 | //
|
---|
105 | class BattleObserver extends BattleAdaptor {
|
---|
106 | public void onBattleCompleted(BattleCompletedEvent e) {
|
---|
107 | for (robocode.BattleResults result : e.getSortedResults()) {
|
---|
108 | if (result.getTeamLeaderName().equals(BattleRunner.player))
|
---|
109 | BattleRunner.score += result.getScore();
|
---|
110 | else
|
---|
111 | BattleRunner.score -= result.getScore();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | } |
---|