Skip to content

Add informative __repr__ and __str__ to AgentSet and GroupBy - #3741

Merged
quaquel merged 5 commits into
mesa:mainfrom
AshNicolus:add-agentset-repr
Jun 15, 2026
Merged

quaquel merged 5 commits into
mesa:mainfrom
AshNicolus:add-agentset-repr

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

Closes #3735

What

The core collection objects had no informative __repr__/__str__, so in a REPL,
notebook, or debugger they showed only the default <... object at 0x...>. Cell
already defines a useful __repr__, so this also removes an API inconsistency.

Change

Per the discussion in #3735:

  • AbstractAgentSet.__repr__ → <AgentSet (200 agents)> (uses the real class name,
    so model.agents shows <_HardKeyAgentSet (200 agents)>).
  • AbstractAgentSet.__str__ → AgentSet with 200 agents.
  • GroupBy.__repr__ → GroupBy(3 groups).

All are count-only and O(1) (len() is constant-time for both AgentSet types and
GroupBy), so they add no overhead. The per-type breakdown was dropped to avoid
iterating all agents on every call, and _repr_html_ is left for a future issue,
both as agreed on the issue.

Test

Added test_agentset_repr_and_str and test_groupby_repr. Full non-viz suite
(508 tests) passes under -Werror; ruff clean.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.5% [-2.0%, -0.9%] 🔵 +0.8% [+0.5%, +1.0%]
BoltzmannWealth large 🔵 -1.3% [-2.0%, -0.6%] 🔵 +4.7% [+2.0%, +7.3%]
Schelling small 🔵 -2.0% [-2.7%, -1.3%] 🔵 -0.1% [-0.8%, +0.6%]
Schelling large 🔵 -1.6% [-2.5%, -0.8%] 🔵 +3.5% [+2.1%, +4.9%]
WolfSheep small 🔵 -0.6% [-1.1%, +0.0%] 🔵 -0.1% [-4.5%, +4.8%]
WolfSheep large 🔵 +0.4% [-1.0%, +2.0%] 🔴 +8.3% [+3.9%, +13.1%]
SugarscapeG1mt small 🟢 -5.8% [-6.5%, -5.1%] 🔵 -2.2% [-3.7%, -0.7%]
SugarscapeG1mt large 🔵 -1.3% [-3.0%, +0.2%] 🔵 -0.3% [-1.9%, +1.6%]
BoidFlockers small 🔵 -2.0% [-2.7%, -1.4%] 🔵 +2.6% [+2.1%, +3.2%]
BoidFlockers large 🔵 -1.5% [-2.1%, -0.8%] 🔵 +1.2% [+0.8%, +1.7%]

@quaquel

quaquel commented Jun 10, 2026

Copy link
Copy Markdown
Member

In groupby, you went with just the number of groups but not the composition of each group (or at least the identifiers for the groups). Why and would it not make sense to at least include the group identifiers?

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Good point. I kept it minimal at first because of the cost concern from #3735, but that was about the AgentSet per-type breakdown, which needs a full pass over the agents. It doesn't apply to GroupBy since the groups are already split, so len(group) per group is cheap.

Updated the repr to include the identifiers and sizes:

GroupBy(2 groups: {'Wolf': 2, 'Sheep': 1})

Test updated too.

@quaquel quaquel added the enhancement Release notes label label Jun 15, 2026
@quaquel
quaquel merged commit 79639de into mesa:main Jun 15, 2026
10 of 11 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
* Add informative __repr__ and __str__ to AgentSet and GroupBy

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Show group identifiers and sizes in GroupBy repr

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
(cherry picked from commit 79639de)
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
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.

Add informative __repr__ to AgentSet and GroupBy

2 participants