Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

space: Document empties property #1888

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,18 @@ class SingleGrid(_Grid):
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.
"""

def place_agent(self, agent: Agent, pos: Coordinate) -> None:
Expand Down Expand Up @@ -526,9 +535,14 @@ class MultiGrid(_Grid):
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.
"""

grid: list[list[MultiGridContent]]
Expand Down Expand Up @@ -745,9 +759,13 @@ class HexSingleGrid(_HexGrid, SingleGrid):
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.
"""


Expand All @@ -758,9 +776,15 @@ class HexMultiGrid(_HexGrid, MultiGrid):
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.
"""


Expand Down Expand Up @@ -797,6 +821,9 @@ class ContinuousSpace:
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.
"""

def __init__(
Expand Down