mesa / mesa/mesa

Should create_agents/AgentSet.set() deepcopy mutable per-agent values by default?

Open
#3,863 4 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
3.9k
Forks
1.3k
Avg merge
2d 12h
Merged PRs (30d)
20

Description

opening this per quaquel's suggestion on mesa-examples#496.

AgentSet.set() and create_agents assign per-agent sequence values as-is, no copying. mesa-examples#496 hit this directly: VirusAntibodyModel needed each of 20 virus agents to get its own dna list, and the naive way to fix the length-mismatch crash there, `dna=[dna]*20`, would have had every agent holding the same list object. VirusAgent.generate_dna mutates dna in place on reproduction (`dna[idx] = ...`), so the first virus that reproduces would've silently corrupted every other virus's starting dna too. The actual fix there is manual, wrap each element in `copy.deepcopy` before passing it in.

question is whether create_agents/set() should deepcopy mutable per-agent values (list, dict, etc) by default instead of leaving every example to know this. quaquel flagged perf as a real concern, especially for numpy arrays, so maybe this only applies to plain mutable containers rather than a blanket deepcopy, or it's opt-in via a flag. not attached to a specific implementation, just opening the discussion.

Contributor guide

Open the contributing guide

Research direction

Start by reading AgentSet.set() and create_agents, then review the linked mesa-examples#496 context and VirusAgent.generate_dna's in-place mutation. Compare the behavior for mutable per-agent values with the stated performance concern around numpy arrays; done requires an agreed scope and implementation approach before coding can begin.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.