Skip to content

feat(agentset): warn when one mutable object is shared across agents - #3920

Merged
quaquel merged 2 commits into
mesa:mainfrom
RudraDudhat2509:warn-shared-per-agent-objects
Oct 28, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
RudraDudhat2509:warn-shared-per-agent-objects

Conversation

@RudraDudhat2509

@RudraDudhat2509 RudraDudhat2509 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • I linked an issue/discussion where a maintainer approved this enhancement/feature for implementation.

Approval Link

#3863 (quaquel: "document the behavior in the docstring and, perhaps, issue a single warning if the object is identical")

Summary

When one builtin mutable container (a list, dict, set, or bytearray) is passed as a per-agent sequence to create_agents or AgentSet.set, every agent that receives it shares the same object. This PR adds a single UserWarning for that case, and documents that per-agent values are assigned by reference.

No copying is done by default.

Motive

[dna] * 20 looks like a fine way to give 20 agents a starting value. It isn't, when the value is mutable and an agent changes it in place. This is the bug behind mesa-examples#496, where the virus model needed a manual copy.deepcopy per agent to stop all viruses sharing one DNA list.

Implementation

  • _resolve_per_agent_values calls a new helper, _warn_if_object_shared_between_agents, when a sequence matches the agent count.
  • The helper walks the sequence once and only looks at builtin containers (list, dict, set, bytearray). It warns on the first one it sees twice. One warning per call.
  • Other shared objects are not checked on purpose. A cell or location reference is often meant to be shared, so warning on it would be noise. The WolfSheep example does exactly this.
  • Scalars and numpy scalars are skipped. Iterating an array makes fresh objects, and reused ids on those would give false positives.
  • stacklevel=4, so the warning points at the caller's line, not at mesa internals.
  • Docstrings for _resolve_per_agent_values and AgentSet.set now say values are assigned by reference.

Usage Examples

dna = [1, 2, 3]

# warns: every agent shares the same list
agents = AgentSet(...)
agents.set("dna", [dna] * len(agents))

# no warning: each agent gets its own copy
agents.set("dna", [copy.deepcopy(dna) for _ in agents])

# no warning: immutables can be repeated safely
agents.set("speed", [0.5] * len(agents))

Additional Notes

  • Related to mesa-examples#496. Its fix already gives each agent a separate deepcopy, so the warning doesn't fire there.
  • Broadcast values (a single scalar or dict passed to every agent) don't warn. That's intentional, since a shared space reference is normal.
  • Tests: tests/test_agentset.py::test_set_warns_when_one_mutable_object_is_shared_by_agents. Red before the change, green after. tests/test_agentset.py and tests/test_agent.py pass (59 tests). ruff check and ruff format --check are clean.
  • Cost: the check is one pass over the sequence, skipping immutables early. I haven't benchmarked large inputs yet.

@github-actions github-actions Bot added the testing Release notes label label Oct 6, 2026
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.3% [-2.9%, -1.8%] 🔵 -0.9% [-1.2%, -0.6%]
BoltzmannWealth large 🔵 +0.6% [+0.1%, +1.1%] 🔵 -4.9% [-6.7%, -2.8%]
Schelling small 🔵 -0.7% [-1.2%, -0.1%] 🔵 -0.2% [-0.9%, +0.6%]
Schelling large 🔵 +1.4% [+0.3%, +2.3%] 🔴 +10.4% [+8.0%, +12.9%]
WolfSheep small 🔵 +1.9% [+1.3%, +2.4%] 🔵 -0.6% [-5.0%, +4.1%]
WolfSheep large 🔵 -0.2% [-1.0%, +0.5%] 🔵 -1.9% [-3.4%, -0.4%]
SugarscapeG1mt small 🔵 -0.8% [-1.3%, -0.3%] 🔵 -1.6% [-3.3%, +0.1%]
SugarscapeG1mt large 🔵 +1.9% [-0.3%, +4.0%] 🔵 -2.2% [-3.5%, -0.9%]
BoidFlockers small 🔵 +3.4% [+2.8%, +4.1%] 🔵 -0.4% [-0.9%, +0.2%]
BoidFlockers large 🔵 +3.3% [+2.6%, +3.9%] 🔵 -1.5% [-1.8%, -1.1%]
MultiLevelAllianceModel small 🔵 +4.4% [+2.8%, +6.0%] 🔴 +12.3% [+11.9%, +12.8%]
MultiLevelAllianceModel large 🔴 +4.7% [+3.6%, +5.7%] 🔴 +15.1% [+13.1%, +17.0%]

@quaquel quaquel added the enhancement Release notes label label Oct 28, 2026
@quaquel

quaquel commented Oct 28, 2026

Copy link
Copy Markdown
Member

can you take a look at the merge conflicts?

@RudraDudhat2509

Copy link
Copy Markdown
Contributor Author

sure give me a few

Comment thread mesa/agentset.py
"it on one agent will change it for every agent that shares it; "
"pass a separate copy per agent if that is not what you want.",
UserWarning,
stacklevel=4,

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 stack level 4?

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.

its the amount of hops between the users code and warnings.warn(), the flow is like this

  1. warnings.warn()line itself line 131.
  2. _resolve_per_agent_values, line 105.
  3. set() line 468.
  4. user's code that called set()

@RudraDudhat2509
RudraDudhat2509 force-pushed the warn-shared-per-agent-objects branch from f2775aa to 673fc65 Compare October 28, 2026 15:41
@RudraDudhat2509

Copy link
Copy Markdown
Contributor Author

merge conflict should be fixed on the latest push, thanks

@quaquel
quaquel merged commit 20ded17 into mesa:main Oct 28, 2026
15 checks passed
@RudraDudhat2509
RudraDudhat2509 deleted the warn-shared-per-agent-objects branch October 28, 2026 15:48
@RudraDudhat2509

Copy link
Copy Markdown
Contributor Author

thx for the review !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants