/** * Compare the {@linkplain Entity} objects returning the desired ordering. * * @param e1 The first {@linkplain Entity} to be used in the comparison. * @param e2 The second {@linkplain Entity} to be used in the comparison. * @return -1 if e1 is less than e2; 0 if e1 and e2 are equal 1 if e2 is greater than e1 */ @Override public int compare(E e1, E e2) { Fitness f1 = e1.getFitness(); Fitness f2 = e2.getFitness(); return f1.compareTo(f2); }
@Override public Vector get(Particle particle) { Vector newPos = (Vector) delegate.get(particle); Particle tmp = particle.getClone(); tmp.setPosition(newPos); Fitness newFitness = particle.getBehaviour().getFitnessCalculator().getFitness(tmp); final UniformDistribution uniform = new UniformDistribution(); Vector newPBest = newPos.plus( Vector.newBuilder() .repeat(newPos.size(), Real.valueOf(1.0)) .build() .multiply( new P1<Number>() { @Override public Number _1() { return uniform.getRandomNumber( -granularity.getParameter(), granularity.getParameter()); } })); tmp.setPosition(newPos); Fitness newPBestFitness = particle.getBehaviour().getFitnessCalculator().getFitness(tmp); if (newPBestFitness.compareTo(newFitness) < 0) { Vector tmpVector = Vector.copyOf(newPos); newPos = newPBest; newPBest = tmpVector; newPBestFitness = newFitness; } double dot = ((Vector) particle.getNeighbourhoodBest().getBestPosition()) .subtract(newPos) .dot(newPBest.subtract(newPos)); if (dot < 0) { return (Vector) particle.getPosition(); } particle.put(Property.BEST_POSITION, newPBest); particle.put(Property.BEST_FITNESS, newPBestFitness); return newPos; }