/** * Function for counting the sum of the scores gathered by the robots. * * @return The sum. */ private double countTotalScore() { double totalScore = 0; for (BattleResults result : results) { totalScore += result.getScore(); } return totalScore; }
/** * The actual test, which asserts that ToyBot has won every round against SittingDuck. * * @param event Details about the completed battle. */ @Override public void onBattleCompleted(BattleCompletedEvent event) { // Return the results in order of getRobotNames. BattleResults[] battleResults = event.getIndexedResults(); // Sanity check that results[1] is ToyBot (not strictly necessary, but illustrative). BattleResults ToyBotResults = battleResults[1]; String robotName = ToyBotResults.getTeamLeaderName(); assertEquals("Check that results[1] is ToyBot", robotName, "lto.ToyBot*"); // Check to make sure ToyBot won every round. assertEquals("Check ToyBot winner", ToyBotResults.getFirsts(), getNumRounds()); }
private BattleResultsWrapper(BattleResults results) { super( results.getTeamLeaderName(), results.getRank(), results.getScore(), results.getSurvival(), results.getLastSurvivorBonus(), results.getBulletDamage(), results.getBulletDamageBonus(), results.getRamDamage(), results.getRamDamageBonus(), results.getFirsts(), results.getSeconds(), results.getThirds()); }
public void runGA(int populationSize, int generationSize, int genomeSize) { int generation = 1; int parentsSelected = 5; BattleResults[] results; ArrayList<String> genomePaths = GAHelper.generatePopulation(populationSize, genomeSize); for (String genomePath : genomePaths) { Genome selected = GenomeIO.load(genomePath); RobotGenerator.compile(selected); String robotName = RobotGenerator.getClassPath(selected); RobotSpecification[] selectedRobots = engine.getLocalRepository(robotName + ",sample.RamFire"); BattleSpecification battleSpec = new BattleSpecification(1, battlefield, selectedRobots); engine.runBattle(battleSpec, true); results = battleObserver.getResults(); BattleResults result = results[0]; // Index 0 will always be the index of our generated Robot // Evaluate fitness - Keeping it simple for now (fitness = score) selected.setFitness(new Double(result.getScore())); GenomeIO.save(selected); System.out.println( String.format( "Genome result: generation:%d id:%d - FITNESS:%f", selected.getGeneration(), selected.getId(), selected.getFitness())); } while (generation != generationSize) { generation++; genomePaths = GAHelper.produceNextGeneration(generation, parentsSelected, populationSize, genomeSize); for (String genomePath : genomePaths) { Genome selected = GenomeIO.load(genomePath); RobotGenerator.compile(selected); String robotName = RobotGenerator.getClassPath(selected); RobotSpecification[] selectedRobots = engine.getLocalRepository(robotName + ",sample.RamFire"); BattleSpecification battleSpec = new BattleSpecification(1, battlefield, selectedRobots); engine.runBattle(battleSpec, true); results = battleObserver.getResults(); BattleResults result = results[0]; // Index 0 will always be the index of our generated Robot // Evaluate fitness - Keeping it simple for now (fitness = score) selected.setFitness(new Double(result.getScore())); GenomeIO.save(selected); System.out.println( String.format( "Genome result: generation:%d id:%d - FITNESS:%f", selected.getGeneration(), selected.getId(), selected.getFitness())); } } engine.close(); System.exit(0); }
public Object getValueAt(int row, int col) { BattleResults statistics = results[row]; switch (col) { case 0: { int place = row + 1; while (place < getRowCount() && statistics.getScore() == results[place].getScore()) { place++; } return StringUtil.getPlacementString(place); } case 1: return statistics.getTeamLeaderName(); case 2: String percent = ""; if (totalScore != 0) { percent = " (" + NumberFormat.getPercentInstance().format(statistics.getScore() / totalScore) + ")"; } return "" + (int) (statistics.getScore() + 0.5) + percent; case 3: return "" + (int) (statistics.getSurvival() + 0.5); case 4: return "" + (int) (statistics.getLastSurvivorBonus() + 0.5); case 5: return "" + (int) (statistics.getBulletDamage() + 0.5); case 6: return "" + (int) (statistics.getBulletDamageBonus() + 0.5); case 7: return "" + (int) (statistics.getRamDamage() + 0.5); case 8: return "" + (int) (statistics.getRamDamageBonus() + 0.5); case 9: return "" + statistics.getFirsts(); case 10: return "" + statistics.getSeconds(); case 11: return "" + statistics.getThirds(); default: return ""; } }