Algorithms#

namespace algorithm#

Single- and multi-objective algorithms that can be used in the GAs.

class Algorithm#

#include <algorithm/algorithm_base.hpp>
class Algorithm : private gapp::selection::Selection, private gapp::replacement::Replacement#

This is the base class used for all of the algorithms.

The algorithms define the way the population is evolved over the generations (i.e. the selection and population replacement methods used). They may be single-, multi-objective, or both.

New algorithms should be derived from this class, and there are 5 virtual methods that should be implemented by them:

  • initializeImpl (optional) : Initializes the algorithm at the start of a run.

  • prepareSelectionsImpl (optional) : Prepares the algorithm for the selections if needed.

  • selectImpl : Selects a candidate from the population for crossover.

  • nextPopulationImpl : Selects the candidates of the next population from the parent and the child populations.

  • optimalSolutionsImpl (optional) : Selects the optimal solutions of the population.

Subclassed by gapp::algorithm::NSGA2, gapp::algorithm::NSGA3, gapp::algorithm::SingleObjective

Public Functions

inline void initialize(const GaInfo &ga)#

Initialize the algorithm if needed.

This method will be called exactly once at start of the run, after the initial population has already been created.

Implemented by initializeImpl().

Parameters:

ga – The GA that uses the algorithm.

inline void prepareSelections(const GaInfo &ga, const FitnessMatrix &fmat)#

Prepare the algorithm for the selections if neccesary.

This method will be called exactly once every generation before the selections are performed. The population will be unchanged since the last call to nextPopulation().

Implemented by prepareSelectionsImpl().

Parameters:
  • ga – The GA that uses the algorithm.

  • fmat – The fitness matrix of the population.

template<typename T>
const Candidate<T> &select(
const GA<T> &ga,
const Population<T> &pop,
const FitnessMatrix &fmat,
) const#

Select a single candidate from the population for crossover.

This method will be called exactly (population_size) or (population_size + 1) times in every generation, depending on which one is even. The population will be the unchanged population that was returned by the last call to nextPopulation() in the previous generation.

Implemented by selectImpl().

Parameters:
  • ga – The GA that uses the algorithm.

  • pop – The current population.

  • fmat – The fitness matrix of the current population.

Returns:

The candidate selected from the population.

template<typename T>
Population<T> nextPopulation(
const GA<T> &ga,
Population<T> parents,
Population<T> children,
)#

Select the candidates of the next generation from the candidates of the current and the child populations.

This method will be called exactly once at the end of each generation before the call to optimalSolutions().

Implemented by nextPopulationImpl().

Parameters:
  • ga – The GA that uses the algorithm.

  • parents – The parent population (current population of the GA).

  • children – The child population, created from the parent population.

Returns:

The candidates of the next generation of the algorithm.

template<typename T>
Candidates<T> optimalSolutions(
const GA<T> &ga,
const Population<T> &pop,
) const#

Find the optimal solutions in the population that was created by nextPopulation().

Implemented by optimalSolutionsImpl(). The default implementation will use a generic method to find the pareto-optimal solutions in the population. This default will be used if optimalSolutionsImpl() returns an empty vector.

Parameters:
  • ga – The GA that uses the algorithm.

  • pop – The current population of the GA.

Returns:

The pareto optimal solutions of the population pop.

~Algorithm() override = default#

Destructor.

Private Functions

inline virtual std::vector<size_t> optimalSolutionsImpl(const GaInfo&) const#

The implementation of the optimalSolutions() function.

Returns the indices of the optimal solutions in the current population, which is the population that was returned by the last call to nextPopulation(). If the optimal solutions can’t be found trivially, it should just return an empty vector (this is the default behaviour).

Parameters:

ga – The GA that uses the algorithm.

Returns:

The indices of the pareto optimal solutions in the current population.