Skip to content

Commit

Permalink
Add AgentSet.shuffle_do()
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH committed Sep 20, 2024
1 parent 7d4a4af commit bc06c74
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,19 @@ def do(self, method: str | Callable, *args, **kwargs) -> AgentSet:

return self

def shuffle_do(self, method: str | Callable, *args, **kwargs) -> AgentSet:
agents = list(self._agents.keys())
self.random.shuffle(agents)

Check warning on line 298 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L297-L298

Added lines #L297 - L298 were not covered by tests

if isinstance(method, str):
for agent in agents:
getattr(agent, method)(*args, **kwargs)

Check warning on line 302 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L302

Added line #L302 was not covered by tests
else:
for agent in agents:
method(agent, *args, **kwargs)

Check warning on line 305 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L305

Added line #L305 was not covered by tests

return self

Check warning on line 307 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L307

Added line #L307 was not covered by tests

def map(self, method: str | Callable, *args, **kwargs) -> list[Any]:
"""Invoke a method or function on each agent in the AgentSet and return the results.
Expand Down

0 comments on commit bc06c74

Please sign in to comment.