Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/15/15 16:39:12 (9 years ago)
Author:
gkronber
Message:

#2069: reviewing and minor changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/BattleRunner.java

    r13010 r13013  
    2727//
    2828public class BattleRunner {
    29     public static String player = "Evaluation.output*";   
    30     public static List<Double> score = new ArrayList<Double>();
     29  public static String player = "Evaluation.output*";   
     30  public static List<Double> score = new ArrayList<Double>();
    3131
    32   // This program requires 5 arguments:
     32  // This program requires 5 arguments:
    3333  // 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.
     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.
    3737  // 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); 
     38  public static void main(String[] args) {
     39    if (args.length < 5)
     40      System.exit(-1); 
    4141
    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];     
     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];
    4747
    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];
     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    }
    5455
    55         RobocodeEngine.setLogMessagesEnabled(false);
    56         RobocodeEngine engine = new RobocodeEngine(new java.io.File(roboCodePath));
    57     engine.setVisible(visible);       
    58         engine.addBattleListener(new BattleObserver());       
     56    RobocodeEngine.setLogMessagesEnabled(false);
     57    RobocodeEngine engine = new RobocodeEngine(new java.io.File(roboCodePath));
     58    engine.setVisible(visible);
     59    engine.addBattleListener(new BattleObserver());
    5960
    60     BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600);
    61         RobotSpecification[] all = engine.getLocalRepository();
    62     List<RobotSpecification> selectedRobots = new ArrayList<RobotSpecification>();
    63    
    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) });
     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    }
    7572
    76             // run our specified battle and wait till the battle finishes
    77             engine.runBattle(battleSpec, true);
    78         }
    79         engine.close();
     73    for (int i = 1; i < selectedRobots.size(); i++) {
     74      BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield, new RobotSpecification[] { selectedRobots.get(0), selectedRobots.get(i) });
    8075
    81     // print out result which is then parsed by HeuristicLab
    82         System.out.println(avg(score));
    83         System.exit(0);
     76      // run our specified battle and wait till the battle finishes
     77      engine.runBattle(battleSpec, true);
    8478    }
    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   }
     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  }
    9393}
    9494
     
    9797//
    9898class 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()) {     
     99  public void onBattleCompleted(BattleCompletedEvent e) {
     100    double robotScore = -1.0;
     101    double opponentScore = -1.0;
     102   
     103        for (robocode.BattleResults result : e.getSortedResults()) {
    104104            if (result.getTeamLeaderName().contains(BattleRunner.player))
    105105                robotScore = result.getScore();
     
    107107                opponentScore = result.getScore();
    108108        }
    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     }
     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));
    116115    }
     116  }
    117117}
Note: See TracChangeset for help on using the changeset viewer.