net.sourceforge.cilib.ff.FFA Maven / Gradle / Ivy
/** __ __
* _____ _/ /_/ /_ Computational Intelligence Library (CIlib)
* / ___/ / / / __ \ (c) CIRG @ UP
* / /__/ / / / /_/ / http://cilib.net
* \___/_/_/_/_.___/
*/
package net.sourceforge.cilib.ff;
import java.util.List;
import net.sourceforge.cilib.algorithm.initialisation.ClonedPopulationInitialisationStrategy;
import net.sourceforge.cilib.algorithm.population.IterationStrategy;
import net.sourceforge.cilib.algorithm.population.SinglePopulationBasedAlgorithm;
import net.sourceforge.cilib.entity.Topologies;
import net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator;
import net.sourceforge.cilib.ff.firefly.Firefly;
import net.sourceforge.cilib.ff.firefly.StandardFirefly;
import net.sourceforge.cilib.ff.iterationstrategies.StandardFireflyIterationStrategy;
import net.sourceforge.cilib.problem.solution.OptimisationSolution;
import com.google.common.collect.Lists;
/**
*
* An implementation of the standard Firefly algorithm.
*
*
* References:
*
*
*
* - Yang, Xin-She. "Firefly algorithms for multimodal optimization."
* Stochastic algorithms: foundations and applications (2009): 169-178.
*
*
*
*/
public class FFA extends SinglePopulationBasedAlgorithm {
private IterationStrategy iterationStrategy;
/**
* Creates a new instance of the Firefly
algorithm.
* All fields are initialised to reasonable defaults.
*/
public FFA() {
iterationStrategy = new StandardFireflyIterationStrategy();
initialisationStrategy = new ClonedPopulationInitialisationStrategy();
initialisationStrategy.setEntityType(new StandardFirefly());
}
/**
* Create a copy of the provided instance.
* @param copy The instance to copy.
*/
public FFA(FFA copy) {
super(copy);
this.iterationStrategy = copy.iterationStrategy;
}
/**
* {@inheritDoc}
*/
@Override
public FFA getClone() {
return new FFA(this);
}
/**
* Perform the required initialisation for the algorithm. Create the fireflies
* and add them to the specified topology.
*/
@Override
public void algorithmInitialisation() {
topology = fj.data.List.iterableList(initialisationStrategy.initialise(optimisationProblem));
for (Firefly f : topology) {
f.calculateFitness();
}
}
/**
* Perform the iteration of the Firefly algorithm, use the appropriate IterationStrategy
* to perform the iteration.
*/
@Override
protected void algorithmIteration() {
iterationStrategy.performIteration(this);
}
/**
* Get the best current solution.
* @return The OptimisationSolution
representing the best solution.
*/
@Override
public OptimisationSolution getBestSolution() {
Firefly bestEntity = Topologies.getBestEntity(topology, new DescendingFitnessComparator());
return new OptimisationSolution(bestEntity.getPosition(), bestEntity.getFitness());
}
/**
* Get the IterationStrategy
of the Firefly algorithm.
* @return Returns the iterationStrategy.
*/
public IterationStrategy getIterationStrategy() {
return iterationStrategy;
}
/**
* Set the IterationStrategy
to be used.
* @param iterationStrategy The iterationStrategy to set.
*/
public void setIterationStrategy(IterationStrategy iterationStrategy) {
this.iterationStrategy = iterationStrategy;
}
/**
* Get the collection of best solutions.
* @return The Collection<OptimisationSolution>
containing the solutions.
*/
@Override
public List getSolutions() {
List solutions = Lists.newLinkedList();
for (Firefly e : Topologies.getNeighbourhoodBestEntities(topology, neighbourhood, new DescendingFitnessComparator())) {
solutions.add(new OptimisationSolution(e.getPosition(), e.getFitness()));
}
return solutions;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy