Skip to content

BUG Preserve Scenario RNG state during serialization - #4284

Merged
quaquel merged 4 commits into
mesa:mainfrom
Anjikya08:fix-scenario-rng-serialization
Nov 30, 2026
Merged

quaquel merged 4 commits into
mesa:mainfrom
Anjikya08:fix-scenario-rng-serialization

Conversation

@Anjikya08

@Anjikya08 Anjikya08 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Fixes loss of Scenario RNG state and shared RNG identity during serialization and restoration.

When a Model and its Scenario share the same NumPy Generator, pickle restoration should preserve that shared identity. Otherwise, they can end up using separate random-number generators, potentially causing divergent random-number streams.

Bug / Issue

Closes #4283

A normally initialized Model satisfies model.rng is model.scenario.rng. Serialization and restoration could fail to preserve this relationship.

Expected behavior:

  • Preserve shared RNG identity across pickle restoration.
  • Keep independently assigned Model and Scenario RNGs distinct.
  • Preserve advanced RNG state and subsequent random draws.

Implementation

  • Preserve the RNG object in the serialized Scenario state.
  • Restore the Scenario's SeedSequence from its restored bit generator.
  • Rely on pickle memoization to preserve shared RNG identity.
  • Preserve distinct RNG instances when Model and Scenario originally use separate generators.
  • Reject invalid or mismatched RNG objects during restoration.

Testing

Added regression tests covering:

  • Standalone Scenario pickle round-trips.
  • Model pickle and deepcopy with shared RNGs.
  • Model pickle and deepcopy with distinct RNGs.
  • Advanced bit-generator state and subsequent random draws.
  • Regression verification against the previous implementation.

Test command:

python -m pytest -p no:solara -q tests/test_model.py tests/experimental/scenarios/test_scenario.py

Result: 38 passed.

Ruff check and format checks passed. git diff --check passed.

Additional Notes

  • Changes are limited to scenario.py and test_scenario.py.
  • Backward compatibility with legacy Scenario pickle states is intentionally unsupported because Scenarios are experimental and this is the Mesa 4.0 development branch.
  • No unrelated behavior or RNG architecture changes are intended.
  • OpenAI Codex was used to assist with implementation and testing.

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 29, 2026
@Anjikya08
Anjikya08 force-pushed the fix-scenario-rng-serialization branch from cb28d59 to 965f3f3 Compare November 29, 2026 18:00
Comment thread mesa/experimental/scenarios/scenario.py Resolved
Comment thread mesa/experimental/scenarios/scenario.py Resolved
@mesa-triage-bot

Copy link
Copy Markdown

Thanks for explaining the restoration logic, @Anjikya08.

@quaquel, the author has answered your questions and offered to remove the legacy-state fallback. Should this preserve only current RNG state and shared identity, or also support older serialized tuples? I recommend dropping the legacy fallback given the experimental status and 4.0 branch, while retaining the current round-trip fix for #4283.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@quaquel quaquel added the breaking Release notes label label Nov 30, 2026
@quaquel
quaquel merged commit 8e2d2bb into mesa:main Nov 30, 2026
17 checks passed
@EwoutH EwoutH removed the breaking Release notes label label Nov 30, 2026
EwoutH added a commit to EwoutH/mesa that referenced this pull request Nov 30, 2026
Seven PRs were merged after the notes were drafted: mesa#4284, mesa#3960,
mesa#4261, mesa#3979, mesa#4286, mesa#4216 and mesa#4301. Also moves the release date to
2026-11-30.

Assisted-by: Claude Code
EwoutH added a commit that referenced this pull request Nov 30, 2026
* Update release notes and version for 4.0.0a1

Add the 4.0.0a1 release notes to HISTORY.md, leaving out changes
already released in Mesa 3.5.1 to 3.5.4, and bump the version to
4.0.0a1. Move the #4182 replication id fix, which was added under
4.0.0a0, into the 4.0.0a1 notes where it was released.

Assisted-by: Claude Opus 5.5

* Leave dependabot and pre-commit.ci bumps out of the 4.0.0a1 notes

Assisted-by: Claude Opus 5.5

* Add PRs merged on 2026-11-30 to the 4.0.0a1 notes

Seven PRs were merged after the notes were drafted: #4284, #3960,
#4261, #3979, #4286, #4216 and #4301. Also moves the release date to
2026-11-30.

Assisted-by: Claude Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG Scenario RNG state and shared identity are not preserved during serialization

3 participants