1#ifndef GRIDPOPULATION_H
2#define GRIDPOPULATION_H
22template <
typename GeneType>
43 throw std::invalid_argument(
"Grid dimensions cannot be zero");
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");
83 return row * cols_m + col;
93 return data_[
index(row, col)];
103 return data_[
index(row, col)];
107 std::size_t
rows()
const {
return rows_m; }
110 std::size_t
cols()
const {
return cols_m; }
127 throw std::out_of_range(
"GridPopulation linear index out of range");
139 throw std::out_of_range(
"GridPopulation linear index out of range");
146 return data_.
empty();
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;
161 if (row >= rows_m || col >= cols_m) {
162 throw std::out_of_range(
"GridPopulation neighbor index out of range");
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;
171 neighbors.push_back({up, col});
172 neighbors.push_back({down, col});
173 neighbors.push_back({row, left});
174 neighbors.push_back({row, right});
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.
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.
const Individual< GeneType > & getBestIndividual() const
Finds and returns the individual with the best fitness score.
std::size_t size() const
Returns the total number of individuals in the population.