Crossovers#

namespace crossover#

Crossover operators used in the GAs.

class Crossover#

#include <crossover/crossover_base.hpp>
template<typename T>
class Crossover#

The base class used for the crossover operators of the GAs.

Crossover operators take 2 candidate solutions (the parents), and create 2 new candidates (children) based on the the parent candidates. The crossover operation is only performed on the 2 parents with a set probability only, the rest of the time the returned children will be the same as the parents.

New crossover operators should be derived from this class, and they must implement the following virtual method:

  • crossover : Perform the crossover on 2 candidate solutions.

Template Parameters:

The – gene type the crossover operator is defined for.

Subclassed by gapp::crossover::Lambda< T >

Public Types

using GeneType = T#

The gene type the crossover operator is defined for.

Public Functions

inline explicit constexpr Crossover() noexcept#

Create a crossover operator using the default crossover probability.

inline explicit constexpr Crossover(Probability pc) noexcept#

Create a crossover operator.

Parameters:

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

inline constexpr void crossover_rate(Probability pc) noexcept#

Set the crossover probability used for the crossovers.

Parameters:

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

inline constexpr Probability crossover_rate() const noexcept#
Returns:

The crossover rate set for the operator.

inline virtual constexpr virtual bool allow_variable_chrom_length () const noexcept

This method specifies whether the crossover operator supports variable chromosome lengths or not. If variable chromosome lengths are supported, the Candidates passed to the crossover operator are allowed to have chromosome lengths different from eachother, and from the chromosome length specified for the GA the operator is used in. Otherwise the chromosome length of every Candidate must be the same.

This method will return false by default. If a particular crossover method allows variable chromosome lengths, it should override this method to return true.

Returns:

True if the crossover operator support variable chromosome lengths.

CandidatePair<T> operator()(
const GA<T> &ga,
const Candidate<T> &parent1,
const Candidate<T> &parent2,
) const#

Perform the crossover operation on 2 candidate solutions with the set probability. This function is implemented by crossover().

Parameters:
  • ga – The genetic algorithm the crossover operator is being used in.

  • parent1 – The first parent solution.

  • parent2 – The second parent solution.

Returns:

The pair of children resulting from the crossover.

virtual ~Crossover() = default#

Destructor.

Private Functions

virtual CandidatePair<T> crossover(
const GA<T> &ga,
const Candidate<T> &parent1,
const Candidate<T> &parent2,
) const = 0#

The implementation of the crossover operator. Performs the crossover operation on 2 parent solutions to generate 2 child solutions from them. The implementation of this function shouldn’t handle the crossover probability, instead it should just perform the crossover operation unconditionally. The chromosomes of the returned children should be valid solutions for the given problem and GA, but the rest of their properties (eg. fitness) are irrelevant.

This method will be called once for every 2 children that need to be generated (ie. population_size/2 number of times, rounded up if the population size is odd) in every generation.

The implementation of this function must be thread-safe.

Parameters:
  • ga – The genetic algorithm the crossover operator is being used in.

  • parent1 – The first parent solution.

  • parent2 – The second parent solution.

Returns:

The pair of children resulting from the crossover.