Skip to content

Tracking Issue: AgentSet Refactoring and _HardKeyAgentSet Implementation using AbstractAgentSet #3209

@codebreaker32

Description

@codebreaker32

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions