Skip to content

add agents property to all spaces #2418

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

Merged
merged 8 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions mesa/experimental/cell_space/discrete_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from random import Random
from typing import Any, Generic, TypeVar

from mesa.agent import AgentSet
from mesa.experimental.cell_space.cell import Cell
from mesa.experimental.cell_space.cell_collection import CellCollection
from mesa.space import PropertyLayer
Expand Down Expand Up @@ -55,6 +56,11 @@ def __init__(
def cutoff_empties(self): # noqa
return 7.953 * len(self._cells) ** 0.384

@property
def agents(self) -> AgentSet:
"""Return an AgentSet with the agents in the space"""
return AgentSet(self.all_cells.agents, random=self.random)

def _connect_cells(self): ...
def _connect_single_cell(self, cell: T): ...

Expand Down
52 changes: 51 additions & 1 deletion mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import numpy.typing as npt

# For Mypy
from .agent import Agent
from .agent import Agent, AgentSet

# for better performance, we calculate the tuple to use in the is_integer function
_types_integer = (int, np.integer)
Expand Down Expand Up @@ -153,6 +153,26 @@
@overload
def __getitem__(self, index: int | Sequence[Coordinate]) -> list[GridContent]: ...

@property
def agents(self) -> AgentSet:
"""Return an AgentSet with the agents in the space"""
agents = []

Check warning on line 159 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L159

Added line #L159 was not covered by tests
for entry in self:
if not entry:
continue

Check warning on line 162 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L162

Added line #L162 was not covered by tests
if not isinstance(entry, list):
entry = [entry] # noqa PLW2901

Check warning on line 164 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L164

Added line #L164 was not covered by tests
for agent in entry:
agents.append(agent)

Check warning on line 166 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L166

Added line #L166 was not covered by tests

# getting the rng is a bit hacky because old style spaces don't have the rng
try:
rng = agents[0].random
except IndexError:

Check warning on line 171 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L169-L171

Added lines #L169 - L171 were not covered by tests
# there are no agents in the space
rng = None
return AgentSet(agents, random=rng)

Check warning on line 174 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L173-L174

Added lines #L173 - L174 were not covered by tests

@overload
def __getitem__(
self, index: tuple[int | slice, int | slice]
Expand Down Expand Up @@ -1333,6 +1353,19 @@
self._index_to_agent: dict[int, Agent] = {}
self._agent_to_index: dict[Agent, int | None] = {}

@property
def agents(self) -> AgentSet:
"""Return an AgentSet with the agents in the space"""
agents = [agent for agent in self._agent_to_index]

Check warning on line 1359 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L1359

Added line #L1359 was not covered by tests

# getting the rng is a bit hacky because old style spaces don't have the rng
try:
rng = agents[0].random
except IndexError:

Check warning on line 1364 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L1362-L1364

Added lines #L1362 - L1364 were not covered by tests
# there are no agents in the space
rng = None
return AgentSet(agents, random=rng)

Check warning on line 1367 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L1366-L1367

Added lines #L1366 - L1367 were not covered by tests

def _build_agent_cache(self):
"""Cache agents positions to speed up neighbors calculations."""
self._index_to_agent = {}
Expand Down Expand Up @@ -1506,6 +1539,23 @@
for node_id in self.G.nodes:
g.nodes[node_id]["agent"] = self.default_val()

@property
def agents(self) -> AgentSet:
"""Return an AgentSet with the agents in the space"""
agents = [

Check warning on line 1545 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L1545

Added line #L1545 was not covered by tests
entry
for node_id in self.G.nodes
if (entry := self.G.nodes[node_id]["agent"])
]

# getting the rng is a bit hacky because old style spaces don't have the rng
try:
rng = agents[0].random
except IndexError:

Check warning on line 1554 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L1552-L1554

Added lines #L1552 - L1554 were not covered by tests
# there are no agents in the space
rng = None
return AgentSet(agents, random=rng)

Check warning on line 1557 in mesa/space.py

View check run for this annotation

Codecov / codecov/patch

mesa/space.py#L1556-L1557

Added lines #L1556 - L1557 were not covered by tests

@staticmethod
def default_val() -> list:
"""Default value for a new node."""
Expand Down
Loading