GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
ElitismSelector.h
Go to the documentation of this file.
1#ifndef ELITISM_SELECTOR_H
2#define ELITISM_SELECTOR_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 ElitismSelector : public DemeSelector<GeneType> {
25 public:
33 std::vector<Individual<GeneType>> selectDeme(
34 const Population<GeneType>& population,
35 std::size_t count
36 ) const override {
37 std::vector<std::size_t> indices(population.size());
38
39 std::iota(indices.begin(), indices.end(), 0);
40
41 count = std::min(count, population.size());
42
43 std::ranges::nth_element(
44 indices,
45 indices.begin() + count,
46 std::less{},
47 [&](const std::size_t i) -> const Individual<GeneType>& { return population[i]; }
48 );
49
50 std::vector<Individual<GeneType>> selectedDeme;
51 selectedDeme.reserve(count);
52 for (std::size_t i = 0; i < count; ++i) {
53 selectedDeme.push_back(population[indices[i]]);
54 }
55
56 return selectedDeme;
57 }
58 };
59
60}
61
62#endif // ELITISM_SELECTOR_H
Interface for choosing individuals to migrate out of the island.
Selection strategy that chooses the best individuals for migration.
std::vector< Individual< GeneType > > selectDeme(const Population< GeneType > &population, std::size_t count) const override
Selects the elite members of the 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