GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
GridPopulation.h
Go to the documentation of this file.
1#ifndef GRIDPOPULATION_H
2#define GRIDPOPULATION_H
3
4
5#pragma once
6#include "Population.h"
7#include <vector>
8#include <utility>
9#include <cstddef>
10#include <stdexcept>
11
12
13namespace galib {
22template <typename GeneType>
24private:
26 std::size_t rows_m;
27 std::size_t cols_m;
28
29public:
37 GridPopulation(std::size_t rows, std::size_t cols, std::size_t num_genes)
38 : data_(rows * cols, num_genes),
39 rows_m(rows),
40 cols_m(cols) {
41
42 if (rows == 0 || cols == 0) {
43 throw std::invalid_argument("Grid dimensions cannot be zero");
44 }
45 }
46
52 void initialize(GeneType lower, GeneType upper) {
53 data_.initialize(lower, upper);
54 }
55
60 std::size_t size() const {
61 return data_.size();
62 }
63
69 return data_.getBestIndividual();
70 }
71
79 std::size_t index(std::size_t row, std::size_t col) const {
80 if (row >= rows_m || col >= cols_m) {
81 throw std::out_of_range("GridPopulation index out of range");
82 }
83 return row * cols_m + col;
84 }
85
92 Individual<GeneType>& at(std::size_t row, std::size_t col) {
93 return data_[index(row, col)];
94 }
95
102 const Individual<GeneType>& at(std::size_t row, std::size_t col) const {
103 return data_[index(row, col)];
104 }
105
107 std::size_t rows() const { return rows_m; }
108
110 std::size_t cols() const { return cols_m; }
111
116 std::size_t getNumGenes() const {
117 return data_.getNumGenes();
118 }
119
126 if (index >= data_.size()) {
127 throw std::out_of_range("GridPopulation linear index out of range");
128 }
129 return data_[index];
130 }
131
137 const Individual<GeneType>& linearAt(std::size_t index) const {
138 if (index >= data_.size()) {
139 throw std::out_of_range("GridPopulation linear index out of range");
140 }
141 return data_[index];
142 }
143
145 bool empty() const {
146 return data_.empty();
147 }
148
158 std::vector<std::pair<std::size_t, std::size_t>>
159 getNeighbors(std::size_t row, std::size_t col) const { std::vector<std::pair<std::size_t, std::size_t>> neighbors;
160
161 if (row >= rows_m || col >= cols_m) {
162 throw std::out_of_range("GridPopulation neighbor index out of range");
163 }
164
165
166 std::size_t up = (row == 0) ? rows_m - 1 : row - 1;
167 std::size_t down = (row + 1) % rows_m;
168 std::size_t left = (col == 0) ? cols_m - 1 : col - 1;
169 std::size_t right = (col + 1) % cols_m;
170
171 neighbors.push_back({up, col});
172 neighbors.push_back({down, col});
173 neighbors.push_back({row, left});
174 neighbors.push_back({row, right});
175
176 return neighbors;
177 }
178
179
180};
181}
182#endif // GRIDPOPULATION_H
A population organized in a 2D grid structure.
std::size_t size() const
Returns the total number of individuals in the grid.
std::size_t rows() const
Returns the number of rows.
Individual< GeneType > & at(std::size_t row, std::size_t col)
Accesses an individual at specific grid coordinates.
const Individual< GeneType > & at(std::size_t row, std::size_t col) const
Accesses an individual at specific grid coordinates (const version).
const Individual< GeneType > & linearAt(std::size_t index) const
Accesses an individual via its linear index (const version).
bool empty() const
Checks if the population is empty.
std::size_t getNumGenes() const
Returns the number of genes for individuals in this population.
std::size_t cols() const
Returns the number of columns.
const Individual< GeneType > & getBestIndividual() const
Returns the best individual found in the entire grid.
std::size_t index(std::size_t row, std::size_t col) const
Converts 2D grid coordinates to a 1D linear index.
GridPopulation(std::size_t rows, std::size_t cols, std::size_t num_genes)
Constructs a grid population with specified dimensions.
std::vector< std::pair< std::size_t, std::size_t > > getNeighbors(std::size_t row, std::size_t col) const
Retrieves the 4-neighborhood (Up, Down, Left, Right) of a cell.
void initialize(GeneType lower, GeneType upper)
Randomly initializes all individuals in the grid.
Individual< GeneType > & linearAt(std::size_t index)
Accesses an individual via its linear index.
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
void initialize(GeneType lower, GeneType upper)
Randomly initializes all individuals within the specified bounds.
Definition Population.h:48
bool empty() const
Checks if the population is empty.
Definition Population.h:74
std::size_t getNumGenes() const
Returns the number of genes for individuals in this population.
Definition Population.h:80
const Individual< GeneType > & getBestIndividual() const
Finds and returns the individual with the best fitness score.
Definition Population.h:112
std::size_t size() const
Returns the total number of individuals in the population.
Definition Population.h:68