Fix: AgentSet initialization should not require explicit random number generator#2789
Fix: AgentSet initialization should not require explicit random number generator#2789
Conversation
|
Performance benchmarks:
|
|
Change int cast that Ruff complained about |
|
Looks good to me. A few minor requests
Thanks! |
|
Agree with @quaquel. |
|
@quaquel I'm not sure how one changes the PR description. The diff on the note on the AgentSet class pretty much describes what I did: |
|
What's the status of this PR? And what's needed to move it forward? |
|
I guess it's stale. I requested 2 small things, neither of which has been done. Fixing the PR descritption can be done by us. I just merged in the main branch to check if there are any new conflicts or failing tests. |
|
@quaquel I updated the title and description, could you review it? Code itself is good? |
quaquel
left a comment
There was a problem hiding this comment.
one typing suggestion, looks fine otherwise
Co-authored-by: Jan Kwakkel <[email protected]>
…r generator (#2789) This PR modifies the AgentSet initialization to automatically use the random number generator from the model of the first agent when no explicit random parameter is provided, eliminating unnecessary UserWarnings in typical usage. Previously, AgentSet initialization would raise a UserWarning whenever random=None, even though in most cases the agents being added to the set already have access to a seeded random number generator through their model. This resulted in users needing to explicitly pass random=model.random in every AgentSet creation, creating unnecessary boilerplate. Co-authored-by: Jan Kwakkel <[email protected]> Co-authored-by: Ewout ter Hoeven <[email protected]>
|
We released Mesa 3.3.1 with this bugfix, so you should now be able to update Mesa ( |
Summary
This PR modifies the
AgentSetinitialization to automatically use the random number generator from the model of the first agent when no explicitrandomparameter is provided, eliminating unnecessaryUserWarnings in typical usage.Bug / Issue
Previously,
AgentSetinitialization would raise aUserWarningwheneverrandom=None, even though in most cases the agents being added to the set already have access to a seeded random number generator through their model. This resulted in users needing to explicitly passrandom=model.randomin everyAgentSetcreation, creating unnecessary boilerplate.Related to discussion #1981.
Implementation
The key changes made:
AgentSet.__init__to check if agents are provided before issuing the warningrandom=Noneand agents existrandom=Nonerandomparameter to includenp.random.Generatorrandom=model.randomparameters where agents are provided, demonstrating the new default behaviorThe implementation maintains backward compatibility - explicitly passing a random number generator still works as before.
Testing
test_agentset_initialization_w_random()to verify the random number generator is correctly inherited from the modelAgentSetwithout specifyingrandomAdditional Notes
This change significantly reduces boilerplate code when working with AgentSets, as users no longer need to explicitly pass the random number generator in the common case where agents with models are being added. The warning is preserved for the edge case where an empty AgentSet is created without a specified RNG, which could lead to non-reproducible models.