GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
WorstReplacer.h
Go to the documentation of this file.
1#ifndef WORST_REPLACER_H
2#define WORST_REPLACER_H
3
4#pragma once
5
7#include "core/Individual.h"
8#include "core/Population.h"
9#include <vector>
10#include <numeric>
11#include <algorithm>
12
13namespace galib {
14
23 template<typename GeneType>
24 class WorstReplacer : public DemeReplacer<GeneType> {
25 public:
33 Population<GeneType>& population,
34 std::vector<Individual<GeneType>>&& deme
35 ) const override {
36 const std::size_t count = std::min(deme.size(), population.size());
37
38 std::vector<std::size_t> indices(population.size());
39
40 std::iota(indices.begin(), indices.end(), 0);
41
42 std::ranges::nth_element(
43 indices,
44 indices.begin() + count,
45 std::greater{},
46 [&](const std::size_t i) { return population[i]; }
47 );
48
49 for (std::size_t i = 0; i < count; ++i) {
50 population[indices[i]] = std::move(deme[i]);
51 }
52 }
53 };
54
55}
56
57#endif //WORST_REPLACER_H
Interface for integrating incoming migrants into the local population.
Represents a single candidate solution in the population.
Definition Individual.h:23
A container for a group of individuals in the genetic algorithm.
Definition Population.h:24
std::size_t size() const
Returns the total number of individuals in the population.
Definition Population.h:68
Replacement strategy that replaces the least fit individuals.
void replaceDeme(Population< GeneType > &population, std::vector< Individual< GeneType > > &&deme) const override
Replaces the worst local individuals.