Repository navigation
Keep unhappy Schelling agents in place when the grid is full - #4286
Merged
Merged
Conversation
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.
|
Performance benchmarks:
|
quaquel
reviewed
Nov 30, 2026
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 | ||
|
|
||
|
|
Member
There was a problem hiding this comment.
See also #4287, not sure about the need for all these tests.
Contributor
Author
There was a problem hiding this comment.
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.
quaquel
approved these changes
Nov 30, 2026
EwoutH
added a commit
to EwoutH/mesa
that referenced
this pull request
Nov 30, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pre-PR Checklist
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.0every cell gets an agent. InSchellingAgent.step(), any unhappy agent callsgrid.select_random_empty_cell(), which correctly raises on a full grid:I found this by running every bundled example at the min and max of each slider in its
app.py. Schelling atdensity=1.0was 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/exceptrather than checkinggrid.emptiesfirst, becauseemptiesscans every cell and Schelling calls this for every unhappy agent each step. I first triedcontextlib.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
density=1.0(rng=42), some agents start unhappy and every agent stays in its cell for 5 steps; onmainthe first step raises theSpaceException. (I first added this as a test, then dropped it after review.)rng=42, 30 steps) gives identical agent positions andhappycounts tomain, so the RNG stream is unchanged.homophily=1,radius=2,density=0.8, 20 steps, 7 repeats) runs at 0.709–0.719s median with this change vs 0.716s onmain, so the difference is within noise.pytest tests/ -W error -W default::PendingDeprecationWarning: 1449 passed, counting the since-removed test. I skippedtests/examples/test_examples_viz.py, which needs Playwright browsers. The 4 Solara tests that needpytest-mockerrored the same way onmain, since I didn't have it installed.ruff check/ruff format --checkare clean.Additional Notes
At density 1.0 the model never reaches
running = Falseif 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.