/** {@inheritDoc} */ @Override public <T extends Entity> T create(T targetEntity, T current, Topology<T> topology) { T bestEntity = Topologies.getBestEntity(topology); List<T> participants = Selection.copyOf(topology) .exclude(targetEntity, bestEntity, current) .orderBy(new RandomArrangement()) .select(Samples.first((int) numberOfDifferenceVectors.getParameter()).unique()); Vector differenceVector = determineDistanceVector(participants); Vector targetVector = ((Vector) targetEntity.getCandidateSolution()) .multiply(1 - greedynessParameter.getParameter()); Vector bestVector = ((Vector) bestEntity.getCandidateSolution()).multiply(greedynessParameter.getParameter()); Vector trialVector = bestVector.plus( targetVector.plus(differenceVector.multiply(scaleParameter.getParameter()))); T trialEntity = (T) current.getClone(); trialEntity.setCandidateSolution(trialVector); return trialEntity; }
/** {@inheritDoc} */ @Override public <T extends Entity> T create(T targetEntity, T current, fj.data.List<T> topology) { int number = Double.valueOf(this.numberOfDifferenceVectors.getParameter()).intValue(); List<T> participants = Selection.copyOf(topology) .exclude(targetEntity, current) .orderBy(new RandomArrangement()) .select(Samples.first(number).unique()); Vector differenceVector = determineDistanceVector(participants); Vector targetVector = (Vector) targetEntity.getCandidateSolution(); Vector trialVector = targetVector.plus( differenceVector.multiply( new P1<Number>() { @Override public Number _1() { return scaleParameter.getParameter(); } })); T trialEntity = (T) current.getClone(); trialEntity.setCandidateSolution(trialVector); return trialEntity; }
@Override public void performIteration(PSO algorithm) { delegate.performIteration(algorithm); Topology<Particle> topology = algorithm.getTopology(); // calculate vAvg Vector avgV = Vectors.mean( Lists.transform( topology, new Function<Particle, Vector>() { @Override public Vector apply(Particle f) { return (Vector) f.getVelocity(); } })); Vector.Builder builder = Vector.newBuilder(); for (Numeric n : avgV) { if (Math.abs(n.doubleValue()) > vMax.getParameter()) { builder.add(vMax.getParameter()); } else { builder.add(n); } } avgV = builder.build(); // mutation Particle gBest = Topologies.getBestEntity(topology, new SocialBestFitnessComparator()); Particle mutated = gBest.getClone(); Vector pos = (Vector) gBest.getBestPosition(); final Bounds bounds = pos.boundsOf(0); pos = pos.plus( avgV.multiply( new Supplier<Number>() { @Override public Number get() { return distribution.getRandomNumber() * bounds.getRange() + bounds.getLowerBound(); } })); mutated.setCandidateSolution(pos); mutated.calculateFitness(); if (gBest.getBestFitness().compareTo(mutated.getFitness()) < 0) { gBest.getProperties().put(EntityType.Particle.BEST_FITNESS, mutated.getBestFitness()); gBest.getProperties().put(EntityType.Particle.BEST_POSITION, mutated.getBestPosition()); } }
/** {@inheritDoc} */ @Override public Entity create(Entity targetEntity, Entity current, Topology<? extends Entity> topology) { List<Entity> participants = (List<Entity>) Selection.copyOf(topology).exclude(targetEntity, current).select(Samples.all()); Vector differenceVector = determineDistanceVector(participants); Vector targetVector = (Vector) targetEntity.getCandidateSolution(); Vector trialVector = targetVector.plus( differenceVector.multiply( new P1<Number>() { @Override public Number _1() { return scaleParameter.getParameter(); } })); Entity trialEntity = current.getClone(); trialEntity.setCandidateSolution(trialVector); return trialEntity; }