PredatorMinimizeDistanceFromPreyObjective extends GridTorusObjective<T> { @Override public double minScore() { int numPreds = Parameters.parameters.integerParameter("torusPredators"); int numPrey = Parameters.parameters.integerParameter("torusPreys"); int height = Parameters.parameters.integerParameter("torusYDimensions"); int width = Parameters.parameters.integerParameter("torusXDimensions"); double maxDist = height / 2 + width / 2; double[] sumOfDists = new double[numPreds]; for (int i = 0; i < numPreds; i++) { sumOfDists[i] = maxDist * numPrey; } return -StatisticsUtilities.sum(sumOfDists); }  static double sumOfPredToPreyDistances(TorusAgent[] preds, TorusAgent[] prey); @Override /** * reward the predators for being as close to the prey as possible at the * end if the prey are dead then distance from that prey is counted as zero * (meaning that the predators are also inherently encouraged to eat the * prey) */ double fitness(Organism<T> individual); @Override /** * worst possible score for a predator is the sum of the maximum possible * distance from each the prey for each pred */ double minScore();  }
@SuppressWarnings({ "unused", "rawtypes", "unchecked" }) @Test public void test() { TorusPredPreyGame game = new TorusPredPreyGame(100, 100, 3, 2); TorusWorld world = new TorusWorld(100, 100); TorusAgent[] prey = game.getPrey(); TorusAgent[] preds = game.getPredators(); PredatorMinimizeDistanceFromPreyObjective objective = new PredatorMinimizeDistanceFromPreyObjective(); double fitness; prey[0].move((int) -prey[0].getX(), (int) -prey[0].getY()); prey[1].move((int) -prey[1].getX(), (int) -prey[1].getY()); preds[0].move((int) -preds[0].getX(), (int) -preds[0].getY()); preds[1].move((int) -preds[1].getX(), (int) -preds[1].getY()); preds[2].move((int) -preds[2].getX(), (int) -preds[2].getY()); fitness = objective.score(game, null); assertEquals(fitness, 0, doubleThreshold); preds[0].move(50, 50); preds[1].move(50, 50); preds[2].move(50, 50); fitness = objective.score(game, null); assertEquals(fitness, -600, doubleThreshold); assertEquals(fitness, objective.minScore(), doubleThreshold); preds[0].move(-50, -50); fitness = objective.score(game, null); assertEquals(fitness, -400, doubleThreshold); preds[1].move(-18, 11); fitness = objective.score(game, null); assertEquals(fitness, -342, doubleThreshold); preds[2].move(41, -44); fitness = objective.score(game, null); assertEquals(fitness, -172, doubleThreshold); prey[1].move(20, 20); fitness = objective.score(game, null); assertEquals(fitness, -222, doubleThreshold); int[][] predMoves = new int[][] { { 0, 1 }, { 0, 0 }, { 0, 0 } }; int[][] preyMoves = new int[][] { { 0, 0 }, { 0, 0 } }; for (int i = 0; i < 20; i++) game.advance(predMoves, preyMoves); predMoves = new int[][] { { 1, 0 }, { 0, 0 }, { 0, 0 } }; preyMoves = new int[][] { { 0, 0 }, { 0, 0 } }; for (int i = 0; i < 20; i++) game.advance(predMoves, preyMoves); assertNull(prey[1]); fitness = objective.score(game, null); assertEquals(fitness, -126, doubleThreshold); }