Skip to content

Commit

Permalink
perf: Speed up Agentset.shuffle (#2010)
Browse files Browse the repository at this point in the history
* Make RandomActivationByType.agents_by_type backward compatible

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* updated warning

* Update .gitignore

* Update global_benchmark.py

* pep8 rename

* Update schelling.py

* Update schelling.py

* Update schelling.py

* Update schelling.py

* Squashed commit of the following:

commit c0de4a1
Author: Ewout ter Hoeven <E.M.terHoeven@student.tudelft.nl>
Date:   Sun Jan 21 14:43:08 2024 +0100

    Add CI workflow for performance benchmarks (#1983)

    This commit introduces a new GitHub Actions workflow for performance benchmarking. The workflow is triggered on `pull_request_target` events and can be triggered with a "/rerun-benchmarks" comment in the PR. It should be compatible with PRs from both forks and the main repository. It includes steps for setting up the environment, checking out the PR and main branches, installing dependencies, and running benchmarks on both branches. The results are then compared, encoded, and posted as a comment on the PR.

* Revert "Squashed commit of the following:"

This reverts commit 9485087.

* Update .gitignore

* further updates

* Update benchmarks/WolfSheep/__init__.py

* shuffle update

* switch to walrus operator in do

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* revert inplace to False

* remove old code

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
quaquel and pre-commit-ci[bot] authored Jan 27, 2024
1 parent 6a4ea54 commit 449d230
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
26 changes: 16 additions & 10 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,21 @@ def shuffle(self, inplace: bool = False) -> AgentSet:
Returns:
AgentSet: A shuffled AgentSet. Returns the current AgentSet if inplace is True.
"""
shuffled_agents = list(self)
self.random.shuffle(shuffled_agents)
return (
AgentSet(shuffled_agents, self.model)
if not inplace
else self._update(shuffled_agents)
)
Note:
Using inplace = True is more performant
"""
weakrefs = list(self._agents.keyrefs())
self.random.shuffle(weakrefs)

if inplace:
self._agents.data = {entry: None for entry in weakrefs}
return self
else:
return AgentSet(
(agent for ref in weakrefs if (agent := ref()) is not None), self.model
)

def sort(
self,
Expand Down Expand Up @@ -251,9 +257,9 @@ def do(
"""
# we iterate over the actual weakref keys and check if weakref is alive before calling the method
res = [
getattr(agentref(), method_name)(*args, **kwargs)
getattr(agent, method_name)(*args, **kwargs)
for agentref in self._agents.keyrefs()
if agentref()
if (agent := agentref()) is not None
]

return res if return_results else self
Expand Down
2 changes: 1 addition & 1 deletion tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_function(agent):
assert all(a1 == a2 for a1, a2 in zip(agentset.select(), agentset))
assert all(a1 == a2 for a1, a2 in zip(agentset.select(n=5), agentset[:5]))

assert len(agentset.shuffle().select(n=5)) == 5
assert len(agentset.shuffle(inplace=False).select(n=5)) == 5

def test_function(agent):
return agent.unique_id
Expand Down

0 comments on commit 449d230

Please sign in to comment.