Skip to content

Fix: AgentSet initialization should not require explicit random number generator#2789

Merged
EwoutH merged 12 commits intomesa:mainfrom
verisimilidude2:test_branch
Nov 7, 2025
Merged

Fix: AgentSet initialization should not require explicit random number generator#2789
EwoutH merged 12 commits intomesa:mainfrom
verisimilidude2:test_branch

Conversation

@verisimilidude2
Copy link
Copy Markdown
Contributor

@verisimilidude2 verisimilidude2 commented May 19, 2025

Summary

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.

Bug / Issue

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.

Related to discussion #1981.

Implementation

The key changes made:

  1. Modified AgentSet.__init__ to check if agents are provided before issuing the warning
  2. Added logic to automatically extract the random number generator from the first agent's model when random=None and agents exist
  3. Updated warning condition to only trigger when both the agent list is empty AND random=None
  4. Expanded type hint for the random parameter to include np.random.Generator
  5. Updated all test cases to remove explicit random=model.random parameters where agents are provided, demonstrating the new default behavior

The implementation maintains backward compatibility - explicitly passing a random number generator still works as before.

Testing

  • All existing tests updated to use the new default behavior
  • Added new test test_agentset_initialization_w_random() to verify the random number generator is correctly inherited from the model
  • Verified warning is still issued when creating an empty AgentSet without specifying random
  • All modified tests pass, confirming agents can access the correct seeded RNG through their model

Additional 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.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.7% [-1.4%, -0.1%] 🔵 -0.6% [-0.9%, -0.4%]
BoltzmannWealth large 🔵 -0.6% [-1.5%, +0.3%] 🔵 -1.6% [-3.9%, +0.5%]
Schelling small 🔵 +0.1% [-0.1%, +0.3%] 🔵 -0.4% [-0.7%, -0.1%]
Schelling large 🔵 -0.3% [-0.9%, +0.2%] 🔵 -0.5% [-2.1%, +0.8%]
WolfSheep small 🔵 -0.1% [-0.4%, +0.2%] 🔵 +0.3% [+0.1%, +0.5%]
WolfSheep large 🔵 -0.4% [-1.4%, +0.7%] 🔵 -0.0% [-0.4%, +0.5%]
BoidFlockers small 🔵 -0.8% [-1.3%, -0.2%] 🔵 -0.5% [-0.7%, -0.2%]
BoidFlockers large 🔵 +0.4% [-0.5%, +1.1%] 🔵 -0.0% [-0.4%, +0.3%]

@verisimilidude2
Copy link
Copy Markdown
Contributor Author

Change int cast that Ruff complained about

@quaquel
Copy link
Copy Markdown
Member

quaquel commented May 20, 2025

Looks good to me. A few minor requests

  1. Can you expand the PR description
  2. Can you resolve the conflict

Thanks!

@jackiekazil
Copy link
Copy Markdown
Member

Agree with @quaquel.
Thank you for the contribution @verisimilidude2! Looking forward to getting this merged in with those updates.

@jackiekazil jackiekazil added the bug Release notes label label May 21, 2025
@verisimilidude2
Copy link
Copy Markdown
Contributor Author

@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:
Was:
A UserWarning is issued if random=None. You can resolve this warning by explicitly
passing a random number generator. In most cases, this will be the seeded random number
generator in the model. So, you would do random=self.random in a Model or Agent instance.
Now:
If random is None then the random number generator in the model of the first agent is used.
If the agents list is empty a user warning is issued and the agent set is an empty list
with no random number generator

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Nov 7, 2025

What's the status of this PR? And what's needed to move it forward?

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Nov 7, 2025

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.

@EwoutH EwoutH changed the title Make random number generator optional in creation of AgentSet, take R… Fix: AgentSet initialization should not require explicit random number generator Nov 7, 2025
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Nov 7, 2025

@quaquel I updated the title and description, could you review it?

Code itself is good?

@EwoutH EwoutH requested a review from quaquel November 7, 2025 10:01
Copy link
Copy Markdown
Member

@quaquel quaquel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one typing suggestion, looks fine otherwise

Co-authored-by: Jan Kwakkel <[email protected]>
@EwoutH EwoutH merged commit 9939b7e into mesa:main Nov 7, 2025
10 of 11 checks passed
EwoutH added a commit that referenced this pull request Nov 7, 2025
…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]>
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Nov 7, 2025

We released Mesa 3.3.1 with this bugfix, so you should now be able to update Mesa (pip install -U mesa) and have this bug resolved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants