1#ifndef TOURNAMENT_SELECTION_H
2#define TOURNAMENT_SELECTION_H
22 template <
typename GeneType>
25 std::size_t tournament_size_m;
33 if (tournament_size_m == 0) {
34 throw std::invalid_argument(
"Tournament size must be at least 1.");
47 thread_local static std::random_device rd;
48 thread_local static std::mt19937 gen(rd());
50 std::size_t pop_size = population.
size();
51 std::uniform_int_distribution<std::size_t> distribution(0, pop_size - 1);
55 for (std::size_t i = 1; i < tournament_size_m; ++i) {
Represents a single candidate solution in the population.
double getFitness() const
Gets the current fitness score.
A container for a group of individuals in the genetic algorithm.
std::size_t size() const
Returns the total number of individuals in the population.
Interface for selection operators.
Performs k-way tournament selection.
std::size_t getTournamentSize() const
const Individual< GeneType > & select(const Population< GeneType > &population) override
Conducts a tournament and returns the winner.
TournamentSelection(size_t tournament_size)
Constructs the selector.