Class Grid2D

Inheritance Relationships

Base Type

Derived Types

Class Documentation

class Grid2D : public kami::GridDomain

A two-dimensional grid where each cell may contain agents.

The grid is linear and may wrap around in its only dimension.

See also

MultiGrid2D

See also

SoloGrid2D

Subclassed by kami::MultiGrid2D, kami::SoloGrid2D

Public Functions

explicit Grid2D(unsigned int maximum_x, unsigned int maximum_y, bool wrap_x = false, bool wrap_y = false)

Constructor.

Parameters
  • maximum_x[in] the length of the grid in the first dimension

  • maximum_y[in] the length of the grid in the second dimension

  • wrap_x[in] should the grid wrap around on itself in the first dimension

  • wrap_y[in] should the grid wrap around on itself in the second dimension

virtual AgentID add_agent(AgentID agent_id, const GridCoord2D &coord) = 0

Place agent on the grid at the specified location.

Parameters
  • agent_id[in] the AgentID of the agent to add.

  • coord[in] the coordinates of the agent.

Returns

false if the agent is not placed at the specified location, otherwise, true.

AgentID delete_agent(AgentID agent_id)

Remove agent from the grid.

Parameters

agent_id[in] the AgentID of the agent to remove.

Returns

false if the agent is not removed, otherwise, true.

AgentID delete_agent(AgentID agent_id, const GridCoord2D &coord)

Remove agent from the grid at the specified location.

Parameters
  • agent_id[in] the AgentID of the agent to remove.

  • coord[in] the coordinates of the agent.

Returns

false if the agent is not removed, otherwise, true.

AgentID move_agent(AgentID agent_id, const GridCoord2D &coord)

Move an agent to the specified location.

Parameters
  • agent_id[in] the AgentID of the agent to move.

  • coord[in] the coordinates of the agent.

bool is_location_empty(const GridCoord2D &coord) const

Inquire if the specified location is empty.

Parameters

coord[in] the coordinates of the query.

Returns

true if the location has no Agents occupying it, false otherwise.

bool is_location_valid(const GridCoord2D &coord) const

Inquire if the specified location is valid within the grid.

Parameters

coord[in] the coordinates of the query.

Returns

true if the location specified is valid, false otherwise.

virtual GridCoord2D get_location_by_agent(const AgentID &agent_id) const

Get the location of the specified agent.

Parameters

agent_id[in] the AgentID of the agent in question.

Returns

the location of the specified Agent

std::shared_ptr<std::set<AgentID>> get_location_contents(const GridCoord2D &coord) const

Get the contents of the specified location.

Parameters

coord[in] the coordinates of the query.

Returns

a pointer to a set of AgentIDs. The pointer is to the internal copy of the agent list at the location, therefore, any changes to that object will update the state of the gird. Further, the pointer should not be deleted when no longer used.

bool get_wrap_x() const

Inquire to whether the grid wraps in the x dimension.

Returns

true if the grid wraps, and false otherwise

bool get_wrap_y() const

Inquire to whether the grid wraps in the y dimension.

Returns

true if the grid wraps, and false otherwise

virtual std::shared_ptr<std::unordered_set<GridCoord2D>> get_neighborhood(AgentID agent_id, bool include_center, GridNeighborhoodType neighborhood_type) const

Return the neighborhood of the specified Agent.

See also

NeighborhoodType

Parameters
  • agent_id[in] the AgentID of the agent in question.

  • neighborhood_type[in] the neighborhood type.

  • include_center[in] should the center-point, occupied by the agent, be in the list.

Returns

a set of GridCoord2D that includes all of the coordinates for all adjacent points.

std::shared_ptr<std::unordered_set<GridCoord2D>> get_neighborhood(const GridCoord2D &coord, bool include_center, GridNeighborhoodType neighborhood_type) const

Return the neighborhood of the specified location.

See also

NeighborhoodType

Parameters
  • coord[in] the coordinates of the specified location.

  • neighborhood_type[in] the neighborhood type.

  • include_center[in] should the center-point, occupied by the agent, be in the list.

Returns

a set of GridCoord2D that includes all of the coordinates for all adjacent points.

unsigned int get_maximum_x() const

Get the size of the grid in the x dimension.

Returns

the length of the grid in the x dimension

unsigned int get_maximum_y() const

Get the size of the grid in the y dimension.

Returns

the length of the grid in the `xy dimension

Protected Functions

GridCoord2D coord_wrap(const GridCoord2D &coord) const

Automatically adjust a coordinate location for wrapping.

Parameters

coord[in] the coordinates of the specified location.

Returns

the adjusted coordinate wrapped if appropriate.

Protected Attributes

const std::vector<GridCoord2D> directions_vonneumann = {GridCoord2D(0, 1), GridCoord2D(1, 0), GridCoord2D(0, -1), GridCoord2D(-1, 0)}

von Neumann neighborhood coordinates

This can be used for addition to coordinates. Direction 0 is the first direction clockwise from “vertical.” Then the additional directions are enumerated clockwise.

const std::vector<GridCoord2D> directions_moore = {GridCoord2D(0, 1), GridCoord2D(1, 1), GridCoord2D(1, 0), GridCoord2D(1, -1), GridCoord2D(0, -1), GridCoord2D(-1, -1), GridCoord2D(-1, 0), GridCoord2D(-1, 1)}

Moore neighborhood coordinates.

This can be used for addition to coordinates. Direction 0 is the first direction clockwise from “vertical.” Then the additional directions are enumerated clockwise.

std::unique_ptr<std::unordered_multimap<GridCoord2D, AgentID>> _agent_grid

A map containing the AgentIDs of all agents assigned to this grid.

std::unique_ptr<std::map<AgentID, GridCoord2D>> _agent_index

A map containing the grid location of each agent.