-
Notifications
You must be signed in to change notification settings - Fork 865
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optimized shuffle_do() method to AgentSet (#2283)
This PR introduces a new `shuffle_do` method to the `AgentSet` class, optimizing the process of shuffling agents and applying a method to them in a single operation. ### Motive The current approach of shuffling agents and then applying a method (`shuffle().do()`) is inefficient, especially for large agent sets. This new method aims to significantly improve performance by combining these operations, reducing memory allocations and iterations. ### Implementation - Added a new `shuffle_do` method to the `AgentSet` class in `mesa/agent.py`. - The method takes a `method` parameter (either a string or callable) and additional args/kwargs. - It shuffles the agents in-place. - The specified method is then applied to each agent in the shuffled order. - Added corresponding unit tests in `tests/test_agent.py`. - Updated the benchmarks (BoltzmannWealth and Wolf-sheep). ### Usage Examples ```python # Using a string method name model.agents.shuffle_do("step") # Using a lambda function model.agents.shuffle_do(lambda agent: agent.move()) ``` Before/After Performance Comparison: | Configuration | shuffle().do() | shuffle(inplace=True).do() | shuffle_do() | |------------------------------|----------------|----------------------------|--------------| | 10,000 agents, 1,000 steps | 8.27 s | 3.65 s | 3.06 s | | 100,000 agents, 100 steps | 13.71 s | 6.31 s | 3.71 s | | 1,000,000 agents, 10 steps | 18.36 s | 9.44 s | 5.75 s | As shown, `shuffle_do()` provides significant performance improvements, especially for larger agent sets. ### Additional Notes - This change is backwards compatible and doesn't affect existing code using `shuffle().do()`. - The performance gain is more pronounced for larger agent sets and more frequent shuffling operations. - Examples and docs can be updated (other PR).
- Loading branch information
Showing
6 changed files
with
59 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters