Skip to content

Fix reproducibility warnings by adding explicit random parameters#2978

Merged
EwoutH merged 2 commits intomesa:mainfrom
codebreaker32:fix-warnings
Dec 23, 2025
Merged

Fix reproducibility warnings by adding explicit random parameters#2978
EwoutH merged 2 commits intomesa:mainfrom
codebreaker32:fix-warnings

Conversation

@codebreaker32
Copy link
Copy Markdown
Collaborator

@codebreaker32 codebreaker32 commented Dec 20, 2025

Summary

Fixes UserWarning about missing random number generators in tests and examples by explicitly passing random parameters to grid constructors.

Motive

Several tests were generating warnings that "Random number generator not specified, this can make models non-reproducible."

Implementation

Added explicit random parameter to all affected grid and cell constructors:

  • Conway's Game of Life: passes random=self.random to inherit model's RNG
  • Test files: passes random=random.Random(42) for deterministic test behavior

Usage Examples

# Before
grid = OrthogonalMooreGrid((width, height), capacity=1, torus=True)  # Warning!
# After
grid = OrthogonalMooreGrid((width, height), capacity=1, random=self.random, torus=True)

Part of #2904.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.4% [+1.9%, +2.9%] 🔴 +4.2% [+4.0%, +4.3%]
BoltzmannWealth large 🔵 +1.5% [+1.1%, +1.8%] 🔵 +3.1% [+2.2%, +4.1%]
Schelling small 🔵 +1.5% [+0.6%, +2.5%] 🔴 +7.7% [+7.2%, +8.1%]
Schelling large 🔵 +0.4% [-0.2%, +0.8%] 🔴 +6.1% [+5.3%, +7.1%]
WolfSheep small 🔵 +1.7% [+1.5%, +1.9%] 🔵 -0.6% [-0.8%, -0.4%]
WolfSheep large 🔵 +0.3% [-3.6%, +2.6%] 🔵 +0.4% [-1.3%, +2.2%]
BoidFlockers small 🔵 +0.5% [+0.1%, +0.8%] 🔵 +0.6% [+0.4%, +0.8%]
BoidFlockers large 🔵 +0.4% [-0.0%, +0.8%] 🔵 +0.2% [-0.1%, +0.4%]

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Dec 22, 2025

@quaquel is it now best-practice that we need to pass random to everything? Can't all components that have access to a model instance just use self.model.random or such?

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Dec 22, 2025

@quaquel is it now best-practice that we need to pass random to everything? Can't all components that have access to a model instance just use self.model.random or such?

That is already how it's done. CellCollection, and the spaces are not aware of the model, so they cannot fall back on it. In contrast, Agent has the model and so uses this. AgentSet also falls back on it via the model reference found in the first agent in the set.

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Dec 22, 2025

While I get where the design originates from, I find this very confusing for end-users. Especially from the concept of guided learning with easy entry and gracefully learning more powerful features (@tpike3 how do you always call that?). Adding a space is often one of the first things people do, and now they get a warning and they have to dive into random number generators and which parts of spaces have access to what model design. That's all unneeded complexity introduced too early to

I get that we need the whole random passing from a reproducibility standpoint, and I acknowledge that may add some complexity here and there. I support adopting SPEC 7. But how can we make it so that the end-users doesn't encounter its complexity so early, but can gradually learn it?

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Dec 22, 2025

We debated this before while developing both the discrete space and the new experimental continuous space.

From a learning perspective, I don't see the problem as long as the tutorials and examples always pass random=model.random/rng=model.rng, which provides a clear pattern for learners to follow.

From an implementation point of view, I also don't know of a simple solution, apart from trying to get model.random from the agent as per AgentSet, but this will add overhead via an if else or try catch and a property:

@properyt
def random:
    try:
        return getattr(self, "_random"
    except AttribtueError:
       # figure it out via agents

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Dec 23, 2025

@quaquel code wise, is this good to go?

@@ -1,5 +1,6 @@
"""Test the backends of the visualization package."""

import random
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this the right random that's imported?

@EwoutH EwoutH changed the title pass random number generator to avoid warnings Fix reproducibility warnings by adding explicit random parameters Dec 23, 2025
@EwoutH EwoutH added the maintenance Release notes label label Dec 23, 2025
@EwoutH EwoutH merged commit def06ad into mesa:main Dec 23, 2025
13 of 14 checks passed
@codebreaker32 codebreaker32 deleted the fix-warnings branch December 29, 2025 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants