Skip to content

Make CellCollection.select_random_cell empty-safe with a default parameter - #3725

Merged
quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:make-select-random-cell-empty-safe
May 31, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:make-select-random-cell-empty-safe

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

What

CellCollection.select_random_cell() calls self.random.choice(self.cells)
directly, which raises a bare IndexError with no context when the collection
is empty.

The sibling method select_random_agent() already handles this gracefully via a
default parameter (returning the default when empty, or raising LookupError
otherwise). This PR brings select_random_cell() in line with that existing API
so the two methods behave consistently.

Change

  • select_random_cell(default=RAISES): returns default when the collection is
    empty, or raises LookupError with a clear message if no default is given.
    Non-empty behavior is unchanged.

This mirrors the existing select_random_agent signature and semantics exactly —
no new patterns introduced.

Test

Added test_select_random_cell_empty_safe, matching the existing
test_select_random_agent_empty_safe:

  • raises LookupError on an empty collection
  • returns the provided default when empty
  • still returns a valid cell for a non-empty collection

All 88 tests in tests/discrete_space/test_discrete_space.py pass; ruff clean.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.4% [-0.1%, +1.0%] 🔵 +1.0% [+0.8%, +1.3%]
BoltzmannWealth large 🔵 +1.4% [+1.0%, +1.7%] 🔵 -0.7% [-1.3%, -0.2%]
Schelling small 🟢 -4.2% [-4.8%, -3.4%] 🟢 -4.0% [-4.9%, -3.2%]
Schelling large 🔵 +2.3% [+1.4%, +3.2%] 🔵 -6.3% [-10.2%, -2.7%]
WolfSheep small 🔵 +0.8% [+0.4%, +1.2%] 🔵 -2.8% [-6.0%, +0.3%]
WolfSheep large 🔵 +2.1% [+1.6%, +2.7%] 🔵 -1.5% [-2.3%, -0.7%]
SugarscapeG1mt small 🟢 -6.5% [-7.5%, -5.4%] 🔵 -2.6% [-4.5%, -0.8%]
SugarscapeG1mt large 🔵 -3.7% [-6.7%, -0.4%] 🔵 -2.2% [-3.9%, -0.4%]
BoidFlockers small 🔵 +1.5% [+1.3%, +1.8%] 🔵 +0.0% [-0.4%, +0.5%]
BoidFlockers large 🔵 +1.7% [+1.5%, +2.0%] 🔵 +0.4% [-0.0%, +0.9%]

@quaquel quaquel added the enhancement Release notes label label May 31, 2026
@quaquel

quaquel commented May 31, 2026

Copy link
Copy Markdown
Member

This makes sense to me and looks good. Thanks for the PR.

@quaquel
quaquel merged commit 8cbbd86 into mesa:main May 31, 2026
20 of 21 checks passed
@quaquel quaquel added the backport-candidate PRs we might want to backport to an earlier branch label May 31, 2026
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate PRs we might want to backport to an earlier branch enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants