@Override public void fight(Individual otherIndividual) { NegamaxPlayer otherNegamaxPlayer = (NegamaxPlayer) otherIndividual; // Get the test ground. Board testBoard = initialTestBoard.cloneBoard(); // Fight until a side wins, or a max number of turns has elapsed. boolean myTurn = true; int iteration = 0; while (testBoard.numberOf(DWARF) > 0 && testBoard.numberOf(TROLL) > 0 && iteration++ < 1000) { if (myTurn) { makeBestMove(testBoard); } else { otherNegamaxPlayer.makeBestMove(testBoard); } myTurn = !myTurn; } // Update the fitnesses of the players. // Since the dwarves play first, the first player, me, scores the dzqrf side. int score = testBoard.numberOf(DWARF) - 4 * testBoard.numberOf(TROLL); fitness += score; otherNegamaxPlayer.fitness -= score; }
/** Calibrates the negamax player, using basic natural selection algorithms. */ public static void calibrate() { BasicNaturalSelection geneticAlgorithm = new BasicNaturalSelection(); geneticAlgorithm.individuals = new ArrayList<>(); for (int i = 0; i < MIXING_FACTOR * (MIXING_FACTOR + 2); i++) { NegamaxPlayer player = new NegamaxPlayer(); player.randomize(); geneticAlgorithm.individuals.add(player); } for (int i = 0; i < 1000; i++) { System.out.println("Iteration " + i); geneticAlgorithm.iterate(); } }
@Override public Individual makeChild(Individual otherIndividual) { NegamaxPlayer otherNegamaxPlayer = (NegamaxPlayer) otherIndividual; NegamaxPlayer child = new NegamaxPlayer(); child.dwarfMaterialRatio = (dwarfMaterialRatio + otherNegamaxPlayer.dwarfMaterialRatio) / 2; child.trollMaterialRatio = (trollMaterialRatio + otherNegamaxPlayer.trollMaterialRatio) / 2; child.dwarfClusteringRatio = (dwarfClusteringRatio + otherNegamaxPlayer.dwarfClusteringRatio) / 2; child.trollClusteringRatio = (trollClusteringRatio + otherNegamaxPlayer.trollClusteringRatio) / 2; child.dwarfMobilityRatio = (dwarfMobilityRatio + otherNegamaxPlayer.dwarfMobilityRatio) / 2; child.trollMobilityRatio = (trollMobilityRatio + otherNegamaxPlayer.trollMobilityRatio) / 2; child.absoluteVictoryBonus = (absoluteVictoryBonus + otherNegamaxPlayer.absoluteVictoryBonus) / 2; return child; }