Skip to content

fix(discrete_space): snapshot CellCollection mappings - #4232

Merged
quaquel merged 1 commit into
mesa:mainfrom
Jenil1905:fix/cellcollection-mapping-snapshot
Nov 25, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
Jenil1905:fix/cellcollection-mapping-snapshot

Conversation

@Jenil1905

Copy link
Copy Markdown
Contributor

[NOTE]

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

Pre-PR Checklist

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

Summary

This PR fixes two issues in CellCollection when initialized with a mapping:

  • Non-dict Mapping implementations were not handled as mappings, causing their supplied key-value pairs to be discarded.
  • The constructor retained the original input dictionary, allowing external mutations to change the collection's membership after initialization. This could leave iteration and len() inconsistent with the cached .cells tuple.

Bug / Issue

A non-dict object implementing collections.abc.Mapping could be treated as an iterable of cells, so CellCollection rebuilt its values from each cell’s internal agent list instead of preserving the supplied mapping values.

When initialized with a mutable dictionary, the collection also retained that dictionary. Mutating it after construction could change iteration and length while the cached .cells tuple still reflected the original membership.

Implementation

  • Handle general collections.abc.Mapping inputs instead of checking only for dict.
  • Create a shallow snapshot of the input mapping using dict(cells).
  • Preserve the supplied keys, values, and agent-list references.
  • Leave iterable-of-cells initialization unchanged.

Testing

  • Add coverage for non-dict mapping inputs using MappingProxyType.
  • Add regression coverage ensuring mutations to the original mapping do not affect the initialized collection.
  • Verify consistency between collection membership, iteration, length, and .cells.

Validation results:

  • tests/discrete_space: 172 passed
  • Ruff lint: passed
  • Ruff format: passed
  • git diff --check: passed

Additional Notes

Codex assisted with investigation, implementation, and validation. I reviewed the changes and test results.

@github-actions github-actions Bot added the testing Release notes label label Nov 24, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.6% [+1.7%, +3.5%] 🔵 +3.0% [+2.5%, +3.5%]
BoltzmannWealth large 🔴 +7.3% [+5.8%, +8.6%] 🔴 +6.6% [+4.3%, +8.8%]
Schelling small 🔵 +3.6% [+2.0%, +5.2%] 🔵 -2.3% [-4.0%, -0.8%]
Schelling large 🔵 -3.5% [-4.9%, -2.1%] 🔵 +2.9% [+0.9%, +4.8%]
WolfSheep small 🔵 -0.1% [-1.0%, +0.7%] 🔵 -5.0% [-7.5%, -2.8%]
WolfSheep large 🔵 -3.6% [-4.7%, -2.1%] 🔵 -2.5% [-4.1%, -0.6%]
SugarscapeG1mt small 🔴 +6.1% [+4.8%, +7.4%] 🔵 -0.0% [-2.2%, +2.1%]
SugarscapeG1mt large 🔵 +4.4% [+2.2%, +6.3%] 🔵 -0.7% [-3.5%, +1.9%]
BoidFlockers small 🔵 +0.5% [-0.4%, +1.3%] 🔵 -2.5% [-3.2%, -1.8%]
BoidFlockers large 🔵 +0.0% [-1.9%, +1.6%] 🔵 -1.5% [-3.3%, +0.1%]
MultiLevelAllianceModel small 🔵 -1.7% [-3.1%, -0.3%] 🔵 -3.4% [-4.4%, -2.2%]
MultiLevelAllianceModel large 🔴 +4.9% [+3.1%, +6.6%] 🔵 +5.1% [+2.3%, +8.2%]

@quaquel quaquel added the bug Release notes label label Nov 25, 2026
@quaquel
quaquel merged commit 1565f74 into mesa:main Nov 25, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants