Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/BattleRunner.java @ 11194

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

#2069 fixed a bug in the BattleRunner

File size: 4.1 KB
RevLine 
[9790]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
[9567]20import robocode.control.*;
21import robocode.control.events.*;
[9884]22import java.util.*;
[9567]23
24//
[9882]25// Add this file to the HL bin folder. Compile it with the following command:
[9891]26// javac -cp c:\robocode\libs\robocode.jar BattleRunner.java
[9567]27//
28public class BattleRunner {
[9882]29    public static String player = "Evaluation.output*";   
[9884]30    public static List<Double> score = new ArrayList<Double>();
[9567]31
[10044]32  // This program requires 5 arguments:
33  // The first argument is the name of the robot.
[9882]34  // The second argument is the path to the robocode installation. If not give, c:\robocode is assumed.
[10044]35  // The third argument: true if Robocode should be shown, otherwise it is hidden.
[9926]36  // The fourth argument is the number of rounds.
[10044]37  // The remaining arguments are the names of the opponents. At least 1 must be provided.
[9926]38  public static void main(String[] args) {
[9947]39    if (args.length < 5)
40      System.exit(-1); 
41
[9884]42    String roboCodePath = "C:\\robocode";
[9882]43    Boolean visible = false;   
44    String bots = "";
45    int numberOfRounds = 3;
[9947]46        String[] robots = new String[1 + args.length - 4];     
[9567]47
[9947]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
[9882]55        RobocodeEngine.setLogMessagesEnabled(false);
56        RobocodeEngine engine = new RobocodeEngine(new java.io.File(roboCodePath));
57    engine.setVisible(visible);       
58        engine.addBattleListener(new BattleObserver());       
59
[9966]60    BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600);
61        RobotSpecification[] all = engine.getLocalRepository();
62    List<RobotSpecification> selectedRobots = new ArrayList<RobotSpecification>();
[9882]63   
[9966]64    for(RobotSpecification rs : all) {
65      for(String r : robots) {
66        if(r.equals(rs.getClassName())) {
67          selectedRobots.add(rs);       
68        }     
69      }
70    }   
71   
72    for (int i = 1; i < selectedRobots.size(); i++) {
73      BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield,
74      new RobotSpecification[] { selectedRobots.get(0), selectedRobots.get(i) });
[9567]75
[9882]76            // run our specified battle and wait till the battle finishes
77            engine.runBattle(battleSpec, true);
[9567]78        }
79        engine.close();
80
[9882]81    // print out result which is then parsed by HeuristicLab
[9884]82        System.out.println(avg(score));
[9567]83        System.exit(0);
84    }
[9884]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  }
[9567]93}
94
95//
[9882]96// The battle listener for handling the battle event we are interested in.
[9567]97//
98class BattleObserver extends BattleAdaptor {
[9884]99  public void onBattleCompleted(BattleCompletedEvent e) {   
100    double robotScore = -1.0;
[10045]101    double opponentScore = -1.0;
[9884]102   
103        for (robocode.BattleResults result : e.getSortedResults()) {     
[10045]104            if (result.getTeamLeaderName().contains(BattleRunner.player))
[9884]105                robotScore = result.getScore();
[9567]106            else
[9884]107                opponentScore = result.getScore();
[9899]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    }
[9567]116    }
117}
Note: See TracBrowser for help on using the repository browser.