Skip to content

Introduce AbstractAgentSet to agent.py and refactor AgentSet to inherit from it#3210

Merged
quaquel merged 8 commits intomesa:mainfrom
codebreaker32:refactor/agentset#1
Jan 28, 2026
Merged

Introduce AbstractAgentSet to agent.py and refactor AgentSet to inherit from it#3210
quaquel merged 8 commits intomesa:mainfrom
codebreaker32:refactor/agentset#1

Conversation

@codebreaker32
Copy link
Copy Markdown
Collaborator

@codebreaker32 codebreaker32 commented Jan 26, 2026

Summary

This PR introduces AbstractAgentSet as an abstract base class for all agent collection implementations in Mesa. This establishes a formal interface and sets the foundation for introducing alternative storage mechanisms (strong vs weak references) while maintaining backward compatibility. Further, It refactors AgentSet class to inherit from it

Motive

For complete discussion: see #3128 and #3160

Changes

  1. New: AbstractAgentSet class: Defines the minimal interface for agent collections via abstract methods.

  2. Refactored: AgentSet class

  • Now inherits from AbstractAgentSet
  • All existing functionality preserved
  • Implements all abstract methods with weakref-optimized versions
  • Retains performance optimizations:

Fully backward compatible - No breaking changes

Ticks (1) from #3209

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.3% [-2.9%, -1.7%] 🟢 -5.8% [-6.0%, -5.6%]
BoltzmannWealth large 🔵 +0.0% [-1.0%, +1.2%] 🔵 -1.2% [-5.8%, +3.2%]
Schelling small 🔵 +0.1% [-0.5%, +0.7%] 🔵 -1.2% [-1.6%, -0.7%]
Schelling large 🔵 -0.5% [-1.1%, +0.0%] 🔵 -3.0% [-6.3%, +0.5%]
WolfSheep small 🔵 -0.5% [-1.5%, +0.5%] 🔵 -1.2% [-1.7%, -0.8%]
WolfSheep large 🔵 -2.1% [-3.5%, -0.9%] 🟢 -6.1% [-7.7%, -4.6%]
BoidFlockers small 🔵 -0.5% [-0.9%, -0.2%] 🔵 +1.1% [+0.9%, +1.4%]
BoidFlockers large 🔵 -0.3% [-0.9%, +0.4%] 🔵 -0.0% [-0.5%, +0.4%]

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

codebreaker32 commented Jan 26, 2026

I also have a refactoring suggestion:

In groupby:

  • Instead of passing a string result_type="list" or "agentset", can we pass a boolean flag as_list.
  • If as_list=True, list will be returned otherwise agentset(or strongagentset)

I have not introduced it here to keep it atomic

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 26, 2026

I also have a refactoring suggestion:

In groupby:

  • Instead of passing a string result_type="list" or "agentset", can we pass a boolean flag as_list.
  • If as_list=True, list will be returned otherwise agentset(or strongagentset)

I have not introduced it here to keep it atomic

It would also be a backward-incompatibility change, so we cannot do that in Mesa 3.x because of semver.

1. AbstractAgentSet no longer inherits from Sequence
2. AgentSet explicitly inherits from Sequence to maintain backward compatibility
@codebyNJ
Copy link
Copy Markdown
Contributor

few observations @codebreaker32:

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

codebreaker32 commented Jan 26, 2026

select() uses type(self) while groupby() hardcodes AgentSet

You are mistaken, both are using type(self)

should StrongAgentSet return weak refs from all methods, or should the base class use type(self) and StrongAgentSet override?)

See The main agenda for StrongAgentSet is performance and determinism, but we know that handling strong references manually creates a high risk of memory leaks for end-users.

The goal is that operations creating a "view" or "copy" of the data (like shuffle(inplace=False) or copy()) should default to returning a standard AgentSet (weak refs). This ensures that while the Model uses strong refs internally for stability, objects handed back to the user are "safe" by default and won't keep dead agents alive.

So yes StrongAgentSet will override them

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 27, 2026

with the update to agent.py, there is now a conflict. I guess it has to do with deprecating sequence behavior on AgentSet. Can you take a look at the conflict and resolve it? I think this will be ready to be merged afterwards.

@codebreaker32
Copy link
Copy Markdown
Collaborator Author

Can you take a look at the conflict and resolve it?

Done.

@quaquel quaquel merged commit 9f8ebd5 into mesa:main Jan 28, 2026
14 checks passed
@EwoutH EwoutH added the enhancement Release notes label label Jan 28, 2026
@codebreaker32 codebreaker32 deleted the refactor/agentset#1 branch January 28, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants