net.sourceforge.cilib.pso.iterationstrategies.ASynchronousIterationStrategy Maven / Gradle / Ivy
/** __ __
* _____ _/ /_/ /_ Computational Intelligence Library (CIlib)
* / ___/ / / / __ \ (c) CIRG @ UP
* / /__/ / / / /_/ / http://cilib.net
* \___/_/_/_/_.___/
*/
package net.sourceforge.cilib.pso.iterationstrategies;
import net.sourceforge.cilib.algorithm.population.AbstractIterationStrategy;
import net.sourceforge.cilib.pso.PSO;
import net.sourceforge.cilib.pso.particle.Particle;
import fj.F;
import fj.F2;
import fj.Function;
import fj.P2;
import fj.data.List;
/**
* Implementation of the asynchronous iteration strategy for PSO.
*/
public class ASynchronousIterationStrategy extends AbstractIterationStrategy {
private static final long serialVersionUID = -3511991873784185698L;
private F additionalStep = Function.identity();
/**
* {@inheritDoc}
*/
@Override
public ASynchronousIterationStrategy getClone() {
return this;
}
/**
* This is an ASynchronous strategy:
*
*
* - For all particles:
*
* - Update the particle velocity
* - Update the particle position
* - Calculate the particle fitness
* - For all particles in the current particle's neighbourhood
*
* - Update the neighbourhood best
*
*
*
*
* @param algorithm the algorithm to which an iteration is to be applied.
*/
@Override
public void performIteration(final PSO algorithm) {
final fj.data.List topology = algorithm.getTopology();
algorithm.setTopology(topology.zipIndex().foldLeft(new F2, P2, fj.data.List>() {
@Override
public List f(List accum, P2 item) {
item._1().updateVelocity();
item._1().updatePosition();
boundaryConstraint.enforce(item._1());
item._1().calculateFitness();
Particle newParticle = additionalStep.f(item._1());
List result = accum.snoc(newParticle);
fj.data.List intermediate = result.append(topology.drop(item._2()+1));
for (Particle other : algorithm.getNeighbourhood().f(intermediate, newParticle)) {
if (newParticle.getSocialFitness().compareTo(other.getNeighbourhoodBest().getSocialFitness()) > 0) {
other.setNeighbourhoodBest(newParticle);
}
}
return result;
}
}, List.nil()));
}
public void setAdditionalStep(F additionalStep) {
this.additionalStep = additionalStep;
}
public F getAdditionalStep() {
return additionalStep;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy