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<const CandidateInfo&(const GaInfo&, const PopulationView&)>#

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<CandidatePtrVec(const GaInfo&, const PopulationView&)>#

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 PopulationView&,
)#

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.

  • pop – A view of the population without the encoding dependent parts of the candidates.

virtual const CandidateInfo &selectImpl(
const GaInfo &ga,
const PopulationView &pop,
) 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 a reference to the selected candidate from the current population pop.

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.

  • pop – A view of the population without the encoding dependent parts of the candidates.

Returns:

The candidate selected from the population pop.

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 CandidatePtrVec nextPopulationImpl(
const GaInfo &ga,
const PopulationView &pop,
) = 0#

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

The @pop parameter describes the combined parent and child populations. The top half (first population_size elements) corresponds to the parent population, while the rest (another population_size elements) corresponds to the child population.

The method should return population_size number of unique pointers to elements of pop, pointing to the candidates that will comprise the next generation’s population.

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

  • pop – A view of the combined parent and child population.

Returns:

A vector of pointers to the candidates that were selected from pop for the next population.

virtual ~Replacement() = default#

Destructor.