Repository navigation
Keep agents on the copied grid's cells after deepcopy - #4306
prathameshfuke wants to merge 1 commit into
Conversation
pickle_gridcell passed a cell's whole state, including its agents, as the reconstructor's arguments. copy.deepcopy copies those arguments before it records the new cell, so for a cell holding several agents it reached the cell again through agent.cell and copied it a second time. In the copy, every agent but one per cell then sat on a detached duplicate whose connections were still coordinate tuples, and the next neighborhood lookup raised AttributeError (Boltzmann wealth, wolf-sheep). pickle has a guard for this case, so only deepcopy was affected. Return the state in the reduce tuple's state slot so the cell is recorded before its agents are copied. unpickle_gridcell still accepts the old two-argument form so existing pickles load.
|
Performance benchmarks:
|
|
Thanks for the Boltzmann reproducer, @prathameshfuke. I reproduced the 34 detached agents and next-step crash on main; applying your reducer change in memory gives zero detached agents and a successful next step. #4025 already contains the same state-slot correction, alongside round-trip tests and a memory-retention fix. @quaquel, should this fix proceed separately or be consolidated into #4025? I'd consolidate into #4025 because it already covers the same cause and includes regression tests. @prathameshfuke, please inspect and resolve the failing Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot. |
arzaanxeng
left a comment
There was a problem hiding this comment.
noticed that unpickle_gridcell() still reconstructs the cell using parent((0, 0)). Would this remain compatible with user-defined cell classes whose constructors require additional arguments?
PR #4025 appears to address this by allocating the instance through base.__new__(base), while also implementing the state-slot correction and adding regression tests.
Given the overlap, would consolidating these changes into #4025 be preferable?
Additionally, it would be helpful to include the multi-agent deepcopy reproducer as a committed regression test if this PR proceeds independently.
Pre-PR Checklist
Summary
copy.deepcopyof a model whose grid cells hold more than one agent leaves most agents on detached duplicate cells that aren't part of the copied grid. Those duplicates still have their neighbours stored as coordinate tuples, so the copied model crashes on its first neighbourhood lookup. Boltzmann wealth and wolf-sheep both hit it; pickling the same models works.Bug / Issue
For a deep-copied wolf-sheep model it's 117 agents. With one agent per cell (e.g. Schelling) nothing goes wrong, which is why it's easy to miss.
Cause.
pickle_gridcellreturns the cell's whole state, including_agents, as the reconstructor's arguments:(unpickle_gridcell, (parent, state)).copy.deepcopycopies the arguments before it creates the new cell and records it in its memo. While copying the agents list it reaches the second agent, thenagent.cell, which is the same cell that isn't recorded yet, so it reduces and copies that cell again. Every agent after the first one in a cell ends up on such a duplicate, and itsconnectionsare still the coordinate tuples fromCell.__getstate__, becauseGrid.__setstate__only rebuilds connections for the cells in_cells.pickledoesn't have this problem, becausesave_reducechecks whether the object was already saved while saving its arguments.deepcopyhas no such check.Implementation
pickle_gridcellnow returns the state in the reduce tuple's state slot:(unpickle_gridcell, (parent,), state). The cell is created and recorded before its agents are copied, soagent.cellresolves to the same copy. Bothcopyandpickleapply a(dict, slots)state the same wayunpickle_gridcelldid by hand.unpickle_gridcellkeeps accepting the old(parent, fields)form, so pickles written before this change still load.Testing
OrthogonalMooreGrid,OrthogonalVonNeumannGridandHexGridwith 1 and 3 agents per cell, underdeepcopyandpickle.main, deepcopy leaves 12 of 18 agents on detached cells with 3 agents per cell.main, Boltzmann's copy raises as above.pickle_gridcellloads with this change (agents, property layer and neighbourhoods intact).pytest tests/discrete_space -W error -W default::PendingDeprecationWarning: 194 passed.tests/(1306 passed), the failing set is identical tomain's. Those are the visualization tests, which need Solara, Matplotlib and Altair, which I didn't install.ruff checkandruff format --checkare clean.Additional Notes
Related to #3914, which fixed the GridCell class after a pickle round trip. This fixes cell identity under
deepcopy.AI use: I used Claude Code to run a pickle/deepcopy round trip over the bundled examples (which surfaced this), trace it to
pickle_gridcell, and draft the change. I checked the cause, the before/after numbers and the test results myself.