Merged
Conversation
- Changed condition from 'if self.capacity' to 'if self.capacity is not None' - This ensures capacity=0 is treated as a valid limit (no agents allowed) - Previously, capacity=0 was treated as falsy, allowing unlimited agents - Added test case to verify capacity=0 raises exception when adding agents
|
Performance benchmarks:
|
quaquel
approved these changes
Dec 22, 2025
Member
|
A very specific logical corner case. Thanks for spotting the bug, fixing it and including tests. |
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.
Fix a bug in
Cell.add_agent()where cells withcapacity=0incorrectly allowed unlimited agents instead of rejecting all agents.Problem
The condition
if self.capacity and n >= self.capacity:treatedcapacity=0as falsy, which skipped the capacity check entirely. This meant cells with zero capacity could hold unlimited agents, which is incorrect behavior.Example of the bug:
Solution
Changed the condition from
if self.capacitytoif self.capacity is not Noneto properly distinguish between:capacity=None→ unlimited agents allowedcapacity=0→ no agents allowedcapacity=1, 2, 3...→ limited by the specified capacityChanges Made
mesa/discrete_space/cell.pycapacity=0intests/test_discrete_space.pyTesting
✓ Existing capacity tests still pass
✓ New test verifies
capacity=0raises exception when adding agents✓ All cell-related tests pass
Impact
This fix addresses a logical bug in the capacity checking mechanism. Please review:
capacity=0behavior (which allowed unlimited agents) could be affected. This is considered fixing unintended behavior rather than a regression.capacity=None(unlimited) or positive capacity values.Reviewers are welcome to discuss whether this change aligns with the intended design and if any deprecation or migration guidance is needed.