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 1 commit
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
22 changes: 22 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,6 +535,9 @@ 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.

Unlike SingleGrid, MultiGrid does not inherently track empty cells due to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empties are tracked in MultiGrid. MultiGrid inherits from _Grid, which defines the empties property.

In [1]: import mesa
g =
In [2]: g = mesa.space.MultiGrid(5,5,False)

In [3]: g.empties
Out[3]:
{(0, 0),
 (0, 1),
...

Copy link
Member Author

@EwoutH EwoutH Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, will update that (also in HexMulti)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

the possibility of multiple agents occupying a single cell.

Properties:
width, height: The grid's width and height.
torus: Boolean which determines whether to treat the grid as a torus.
Expand Down Expand Up @@ -745,9 +757,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,6 +774,9 @@ 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 grid does not automatically track
empty cells due to the possibility of multiple agents in one cell.

Properties:
width, height: The grid's width and height.
torus: Boolean which determines whether to treat the grid as a torus.
Expand Down Expand Up @@ -797,6 +816,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