Fitness functions#

namespace gapp

class FitnessFunction#

#include <core/fitness_function.hpp>
template<typename T, size_t ChromLen>
class FitnessFunction : public gapp::FitnessFunctionBase<T>#

The base class of the fitness functions used in the algorithms. The fitness functions take a candidate solution (chromosome) as a parameter and return a fitness vector after evaluating the chromosome.

This should only be used as the base class for fitness functions if the chromosome length is known at compile time. If the chromosome length is not known at compile, use FitnessFunctionBase as the base class instead.

Template Parameters:
  • T – The gene type expected by the fitness function.

  • ChromLen – The length of the chromosomes expected by the fitness function. Must be at least 1. For variable chromosome length problems, this value will only be used during the generation of the initial population.

Public Functions

inline constexpr FitnessFunction(bool dynamic = false) noexcept#

Create a fitness function.

Parameters:

dynamic – Should be true if the fitness vector returned for a chromosome will not always be the same for the same chromosome (eg. it changes over time or isn’t deterministic).

class FitnessFunctionBase#

#include <core/fitness_function.hpp>
template<typename T>
class FitnessFunctionBase : public gapp::FitnessFunctionInfo#

The base class of the fitness functions used in the GAs. The fitness functions take a candidate solution (chromosome) as a parameter and return a fitness vector after evaluating the chromosome.

This should be used as the base class for fitness functions if the chromosome length is not known at compile time. If the chromosome length is known at compile, use FitnessFunction as the base class instead.

Template Parameters:

T – The gene type expected by the fitness function.

Subclassed by gapp::problems::BenchmarkFunction< IntegerGene >, gapp::problems::BenchmarkFunction< PermutationGene >, gapp::FitnessFunction< T, ChromLen >, gapp::detail::FitnessLambda< T >, gapp::problems::BenchmarkFunction< T >

Public Types

using GeneType = T#

The gene type of the chromosomes that can be evaluated by the fitness function.

Public Functions

inline FitnessVector operator()(const Chromosome<T> &chrom) const#

Compute the fitness value of a chromosome.

The size of the chromosome must be equal to the chromosome length set for the fitness function, unless variable chromosome lengths are allowed.

Parameters:

chrom – The chromosome to evaluate.

Returns:

The fitness vector of the chromosome, with a size equal to the number of objectives.

inline constexpr FitnessFunctionInfo(
Positive<size_t> chrom_len,
bool is_dynamic = false,
) noexcept#

Create a fitness function.

Parameters:
  • chrom_len – The chromosome length that is expected by the fitness function, and will be used for the candidate solutions in the GA

    .

    Must be at least 1, and a value must be specified even if the chromosome lengths are variable, as it will still be used to generate the initial population.

  • is_dynamic – Should be true if the fitness vector returned for a chromosome will not always be the same for the same chromosome (eg. it changes over time or isn’t deterministic).

Private Functions

virtual FitnessVector invoke(const Chromosome<T> &chrom) const = 0#

The implementation of the fitness function. Should be thread-safe.