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

Make RandomActivationByType.agents_by_type backward compatible #1965

Merged
merged 3 commits into from
Jan 15, 2024
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
34 changes: 25 additions & 9 deletions mesa/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import heapq
import warnings
import weakref
from collections import defaultdict
from collections.abc import Iterable

# mypy
Expand Down Expand Up @@ -287,6 +288,21 @@
- get_type_count: Returns the count of agents of a specific type.
"""

@property
def agents_by_type(self):
warnings.warn(

Check warning on line 293 in mesa/time.py

View check run for this annotation

Codecov / codecov/patch

mesa/time.py#L293

Added line #L293 was not covered by tests
"Because of the shift to using AgentSet, in the future this attribute will return a dict with"
"type as key as AgentSet as value. Future behavior is available via RandomActivationByType._agents_by_type",
DeprecationWarning,
stacklevel=2,
)

agentsbytype = defaultdict(dict)

Check warning on line 300 in mesa/time.py

View check run for this annotation

Codecov / codecov/patch

mesa/time.py#L300

Added line #L300 was not covered by tests
for k, v in self._agents_by_type.items():
agentsbytype[k] = {agent: agent.unique_id for agent in v}

return agentsbytype

Check warning on line 304 in mesa/time.py

View check run for this annotation

Codecov / codecov/patch

mesa/time.py#L304

Added line #L304 was not covered by tests

def __init__(self, model: Model, agents: Iterable[Agent] | None = None) -> None:
super().__init__(model, agents)
"""
Expand All @@ -297,14 +313,14 @@
"""

# can't be a defaultdict because we need to pass model to AgentSet
self.agents_by_type: [type, AgentSet] = {}
self._agents_by_type: [type, AgentSet] = {}

if agents is not None:
for agent in agents:
try:
self.agents_by_type[type(agent)].add(agent)
self._agents_by_type[type(agent)].add(agent)
except KeyError:
self.agents_by_type[type(agent)] = AgentSet([agent], self.model)
self._agents_by_type[type(agent)] = AgentSet([agent], self.model)

def add(self, agent: Agent) -> None:
"""
Expand All @@ -316,16 +332,16 @@
super().add(agent)

try:
self.agents_by_type[type(agent)].add(agent)
self._agents_by_type[type(agent)].add(agent)
except KeyError:
self.agents_by_type[type(agent)] = AgentSet([agent], self.model)
self._agents_by_type[type(agent)] = AgentSet([agent], self.model)

def remove(self, agent: Agent) -> None:
"""
Remove all instances of a given agent from the schedule.
"""
super().remove(agent)
self.agents_by_type[type(agent)].remove(agent)
self._agents_by_type[type(agent)].remove(agent)

Check warning on line 344 in mesa/time.py

View check run for this annotation

Codecov / codecov/patch

mesa/time.py#L344

Added line #L344 was not covered by tests

def step(self, shuffle_types: bool = True, shuffle_agents: bool = True) -> None:
"""
Expand All @@ -339,7 +355,7 @@
"""
# To be able to remove and/or add agents during stepping
# it's necessary to cast the keys view to a list.
type_keys: list[type[Agent]] = list(self.agents_by_type.keys())
type_keys: list[type[Agent]] = list(self._agents_by_type.keys())
if shuffle_types:
self.model.random.shuffle(type_keys)
for agent_class in type_keys:
Expand All @@ -355,7 +371,7 @@
Args:
agenttype: Class object of the type to run.
"""
agents = self.agents_by_type[agenttype]
agents = self._agents_by_type[agenttype]

if shuffle_agents:
agents.shuffle(inplace=True)
Expand All @@ -365,7 +381,7 @@
"""
Returns the current number of agents of certain type in the queue.
"""
return len(self.agents_by_type[agenttype])
return len(self._agents_by_type[agenttype])


class DiscreteEventScheduler(BaseScheduler):
Expand Down