Skip to content

Commit 0da4a01

Browse files
committed
Model: Replace get_agents_of_type method with agents_by_type property
1 parent 0a1b2a3 commit 0da4a01

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

benchmarks/WolfSheep/wolf_sheep.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ def __init__(
227227
patch.move_to(cell)
228228

229229
def step(self):
230-
self.agents_of_type(Sheep).shuffle(inplace=True).do("step")
231-
self.agents_of_type(Wolf).shuffle(inplace=True).do("step")
230+
self.agents_by_type[Sheep].shuffle(inplace=True).do("step")
231+
self.agents_by_type[Wolf].shuffle(inplace=True).do("step")
232232

233233

234234
if __name__ == "__main__":

mesa/experimental/devs/examples/wolf_sheep.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ def __init__(
231231
self.grid.place_agent(patch, pos)
232232

233233
def step(self):
234-
self.agents_of_type(Sheep).shuffle(inplace=True).do("step")
235-
self.agents_of_type(Wolf).shuffle(inplace=True).do("step")
234+
self.agents_by_type[Sheep].shuffle(inplace=True).do("step")
235+
self.agents_by_type[Wolf].shuffle(inplace=True).do("step")
236236

237237

238238
if __name__ == "__main__":

mesa/model.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,33 +106,26 @@ def agent_types(self) -> list[type]:
106106
"""Return a list of all unique agent types registered with the model."""
107107
return list(self._agents_by_type.keys())
108108

109-
def agents_of_type(self, agenttype: type[Agent]) -> AgentSet:
110-
"""Retrieves an AgentSet containing all agents of the specified type.
111-
112-
Args:
113-
agenttype: The type of agent to retrieve.
114-
115-
Raises:
116-
KeyError: If agenttype does not exist
117-
118-
119-
"""
120-
return self._agents_by_type[agenttype]
109+
@property
110+
def agents_by_type(self) -> dict[type[Agent], AgentSet]:
111+
"""A dictionary where the keys are agent types and the values are the corresponding AgentSets."""
112+
return self._agents_by_type
121113

122114
def get_agents_of_type(self, agenttype: type[Agent]) -> AgentSet:
123115
"""Deprecated: Retrieves an AgentSet containing all agents of the specified type."""
124116
warnings.warn(
125-
"get_agents_of_type is deprecated, please use agents_of_type instead.",
117+
f"Model.get_agents_of_type() is deprecated, please replace get_agents_of_type({agenttype})"
118+
f"with the property agents_by_type[{agenttype}].",
126119
DeprecationWarning,
127120
stacklevel=2,
128121
)
129-
return self.agents_of_type(agenttype)
122+
return self.agents_by_type[agenttype]
130123

131124
def _setup_agent_registration(self):
132125
"""helper method to initialize the agent registration datastructures"""
133126
self._agents = {} # the hard references to all agents in the model
134127
self._agents_by_type: dict[
135-
type, AgentSet
128+
type[Agent], AgentSet
136129
] = {} # a dict with an agentset for each class of agents
137130
self._all_agents = AgentSet([], self) # an agenset with all agents
138131

tests/test_time.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def test_random_activation_counts(self):
320320
agent_types = model.agent_types
321321
for agent_type in agent_types:
322322
assert model.schedule.get_type_count(agent_type) == len(
323-
model.agents_of_type(agent_type)
323+
model.agents_by_type[agent_type]
324324
)
325325

326326
# def test_add_non_unique_ids(self):

0 commit comments

Comments
 (0)