GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
Public Member Functions | List of all members
galib::GridPopulation< GeneType > Class Template Reference

A population organized in a 2D grid structure. More...

#include <GridPopulation.h>

Public Member Functions

 GridPopulation (std::size_t rows, std::size_t cols, std::size_t num_genes)
 Constructs a grid population with specified dimensions.
 
void initialize (GeneType lower, GeneType upper)
 Randomly initializes all individuals in the grid.
 
std::size_t size () const
 Returns the total number of individuals in the grid.
 
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.
 
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).
 
std::size_t rows () const
 Returns the number of rows.
 
std::size_t cols () const
 Returns the number of columns.
 
std::size_t getNumGenes () const
 Returns the number of genes for individuals in this population.
 
Individual< GeneType > & linearAt (std::size_t index)
 Accesses an individual via its linear index.
 
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::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.
 

Detailed Description

template<typename GeneType>
class galib::GridPopulation< GeneType >

A population organized in a 2D grid structure.

Primarily used by Cellular Genetic Algorithms where spatial locality and neighborhood relationships are important for selection.

Template Parameters
GeneTypeThe type of the genes.

Definition at line 23 of file GridPopulation.h.

Constructor & Destructor Documentation

◆ GridPopulation()

template<typename GeneType >
galib::GridPopulation< GeneType >::GridPopulation ( std::size_t  rows,
std::size_t  cols,
std::size_t  num_genes 
)
inline

Constructs a grid population with specified dimensions.

Parameters
rowsNumber of rows in the grid.
colsNumber of columns in the grid.
num_genesNumber of genes each individual should have.
Exceptions
std::invalid_argumentif dimensions are zero.

Definition at line 37 of file GridPopulation.h.

References galib::GridPopulation< GeneType >::cols(), and galib::GridPopulation< GeneType >::rows().

Member Function Documentation

◆ at() [1/2]

template<typename GeneType >
Individual< GeneType > & galib::GridPopulation< GeneType >::at ( std::size_t  row,
std::size_t  col 
)
inline

Accesses an individual at specific grid coordinates.

Parameters
rowGrid row.
colGrid column.
Returns
Reference to the individual.

Definition at line 92 of file GridPopulation.h.

References galib::GridPopulation< GeneType >::index().

◆ at() [2/2]

template<typename GeneType >
const Individual< GeneType > & galib::GridPopulation< GeneType >::at ( std::size_t  row,
std::size_t  col 
) const
inline

Accesses an individual at specific grid coordinates (const version).

Parameters
rowGrid row.
colGrid column.
Returns
Constant reference to the individual.

Definition at line 102 of file GridPopulation.h.

References galib::GridPopulation< GeneType >::index().

◆ cols()

template<typename GeneType >
std::size_t galib::GridPopulation< GeneType >::cols ( ) const
inline

Returns the number of columns.

Definition at line 110 of file GridPopulation.h.

Referenced by galib::GridPopulation< GeneType >::GridPopulation().

◆ empty()

template<typename GeneType >
bool galib::GridPopulation< GeneType >::empty ( ) const
inline

Checks if the population is empty.

Definition at line 145 of file GridPopulation.h.

References galib::Population< GeneType >::empty().

◆ getBestIndividual()

template<typename GeneType >
const Individual< GeneType > & galib::GridPopulation< GeneType >::getBestIndividual ( ) const
inline

Returns the best individual found in the entire grid.

Returns
Constant reference to the best individual.

Definition at line 68 of file GridPopulation.h.

References galib::Population< GeneType >::getBestIndividual().

◆ getNeighbors()

template<typename GeneType >
std::vector< std::pair< std::size_t, std::size_t > > galib::GridPopulation< GeneType >::getNeighbors ( std::size_t  row,
std::size_t  col 
) const
inline

Retrieves the 4-neighborhood (Up, Down, Left, Right) of a cell.

Uses periodic (toroidal) boundary conditions.

Parameters
rowCell row.
colCell column.
Returns
Vector of (row, col) pairs representing neighbors.

Definition at line 159 of file GridPopulation.h.

◆ getNumGenes()

template<typename GeneType >
std::size_t galib::GridPopulation< GeneType >::getNumGenes ( ) const
inline

Returns the number of genes for individuals in this population.

Returns
Gene count.

Definition at line 116 of file GridPopulation.h.

References galib::Population< GeneType >::getNumGenes().

◆ index()

template<typename GeneType >
std::size_t galib::GridPopulation< GeneType >::index ( std::size_t  row,
std::size_t  col 
) const
inline

Converts 2D grid coordinates to a 1D linear index.

Parameters
rowGrid row.
colGrid column.
Returns
Linear index in the underlying population.
Exceptions
std::out_of_rangeif coordinates are invalid.

Definition at line 79 of file GridPopulation.h.

Referenced by galib::GridPopulation< GeneType >::at(), galib::GridPopulation< GeneType >::at(), galib::GridPopulation< GeneType >::linearAt(), and galib::GridPopulation< GeneType >::linearAt().

◆ initialize()

template<typename GeneType >
void galib::GridPopulation< GeneType >::initialize ( GeneType  lower,
GeneType  upper 
)
inline

Randomly initializes all individuals in the grid.

Parameters
lowerLower bound for genes.
upperUpper bound for genes.

Definition at line 52 of file GridPopulation.h.

References galib::Population< GeneType >::initialize().

◆ linearAt() [1/2]

template<typename GeneType >
Individual< GeneType > & galib::GridPopulation< GeneType >::linearAt ( std::size_t  index)
inline

Accesses an individual via its linear index.

Parameters
indexLinear index.
Returns
Reference to the individual.

Definition at line 125 of file GridPopulation.h.

References galib::GridPopulation< GeneType >::index(), and galib::Population< GeneType >::size().

◆ linearAt() [2/2]

template<typename GeneType >
const Individual< GeneType > & galib::GridPopulation< GeneType >::linearAt ( std::size_t  index) const
inline

Accesses an individual via its linear index (const version).

Parameters
indexLinear index.
Returns
Constant reference to the individual.

Definition at line 137 of file GridPopulation.h.

References galib::GridPopulation< GeneType >::index(), and galib::Population< GeneType >::size().

◆ rows()

template<typename GeneType >
std::size_t galib::GridPopulation< GeneType >::rows ( ) const
inline

Returns the number of rows.

Definition at line 107 of file GridPopulation.h.

Referenced by galib::GridPopulation< GeneType >::GridPopulation().

◆ size()

template<typename GeneType >
std::size_t galib::GridPopulation< GeneType >::size ( ) const
inline

Returns the total number of individuals in the grid.

Returns
rows * cols.

Definition at line 60 of file GridPopulation.h.

References galib::Population< GeneType >::size().


The documentation for this class was generated from the following file: