Mesa Space Module#
Objects used to add a spatial component to a model.
Grid: base grid, which creates a rectangular grid. SingleGrid: extension to Grid which strictly enforces one agent per cell. MultiGrid: extension to Grid where each cell can contain a set of agents. HexGrid: extension to Grid to handle hexagonal neighbors. ContinuousSpace: a two-dimensional space where each agent has an arbitrary
position of float’s.
NetworkGrid: a network where each node contains zero or more agents.
- accept_tuple_argument(wrapped_function: F) F [source]#
Decorator to allow grid methods that take a list of (x, y) coord tuples to also handle a single position, by automatically wrapping tuple in single-item list rather than forcing user to do it.
- class SingleGrid(width: int, height: int, torus: bool)[source]#
Rectangular grid where each cell contains exactly at most one agent.
Grid cells are indexed by [x, y], where [0, 0] is assumed to be the bottom-left and [width-1, height-1] is the top-right. If a grid is toroidal, the top and bottom, and left and right, edges wrap to each other.
This class provides a property empties that returns a set of coordinates for all empty cells in the grid. It is automatically updated whenever agents are added or removed from the grid. The empties property should be used for efficient access to current empty cells rather than manually iterating over the grid to check for emptiness.
- Properties:
width, height: The grid’s width and height. torus: Boolean which determines whether to treat the grid as a torus. empties: Returns a set of (x, y) tuples for all empty cells. This set is
maintained internally and provides a performant way to query the grid for empty spaces.
Create a new grid.
- Args:
width, height: The width and height of the grid torus: Boolean whether the grid wraps or not.
- place_agent(agent: Agent, pos: Tuple[int, int]) None [source]#
Place the agent at the specified location, and set its pos variable.
- remove_agent(agent: Agent) None [source]#
Remove the agent from the grid and set its pos attribute to None.
- coord_iter() Iterator[tuple[Agent | None, Tuple[int, int]]] #
An iterator that returns positions as well as cell contents.
- get_neighborhood(pos: Tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) Sequence[Tuple[int, int]] #
Return a list of cells that are in the neighborhood of a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. moore: If True, return Moore neighborhood
(including diagonals) If False, return Von Neumann neighborhood (exclude diagonals)
- include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of coordinate tuples representing the neighborhood; With radius 1, at most 9 if Moore, 5 if Von Neumann (8 and 4 if not including the center).
- get_neighbors(pos: Tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) list[Agent] #
Return a list of neighbors to a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. moore: If True, return Moore neighborhood
(including diagonals)
- If False, return Von Neumann neighborhood
(exclude diagonals)
- include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of non-None objects in the given neighborhood; at most 9 if Moore, 5 if Von-Neumann (8 and 4 if not including the center).
- iter_neighborhood(pos: Tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) Iterator[Tuple[int, int]] #
Return an iterator over cell coordinates that are in the neighborhood of a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. moore: If True, return Moore neighborhood
(including diagonals)
- If False, return Von Neumann neighborhood
(exclude diagonals)
- include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of coordinate tuples representing the neighborhood. For example with radius 1, it will return list with number of elements equals at most 9 (8) if Moore, 5 (4) if Von Neumann (if not including the center).
- iter_neighbors(pos: Tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) Iterator[Agent] #
Return an iterator over neighbors to a certain point.
- Args:
pos: Coordinates for the neighborhood to get. moore: If True, return Moore neighborhood
(including diagonals)
- If False, return Von Neumann neighborhood
(exclude diagonals)
- include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of non-None objects in the given neighborhood; at most 9 if Moore, 5 if Von-Neumann (8 and 4 if not including the center).
- move_agent(agent: Agent, pos: Tuple[int, int]) None #
Move an agent from its current position to a new position.
- Args:
- agent: Agent object to move. Assumed to have its current location
stored in a ‘pos’ tuple.
pos: Tuple of new position to move the agent to.
- class MultiGrid(width: int, height: int, torus: bool)[source]#
Rectangular grid where each cell can contain more than one agent.
Grid cells are indexed by [x, y], where [0, 0] is assumed to be at bottom-left and [width-1, height-1] is the top-right. If a grid is toroidal, the top and bottom, and left and right, edges wrap to each other.
This class maintains an empties property, which is a set of coordinates for all cells that currently contain no agents. This property is updated automatically as agents are added to or removed from the grid.
- Properties:
width, height: The grid’s width and height. torus: Boolean which determines whether to treat the grid as a torus. empties: Returns a set of (x, y) tuples for all empty cells.
Create a new grid.
- Args:
width, height: The width and height of the grid torus: Boolean whether the grid wraps or not.
- place_agent(agent: Agent, pos: Tuple[int, int]) None [source]#
Place the agent at the specified location, and set its pos variable.
- remove_agent(agent: Agent) None [source]#
Remove the agent from the given location and set its pos attribute to None.
- coord_iter() Iterator[tuple[Agent | None, Tuple[int, int]]] #
An iterator that returns positions as well as cell contents.
- get_neighborhood(pos: Tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) Sequence[Tuple[int, int]] #
Return a list of cells that are in the neighborhood of a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. moore: If True, return Moore neighborhood
(including diagonals) If False, return Von Neumann neighborhood (exclude diagonals)
- include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of coordinate tuples representing the neighborhood; With radius 1, at most 9 if Moore, 5 if Von Neumann (8 and 4 if not including the center).
- get_neighbors(pos: Tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) list[Agent] #
Return a list of neighbors to a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. moore: If True, return Moore neighborhood
(including diagonals)
- If False, return Von Neumann neighborhood
(exclude diagonals)
- include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of non-None objects in the given neighborhood; at most 9 if Moore, 5 if Von-Neumann (8 and 4 if not including the center).
- iter_neighborhood(pos: Tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) Iterator[Tuple[int, int]] #
Return an iterator over cell coordinates that are in the neighborhood of a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. moore: If True, return Moore neighborhood
(including diagonals)
- If False, return Von Neumann neighborhood
(exclude diagonals)
- include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of coordinate tuples representing the neighborhood. For example with radius 1, it will return list with number of elements equals at most 9 (8) if Moore, 5 (4) if Von Neumann (if not including the center).
- iter_neighbors(pos: Tuple[int, int], moore: bool, include_center: bool = False, radius: int = 1) Iterator[Agent] #
Return an iterator over neighbors to a certain point.
- Args:
pos: Coordinates for the neighborhood to get. moore: If True, return Moore neighborhood
(including diagonals)
- If False, return Von Neumann neighborhood
(exclude diagonals)
- include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of non-None objects in the given neighborhood; at most 9 if Moore, 5 if Von-Neumann (8 and 4 if not including the center).
- move_agent(agent: Agent, pos: Tuple[int, int]) None #
Move an agent from its current position to a new position.
- Args:
- agent: Agent object to move. Assumed to have its current location
stored in a ‘pos’ tuple.
pos: Tuple of new position to move the agent to.
- class HexSingleGrid(width: int, height: int, torus: bool)[source]#
Hexagonal SingleGrid: a SingleGrid where neighbors are computed according to a hexagonal tiling of the grid.
Functions according to odd-q rules. See http://www.redblobgames.com/grids/hexagons/#coordinates for more.
This class also maintains an empties property, similar to SingleGrid, which provides a set of coordinates for all empty hexagonal cells.
- Properties:
width, height: The grid’s width and height. torus: Boolean which determines whether to treat the grid as a torus. empties: Returns a set of hexagonal coordinates for all empty cells.
Create a new grid.
- Args:
width, height: The width and height of the grid torus: Boolean whether the grid wraps or not.
- coord_iter() Iterator[tuple[Agent | None, Tuple[int, int]]] #
An iterator that returns positions as well as cell contents.
- get_neighborhood(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) list[Tuple[int, int]] #
Return a list of coordinates that are in the neighborhood of a certain point. To calculate the neighborhood for a HexGrid the parity of the x coordinate of the point is important, the neighborhood can be sketched as:
Always: (0,-), (0,+) When x is even: (-,+), (-,0), (+,+), (+,0) When x is odd: (-,0), (-,-), (+,0), (+,-)
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of coordinate tuples representing the neighborhood. For example with radius 1, it will return list with number of elements equals at most 9 (8) if Moore, 5 (4) if Von Neumann (if not including the center).
- get_neighbors(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) list[Agent] #
Return a list of neighbors to a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of non-None objects in the given neighborhood
- iter_neighborhood(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) Iterator[Tuple[int, int]] #
Return an iterator over cell coordinates that are in the neighborhood of a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of coordinate tuples representing the neighborhood.
- iter_neighbors(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) Iterator[Agent] #
Return an iterator over neighbors to a certain point.
- Args:
pos: Coordinates for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of non-None objects in the given neighborhood
- move_agent(agent: Agent, pos: Tuple[int, int]) None #
Move an agent from its current position to a new position.
- Args:
- agent: Agent object to move. Assumed to have its current location
stored in a ‘pos’ tuple.
pos: Tuple of new position to move the agent to.
- out_of_bounds(pos: Tuple[int, int]) bool #
Determines whether position is off the grid, returns the out of bounds coordinate.
- class HexMultiGrid(width: int, height: int, torus: bool)[source]#
Hexagonal MultiGrid: a MultiGrid where neighbors are computed according to a hexagonal tiling of the grid.
Functions according to odd-q rules. See http://www.redblobgames.com/grids/hexagons/#coordinates for more.
Similar to the standard MultiGrid, this class maintains an empties property, which is a set of coordinates for all hexagonal cells that currently contain no agents. This property is updated automatically as agents are added to or removed from the grid.
- Properties:
width, height: The grid’s width and height. torus: Boolean which determines whether to treat the grid as a torus. empties: Returns a set of hexagonal coordinates for all empty cells.
Create a new grid.
- Args:
width, height: The width and height of the grid torus: Boolean whether the grid wraps or not.
- coord_iter() Iterator[tuple[Agent | None, Tuple[int, int]]] #
An iterator that returns positions as well as cell contents.
- get_neighborhood(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) list[Tuple[int, int]] #
Return a list of coordinates that are in the neighborhood of a certain point. To calculate the neighborhood for a HexGrid the parity of the x coordinate of the point is important, the neighborhood can be sketched as:
Always: (0,-), (0,+) When x is even: (-,+), (-,0), (+,+), (+,0) When x is odd: (-,0), (-,-), (+,0), (+,-)
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of coordinate tuples representing the neighborhood. For example with radius 1, it will return list with number of elements equals at most 9 (8) if Moore, 5 (4) if Von Neumann (if not including the center).
- get_neighbors(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) list[Agent] #
Return a list of neighbors to a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of non-None objects in the given neighborhood
- iter_neighborhood(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) Iterator[Tuple[int, int]] #
Return an iterator over cell coordinates that are in the neighborhood of a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of coordinate tuples representing the neighborhood.
- iter_neighbors(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) Iterator[Agent] #
Return an iterator over neighbors to a certain point.
- Args:
pos: Coordinates for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of non-None objects in the given neighborhood
- move_agent(agent: Agent, pos: Tuple[int, int]) None #
Move an agent from its current position to a new position.
- Args:
- agent: Agent object to move. Assumed to have its current location
stored in a ‘pos’ tuple.
pos: Tuple of new position to move the agent to.
- out_of_bounds(pos: Tuple[int, int]) bool #
Determines whether position is off the grid, returns the out of bounds coordinate.
- place_agent(agent: Agent, pos: Tuple[int, int]) None #
Place the agent at the specified location, and set its pos variable.
- class HexGrid(width: int, height: int, torus: bool)[source]#
Hexagonal Grid: a Grid where neighbors are computed according to a hexagonal tiling of the grid.
Functions according to odd-q rules. See http://www.redblobgames.com/grids/hexagons/#coordinates for more.
- Properties:
width, height: The grid’s width and height. torus: Boolean which determines whether to treat the grid as a torus.
Create a new grid.
- Args:
width, height: The width and height of the grid torus: Boolean whether the grid wraps or not.
- coord_iter() Iterator[tuple[Agent | None, Tuple[int, int]]] #
An iterator that returns positions as well as cell contents.
- get_neighborhood(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) list[Tuple[int, int]] #
Return a list of coordinates that are in the neighborhood of a certain point. To calculate the neighborhood for a HexGrid the parity of the x coordinate of the point is important, the neighborhood can be sketched as:
Always: (0,-), (0,+) When x is even: (-,+), (-,0), (+,+), (+,0) When x is odd: (-,0), (-,-), (+,0), (+,-)
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of coordinate tuples representing the neighborhood. For example with radius 1, it will return list with number of elements equals at most 9 (8) if Moore, 5 (4) if Von Neumann (if not including the center).
- get_neighbors(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) list[Agent] #
Return a list of neighbors to a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
A list of non-None objects in the given neighborhood
- iter_neighborhood(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) Iterator[Tuple[int, int]] #
Return an iterator over cell coordinates that are in the neighborhood of a certain point.
- Args:
pos: Coordinate tuple for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of coordinate tuples representing the neighborhood.
- iter_neighbors(pos: Tuple[int, int], include_center: bool = False, radius: int = 1) Iterator[Agent] #
Return an iterator over neighbors to a certain point.
- Args:
pos: Coordinates for the neighborhood to get. include_center: If True, return the (x, y) cell as well.
Otherwise, return surrounding cells only.
radius: radius, in cells, of neighborhood to get.
- Returns:
An iterator of non-None objects in the given neighborhood
- move_agent(agent: Agent, pos: Tuple[int, int]) None #
Move an agent from its current position to a new position.
- Args:
- agent: Agent object to move. Assumed to have its current location
stored in a ‘pos’ tuple.
pos: Tuple of new position to move the agent to.
- out_of_bounds(pos: Tuple[int, int]) bool #
Determines whether position is off the grid, returns the out of bounds coordinate.
- class ContinuousSpace(x_max: float, y_max: float, torus: bool, x_min: float = 0, y_min: float = 0)[source]#
Continuous space where each agent can have an arbitrary position.
Assumes that all agents have a pos property storing their position as an (x, y) tuple.
This class uses a numpy array internally to store agents in order to speed up neighborhood lookups. This array is calculated on the first neighborhood lookup, and is updated if agents are added or removed.
The concept of ‘empty cells’ is not directly applicable in continuous space, as positions are not discretized.
Create a new continuous space.
- Args:
x_max, y_max: Maximum x and y coordinates for the space. torus: Boolean for whether the edges loop around. x_min, y_min: (default 0) If provided, set the minimum x and y
coordinates for the space. Below them, values loop to the other edge (if torus=True) or raise an exception.
- place_agent(agent: Agent, pos: Tuple[float, float] | ndarray[Any, dtype[float]]) None [source]#
Place a new agent in the space.
- Args:
agent: Agent object to place. pos: Coordinate tuple for where to place the agent.
- move_agent(agent: Agent, pos: Tuple[float, float] | ndarray[Any, dtype[float]]) None [source]#
Move an agent from its current position to a new position.
- Args:
agent: The agent object to move. pos: Coordinate tuple to move the agent to.
- remove_agent(agent: Agent) None [source]#
Remove an agent from the space.
- Args:
agent: The agent object to remove
- get_neighbors(pos: Tuple[float, float] | ndarray[Any, dtype[float]], radius: float, include_center: bool = True) list[Agent] [source]#
Get all agents within a certain radius.
- Args:
pos: (x,y) coordinate tuple to center the search at. radius: Get all the objects within this distance of the center. include_center: If True, include an object at the exact provided
coordinates. i.e. if you are searching for the neighbors of a given agent, True will include that agent in the results.
- get_heading(pos_1: Tuple[float, float] | ndarray[Any, dtype[float]], pos_2: Tuple[float, float] | ndarray[Any, dtype[float]]) Tuple[float, float] | ndarray[Any, dtype[float]] [source]#
Get the heading vector between two points, accounting for toroidal space. It is possible to calculate the heading angle by applying the atan2 function to the result.
- Args:
pos_1, pos_2: Coordinate tuples for both points.
- get_distance(pos_1: Tuple[float, float] | ndarray[Any, dtype[float]], pos_2: Tuple[float, float] | ndarray[Any, dtype[float]]) float [source]#
Get the distance between two point, accounting for toroidal space.
- Args:
pos_1, pos_2: Coordinate tuples for both points.
- torus_adj(pos: Tuple[float, float] | ndarray[Any, dtype[float]]) Tuple[float, float] | ndarray[Any, dtype[float]] [source]#
Adjust coordinates to handle torus looping.
If the coordinate is out-of-bounds and the space is toroidal, return the corresponding point within the space. If the space is not toroidal, raise an exception.
- Args:
pos: Coordinate tuple to convert.
- class NetworkGrid(g: Any)[source]#
Network Grid where each node contains zero or more agents.
Create a new network.
- Args:
G: a NetworkX graph instance.
- get_neighborhood(node_id: int, include_center: bool = False, radius: int = 1) list[int] [source]#
Get all adjacent nodes within a certain radius
- get_neighbors(node_id: int, include_center: bool = False) list[Agent] [source]#
Get all agents in adjacent nodes.
- move_agent(agent: Agent, node_id: int) None [source]#
Move an agent from its current node to a new node.
- remove_agent(agent: Agent) None [source]#
Remove the agent from the network and set its pos attribute to None.