Skip to content

Keep replication ids increasing across spawn calls - #4182

Merged
quaquel merged 2 commits into
mesa:mainfrom
atul-upadhyay-7:fix/replication-ids-4076
Nov 23, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
atul-upadhyay-7:fix/replication-ids-4076

Conversation

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

Continue replication ids across repeated spawn_replications() calls on the same parent.

Bug / Issue

Fixes #4076.

The seeds advance on each call, but the ids restart at zero. Later batches can then replace earlier results with the same run ids.

Implementation

Keep a private count on the parent and use it as the starting id for the next batch, following the approach discussed in #4076. Save the count and child-seed position when pickling a scenario.

This replaces the runner-level check proposed in #4078. No runner or store behavior changes.

Testing

  • Regression tests on unchanged main: 12 failed, 3 passed.
  • Scenario tests: 152 passed in each of five runs.
  • Full suite: 1,220 passed, including visualization tests.
  • Ruff 0.17.3 lint and format checks passed.

Tests cover repeated batches, separate experiments sharing an in-memory store, memory and disk output, process workers, deterministic seeds, empty and invalid batches, subclasses and pickling.

The test runs report 16 Python multiprocessing fork warnings. The original scenario suite also reports this warning; the extra process-worker cases exercise it six more times.

Additional Notes

The counter is private and does not become a model parameter. This does not add DiskStore append or resume support.

@github-actions github-actions Bot added docs Release notes label experimental Release notes label testing Release notes label labels Nov 22, 2026
for i in range(n)
for i, seed in enumerate(child_seeds)
]
object.__setattr__(self, "_replications_spawned", start + len(replications))

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.

why so complicated?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i simplified the numbering to use enumerate(child_seeds, start=start). object.setattr is there because Scenario is frozen; normal assignment to the counter raises TypeError. keeping the update after the list also means a failed child constructor does not advance the id counter. the pickle behavior is unchanged. the scenario tests passed five times and the full suite passed.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔴 +17.1% [+15.8%, +18.5%] 🔵 +1.0% [+0.3%, +2.0%]
BoltzmannWealth large 🔴 +10.3% [+9.1%, +11.6%] 🔴 +47.8% [+42.6%, +52.7%]
Schelling small 🔴 +6.5% [+5.1%, +8.0%] 🔵 +3.2% [+2.0%, +4.4%]
Schelling large 🔵 -2.0% [-3.5%, -0.7%] 🟢 -7.6% [-11.0%, -4.4%]
WolfSheep small 🟢 -5.6% [-6.2%, -5.0%] 🔵 -4.0% [-7.4%, -0.4%]
WolfSheep large 🟢 -14.8% [-16.6%, -13.0%] 🟢 -15.0% [-16.6%, -13.3%]
SugarscapeG1mt small 🔵 -0.1% [-3.2%, +2.8%] 🔵 -2.2% [-4.2%, -0.1%]
SugarscapeG1mt large 🔵 +1.9% [-1.9%, +6.4%] 🔵 +1.4% [-1.5%, +4.1%]
BoidFlockers small 🔵 -1.6% [-2.8%, -0.4%] 🔵 -2.3% [-2.8%, -1.7%]
BoidFlockers large 🟢 -7.0% [-8.4%, -5.4%] 🔵 -3.7% [-4.7%, -2.5%]
MultiLevelAllianceModel small 🟢 -20.8% [-22.1%, -19.7%] 🟢 -6.6% [-7.4%, -5.9%]
MultiLevelAllianceModel large 🟢 -12.1% [-14.2%, -10.5%] 🟢 -27.4% [-30.3%, -24.1%]

@quaquel
quaquel merged commit 08b7ddc into mesa:main Nov 23, 2026
18 checks passed
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

docs Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repeated spawn_replications calls reuse RunIds and overwrite results

2 participants