Encodings

Contents

Encodings#

namespace gapp

struct GaTraits#

#include <core/ga_traits.hpp>
template<typename GeneType>
struct GaTraits#

Traits class describing some attributes of each GA class.

When defining a new genetic algorithm class that inherits from the GA class, this class must be specialized for the gene type T used before the derived class is declared, and must have the following members:

  • DefaultCrossover type (must be default constructible)

  • DefaultMutation type (must be constructible using the return value of defaultMutationRate)

  • defaultMutationRate(size_t chrom_len) -> Probability (static member function)

The following gene types are reserved for the GAs already implemented in the library and can’t be used as the gene type of new encodings:

  • std::uint8_t

  • std::size_t

  • int

  • double

Example:

template<>
struct GaTraits<MyGeneType>
{
  using DefaultCrossover = MyCrossoverType;
  using DefaultMutation  = MyMutationType;
  static Probability defaultMutationRate(size_t chromosome_size) { return 0.01; }
};

See also

GA

bool is_bounded#

#include <core/ga_traits.hpp>
template<typename GeneType>
constexpr bool gapp::is_bounded = false#

Type trait to check if a particular gene type is bounded or not. This variable should be specialized for custom gene types if they are bounded, otherwise they will be treated as if they were not bounded.

Example:

template<> inline constexpr bool is_bounded<MyGene> = true;

class GA#

#include <core/ga_base.hpp>
template<typename T>
class GA : public gapp::GaInfo#

The base class used for all of the genetic algorithms. Contains all of the encoding/gene specific information of the GA in addition to the non-encoding dependent parts contained by GaInfo.

This class should be used as the base class of new genetic algorithms. It has 2 virtual functions that should be implemented in the derived classes:

  • initialize (optional) : Perform some additional initialization specific to the derived class.

  • generateCandidate : Returns a (randomly) generated candidate solution. Used to create the initial population.

See also

GaTraits, is_bounded

Note

Before declaring a class that is derived from this class, the GaTraits struct should be specialized for the particular gene type that is used. If the gene type is bounded, the is_bounded variable template should also be specialized.

Template Parameters:

T – The gene type used for the candidate’s chromosomes.

Public Types

using GeneType = T#

The gene type of the candidates.

using CrossoverCallable = std::function<CandidatePair<T>(const GA&, const Candidate<T>&, const Candidate<T>&)>#

The general callable type that can be used as a crossover method in the GA (when not using a crossover method that is derived from crossover::Crossover). The function takes two candidate solutions (parents), and returns two candidates generated from these (children).

See also

crossover_method

using MutationCallable = std::function<void(const GA&, const Candidate<T>&, Chromosome<T>&)>#

The general callable type that can be used as a mutation method in the GA (when not using a mutation method derived from mutation::Mutation). The function takes a candidate solution, and changes this solution’s chromosome.

See also

mutation_method

using RepairCallable = std::function<Chromosome<T>(const GA&, const Chromosome<T>&)>#

The general callable type that can be used as a repair function in the GA. The function takes a candidate solution, and performs some operation on it.

See also

repair_function

Public Functions

GA(Positive<size_t> population_size = DEFAULT_POPSIZE)#

Create a genetic algorithm using the default genetic operators.

The algorithm used will be deduced from the number of objectives of the fitness function (single- or multi-objective), along with the mutation probability used, which will be deduced from the chromosome length.

Parameters:

population_size – The number of candidates in the population. Must be at least 1.

GA(
Positive<size_t> population_size,
std::unique_ptr<algorithm::Algorithm> algorithm,
)#

Create a genetic algorithm using the default genetic operators. The mutation probability used will be deduced from the chromosome length.

Parameters:
  • population_size – The number of candidates in the population. Must be at least 1.

  • algorithm – The algorithm to use. The default algorithm will be used if it’s a nullptr.

GA(
Positive<size_t> population_size,
std::unique_ptr<crossover::Crossover<T>> crossover,
std::unique_ptr<mutation::Mutation<T>> mutation,
std::unique_ptr<stopping::StopCondition> stop_condition = std::make_unique<stopping::NoEarlyStop>(),
)#

Create a genetic algorithm using the specified operators. The algorithm used will be deduced from the number of objectives of the fitness function (single- or multi-objective).

Parameters:
  • population_size – The number of candidates in the population. Must be at least 1.

  • crossover – The crossover operator to use. Can’t be a nullptr.

  • mutation – The mutation operator to use. Can’t be a nullptr.

  • stop_condition – The early-stop condition to use. No early-stopping will be used if it’s a nullptr.

GA(
Positive<size_t> population_size,
std::unique_ptr<algorithm::Algorithm> algorithm,
std::unique_ptr<crossover::Crossover<T>> crossover,
std::unique_ptr<mutation::Mutation<T>> mutation,
std::unique_ptr<stopping::StopCondition> stop_condition = std::make_unique<stopping::NoEarlyStop>(),
)#

Create a genetic algorithm using the specified algorithm and operators.

Parameters:
  • population_size – The number of candidates in the population. Must be at least 1.

  • algorithm – The algorithm to use. The default algorithm will be used if it’s a nullptr.

  • crossover – The crossover operator to use. Can’t be a nullptr.

  • mutation – The mutation operator to use. Can’t be a nullptr.

  • stop_condition – The early-stop condition to use. No early-stopping will be used if it’s a nullptr.

template<typename AlgorithmType>
GA(
Positive<size_t> population_size,
AlgorithmType algorithm,
)#

Create a genetic algorithm using the default genetic operators. The mutation probability will be deduced from the chromosome length.

Parameters:
  • population_size – The number of candidates in the population. Must be at least 1.

  • algorithm – The algorithm to use.

template<typename CrossoverType, typename MutationType, typename StoppingType = stopping::NoEarlyStop>
GA(
Positive<size_t> population_size,
CrossoverType crossover,
MutationType mutation,
StoppingType stop_condition = {},
)#

Create a genetic algorithm using the specified operators. The algorithm used will be deduced from the number of objectives of the fitness function (single- or multi-objective).

Parameters:
  • population_size – The number of candidates in the population. Must be at least 1.

  • crossover – The crossover operator to use.

  • mutation – The mutation operator to use.

  • stop_condition – The early-stop condition to use.

template<typename AlgorithmType, typename CrossoverType, typename MutationType, typename StoppingType = stopping::NoEarlyStop>
GA(
Positive<size_t> population_size,
AlgorithmType algorithm,
CrossoverType crossover,
MutationType mutation,
StoppingType stop_condition = {},
)#

Create a genetic algorithm using the specified algorithm and operators.

Parameters:
  • population_size – The number of candidates in the population. Must be at least 1.

  • algorithm – The algorithm to use.

  • crossover – The crossover operator to use.

  • mutation – The mutation operator to use.

  • stop_condition – The early-stop condition to use.

inline virtual const FitnessFunctionBase<T> *fitness_function(
) const & noexcept final#
Returns:

The fitness function used. A nullptr is returned if no fitness function is set.

inline virtual size_t chrom_len() const noexcept final#
Returns:

The chromosome length used. Returns 0 if the chromosome length is unspecified (ie. no fitness function was set yet).

inline const BoundsVector<T> &gene_bounds() const noexcept#

See also

Bounds

Returns:

The lower and upper bounds for each of the chromosomes’ genes (the ranges are inclusive).

template<typename F>
inline void crossover_method(F f)#

Set the crossover method the GA will use. The crossover method should be thread-safe if parallel execution is enabled (true by default).

Note

As the crossover probability is associated with a crossover method, setting a new crossover method will override any previous crossover probability set using crossover_rate().

Parameters:

f – The crossover method to use.

inline void crossover_method(std::unique_ptr<crossover::Crossover<T>> f)#

Set the crossover method the GA will use. The crossover method should be thread-safe if parallel execution is enabled (true by default).

Note

As the crossover probability is associated with a crossover method, setting a new crossover method will override any previous crossover probability set using crossover_rate().

Parameters:

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

inline void crossover_method(CrossoverCallable f)#

Set the crossover method the GA will use. The crossover method should be thread-safe if parallel execution is enabled (true by default).

Note

As the crossover probability is associated with a crossover method, setting a new crossover method will override any previous crossover probability set using crossover_rate(). The crossover probability that will be used for simple callables will be the default crossover rate by default, but it can be changed using crossover_rate().

Parameters:

f – The crossover method to use.

inline const crossover::Crossover<T> &crossover_method() const & noexcept#
Returns:

The crossover operator used by the GA.

inline virtual void crossover_rate(Probability pc) noexcept final#

Set the crossover rate of the crossover operator used.

See also

crossover_method

Note

This crossover rate will be set for the current crossover method, and it will be changed if a new crossover method is set.

Parameters:

pc – The crossover probability. Must be in the closed interval [0.0, 1.0].

inline virtual Probability crossover_rate() const noexcept final#
Returns:

The crossover rate set for the crossover operator.

template<typename F>
inline void mutation_method(F f)#

Set the mutation method the GA will use. The mutation method should be thread-safe if parallel execution is enabled (true by default).

Note

As the mutation probability is associated with a mutation method, setting a new mutation method will override any previous mutation rate set using mutation_rate().

Parameters:

f – The mutation method to use.

inline void mutation_method(std::unique_ptr<mutation::Mutation<T>> f)#

Set the mutation method the GA will use. The mutation method should be thread-safe if parallel execution is enabled (true by default).

Note

As the mutation probability is associated with a mutation method, setting a new mutation method will override any previous mutation rate set using mutation_rate().

Parameters:

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

inline void mutation_method(MutationCallable f)#

Set the mutation method the GA will use. The mutation method should be thread-safe if parallel execution is enabled (true by default).

See also

MutationCallable

Note

As the mutation probability is associated with a mutation method, setting a new mutation method will override any previous mutation rate set using mutation_rate(). The mutation probability that will be used for simple callables will be the default (deduced) mutation rate by default, but it can be changed using mutation_rate().

Parameters:

f – The mutation method to use.

inline const mutation::Mutation<T> &mutation_method() const & noexcept#
Returns:

The mutation operator used by the GA.

inline virtual void mutation_rate(Probability pm) noexcept final#

Set the mutation rate of the mutation operator.

See also

mutation_method

Note

This mutation rate will be set for the current mutation method, and it will be changed if a new mutation method is set.

Parameters:

pm – The mutation probability. Must be in the closed interval [0.0, 1.0].

inline virtual Probability mutation_rate() const noexcept final#
Returns:

The mutation probability set for the mutation operator.

inline void repair_function(RepairCallable f)#

Set a repair function for the GA. This function will be invoked for each chromosome in every generation, after the chromosomes were mutated.

Using a repair function is optional, and no repair function is used in the GA by default. A nullptr can be used as the argument to this function to disable the use of a repair function.

The repair function should be thread-safe if parallel execution is enabled (true by default).

Parameters:

f – The repair function to use. Allowed to be a nullptr.

inline void cache_size(size_t generations) noexcept#

Set the number of generations whose solutions will be cached by the GA. This can be used to reduce the number of fitness function evaluations performed during the runs.

If not specified, or if generations is specified as 0, the default behaviour is to not cache any solutions. The cache will also be disabled when using a dynamic fitness function, regardless of the number specified for generations.

When using a cache, it is recommended to only cache a small number of generations (1 or 2), as larger values will typically have very little additional benefit. Using the cache should also be avoided for the real-encoded GA, since the cache hit rates will typically be very low due to the floating-point encoding used.

The cache is not kept between runs, and setting a new size will also clear the current cache.

Parameters:

generations – The number of generations to cache. Specifying 0 as the value will disable the cache.

inline const Candidates<T> &solutions() const & noexcept#
Returns:

The pareto-optimal solutions found by the GA. These are the optimal solutions of the last generation’s population if keep_all_optimal_solutions() is not set, otherwise it contains every optimal solution found during the run (the solution set is updated in every generation in this case).

inline const Population<T> &population() const & noexcept#
Returns:

The current population of the algorithm. This is the entire population, which may contain non optimal solutions, unlike the population returned by solutions().

Candidates<T> solve(
std::unique_ptr<FitnessFunctionBase<T>> fitness_function,
size_t generations,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of. Can’t be a nullptr.

  • generations – The maximum number of generations the run can last for.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

Candidates<T> solve(
std::unique_ptr<FitnessFunctionBase<T>> fitness_function,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of. Can’t be a nullptr.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

template<typename F>
Candidates<T> solve(
F fitness_function,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

template<typename F>
Candidates<T> solve(
F fitness_function,
size_t generations,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of.

  • generations – The maximum number of generations the run can last for.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

Candidates<T> solve(
std::unique_ptr<FitnessFunctionBase<T>> fitness_function,
BoundsVector<T> bounds,
size_t generations,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of. Can’t be a nullptr.

  • bounds – The lower and upper bounds of each of the chromosomes’ genes.

  • generations – The maximum number of generations the run can last for.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

Candidates<T> solve(
std::unique_ptr<FitnessFunctionBase<T>> fitness_function,
Bounds<T> bounds,
size_t generations,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of. Can’t be a nullptr.

  • bounds – The lower and upper bounds of a gene. The same bounds will be used for every gene of the chromosomes.

  • generations – The maximum number of generations the run can last for.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

Candidates<T> solve(
std::unique_ptr<FitnessFunctionBase<T>> fitness_function,
BoundsVector<T> bounds,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of. Can’t be a nullptr.

  • bounds – The lower and upper bounds of each of the chromosomes’ genes.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

Candidates<T> solve(
std::unique_ptr<FitnessFunctionBase<T>> fitness_function,
Bounds<T> bounds,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of. Can’t be a nullptr.

  • bounds – The lower and upper bounds of a gene. The same bounds will be used for every gene of the chromosomes.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

template<typename F>
Candidates<T> solve(
F fitness_function,
BoundsVector<T> bounds,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of.

  • bounds – The lower and upper bounds of each of the chromosomes’ genes.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

template<typename F>
Candidates<T> solve(
F fitness_function,
Bounds<T> bounds,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of.

  • bounds – The lower and upper bounds of a gene. The same bounds will be used for every gene of the chromosomes.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

template<typename F>
Candidates<T> solve(
F fitness_function,
BoundsVector<T> bounds,
size_t generations,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of.

  • bounds – The lower and upper bounds of each of the chromosomes’ genes.

  • generations – The maximum number of generations the run can last for.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

template<typename F>
Candidates<T> solve(
F fitness_function,
Bounds<T> bounds,
size_t generations,
Population<T> initial_population = {},
)#

Find the maximum of a fitness function using the genetic algorithm.

Specifying an initial population is optional and it may have any number of solutions, but only the first population_size() elements will be used if it’s greater than the population size set for the GA. If it has less candidates than the population size, the rest of the candidates will be generated using the generateCandidate() method.

Parameters:
  • fitness_function – The fitness function to find the maximum of.

  • bounds – The lower and upper bounds of a gene. The same bounds will be used for every gene of the chromosomes.

  • generations – The maximum number of generations the run can last for.

  • initial_population – The starting population to use as the first generation of the GA.

Returns:

The pareto-optimal solutions found (this is not the final population).

Private Functions

inline virtual void initialize()#

Initialize the derived genetic algorithm. This method will be called exactly once at the start of each run.

The implementation of this method should only contain the initialization logic specific to the derived class, as the initialization of the GA class is separate.

Implementing this in the derived classes is optional, the default implementation does nothing.

virtual Candidate<T> generateCandidate() const = 0#

Generate a candidate solution. This method will be used to generate the initial population of the GA if the initial population is not fully specified in solve().

Has to be implemented in the derived classes, and every generated candidate must be valid (eg. the chromosome sizes must be correct, its genes must be within the specified bounds etc.).

class GaInfo#

#include <core/ga_info.hpp>
class GaInfo#

The base class that all GAs are derived from. Contains all of the general properties of a genetic algorithm that does not depend on the encoding type.

GA implementations should use the GA class as their base class instead of inheriting from this class directly.

Move-only.

Subclassed by gapp::GA< BinaryGene >, gapp::GA< IntegerGene >, gapp::GA< PermutationGene >, gapp::GA< RealGene >, gapp::GA< T >

Public Types

using StopConditionCallable = std::function<bool(const GaInfo&)>#

The general callable type that can be used as a stop condition in the algorithm (when not using a stop condition that is derived from StopCondition). The function should return true when the algorithm should be stopped.

See also

stop_condition

using GaInfoCallback = std::function<void(const GaInfo&)>#

The type of a generic callback function that can be provided for the algorithm.

Public Functions

GaInfo(
Positive<size_t> population_size,
std::unique_ptr<algorithm::Algorithm> algorithm,
std::unique_ptr<stopping::StopCondition> stop_condition,
)#

Create a genetic algorithm.

Parameters:
  • population_size – The size of the population. Must be at least 1.

  • algorithm – The algorithm to use. Should be consistent with the objective function (ie. a single-objective algorithm should be used for single-objective fitness functions, and a multi-objective algorithm should be used for multi-objective problems).

  • stop_condition – The early-stop condition to use for the algorithm. The algorithm will stop when reaching the maximum number of generations set regardless of the stop condition specified here.

inline void population_size(Positive<size_t> size) noexcept#

Set the number of candidate solutions used in the population.

Parameters:

size – The number of candidates in the population. Must be at least 1.

inline size_t population_size() const noexcept#
Returns:

The number of candidate solutions in the population.

inline void max_gen(Positive<size_t> max_gen) noexcept#

Set the maximum number of generations a run can last for. The algorthm will always stop when reaching this generation, even if there is another stop-condition being used.

See also

stop_condition

Parameters:

max_gen – The maximum number of generations. Must be at least 1.

inline size_t max_gen() const noexcept#
Returns:

The maximum number of generations set for the algorithm.

virtual const FitnessFunctionInfo *fitness_function() const & noexcept = 0#
Returns:

The fitness function used. A nullptr is returned if no fitness function is set.

virtual size_t chrom_len() const noexcept = 0#
Returns:

The chromosome length used. Returns 0 if the chromosome length is unspecified (ie. no fitness function was set yet).

inline size_t num_objectives() const noexcept#
Returns:

The number of objectives of the fitness function.

inline const FitnessMatrix &fitness_matrix() const noexcept#
Returns:

The fitness matrix of the population. Each row of the matrix is the fitness vector of the corresponding solution in the population.

For example, fmat[0] is the fitness vector of the first member of the population.

size_t num_fitness_evals() const noexcept#
Returns:

The number of fitness evaluations performed during the run so far. This value is updated after every objective function evaluation.

inline size_t generation_cntr() const noexcept#
Returns:

The current generation’s number. This value will be in the range [0, max_gen), where 0 corresponds to the initial/first generation.

virtual void crossover_rate(Probability pc) noexcept = 0#

Set the crossover rate of the crossover operator used by the algorithm.

Parameters:

pc – The crossover probability. Must be in the closed interval [0.0, 1.0].

virtual Probability crossover_rate() const noexcept = 0#
Returns:

The crossover rate set for the crossover operator.

virtual void mutation_rate(Probability pm) noexcept = 0#

Set the mutation rate of the mutation operator used by the algorithm.

Parameters:

pm – The mutation probability. Must be in the closed interval [0.0, 1.0].

virtual Probability mutation_rate() const noexcept = 0#
Returns:

The mutation rate set for the mutation operator.

template<typename F>
inline void algorithm(F f)#

Set the algorithm used by the GA.

The algorithm type should be consistent with the fitness function (ie. it should be a single-objective algorithm for single-objective problems, and a multi-objective algorithm for multi-objective problems).

Parameters:

f – The algorithm used by the GA.

void algorithm(std::unique_ptr<algorithm::Algorithm> f)#

Set the algorithm used by the GA.

The algorithm type should be consistent with the fitness function (ie. it should be a single-objective algorithm for single-objective problems, and a multi-objective algorithm for multi-objective problems).

Parameters:

f – The algorithm used by the GA. The default algorithm will be used if it’s a nullptr.

void algorithm(std::nullptr_t)#

Clear the algorithm currently set for the GA

.

The

GA will use the default algorithm that is selected based on the number of objectives of the fitness functions.

inline const algorithm::Algorithm &algorithm() const & noexcept#
Returns:

The algorithm used by the GA.

template<typename F>
inline void stop_condition(F f)#

Set an early-stop condition for the algorithm.

This is an optional early-stop condition that can be used to stop the run before the maximum number of generations is reached, but the algorithm will always stop when reaching the maximum generations regardless of the stop condition set here.

Parameters:

f – The early stop condition the GA should use.

void stop_condition(std::unique_ptr<stopping::StopCondition> f)#

Set an early-stop condition for the algorithm.

This is an optional early-stop condition that can be used to stop the run before the maximum number of generations is reached, but the algorithm will always stop when reaching the maximum generations regardless of the stop condition set here.

Parameters:

f – The stop condition the GA should use. No early-stopping will be used if it’s a nullptr.

void stop_condition(std::nullptr_t)#

Clear the early-stop condition currently set for the GA

.

The

GA will run for the maximum number of generations set without the possibility of stopping earlier.

void stop_condition(StopConditionCallable f)#

Set an early-stop condition for the algorithm.

This is an optional early-stop condition that can be used to stop the run before the maximum number of generations is reached, but the algorithm will always stop when reaching the maximum generations regardless of the stop condition set here.

Parameters:

f – The function used to check for the early-stop condition.

inline const stopping::StopCondition &stop_condition() const & noexcept#
Returns:

The stop condition used by the GA.

template<typename ...Metrics>
inline void track(Metrics... metrics)#

Set a number of metrics to track throughout the run of the algorithm. The values of these metrics will be computed and saved for each generation of the run.

Example:

GA.track(metrics::FitnessMin{}, metrics::FitnessMax{});
GA.solve(...);
const auto& fmin = GA.get_metric<metrics::FitnessMin>();

The same metric type may be present multiple times in the parameters, but it is unspecified which one get_metric() will return in this case.

Warning

Setting a new set of metrics to be tracked will invalidate all references returned by get_metric().

Parameters:

metrics – The metrics to track during the runs of the algorithm.

template<typename Metric>
inline const Metric &get_metric() const noexcept#

Get one of the metrics tracked by the algorithm.

Example:

GA.track(metrics::FitnessMin{}, metrics::FitnessMax{});
GA.solve(...);
const auto& fmin = GA.get_metric<metrics::FitnessMin>();

If the set of tracked metrics contains the same metric multiple times, one of them will be returned, but it is unspecified which one.

Warning

The type parameter Metric must be the type of one of the metrics being tracked, otherwise the behaviour of this method is undefined.

Warning

Setting a new set of metrics to be tracked via track() will invalidate all references returned by this function.

Template Parameters:

Metric – The tracked metric to get.

Returns:

The metric of the specified type if it is tracked, otherwise undefined.

template<typename Metric>
inline const Metric *get_metric_if() const noexcept#

Get one of the metrics tracked by the algorithm.

This function is similar to the get_metric() method, but it returns a pointer to the metric, which will be a nullptr if the given metric is not being tracked by the GA.

Template Parameters:

Metric – The tracked metric to get.

Returns:

The metric of the specified type if it is tracked, or nullptr otherwise.

inline void keep_all_optimal_solutions(bool enable) noexcept#

When set to true, all pareto-optimal candidates found by the GA during a run will be kept and stored in the solutions set, regardless of which generation they were found in.

When set to false, the optimal solutions returned by the GA at the end of a run are the optimal solutions of the last generation only, optimal solutions found in earlier generations are discarded.

Disabled by default.

Note

This will most likely have no effect for single-objective problems, as they likely only have a single optimal solution.

Warning

Setting this to True can significantly increase the run-time of the GA.

Parameters:

enable – Whether all pareto optimal solutions should be kept.

inline bool keep_all_optimal_solutions() const noexcept#
Returns:

True if all pareto-optimal solutions are kept during a run.

inline void on_generation_end(GaInfoCallback f) noexcept#

Set a generic callback function that will be called exactly once at the end of each generation of a run.

Parameters:

f – The function that will be invoked at the end of each generation.

virtual ~GaInfo() noexcept#

Destructor.

Private Static Attributes

static constexpr size_t DEFAULT_POPSIZE = 100#

The default population size used in the GA if none is specified.