TorusPredatorsByProximitySensorBlock implements TorusPredPreySensorBlock { @SuppressWarnings("unchecked") @Override public double[] sensorValues(TorusAgent me, TorusWorld world, TorusAgent[] preds, TorusAgent[] prey) { double[] predOffsets = NNTorusPredPreyController.getAgentOffsets(me, world, preds); boolean self = false; if(predOffsets.length == numPredators * 2 - 2){ self = true; } Pair<Double,Double>[] pairs = new Pair[predOffsets.length/2]; for (int i = 0; i < pairs.length; i++) { pairs[i] = new Pair<Double,Double>(predOffsets[2*i], predOffsets[2*i + 1]); } Arrays.sort(pairs, new Comparator<Pair<Double,Double>>(){ public int compare(Pair<Double,Double> o1, Pair<Double,Double> o2){ return (int) Math.signum((Math.abs(o1.t1) + Math.abs(o1.t2)) - (Math.abs(o2.t1) + Math.abs(o2.t2))); } }); double[] proximityPreds = new double[self ? (2 * numPredators - 2) : (2 * numPredators)]; for (int i = 0; i < pairs.length; i++) { proximityPreds[2 * i] = pairs[i].t1; proximityPreds[2 * i + 1] = pairs[i].t2; } if(me.getAgentType()==TorusPredPreyGame.AGENT_TYPE_PRED && !Parameters.parameters.booleanParameter("predsSenseAllPreds")){ double[] adjustedOffsets = new double[Parameters.parameters.integerParameter("numberPredsSensedByPreds") * 2]; System.arraycopy(proximityPreds, 0, adjustedOffsets, 0, Parameters.parameters.integerParameter("numberPredsSensedByPreds") * 2); return adjustedOffsets; } if(me.getAgentType()==TorusPredPreyGame.AGENT_TYPE_PREY && !Parameters.parameters.booleanParameter("preySenseAllPreds")){ double[] adjustedOffsets = new double[Parameters.parameters.integerParameter("numberPredsSensedByPrey") * 2]; System.arraycopy(proximityPreds, 0, adjustedOffsets, 0, Parameters.parameters.integerParameter("numberPredsSensedByPrey") * 2); return adjustedOffsets; } return proximityPreds; } TorusPredatorsByProximitySensorBlock(); @SuppressWarnings("unchecked") @Override /** * @return the sensor inputs for the predators by proximity. The inputs will * be the X and Y offsets to each predator from this agent. Index 0 * will hold the closest predator and the last index in the array * will hold the furthest predator (array in ascending order by * distance) Distance here is defined as the sum of the absolute * value of the X and Y offsets */ double[] sensorValues(TorusAgent me, TorusWorld world, TorusAgent[] preds, TorusAgent[] prey); @Override /** * @return the total number of sensors for the predators (X and Y offsets to * each pred) */ int numSensors(boolean isPredator); @Override /** * Finds the sensor labels for this agent and returns them in an array of * Strings The sensor labels for this class will be the list of Predators * being sensed by proximity * * @return the sensorLabels for the predators by proximity */ String[] sensorLabels(boolean isPredator);  }
@Test public void testSensorValues() { TorusPredatorsByProximitySensorBlock block = new TorusPredatorsByProximitySensorBlock(); TorusPredPreyGame game = new TorusPredPreyGame(100, 100, 3, 2); TorusWorld world = new TorusWorld(100, 100); TorusAgent[] prey = game.getPrey(); TorusAgent[] preds = game.getPredators(); 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()); preds[0].move(11, 22); preds[1].move(48, 85); preds[2].move(99, 36); prey[1].move(50, 57); assertEquals(Arrays.toString(block.sensorValues(prey[0], world, preds, prey)), "[0.11, 0.22, -0.01, 0.36, 0.48, -0.15]"); assertEquals(Arrays.toString(block.sensorValues(prey[1], world, preds, prey)), "[-0.02, 0.28, 0.49, -0.21, -0.39, -0.35]"); assertEquals(Arrays.toString(block.sensorValues(preds[0], world, preds, prey)), "[-0.12, 0.14, 0.37, -0.37]"); assertEquals(Arrays.toString(block.sensorValues(preds[1], world, preds, prey)), "[-0.37, 0.37, -0.49, -0.49]"); assertEquals(Arrays.toString(block.sensorValues(preds[2], world, preds, prey)), "[0.12, -0.14, 0.49, 0.49]"); }