Skip to content

Update model.py to replace AgentSet with _HardKeyAgentSet#3224

Merged
quaquel merged 1 commit intomesa:mainfrom
codebreaker32:refactor/agentset#3
Jan 30, 2026
Merged

Update model.py to replace AgentSet with _HardKeyAgentSet#3224
quaquel merged 1 commit intomesa:mainfrom
codebreaker32:refactor/agentset#3

Conversation

@codebreaker32
Copy link
Copy Markdown
Collaborator

@codebreaker32 codebreaker32 commented Jan 29, 2026

This PR finalizes the agent storage refactor by fully integrating _HardKeyAgentSet as the primary storage mechanism in Model.
Previously, Model maintained two parallel structures: self._agents(for lookups and maintaining Hard References of agents) and self._all_agents for iteration. This duality created memory redundancy.

This PR resolves it by:

  1. Removing self._agents (the raw dictionary).
  2. Updating self._all_agents to be an instance of _HardKeyAgentSet.
  3. Updating self._agents_by_type to use _HardKeyAgentSet for type-specific groups.
  4. Updating docstrings to reflect the changes and warnings accordingly.

See #3128 and #3160 for complete discussion
Ticks (3) from #3209
Resolves #3128

This is a follow-up PR of #3210 and #3219

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -7.2% [-7.4%, -6.9%] 🟢 -3.6% [-3.9%, -3.4%]
BoltzmannWealth large 🟢 -7.6% [-8.1%, -7.1%] 🔵 -1.5% [-2.1%, -1.0%]
Schelling small 🟢 -4.0% [-4.3%, -3.8%] 🔵 -1.2% [-1.3%, -1.1%]
Schelling large 🔵 -3.5% [-4.2%, -2.9%] 🔵 +1.4% [+0.7%, +2.0%]
WolfSheep small 🟢 -9.3% [-9.5%, -9.1%] 🟢 -4.4% [-4.5%, -4.2%]
WolfSheep large 🟢 -8.3% [-9.0%, -7.5%] 🟢 -6.1% [-6.6%, -5.6%]
BoidFlockers small 🟢 -8.6% [-8.9%, -8.4%] 🔵 -2.9% [-3.1%, -2.7%]
BoidFlockers large 🟢 -9.1% [-9.6%, -8.8%] 🔵 -0.5% [-0.8%, -0.2%]

Copy link
Copy Markdown
Member

@quaquel quaquel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I particularly appreciate the very clear docstrings.

@quaquel quaquel merged commit 1c1a75e into mesa:main Jan 30, 2026
15 checks passed
@EwoutH EwoutH added the maintenance Release notes label label Feb 11, 2026
@codebreaker32 codebreaker32 deleted the refactor/agentset#3 branch February 17, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do we really need weakrefs in AgentSet?

3 participants