Skip to content

Keep unhappy Schelling agents in place when the grid is full - #4286

Merged
quaquel merged 4 commits into
mesa:mainfrom
prathameshfuke:fix/schelling-full-grid
Nov 30, 2026
Merged

quaquel merged 4 commits into
mesa:mainfrom
prathameshfuke:fix/schelling-full-grid

Conversation

@prathameshfuke

@prathameshfuke prathameshfuke commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

The Schelling example crashes on its first step when the "Agent density" slider is at its maximum. The app offers Slider("Agent density", 0.8, 0.1, 1.0, 0.1), so 1.0 is one click from the default.

Bug / Issue

At density=1.0 every cell gets an agent. In SchellingAgent.step(), any unhappy agent calls grid.select_random_empty_cell(), which correctly raises on a full grid:

from mesa.examples.basic.schelling.model import Schelling, SchellingScenario

Schelling(scenario=SchellingScenario(rng=42, density=1.0)).step()
# SpaceException: OrthogonalMooreGrid is completely full. No empty cells available.

I found this by running every bundled example at the min and max of each slider in its app.py. Schelling at density=1.0 was the only combination that failed.

I didn't open a separate issue since it's a small, reproducible bug in an example. Happy to file one if you'd rather track it that way.

Implementation

An unhappy agent that has nowhere to go stays where it is. That's the usual Schelling reading of a full grid, and the grid's own behaviour (raising when it's full) stays as it is.

I used try/except rather than checking grid.empties first, because empties scans every cell and Schelling calls this for every unhappy agent each step. I first tried contextlib.suppress, but it measured about 5% slower on the benchmark configuration, so I dropped it.

Capping the slider at 0.9 would also stop the crash in the app, but not for someone constructing the model directly, so I fixed the agent instead.

Testing

  • At density=1.0 (rng=42), some agents start unhappy and every agent stays in its cell for 5 steps; on main the first step raises the SpaceException. (I first added this as a test, then dropped it after review.)
  • A seeded default run (rng=42, 30 steps) gives identical agent positions and happy counts to main, so the RNG stream is unchanged.
  • The benchmark config (100×100, homophily=1, radius=2, density=0.8, 20 steps, 7 repeats) runs at 0.709–0.719s median with this change vs 0.716s on main, so the difference is within noise.
  • pytest tests/ -W error -W default::PendingDeprecationWarning: 1449 passed, counting the since-removed test. I skipped tests/examples/test_examples_viz.py, which needs Playwright browsers. The 4 Solara tests that need pytest-mock errored the same way on main, since I didn't have it installed.
  • ruff check / ruff format --check are clean.

Additional Notes

At density 1.0 the model never reaches running = False if some agents are unhappy, because nobody can move. That's the same as any run that doesn't converge, and the app just keeps stepping with no changes.

AI use: I used Claude Code to write the slider sweep that found this, and to draft the fix and test. I checked the reproduction, the benchmark numbers and the test results myself.

The Schelling app lets the density slider go up to 1.0. At that value
every cell is occupied, so the first unhappy agent's move raises
SpaceException from select_random_empty_cell() and the model crashes on
its first step. An unhappy agent with nowhere to go now stays where it
is.
Copilot AI balanced review requested due to automatic review settings November 29, 2026 18:23
@github-actions github-actions Bot added example Changes the examples or adds to them. testing Release notes label labels Nov 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.6% [-3.4%, +0.0%] 🔵 +0.6% [+0.1%, +1.0%]
BoltzmannWealth large 🔵 +0.9% [+0.2%, +1.6%] 🔵 +1.5% [-1.4%, +3.8%]
Schelling small 🔵 -0.8% [-1.5%, -0.0%] 🔵 -0.1% [-1.0%, +0.7%]
Schelling large 🔵 -1.5% [-2.1%, -0.9%] 🔵 +2.7% [+0.6%, +5.6%]
WolfSheep small 🔵 +1.2% [+0.7%, +1.6%] 🔵 +2.7% [+1.0%, +4.5%]
WolfSheep large 🔵 -0.4% [-1.9%, +1.1%] 🔵 +1.5% [-0.3%, +3.0%]
SugarscapeG1mt small 🔵 -0.9% [-1.9%, +0.1%] 🔵 -1.3% [-2.7%, +0.2%]
SugarscapeG1mt large 🔵 -1.8% [-3.5%, +0.6%] 🔵 +2.6% [+0.1%, +5.1%]
BoidFlockers small 🔵 +3.0% [+2.6%, +3.5%] 🔵 +1.8% [+1.5%, +2.1%]
BoidFlockers large 🔵 +1.8% [+0.9%, +2.7%] 🔵 +1.0% [+0.5%, +1.3%]
MultiLevelAllianceModel small 🔵 -1.5% [-3.5%, +0.8%] 🔵 +0.8% [+0.1%, +1.6%]
MultiLevelAllianceModel large 🔵 -1.3% [-2.7%, +0.0%] 🔵 -5.1% [-10.1%, +0.1%]

Comment thread tests/examples/test_examples.py Outdated
Comment on lines +97 to +107
def test_schelling_model_full_grid():
"""At density 1.0 (the app slider's maximum) unhappy agents stay put."""
model = Schelling(scenario=SchellingScenario(rng=42, density=1.0))
cells = {agent: agent.cell for agent in model.agents}
assert model.happy < len(model.agents)

model.run_for(5)

assert {agent: agent.cell for agent in model.agents} == cells


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also #4287, not sure about the need for all these tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. The fix is four lines and the repro is in the description, so I've dropped the test to keep the suite lean.

@github-actions github-actions Bot removed the testing Release notes label label Nov 30, 2026
@quaquel
quaquel merged commit 748a0fb into mesa:main Nov 30, 2026
15 of 17 checks passed
EwoutH added a commit to EwoutH/mesa that referenced this pull request Nov 30, 2026
Seven PRs were merged after the notes were drafted: mesa#4284, mesa#3960,
mesa#4261, mesa#3979, mesa#4286, mesa#4216 and mesa#4301. Also moves the release date to
2026-11-30.

Assisted-by: Claude Code
EwoutH added a commit that referenced this pull request Nov 30, 2026
* Update release notes and version for 4.0.0a1

Add the 4.0.0a1 release notes to HISTORY.md, leaving out changes
already released in Mesa 3.5.1 to 3.5.4, and bump the version to
4.0.0a1. Move the #4182 replication id fix, which was added under
4.0.0a0, into the 4.0.0a1 notes where it was released.

Assisted-by: Claude Opus 5.5

* Leave dependabot and pre-commit.ci bumps out of the 4.0.0a1 notes

Assisted-by: Claude Opus 5.5

* Add PRs merged on 2026-11-30 to the 4.0.0a1 notes

Seven PRs were merged after the notes were drafted: #4284, #3960,
#4261, #3979, #4286, #4216 and #4301. Also moves the release date to
2026-11-30.

Assisted-by: Claude Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

example Changes the examples or adds to them.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants