Skip to content
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
16 changes: 15 additions & 1 deletion mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import operator
import warnings
import weakref
from collections import defaultdict
from collections.abc import MutableSet, Sequence
from random import Random

Expand Down Expand Up @@ -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."""

Expand Down
2 changes: 1 addition & 1 deletion mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down