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 | import java.util.*;
|
---|
23 |
|
---|
24 | //
|
---|
25 | // Add this file to the HL bin folder. Compile it with the following command:
|
---|
26 | // javac -cp c:\robocode\libs\robocode.jar BattleRunner.java
|
---|
27 | //
|
---|
28 | public class BattleRunner {
|
---|
29 | public static String player = "Evaluation.output*";
|
---|
30 | public static List<Double> score = new ArrayList<Double>();
|
---|
31 |
|
---|
32 | // This program requires at least 1 argument: the name of the robot.
|
---|
33 | // The second argument is the path to the robocode installation. If not give, c:\robocode is assumed.
|
---|
34 | // The third argument is optional. If it is true, robocode is shown, otherwise it is hidden.
|
---|
35 | // The fourth argument is the number of rounds.
|
---|
36 | public static void main(String[] args) {
|
---|
37 | if (args.length < 5)
|
---|
38 | System.exit(-1);
|
---|
39 |
|
---|
40 | String roboCodePath = "C:\\robocode";
|
---|
41 | Boolean visible = false;
|
---|
42 | String bots = "";
|
---|
43 | int numberOfRounds = 3;
|
---|
44 | String[] robots = new String[1 + args.length - 4];
|
---|
45 |
|
---|
46 | player = robots[0] = args[0];
|
---|
47 | roboCodePath = args[1];
|
---|
48 | visible = Boolean.valueOf(args[2]);
|
---|
49 | numberOfRounds = Integer.valueOf(args[3]);
|
---|
50 | for (int i = 4; i < args.length; i++)
|
---|
51 | robots[i - 3] = args[i];
|
---|
52 |
|
---|
53 | RobocodeEngine.setLogMessagesEnabled(false);
|
---|
54 | RobocodeEngine engine = new RobocodeEngine(new java.io.File(roboCodePath));
|
---|
55 | engine.setVisible(visible);
|
---|
56 | engine.addBattleListener(new BattleObserver());
|
---|
57 |
|
---|
58 | // setup the battle specification
|
---|
59 | for (String s : robots)
|
---|
60 | bots += s + ", ";
|
---|
61 | bots = bots.substring(0, bots.length() - 1);
|
---|
62 |
|
---|
63 | BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600);
|
---|
64 | RobotSpecification[] selectedRobots = engine.getLocalRepository(bots);
|
---|
65 |
|
---|
66 | for (int i = 1; i < selectedRobots.length; i++) {
|
---|
67 | BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield,
|
---|
68 | new RobotSpecification[] { selectedRobots[0], selectedRobots[i] });
|
---|
69 |
|
---|
70 | // run our specified battle and wait till the battle finishes
|
---|
71 | engine.runBattle(battleSpec, true);
|
---|
72 | }
|
---|
73 | engine.close();
|
---|
74 |
|
---|
75 | // print out result which is then parsed by HeuristicLab
|
---|
76 | System.out.println(avg(score));
|
---|
77 | System.exit(0);
|
---|
78 | }
|
---|
79 |
|
---|
80 | private static double avg(List <Double> lst) {
|
---|
81 | double sum = 0;
|
---|
82 | for (double val : lst) {
|
---|
83 | sum += val;
|
---|
84 | }
|
---|
85 | return sum / lst.size();
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | //
|
---|
90 | // The battle listener for handling the battle event we are interested in.
|
---|
91 | //
|
---|
92 | class BattleObserver extends BattleAdaptor {
|
---|
93 | public void onBattleCompleted(BattleCompletedEvent e) {
|
---|
94 | double robotScore = -1.0;
|
---|
95 | double opponentScore = - 1.0;
|
---|
96 |
|
---|
97 | for (robocode.BattleResults result : e.getSortedResults()) {
|
---|
98 | if (result.getTeamLeaderName().equals(BattleRunner.player))
|
---|
99 | robotScore = result.getScore();
|
---|
100 | else
|
---|
101 | opponentScore = result.getScore();
|
---|
102 | }
|
---|
103 |
|
---|
104 | //prevent div / 0 which can happen if both robots do not score
|
---|
105 | if((robotScore + opponentScore) == 0) {
|
---|
106 | BattleRunner.score.add(0.0);
|
---|
107 | } else {
|
---|
108 | BattleRunner.score.add(robotScore / (robotScore + opponentScore));
|
---|
109 | }
|
---|
110 | }
|
---|
111 | } |
---|