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

Allow user models to assign Model.agents for now, but add warning #1976

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@

# register agent
try:
self.model._agents[type(self)][self] = None
self.model.agents_[type(self)][self] = None
except AttributeError:
# model super has not been called
self.model._agents = defaultdict(dict)
self.model.agents_ = defaultdict(dict)

Check warning on line 56 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L56

Added line #L56 was not covered by tests
self.model.agentset_experimental_warning_given = False

warnings.warn(
Expand All @@ -65,7 +65,7 @@
def remove(self) -> None:
"""Remove and delete the agent from the model."""
with contextlib.suppress(KeyError):
self.model._agents[type(self)].pop(self)
self.model.agents_[type(self)].pop(self)

def step(self) -> None:
"""A single step of the agent."""
Expand Down
29 changes: 22 additions & 7 deletions mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import itertools
import random
import warnings
from collections import defaultdict

# mypy
Expand All @@ -29,7 +30,7 @@
running: A boolean indicating if the model should continue running.
schedule: An object to manage the order and execution of agent steps.
current_id: A counter for assigning unique IDs to agents.
_agents: A defaultdict mapping each agent type to a dict of its instances.
agents_: A defaultdict mapping each agent type to a dict of its instances.
This private attribute is used internally to manage agents.

Properties:
Expand Down Expand Up @@ -65,27 +66,41 @@
self.running = True
self.schedule = None
self.current_id = 0
self._agents: defaultdict[type, dict] = defaultdict(dict)
self.agents_: defaultdict[type, dict] = defaultdict(dict)

# Warning flags for current experimental features. These make sure a warning is only printed once per model.
self.agentset_experimental_warning_given = False

@property
def agents(self) -> AgentSet:
"""Provides an AgentSet of all agents in the model, combining agents from all types."""
all_agents = itertools.chain(
*(agents_by_type.keys() for agents_by_type in self._agents.values())

if hasattr(self, "_agents"):
return self._agents

Check warning on line 79 in mesa/model.py

View check run for this annotation

Codecov / codecov/patch

mesa/model.py#L79

Added line #L79 was not covered by tests
else:
all_agents = itertools.chain.from_iterable(self.agents_.values())
return AgentSet(all_agents, self)

@agents.setter
def agents(self, agents: Any) -> None:
warnings.warn(

Check warning on line 86 in mesa/model.py

View check run for this annotation

Codecov / codecov/patch

mesa/model.py#L86

Added line #L86 was not covered by tests
"You are trying to set model.agents. In a next release, this attribute is used "
"by MESA itself so you cannot use it directly anymore."
"Please adjust your code to use a different attribute name for custom agent storage",
UserWarning,
stacklevel=2,
)
return AgentSet(all_agents, self)

self._agents = agents

Check warning on line 94 in mesa/model.py

View check run for this annotation

Codecov / codecov/patch

mesa/model.py#L94

Added line #L94 was not covered by tests

@property
def agent_types(self) -> list[type]:
"""Return a list of different agent types."""
return list(self._agents.keys())
return list(self.agents_.keys())

def get_agents_of_type(self, agenttype: type[Agent]) -> AgentSet:
"""Retrieves an AgentSet containing all agents of the specified type."""
return AgentSet(self._agents[agenttype].keys(), self)
return AgentSet(self.agents_[agenttype].keys(), self)

def run_model(self) -> None:
"""Run the model until the end condition is reached. Overload as
Expand Down