23 template <
typename GeneType>
26 std::vector<Individual<GeneType>> individuals;
27 std::size_t num_genes_m;
35 explicit Population(std::size_t
size, std::size_t num_genes) : num_genes_m(num_genes) {
36 if (
size == 0)
throw std::invalid_argument(
"Population size cannot be zero");
51 std::random_device rd;
52 std::mt19937 rng(rd());
53 std::uniform_real_distribution<GeneType> distribution(lower, upper);
55 #pragma omp for schedule(static)
56 for (
int i = 0; i < static_cast<int>(individuals.size()); ++i) {
57 for (
auto& gene : individuals[i].getGenotype()) {
58 gene = distribution(rng);
68 [[nodiscard]] std::size_t
size()
const {
return individuals.size(); }
74 [[nodiscard]]
bool empty()
const {
return individuals.empty(); }
80 [[nodiscard]] std::size_t
getNumGenes()
const {
return num_genes_m; }
97 typename std::vector<Individual<GeneType>>::iterator
begin() {
return individuals.begin(); }
100 typename std::vector<Individual<GeneType>>::iterator
end() {
return individuals.end(); }
103 typename std::vector<Individual<GeneType>>::const_iterator
begin()
const {
return individuals.begin(); }
106 typename std::vector<Individual<GeneType>>::const_iterator
end()
const {
return individuals.end(); }
113 return *std::min_element(individuals.begin(), individuals.end());
Represents a single candidate solution in the population.
A container for a group of individuals in the genetic algorithm.
void initialize(GeneType lower, GeneType upper)
Randomly initializes all individuals within the specified bounds.
bool empty() const
Checks if the population is empty.
std::size_t getNumGenes() const
Returns the number of genes for individuals in this population.
Population(std::size_t size, std::size_t num_genes)
Constructs a population of a given size.
const Individual< GeneType > & getBestIndividual() const
Finds and returns the individual with the best fitness score.
std::vector< Individual< GeneType > >::iterator begin()
Returns an iterator to the beginning.
Individual< GeneType > & operator[](std::size_t index)
Accesses an individual by its index.
std::size_t size() const
Returns the total number of individuals in the population.
std::vector< Individual< GeneType > >::iterator end()
Returns an iterator to the end.
std::vector< Individual< GeneType > >::const_iterator end() const
Returns a constant iterator to the end.
std::vector< Individual< GeneType > >::const_iterator begin() const
Returns a constant iterator to the beginning.