AggressivePredatorController extends TorusPredPreyController { @Override public int[] getAction(TorusAgent me, TorusWorld world, TorusAgent[] preds, TorusAgent[] prey) { TorusAgent target = me.closestAgent(prey); double[] moveDistances = new double[predatorActions().length]; for (int i = 0; i < predatorActions().length; i++) { moveDistances[i] = target .distance(me.getPosition().add(new Tuple2D(predatorActions()[i][0], predatorActions()[i][1]))); } return predatorActions()[StatisticsUtilities.argmin(moveDistances)]; }  @Override /** * The getAction method takes in the controlled agent, the world and all * predators and prey as arrays The closest prey is chosen and the * TorusAgent moves towards this prey. */ int[] getAction(TorusAgent me, TorusWorld world, TorusAgent[] preds, TorusAgent[] prey);  }
@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(); AggressivePredatorController controller = new AggressivePredatorController(); 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()); prey[0].move(10, 10); prey[1].move(50, 50); preds[0].move(8, 10); int[] predAct0 = controller.getAction(preds[0], world, preds, prey); int[] predAct1 = controller.getAction(preds[1], world, preds, prey); assertEquals(predAct0[0], 1); assertEquals(predAct0[1], 0); assertTrue((predAct1[0] == 1 && predAct1[1] == 0) || (predAct1[0] == 0 && predAct1[1] == 1)); preds[0].move(predAct0[0], predAct0[1]); preds[1].move(predAct1[0], predAct1[1]); assertTrue(preds[0].getX() == 9 && preds[0].getY() == 10); assertTrue((preds[1].getX() == 1 && preds[1].getY() == 0) || (preds[1].getX() == 0 && preds[1].getY() == 1)); predAct0 = controller.getAction(preds[0], world, preds, prey); assertEquals(predAct0[0], 1); assertEquals(predAct0[1], 0); preds[0].move(predAct0[0], predAct0[1]); assertTrue(preds[0].getX() == 10 && preds[0].getY() == 10); assertTrue(preds[0].isCoLocated(prey[0])); predAct0 = controller.getAction(preds[0], world, preds, prey); assertEquals(predAct0[0], 0); assertEquals(predAct0[1], 0); preds[2].move(51, 51); int[] predAct2 = controller.getAction(preds[2], world, preds, prey); assertTrue((predAct2[0] == -1 && predAct2[1] == 0) || (predAct2[0] == 0 && predAct2[1] == -1)); preds[2].move(predAct2[0], predAct2[1]); assertTrue( (preds[2].getX() == 50 && preds[2].getY() == 51) || (preds[2].getX() == 51 && preds[2].getY() == 50)); predAct2 = controller.getAction(preds[2], world, preds, prey); preds[2].move(predAct2[0], predAct2[1]); assertTrue(preds[2].isCoLocated(prey[1])); }