1 | /* HeuristicLab
|
---|
2 | * Copyright (C) 2002-2016 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 5 arguments:
|
---|
33 | // The first argument is the name of the robot.
|
---|
34 | // The second argument is the path to the robocode installation. If not give, c:\robocode is assumed.
|
---|
35 | // The third argument: true if Robocode should be shown, otherwise it is hidden.
|
---|
36 | // The fourth argument is the number of rounds.
|
---|
37 | // The remaining arguments are the names of the opponents. At least 1 must be provided.
|
---|
38 | public static void main(String[] args) {
|
---|
39 | if (args.length < 5)
|
---|
40 | System.exit(-1);
|
---|
41 |
|
---|
42 | String roboCodePath = "C:\\robocode";
|
---|
43 | Boolean visible = false;
|
---|
44 | String bots = "";
|
---|
45 | int numberOfRounds = 3;
|
---|
46 | String[] robots = new String[1 + args.length - 4];
|
---|
47 |
|
---|
48 | player = robots[0] = args[0];
|
---|
49 | roboCodePath = args[1];
|
---|
50 | visible = Boolean.valueOf(args[2]);
|
---|
51 | numberOfRounds = Integer.valueOf(args[3]);
|
---|
52 | for (int i = 4; i < args.length; i++) {
|
---|
53 | robots[i - 3] = args[i];
|
---|
54 | }
|
---|
55 |
|
---|
56 | RobocodeEngine.setLogMessagesEnabled(false);
|
---|
57 | RobocodeEngine engine = new RobocodeEngine(new java.io.File(roboCodePath));
|
---|
58 | engine.setVisible(visible);
|
---|
59 | engine.addBattleListener(new BattleObserver());
|
---|
60 |
|
---|
61 | BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600);
|
---|
62 | RobotSpecification[] all = engine.getLocalRepository();
|
---|
63 | List<RobotSpecification> selectedRobots = new ArrayList<RobotSpecification>();
|
---|
64 |
|
---|
65 | for(RobotSpecification rs : all) {
|
---|
66 | for(String r : robots) {
|
---|
67 | if(r.equals(rs.getClassName())) {
|
---|
68 | selectedRobots.add(rs);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | for (int i = 1; i < selectedRobots.size(); i++) {
|
---|
74 | BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield, new RobotSpecification[] { selectedRobots.get(0), selectedRobots.get(i) });
|
---|
75 |
|
---|
76 | // run our specified battle and wait till the battle finishes
|
---|
77 | engine.runBattle(battleSpec, true);
|
---|
78 | }
|
---|
79 | engine.close();
|
---|
80 |
|
---|
81 | // print out result which is then parsed by HeuristicLab
|
---|
82 | System.out.println(avg(score));
|
---|
83 | System.exit(0);
|
---|
84 | }
|
---|
85 |
|
---|
86 | private static double avg(List <Double> lst) {
|
---|
87 | double sum = 0;
|
---|
88 | for (double val : lst) {
|
---|
89 | sum += val;
|
---|
90 | }
|
---|
91 | return sum / lst.size();
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | //
|
---|
96 | // The battle listener for handling the battle event we are interested in.
|
---|
97 | //
|
---|
98 | class BattleObserver extends BattleAdaptor {
|
---|
99 | public void onBattleCompleted(BattleCompletedEvent e) {
|
---|
100 | double robotScore = -1.0;
|
---|
101 | double opponentScore = -1.0;
|
---|
102 |
|
---|
103 | for (robocode.BattleResults result : e.getSortedResults()) {
|
---|
104 | if (result.getTeamLeaderName().contains(BattleRunner.player))
|
---|
105 | robotScore = result.getScore();
|
---|
106 | else
|
---|
107 | opponentScore = result.getScore();
|
---|
108 | }
|
---|
109 |
|
---|
110 | //prevent div / 0 which can happen if both robots do not score
|
---|
111 | if((robotScore + opponentScore) == 0) {
|
---|
112 | BattleRunner.score.add(0.0);
|
---|
113 | } else {
|
---|
114 | BattleRunner.score.add(robotScore / (robotScore + opponentScore));
|
---|
115 | }
|
---|
116 | }
|
---|
117 | } |
---|