- Timestamp:
- 10/15/15 16:39:12 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/BattleRunner.java
r13010 r13013 27 27 // 28 28 public class BattleRunner { 29 30 29 public static String player = "Evaluation.output*"; 30 public static List<Double> score = new ArrayList<Double>(); 31 31 32 32 // This program requires 5 arguments: 33 33 // The first argument is the name of the robot. 34 35 36 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 37 // The remaining arguments are the names of the opponents. At least 1 must be provided. 38 39 40 38 public static void main(String[] args) { 39 if (args.length < 5) 40 System.exit(-1); 41 41 42 43 Boolean visible = false; 44 45 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]; 47 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]; 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 } 54 55 55 56 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()); 59 60 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 } 75 72 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) }); 80 75 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); 84 78 } 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 } 93 93 } 94 94 … … 97 97 // 98 98 class BattleObserver extends BattleAdaptor { 99 public void onBattleCompleted(BattleCompletedEvent e) { 100 101 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()) { 104 104 if (result.getTeamLeaderName().contains(BattleRunner.player)) 105 105 robotScore = result.getScore(); … … 107 107 opponentScore = result.getScore(); 108 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 } 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)); 116 115 } 116 } 117 117 }
Note: See TracChangeset
for help on using the changeset viewer.