Tracking Issue: AgentSet Refactoring and _HardKeyAgentSet Implementation using AbstractAgentSet
This is a tracking issue(See #3128 and #3160 for more)
The current AgentSet implementation relies on WeakKeyDictionary for storage. While memory-efficient, this approach introduces performance overheads. To solve this fundamentally, we propose refactoring the AgentSet hierarchy to use an Abstract Base Class (ABC) pattern(credits to @quaquel) . This ensures a clean separation between the interface and the storage mechanism (Weak vs. Strong).
Proposed Roadmap
This refactor will be executed in three stages to maintain backward compatibility and easy to review
Phase 1: Introduce AbstractAgentSet
- Define a new
abc.ABCnamedAbstractAgentSet. - Move common logic (iteration, selection, etc) into the abstract base.
- Refactor the existing
AgentSetto inherit fromAbstractAgentSet, preserving its currentWeakKeyDictionarybehavior for backward compatibility.
Merged PR: #3210
Phase 2: Implement StrongAgentSet (Hard Refs)
- Create a new class
StrongAgentSet(orHardKeyAgentSet) inheriting fromAbstractAgentSet. - Implement storage using a standard Python
dictto guarantee deterministic behavior.
Merged PR: #3219
Phase 3: Integration & Cleanup
- Update
mesa.Modelto useStrongAgentSetas the internal storage formodel._agents_by_typeandmodel_all_agents. - Remove redundant internal attributes such as
model._agents.
Merged PR: #3224
Definition of Done
- [x]
AbstractAgentSetimplemented and tested. - [x]
StrongAgentSetimplemented with full test coverage for liveness/removal scenarios. - [x]
mesa.Modelupdated to useStrongAgentSet - [x] Legacy internal attributes removed from
Model.
Hi @EwoutH, @codebreaker32 , and @quaquel ,
I’ve been following this discussion closely and the proposed roadmap aligns well with some local experimentation I’ve been doing around an AbstractAgentSet + strong/weak-backed implementations.
The ABC-based split cleanly separates interface from storage and seems to address both:
the performance concerns around WeakKeyDictionary, and
the safety/use-case flexibility of keeping a weak-ref option available.
I’m particularly interested in helping with Phase 1 / Phase 2 (introducing AbstractAgentSet and a StrongAgentSet implementation), while keeping backward compatibility intact.
Before moving further, I wanted to check:
whether the proposed class naming (StrongAgentSet vs HardKeyAgentSet) is preferred, and
if there are any constraints I should be aware of before opening a PR aligned with this issue.
Happy to coordinate and split work if needed.
@codebreaker32 is already working on this.
Hi @Srinath0916, I’m planning to open a PR for Phase 2 soon. If you notice any performance implications or potential issues we might have missed in #3128 or #3160, feel free to share
Closed via #3210, #3219 and #3224
Thanks for working on this. Cool to see the performance improvements!
Could you write up how it now exactly works in the final design, and why it works / was designed this way, for future reference?
Thanks for working on this. Cool to see the performance improvements!
Glad it helped
Could you write up how it now exactly works in the final design
I have explicitly mentioned it in the docstrings. Are you asking for something else? Could you please elaborate?
I am also wondering what more is needed?
At its core, these PRs have moved model.agents and model.agents_by_type over to use a dictionary-based AgenSet (_HardKeyAgentSet). This has made the old model._agents redundant because we now have hard refs already. So, model._agents is removed. Finally, model.register_agent, and model.deregister_agent have been modified to interact with model.agents. They already did interact with model.agents_by_type.
Thanks @codebreaker32 and @quaquel for your great work on this.
Last year we had a brief discussion on unifying AgentSet and CellCollection under a common collection semantics: https://github.com/mesa/mesa/issues/2671. It appears that the newly introduced AbstractAgentSet is much similar to what we wanted as a base collection type, despite its name. Is there any chance to generalize (and perhaps rename) it to be a parent class for CellCollection too?
Thanks @wang-boyu for bringing this up,
I went through the entire discussion in #2671 and honestly I am a bit unsure about making AbstractAgentSet the parent class for both for two reasons:
-
AbstractAgentSetexplicitly inherits fromMutableSet. This means it strictly requires the implementation of mutating methods like add, discard, and remove. However,CellCollectionis specifically designed and documented to be an immutable collection of cells. Forcing an immutable class to inherit from aMutableSetbase would never be a good idea -
AbstractAgentSetis explicitly bound to the Agent type. Many of its internal methods rely on agent-specific behavior
Instead of forcing inheritance, I agree we can define a MesaCollectionProtocol( or some better name).