Single-objective algorithms#

class SingleObjective#

#include <algorithm/single_objective.hpp>
namespace algorithm

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

class SingleObjective : public gapp::algorithm::Algorithm#

A generic algorithm for single-objective optimization.

The algorithm combines a selection method and a population replacement method. The selection method is used to select candidates from the populations for crossover, while the population replacement method is used to create the population for the next generation of the algorithm from the combined parent and child populations.

Move-only.

Public Types

using DefaultSelection = selection::Tournament#

The selection method used when not specified explicitly.

using DefaultReplacement = replacement::KeepBest#

The population update method used when not specified explicitly.

using SelectionCallable = std::function<size_t(const GaInfo&, const FitnessMatrix&)>#

The general callable type that can be used as a selection method, when not using a selection method derived from selection::Selection.

using ReplacementCallable = std::function<small_vector<size_t>(const GaInfo&, const FitnessMatrix&)>#

The general callable type that can be used as a population replacement policy, when not using a replacement policy derived from replacement::Replacement.

Public Functions

template<typename S = DefaultSelection, typename R = DefaultReplacement>
inline explicit SingleObjective(
S selection = S{},
R replacement = R{},
)#

Create a single-objective algorithm.

Parameters:
  • selection – The selection method to use.

  • replacement – The replacement policy to use.

explicit SingleObjective(
std::unique_ptr<selection::Selection> selection,
std::unique_ptr<replacement::Replacement> replacement = std::make_unique<DefaultReplacement>(),
)#

Create a single-objective algorithm.

Parameters:
  • selection – The selection method to use. Can’t be a nullptr.

  • replacement – The replacement policy to use. Can’t be a nullptr.

explicit SingleObjective(SelectionCallable selection)#

Create a single-objective algorithm using the default replacement method.

Parameters:

selection – The selection method to use. Can’t be a nullptr.

SingleObjective(SelectionCallable selection, ReplacementCallable replacement)#

Create a single-objective algorithm.

Parameters:
  • selection – The selection method to use. Can’t be a nullptr.

  • replacement – The replacement policy to use. Can’t be a nullptr.

template<typename S>
inline void selection_method(S selection)#

Set the selection method used by the algorithm.

Parameters:

selection – The selection method to use.

void selection_method(std::unique_ptr<selection::Selection> selection)#

Set the selection method used by the algorithm.

Parameters:

selection – The selection method to use. Can’t be a nullptr.

void selection_method(SelectionCallable f)#

Set the selection method used by the algorithm. The function used should be thread-safe if parallel execution is enabled (which is true by default).

Parameters:

f – The selection method to use. Can’t be a nullptr.

inline selection::Selection &selection_method() & noexcept#
Returns:

The selection operator used by the algorithm.

inline const selection::Selection &selection_method() const & noexcept#
Returns:

The selection operator used by the algorithm.

template<typename R>
inline void replacement_method(R replacement)#

Set the population replacement policy used by the algorithm.

Parameters:

replacement – The replacement policy to use.

void replacement_method(
std::unique_ptr<replacement::Replacement> replacement,
)#

Set the population replacement policy used by the algorithm.

Parameters:

replacement – The replacement policy to use. Can’t be a nullptr.

void replacement_method(ReplacementCallable f)#

Set the population replacement policy used by the algorithm.

Parameters:

f – The replacement policy to use. Can’t be a nullptr.

inline replacement::Replacement &replacement_method() & noexcept#
Returns:

The population replacement policy used by the algorithm.

inline const replacement::Replacement &replacement_method() const & noexcept#
Returns:

The population replacement policy used by the algorithm.

class Selection#

#include <algorithm/selection_base.hpp>
namespace selection

Selection operators for the single-objective algorithm.

class Selection#

This is the base class used for all of the single-objective selection operators. The selection operator is used to select candidates from the population for the crossovers.

New selection methods for the single-objective algorithm should be derived from this class and there are 3 methods that should be implemented by them:

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

  • prepareSelectionsImpl (optional) : Prepare the operator for the selections if necessary.

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

Subclassed by gapp::algorithm::Algorithm, gapp::selection::Boltzmann, gapp::selection::Lambda, gapp::selection::Rank, gapp::selection::Roulette, gapp::selection::Sigma, gapp::selection::Tournament

Public Functions

inline virtual void initializeImpl(const GaInfo&)#

Initialize the selection operator if necessary.

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

The default implementation does nothing.

Parameters:

ga – The GA that uses the algorithm.

inline virtual void prepareSelectionsImpl(
const GaInfo&,
const FitnessMatrix&,
)#

Prepare the operator for the selections if necessary.

This method will be called exactly once every generation right before the selections are performed.

The default implementation does nothing.

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

  • fmat – The fitness matrix of the population.

virtual size_t selectImpl(const GaInfo &ga, const FitnessMatrix &fmat) const = 0#

Select a single candidate for crossover from the population.

This method will be called exactly (population_size) or (population_size + 1) times in every generation (depending on which number is even).

The method should return the index of the selected candidate based on the fitness matrix of the current population (i.e. return an index from the fitness matrix).

The implementation should be thread-safe if parallel execution is enabled for the GAs (which is true by default).

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

  • fmat – The fitness matrix of the current population.

Returns:

The index of the candidate selected from the population.

virtual ~Selection() = default#

Destructor.

class Replacement#

#include <algorithm/replacement_base.hpp>
namespace replacement

Population replacement policies for the single-objective algorithms.

class Replacement#

This is the base class used for all of the single-objective population replacement policies. The replacement operator is used to select the candidates of the next population from the combined parent and child populations.

New replacement policies for the single-objective algorithm should be derived from this class, and there is a single method that should be implemented in the derived classes:

  • nextPopulationImpl : Selects the candidates for the next population.

Subclassed by gapp::algorithm::Algorithm, gapp::replacement::Elitism, gapp::replacement::KeepBest, gapp::replacement::KeepChildren, gapp::replacement::Lambda

Public Functions

virtual small_vector<size_t> nextPopulationImpl(
const GaInfo &ga,
const FitnessMatrix &fmat,
) = 0#

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

The fitness matrix is given as the combined fitness matrix of the parent and child populations’ fitness matrices. The top half (first population_size elements) of the matrix corresponds to the parent population, while the rest (another population_size elements) is the fitness matrix of the child population.

The method should return (population_size) number of unique indices from this fitness matrix.

Parameters:
  • ga – The GA that uses the update method.

  • fmat – The fitness matrix of the combined parent and child populations.

Returns:

The indices of the candidates selected from the fitness matrix.

virtual ~Replacement() = default#

Destructor.