fpmas 1.6
Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | List of all members
fpmas::model::detail::GridBuilder< CellType > Class Template Referenceabstract

#include <grid_builder.h>

Inheritance diagram for fpmas::model::detail::GridBuilder< CellType >:
Inheritance graph
[legend]
Collaboration diagram for fpmas::model::detail::GridBuilder< CellType >:
Collaboration graph
[legend]

Public Member Functions

 GridBuilder (api::model::GridCellFactory< CellType > &cell_factory, DiscreteCoordinate width, DiscreteCoordinate height)
 
 GridBuilder (DiscreteCoordinate width, DiscreteCoordinate height)
 
DiscreteCoordinate width () const
 
DiscreteCoordinate height () const
 
std::vector< CellType * > build (api::model::SpatialModel< CellType > &spatial_model) const override
 
std::vector< CellType * > build (api::model::SpatialModel< CellType > &spatial_model, api::model::GroupList groups) const override
 
void initSample (std::size_t n, std::function< void(CellType *)> init_function) const
 
template<typename T >
void initSequence (const std::vector< T > &items, std::function< void(CellType *, typename std::vector< T >::const_reference)> init_function) const
 
virtual std::vector< CellType * > build (SpatialModel< CellType > &spatial_model) const =0
 
virtual std::vector< CellType * > build (SpatialModel< CellType > &spatial_model, GroupList groups) const =0
 

Static Public Attributes

static GridCellFactory< CellType > default_cell_factory
 

Protected Types

typedef std::vector< std::vector< CellType * > > CellMatrix
 

Protected Member Functions

virtual void buildLocalGrid (api::model::SpatialModel< CellType > &model, GridDimensions local_dimensions, CellMatrix &cells) const =0
 
virtual void linkFrontiers (api::model::SpatialModel< CellType > &model, GridDimensions local_dimensions, CellMatrix &local_cells, std::vector< CellType * > &frontier) const =0
 

Detailed Description

template<typename CellType>
class fpmas::model::detail::GridBuilder< CellType >

Grid builder base class.

This class defines a generic algorithm that can be used to build VonNeumann or Moore grids.

See also
VonNeumannGridBuilder
MooreGridBuilder

Member Typedef Documentation

◆ CellMatrix

template<typename CellType >
typedef std::vector<std::vector<CellType*> > fpmas::model::detail::GridBuilder< CellType >::CellMatrix
protected

Type used to describe a cell matrix.

By convention, the first index represent lines, the second represents columns.

Constructor & Destructor Documentation

◆ GridBuilder() [1/2]

template<typename CellType >
fpmas::model::detail::GridBuilder< CellType >::GridBuilder ( api::model::GridCellFactory< CellType > &  cell_factory,
DiscreteCoordinate  width,
DiscreteCoordinate  height 
)
inline

GridBuilder constructor.

The cell_factory parameter allows to build the grid using custom GridCell extensions (with extra user defined behaviors for example).

Parameters
cell_factorycustom cell factory, that will be used instead of default_cell_factory
widthwidth of the grid
heightheight of the grid

◆ GridBuilder() [2/2]

template<typename CellType >
fpmas::model::detail::GridBuilder< CellType >::GridBuilder ( DiscreteCoordinate  width,
DiscreteCoordinate  height 
)
inline

GridBuilder constructor.

The grid will be built using the default_cell_factory.

Parameters
widthwidth of the grid
heightheight of the grid

Member Function Documentation

◆ buildLocalGrid()

template<typename CellType >
virtual void fpmas::model::detail::GridBuilder< CellType >::buildLocalGrid ( api::model::SpatialModel< CellType > &  model,
GridDimensions  local_dimensions,
CellMatrix cells 
) const
protectedpure virtual

Builds links between cells on the current process to build the required grid shape.

The specified CellMatrix shape is [local_dimensions.height()][local_dimensions.width()], and cells locations are initialized accordingly, i.e. the cell at cells[j][i] is located at DiscretePoint(i, j). (j = line index = y coordinate, i = column index = x coordinate)

Links across processes should not be considered in this method.

Parameters
modelmodel in which cells are built
local_dimensionsdimension of the local grid, that is only a subpart of the global grid
cellsmatrix containing LOCAL cells built on the current process

◆ linkFrontiers()

template<typename CellType >
virtual void fpmas::model::detail::GridBuilder< CellType >::linkFrontiers ( api::model::SpatialModel< CellType > &  model,
GridDimensions  local_dimensions,
CellMatrix local_cells,
std::vector< CellType * > &  frontier 
) const
protectedpure virtual

Links the DISTANT cells in frontier to the appropriate local_cells to build the required grid shape.

The order of cells in frontier is not guaranteed, but it is ensured that the provided DISTANT cells complete, if required, the Moore neighborhood of all cells in local_cells.

For example, if local_dimensions and local_cells provide the following LOCAL cells:

OOOO
OOOO

then the frontier corresponds to the following cells:

XXXXXX
X X
X X
XXXXXX

In this example, angles are provided even if the implementation is supposed to build the VonNeumann neighborhood of each cells. In other terms, it is safe to ignore cells provided in the frontier.

Parameters
modelmodel into which SUCCESSOR links must be built
local_dimensionsgrid dimensions describing the grid part built on the current process
local_cellsmatrix containing LOCAL cells built on the current process
frontiera list of DISTANT cells that can be linked to local_cells

◆ width()

template<typename CellType >
DiscreteCoordinate fpmas::model::detail::GridBuilder< CellType >::width ( ) const
inline

Grid width.

◆ height()

template<typename CellType >
DiscreteCoordinate fpmas::model::detail::GridBuilder< CellType >::height ( ) const
inline

Grid height.

◆ build() [1/2]

template<typename CellType >
std::vector< CellType * > fpmas::model::detail::GridBuilder< CellType >::build ( api::model::SpatialModel< CellType > &  spatial_model) const
inlineoverridevirtual

Builds a grid into the specified spatial_model, according to the grid shape specified by the buildLocalGrid(), linkHorizontalFrontiers() and linkVerticalFrontiers() methods.

The process is distributed so that each available process instantiates a part of the global grid.

The size of the global grid is equal to width x height, according to the parameters specified in the constructor. The origin of the grid is considered at (0, 0), and so the opposite corner is at (width-1, height-1).

Each GridCell is built using the cell_factory specified in the constructor, the default_cell_factory otherwise.

Parameters
spatial_modelspatial model in which cells will be added
Returns
cells built on the current process

Implements fpmas::api::model::CellNetworkBuilder< CellType >.

◆ build() [2/2]

template<typename CellType >
std::vector< CellType * > fpmas::model::detail::GridBuilder< CellType >::build ( api::model::SpatialModel< CellType > &  spatial_model,
api::model::GroupList  groups 
) const
inlineoverridevirtual

Same as build(spatial_model), but also adds built cells to groups, in order to assign behaviors to cells.

Parameters
spatial_modelspatial model in which cells will be added
groupsgroups to which built cells will be added
Returns
cells built on the current process

Implements fpmas::api::model::CellNetworkBuilder< CellType >.

◆ initSample()

template<typename CellType >
void fpmas::model::detail::GridBuilder< CellType >::initSample ( std::size_t  n,
std::function< void(CellType *)>  init_function 
) const

Initializes a sample of n cells selected from the previously built cells with the provided init_function.

The sample of cells selected is deterministic: it is guaranteed that the same cells are initialized independently of the current distribution.

The selection process can be seeded with fpmas::seed().

Successive calls can be used to independently initialize several cell states.

Example
grid_builder.build(
grid_model,
{cell_group}
);
// Sets 10 random cells to be GROWN
grid_builder.initSample(
10, [] (UserCell* cell) {
cell->setState(GROWN);
}
);
// Sets 15 random agents to be RED
grid_builder.initSample(
15, [] (UserCell* cell) {
cell->setColor(RED);
}
);
Definition: moore_grid.h:47
DiscreteCoordinate width() const
Definition: grid_builder.h:176
DiscreteCoordinate height() const
Definition: grid_builder.h:182
Parameters
nsample size
init_functioninitialization function

◆ initSequence()

template<typename CellType >
template<typename T >
void fpmas::model::detail::GridBuilder< CellType >::initSequence ( const std::vector< T > &  items,
std::function< void(CellType *, typename std::vector< T >::const_reference)>  init_function 
) const

Sequentially initializes built cells from the input items.

Each item is assigned to a cell using the specified init_function.

The item assignment is deterministic: it is guaranteed that each cell is always initialized with the same item independently of the current distribution.

Example
grid_builder.build(
grid_model,
{cell_group}
);
// Vector containing an item for each cell
std::vector<unsigned long> items(width * height);
// items initialization
...
// Assign items to agents
grid_builder.initSequence(
items, [] (
UserCell* cell,
const unsigned long& item
) {
cell->setData(item);
}
);
Parameters
itemsItems to assign to built cells. The number of items must be greater or equal to the total number of built cells, N=width()*height(). Only the first N items are assigned, other are ignored.
init_functionitem assignment function

Member Data Documentation

◆ default_cell_factory

template<typename CellType >
GridCellFactory< CellType > fpmas::model::detail::GridBuilder< CellType >::default_cell_factory
static

Default GridCellFactory.


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