mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Tracking Issue: AgentSet Refactoring and _HardKeyAgentSet Implementation using AbstractAgentSet

Open codebreaker32 opened this issue 10 months ago • 9 comments

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.ABC named AbstractAgentSet.
  • Move common logic (iteration, selection, etc) into the abstract base.
  • Refactor the existing AgentSet to inherit from AbstractAgentSet, preserving its current WeakKeyDictionary behavior for backward compatibility.

Merged PR: #3210

Phase 2: Implement StrongAgentSet (Hard Refs)

  • Create a new class StrongAgentSet (or HardKeyAgentSet) inheriting from AbstractAgentSet.
  • Implement storage using a standard Python dict to guarantee deterministic behavior.

Merged PR: #3219

Phase 3: Integration & Cleanup

  • Update mesa.Model to use StrongAgentSet as the internal storage for model._agents_by_type and model_all_agents.
  • Remove redundant internal attributes such as model._agents.

Merged PR: #3224

Definition of Done

  • [x] AbstractAgentSet implemented and tested.
  • [x] StrongAgentSet implemented with full test coverage for liveness/removal scenarios.
  • [x] mesa.Model updated to use StrongAgentSet
  • [x] Legacy internal attributes removed from Model.

codebreaker32 avatar Jan 25 '26 20:01 codebreaker32

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.

Srinath0916 avatar Jan 28 '26 00:01 Srinath0916

@codebreaker32 is already working on this.

quaquel avatar Jan 28 '26 06:01 quaquel

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

codebreaker32 avatar Jan 28 '26 12:01 codebreaker32

Closed via #3210, #3219 and #3224

codebreaker32 avatar Jan 30 '26 17:01 codebreaker32

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?

EwoutH avatar Jan 30 '26 20:01 EwoutH

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?

codebreaker32 avatar Jan 31 '26 06:01 codebreaker32

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.

quaquel avatar Jan 31 '26 09:01 quaquel

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?

wang-boyu avatar Feb 15 '26 16:02 wang-boyu

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:

  1. AbstractAgentSet explicitly inherits from MutableSet. This means it strictly requires the implementation of mutating methods like add, discard, and remove. However, CellCollection is specifically designed and documented to be an immutable collection of cells. Forcing an immutable class to inherit from a MutableSet base would never be a good idea

  2. AbstractAgentSet is 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).

codebreaker32 avatar Feb 16 '26 18:02 codebreaker32