/** * 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); }
/** * After every {@link #interval} iteration, pick {@link #numberOfSentries a number of} random * entities from the given {@link Algorithm algorithm's} topology and compare their previous * fitness values with their current fitness values. An environment change is detected when the * difference between the previous and current fitness values are >= the specified {@link * #epsilon} value. * * @param algorithm used to get hold of topology of entities and number of iterations * @return true if a change has been detected, false otherwise */ @Override public <A extends HasTopology & Algorithm & HasNeighbourhood> boolean detect(A algorithm) { if ((AbstractAlgorithm.get().getIterations() % interval == 0) && (AbstractAlgorithm.get().getIterations() != 0)) { List all = Java.List_ArrayList().f(algorithm.getTopology()); for (int i = 0; i < numberOfSentries.getParameter(); i++) { // select random sentry entity int random = Rand.nextInt(all.size()); StandardParticle sentry = (StandardParticle) all.get(random); // check for change // double previousFitness = sentry.getFitness().getValue(); boolean detectedChange = false; if (sentry.getFitness().getClass().getName().matches("MinimisationFitness")) { Fitness previousFitness = sentry.getFitness(); sentry.calculateFitness(); Fitness currentFitness = sentry.getFitness(); if (Math.abs(previousFitness.getValue() - currentFitness.getValue()) >= epsilon) { detectedChange = true; } } else if (sentry.getFitness().getClass().getName().matches("StandardMOFitness")) { MOFitness previousFitness = (MOFitness) sentry.getFitness(); sentry.calculateFitness(); MOFitness currentFitness = (MOFitness) sentry.getFitness(); for (int k = 0; k < previousFitness.getDimension(); k++) if (Math.abs( previousFitness.getFitness(k).getValue() - currentFitness.getFitness(k).getValue()) >= epsilon) { detectedChange = true; break; } } if (detectedChange) { System.out.println("Detected a change"); return true; } // remove the selected element from the all list preventing it from being selected again all.remove(random); } } return false; }
@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; }