Skip to content

Fix/cell capacity zero#2990

Merged
quaquel merged 2 commits intomesa:mainfrom
ahmednabiled:fix/cell-capacity-zero
Dec 22, 2025
Merged

Fix/cell capacity zero#2990
quaquel merged 2 commits intomesa:mainfrom
ahmednabiled:fix/cell-capacity-zero

Conversation

@ahmednabiled
Copy link
Copy Markdown
Contributor

Fix a bug in Cell.add_agent() where cells with capacity=0 incorrectly allowed unlimited agents instead of rejecting all agents.

Problem

The condition if self.capacity and n >= self.capacity: treated capacity=0 as 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:

cell = Cell(coordinate=(0, 0), capacity=0)
agent = CellAgent(model)
cell.add_agent(agent)  # Should raise exception, but didn't
print(len(cell.agents))  # Output: 1 (should be 0)

Solution

Changed the condition from if self.capacity to if self.capacity is not None to properly distinguish between:

  • capacity=None → unlimited agents allowed
  • capacity=0 → no agents allowed
  • capacity=1, 2, 3... → limited by the specified capacity

Changes Made

  • Fixed the capacity check logic in mesa/discrete_space/cell.py
  • Added test case for capacity=0 in tests/test_discrete_space.py

Testing

✓ Existing capacity tests still pass
✓ New test verifies capacity=0 raises exception when adding agents
✓ All cell-related tests pass

Impact

This fix addresses a logical bug in the capacity checking mechanism. Please review:

  • Code that may have relied on the previous capacity=0 behavior (which allowed unlimited agents) could be affected. This is considered fixing unintended behavior rather than a regression.
  • No expected impact on code using 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.

ahmednabiled and others added 2 commits December 22, 2025 21:19
- 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
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.9% [+0.4%, +1.4%] 🔵 -1.5% [-1.6%, -1.3%]
BoltzmannWealth large 🔵 +1.4% [-0.8%, +4.6%] 🔵 -2.1% [-4.2%, -0.1%]
Schelling small 🔵 +0.0% [-0.9%, +0.9%] 🔵 +1.0% [-0.3%, +2.3%]
Schelling large 🔵 +1.3% [-0.0%, +3.5%] 🔵 +2.5% [-1.2%, +6.6%]
WolfSheep small 🔵 -1.8% [-2.8%, -1.0%] 🔵 -1.1% [-1.7%, -0.7%]
WolfSheep large 🔵 -1.8% [-4.8%, +0.0%] 🔵 -1.2% [-2.4%, +0.3%]
BoidFlockers small 🔵 +1.1% [+0.4%, +1.7%] 🔵 +2.1% [+1.9%, +2.4%]
BoidFlockers large 🔵 -0.3% [-1.3%, +0.6%] 🔵 +1.7% [+1.3%, +2.1%]

@quaquel quaquel added the bug Release notes label label Dec 22, 2025
@quaquel
Copy link
Copy Markdown
Member

quaquel commented Dec 22, 2025

A very specific logical corner case. Thanks for spotting the bug, fixing it and including tests.

@quaquel quaquel merged commit de6468d into mesa:main Dec 22, 2025
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants