diff --git a/mesa/agent.py b/mesa/agent.py index 0bb2f21875a..d62994eae14 100644 --- a/mesa/agent.py +++ b/mesa/agent.py @@ -11,6 +11,7 @@ import operator import warnings import weakref +from collections import defaultdict from collections.abc import MutableSet, Sequence from random import Random @@ -47,13 +48,26 @@ def __init__(self, unique_id: int, model: Model) -> None: self.pos: Position | None = None # register agent - self.model._agents[type(self)][self] = None + try: + self.model._agents[type(self)][self] = None + except AttributeError: + # model super has not been called + self.model._agents = defaultdict(dict) + self.model.agentset_experimental_warning_given = False + + warnings.warn( + "The Mesa Model class wasn’t initialized. In the future, you need to explicitly initialize the Model by calling super().__init__() on initialization.", + FutureWarning, + stacklevel=2, + ) + def remove(self) -> None: """Remove and delete the agent from the model.""" with contextlib.suppress(KeyError): self.model._agents[type(self)].pop(self) + def step(self) -> None: """A single step of the agent.""" diff --git a/mesa/model.py b/mesa/model.py index 22e654483dd..6b5338dae8b 100644 --- a/mesa/model.py +++ b/mesa/model.py @@ -85,7 +85,7 @@ def agent_types(self) -> list[type]: def get_agents_of_type(self, agenttype: type) -> AgentSet: """Retrieves an AgentSet containing all agents of the specified type.""" - return AgentSet(self._agents[agenttype].values(), self) + return AgentSet(self._agents[agenttype].keys(), self) def run_model(self) -> None: """Run the model until the end condition is reached. Overload as