Prevent RecursionError and data loss in Cell/Grid deepcopy#3222
Merged
Prevent RecursionError and data loss in Cell/Grid deepcopy#3222
Conversation
|
Performance benchmarks:
|
|
Performance benchmarks:
|
…epcopy - Implement coordinate-based serialization in 'Cell.__getstate__' to break circular references, preventing 'RecursionError' in large grids. - Add relinking logic to [DiscreteSpace] and [Grid] [__setstate__] to reconstruct neighbor object references from coordinates. - Fix a bug where standalone [Cell]deepcopies resulted in 'None' connections. - Ensure manual grid modifications are preserved after serialization. - Add 'test_cell_deepcopy' to verify graph integrity after copy.
5febe1d to
9b843c4
Compare
|
Performance benchmarks:
|
quaquel
reviewed
Jan 29, 2026
quaquel
reviewed
Jan 29, 2026
quaquel
reviewed
Jan 29, 2026
7980279 to
99aa64c
Compare
99aa64c to
d5da656
Compare
quaquel
reviewed
Jan 30, 2026
mesa/discrete_space/grid.py
Outdated
Comment on lines
200
to
204
| self.cell_klass = type( | ||
| self._cells[(0, 0)] | ||
| next(iter(self._cells.values())) | ||
| ) # the __reduce__ function handles this for us nicely | ||
| for layer in self._mesa_property_layers.values(): | ||
| setattr( |
Member
There was a problem hiding this comment.
After reviewing your code, I realize this should be moved to the DiscreteSpace class.
quaquel
requested changes
Jan 30, 2026
Member
quaquel
left a comment
There was a problem hiding this comment.
one last minor change and then I think this is ready to be merged.
3d6bced to
6153042
Compare
quaquel
approved these changes
Jan 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses critical bugs in the new
discrete_spacesystem when copying or pickling model states. This is essential for model branching, batch runs, and saving/loading model progress.Problem
deepcopyalgorithm exceeds Python's maximum recursion depth due to the high number of interconnected circular cell references.Cell.__getstate__implementation attempted to avoid recursion by nullifying neighbor references. This made standalone copied cells unusable (neighbors became None) and broke relational integrity within the grid.Grid.__setstate__was re-calling_connect_cells(), which would blindly recalculate the default grid pattern and overwrite any custom connections or removals made to the grid topology before serialization.Solution
Cell.__getstate__now extracts the neighbor's coordinate instead of the object reference during state collection. This "breaks" the circular object chain for the pickler, preventingRecursionError.DiscreteSpaceandGridhandle the "relinking" phase insetstate. Once all cells are restored, the parent space iterates through them and replaces the coordinate placeholders with actualCellobject pointers from its internal mapping.Changes
mesa/discrete_space/cell.py: Updatedgetstateto use coordinate mapping.mesa/discrete_space/discrete_space.py: Added coordinate-based relinking insetstate.mesa/discrete_space/grid.py: Updatedsetstateto use the new relinking logic and removed the redundant_connect_cells()call.tests/discrete_space/test_discrete_space.py: Added a new test test_cell_deepcopy to verify both standalone and grid-based relational integrity.Closes #3221