1#ifndef ARITHMETIC_CROSSOVER_H
2#define ARITHMETIC_CROSSOVER_H
23 template <
typename GeneType>
37 if (parent1.
size() != parent2.
size()) {
38 throw std::invalid_argument(
"Parents must have the same genotype size");
41 thread_local static std::random_device rd;
42 thread_local static std::mt19937 gen(rd());
43 thread_local static std::uniform_real_distribution<double> alpha_dist(0.0, 1.0);
45 double alpha = alpha_dist(gen);
56 for (std::size_t i = 0; i < g1.size(); ++i) {
57 c1[i] =
static_cast<GeneType
>(alpha * g1[i] + (1.0 - alpha) * g2[i]);
58 c2[i] =
static_cast<GeneType
>(alpha * g2[i] + (1.0 - alpha) * g1[i]);
61 return {child1, child2};
Performs arithmetic (convex) crossover.
std::pair< Individual< GeneType >, Individual< GeneType > > crossover(const Individual< GeneType > &parent1, const Individual< GeneType > &parent2) override
Produces children via linear interpolation of parent genes.
Interface for crossover (recombination) operators.
Represents a single candidate solution in the population.
std::size_t size() const
Returns the number of genes in the genotype.
std::vector< GeneType > & getGenotype()
Returns a reference to the genotype vector.