Skip to content

Fix unpickling of continuous spaces - #4108

Merged
EwoutH merged 1 commit into
mesa:mainfrom
EwoutH:fix-continuous-space-unpickling
Nov 15, 2026
Merged

EwoutH merged 1 commit into
mesa:mainfrom
EwoutH:fix-continuous-space-unpickling

Conversation

@EwoutH

@EwoutH EwoutH commented Nov 15, 2026

Copy link
Copy Markdown
Member

Pre-PR Checklist

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

Summary

An unpickled ContinuousSpace detached agent_positions from its backing array, so an agent's moves were lost as soon as another agent was added or removed.

Bug / Issue

Unpickling a ContinuousSpace turned agent_positions into an independent array instead of a view on the backing array. Positions written through agents then went only to the copy, and were lost as soon as an agent was added or removed and the view was taken again. This came up in #3939. The Grid pickling bug found at the same time was fixed in #3914.

Implementation

Take the view again in ContinuousSpace.__setstate__.

Testing

test_pickled_space_keeps_position_view fails without the fix and passes with it.

Additional Notes

Split from #3940, which is stacked on this PR. Written with help from Claude Code (Claude Opus 5.5).

Unpickling a ContinuousSpace turned agent_positions into an independent
array instead of a view on the backing array. Positions written through
agents then went only to the copy, and were lost as soon as an agent was
added or removed and the view was taken again. Take the view again in
__setstate__.

Assisted-by: Claude Opus 5.5
@EwoutH EwoutH added the bug Release notes label label Nov 15, 2026
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 15, 2026
@EwoutH

EwoutH commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

I split this off from #3940 on the (fair) request of the bot (probably should have done it earlier). This makes #3940 smaller, atomic and thus a bit easier to review.

This PR itself is also very small.

@quaquel @codebreaker32 could you review?

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -7.3% [-8.0%, -6.7%] 🔵 -1.2% [-1.5%, -1.0%]
BoltzmannWealth large 🟢 -5.0% [-6.2%, -3.5%] 🟢 -11.4% [-13.5%, -9.7%]
Schelling small 🟢 -7.2% [-8.1%, -6.3%] 🔵 -1.2% [-2.1%, -0.5%]
Schelling large 🔵 -1.5% [-1.8%, -1.2%] 🔵 -0.5% [-1.9%, +1.0%]
WolfSheep small 🔵 -2.1% [-2.4%, -1.7%] 🔵 +3.6% [-0.3%, +7.6%]
WolfSheep large 🔵 -2.1% [-2.6%, -1.6%] 🔵 -0.6% [-1.6%, +0.5%]
SugarscapeG1mt small 🔵 -0.9% [-1.1%, -0.6%] 🔵 +0.2% [-1.4%, +1.9%]
SugarscapeG1mt large 🔵 -1.9% [-2.9%, -1.0%] 🔵 +0.1% [-1.3%, +1.6%]
BoidFlockers small 🔵 -1.7% [-2.0%, -1.4%] 🔵 -1.5% [-1.9%, -1.1%]
BoidFlockers large 🔵 -2.0% [-2.4%, -1.6%] 🔵 -0.4% [-0.7%, -0.2%]
MultiLevelAllianceModel small 🔵 -2.2% [-3.1%, -1.4%] 🔵 -0.5% [-0.8%, -0.3%]
MultiLevelAllianceModel large 🔵 -1.7% [-2.4%, -1.1%] 🔵 -0.3% [-0.9%, +0.2%]

@EwoutH
EwoutH merged commit 28e4136 into mesa:main Nov 15, 2026
20 of 21 checks passed
EwoutH added a commit that referenced this pull request Nov 22, 2026
Unpickling a ContinuousSpace turned agent_positions into an independent
array instead of a view on the backing array. Positions written through
agents then went only to the copy, and were lost as soon as an agent was
added or removed and the view was taken again. Take the view again in
__setstate__.

(cherry picked from commit 28e4136)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant