Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/External Evaluator/BattleRunner.java @ 9891

Last change on this file since 9891 was 9891, checked in by ascheibe, 11 years ago

#2069 moved battle runner from Robocode directory to HL bin

File size: 4.4 KB
Line 
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
20import robocode.control.*;
21import robocode.control.events.*;
22import 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//
28public class BattleRunner {
29    static String[] defaultRobots = new String[] {
30        "Evaluation.BestSolution*",
31        "sample.Corners",
32        "sample.Crazy",
33        "sample.Fire",
34        "sample.TrackFire",
35        "sample.Walls",
36        "sample.SpinBot",
37        "sample.SittingDuck"
38    };
39    public static String player = "Evaluation.output*";   
40    public static List<Double> score = new ArrayList<Double>();
41
42  //
43  // This program requires at least 1 argument: the name of the robot.
44  // The second argument is the path to the robocode installation. If not give, c:\robocode is assumed.
45  // The third argument is optional. If it is true, robocode is shown, otherwise it is hidden.
46  //
47    public static void main(String[] args) {
48    String roboCodePath = "C:\\robocode";
49    Boolean visible = false;   
50    String bots = "";
51    int numberOfRounds = 3;
52        String[] robots = new String[defaultRobots.length];
53        robots = defaultRobots.clone();   
54 
55    if (args.length == 1)
56        {
57            robots[0] = args[0];           
58      player = args[0];           
59        }
60        else if (args.length == 2)
61        {
62            robots[0] = args[0];           
63      player = args[0];       
64      roboCodePath = args[1];           
65        }
66    else if (args.length == 3)
67        {
68            robots[0] = args[0];           
69      player = args[0];       
70      roboCodePath = args[1];
71            visible = Boolean.valueOf(args[2]);
72        }
73        else
74    {
75            System.exit(-1);
76    }   
77
78        RobocodeEngine.setLogMessagesEnabled(false);
79        RobocodeEngine engine = new RobocodeEngine(new java.io.File(roboCodePath));
80    engine.setVisible(visible);       
81        engine.addBattleListener(new BattleObserver());       
82
83     // setup the battle specification 
84        for (String s : robots)
85            bots += s + ", ";
86        bots = bots.substring(0, bots.length() - 1);
87   
88        BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600);
89        RobotSpecification[] selectedRobots = engine.getLocalRepository(bots);
90
91        for (int i = 1; i < selectedRobots.length; i++) {
92            BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield,
93      new RobotSpecification[] { selectedRobots[0], selectedRobots[i] });
94
95            // run our specified battle and wait till the battle finishes
96            engine.runBattle(battleSpec, true);
97        }
98        engine.close();
99
100    // print out result which is then parsed by HeuristicLab
101        System.out.println(avg(score));
102        System.exit(0);
103    }
104 
105  private static double avg(List <Double> lst) {
106    double sum = 0;
107    for (double val : lst) {
108      sum += val;
109    }
110    return sum / lst.size();
111  }
112}
113
114//
115// The battle listener for handling the battle event we are interested in.
116//
117class BattleObserver extends BattleAdaptor {
118  public void onBattleCompleted(BattleCompletedEvent e) {   
119    double robotScore = -1.0;
120    double opponentScore = - 1.0;
121   
122        for (robocode.BattleResults result : e.getSortedResults()) {     
123            if (result.getTeamLeaderName().equals(BattleRunner.player))
124                robotScore = result.getScore();
125            else
126                opponentScore = result.getScore();
127        }   
128    BattleRunner.score.add(robotScore / (robotScore + opponentScore));   
129    }
130}
Note: See TracBrowser for help on using the repository browser.