Conversation
…empty_cell() when grid is full
This comment was marked as outdated.
This comment was marked as outdated.
Contributor
Author
|
Hi! I was going through the project setup and noticed that Docker is mentioned in readme. I use Docker quite often, but I couldn’t find a docker-compose.yml file in the repository. |
quaquel
reviewed
Mar 10, 2026
…ll() for full grids
quaquel
reviewed
Mar 11, 2026
quaquel
approved these changes
Mar 11, 2026
Contributor
Author
|
Done! ThankYou @quaquel |
|
Performance benchmarks:
|
|
Performance benchmarks:
|
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.
Summary
Fixes a crash in
Grid.select_random_empty_cell()when the grid is completely full by wrappingrandom.choice()in a try-catch block and raising a clearValueErrorinstead of a crypticIndexError.Bug / Issue
Fixes #3014
When a user's agent-based model fills a grid to 100% capacity and calls
select_random_empty_cell(), the method crashes with an unhelpful error:This happens because the heuristic tries 50 random cell selections (all fail on a full grid), then falls back to
np.argwhere(self.property_layers["empty"])which returns an empty array. Whenrandom.choice()is called on an empty array, it raisesIndexError, leaving users confused about what went wrong.Implementation
Wrapped
self.random.choice(empty_coords)in a try-except block inGrid.select_random_empty_cell()(mesa/discrete_space/grid.py) to catchIndexErrorand raise a clearValueErrorwith proper exception chaining.Code change:
This approach follows the EAFP (Easier to Ask for Forgiveness than Permission) principle, is more Pythonic, and provides better exception chaining for debugging.
Testing
Added unit test
test_infinite_loop_on_full_grid()in test_discrete_space.py that fills a 2×2 grid completely and asserts that the newValueErrorwith the descriptive message is raised.Verified test passes with the implementation.
Additional Notes
This change creates a better Developer Experience (DX) by "failing fast" with an explicit, actionable error message rather than crashing deep in the code with a cryptic exception. Users immediately understand that their grid is full and can adjust their model parameters accordingly (more cells, faster agent movement, etc.).
No breaking API changes — only the error type changed, which improves clarity without reducing functionality.