pulsatrix
Loading...
Searching...
No Matches
survivor_selection.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include <algorithm>
14#include <stdexcept>
15#include <vector>
16
18
19namespace pulsatrix {
20
26template <typename Genotype, typename FitnessT>
27std::vector<Individual<Genotype, FitnessT>> GenerationalReplacement(
28 const std::vector<Individual<Genotype, FitnessT>>& /*population*/,
29 std::vector<Individual<Genotype, FitnessT>> offspring, size_t mu) {
30 if (offspring.size() != mu) {
31 throw std::invalid_argument("GenerationalReplacement: offspring.size() must equal mu");
32 }
33 return offspring;
34}
35
42template <typename Genotype, typename FitnessT>
43std::vector<Individual<Genotype, FitnessT>> MuPlusLambdaReplacement(
44 const std::vector<Individual<Genotype, FitnessT>>& population,
45 std::vector<Individual<Genotype, FitnessT>> offspring, size_t mu) {
46 if (population.size() + offspring.size() < mu) {
47 throw std::invalid_argument(
48 "MuPlusLambdaReplacement: population.size() + offspring.size() must be >= mu");
49 }
50 std::vector<Individual<Genotype, FitnessT>> combined = population;
51 combined.insert(combined.end(), std::make_move_iterator(offspring.begin()),
52 std::make_move_iterator(offspring.end()));
53 std::partial_sort(combined.begin(), combined.begin() + static_cast<std::ptrdiff_t>(mu),
54 combined.end(),
55 [](const auto& a, const auto& b) { return a.fitness > b.fitness; });
56 combined.resize(mu);
57 return combined;
58}
59
66template <typename Genotype, typename FitnessT>
67std::vector<Individual<Genotype, FitnessT>> MuCommaLambdaReplacement(
68 const std::vector<Individual<Genotype, FitnessT>>& /*population*/,
69 std::vector<Individual<Genotype, FitnessT>> offspring, size_t mu) {
70 if (offspring.size() < mu) {
71 throw std::invalid_argument("MuCommaLambdaReplacement: offspring.size() must be >= mu");
72 }
73 std::partial_sort(offspring.begin(), offspring.begin() + static_cast<std::ptrdiff_t>(mu),
74 offspring.end(),
75 [](const auto& a, const auto& b) { return a.fitness > b.fitness; });
76 offspring.resize(mu);
77 return offspring;
78}
79
80} // namespace pulsatrix
A genetic-algorithm candidate solution: a genotype paired with its fitness.
Definition acquisition_functions.hpp:16
std::vector< Individual< Genotype, FitnessT > > GenerationalReplacement(const std::vector< Individual< Genotype, FitnessT > > &, std::vector< Individual< Genotype, FitnessT > > offspring, size_t mu)
Generational replacement (DEAP's eaSimple): the offspring pool becomes the entire next generation; po...
Definition survivor_selection.hpp:27
std::vector< Individual< Genotype, FitnessT > > MuCommaLambdaReplacement(const std::vector< Individual< Genotype, FitnessT > > &, std::vector< Individual< Genotype, FitnessT > > offspring, size_t mu)
(mu,lambda) replacement: the next generation is the fittest mu individuals from offspring only (popul...
Definition survivor_selection.hpp:67
std::vector< Individual< Genotype, FitnessT > > MuPlusLambdaReplacement(const std::vector< Individual< Genotype, FitnessT > > &population, std::vector< Individual< Genotype, FitnessT > > offspring, size_t mu)
(mu+lambda) replacement: the next generation is the fittest mu individuals from population union offs...
Definition survivor_selection.hpp:43
A single candidate solution in a genetic algorithm population.
Definition individual.hpp:25