Skip to content

fix(discrete_space): do not flip empty flag when add_agent is rejected (#3883) - #3889

Merged
quaquel merged 1 commit into
mesa:mainfrom
rishiiicreates:fix/cell-add-agent-empty-flag
Oct 24, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
rishiiicreates:fix/cell-add-agent-empty-flag

Conversation

@rishiiicreates

Copy link
Copy Markdown
Contributor

Note

Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.

Pre-PR Checklist

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

Summary

Fixes an issue where Cell.add_agent sets self.empty = False before checking cell capacity. When addition is rejected with a CellFullException, self.empty remained False, leaving it out of sync with self.is_empty.

Bug / Issue

Fixes #3883.
When calling cell.add_agent(agent) on a cell that has reached its capacity limit, a CellFullException is raised. However, because self.empty = False was previously placed before the capacity check, the cell was marked as non-empty even though no agent was added.

Implementation

Moved self.empty = False in Cell.add_agent to run after the capacity validation passes and self._agents.append(agent) succeeds. This matches the pattern in Cell.remove_agent where empty state is updated only after mutating _agents.

Testing

  • Added regression assertion in tests/discrete_space/test_discrete_space.py checking that cell.empty and cell.is_empty remain True when CellFullException is raised.
  • Ran pytest tests/discrete_space/test_discrete_space.py: 98 passed.
  • Ran ruff check and ruff format --check: all clean.

Copilot AI balanced review requested due to automatic review settings September 29, 2026 02:25

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 reached their quota limit.

@github-actions github-actions Bot added the testing Release notes label label Sep 29, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.6% [-1.2%, -0.1%] 🔵 +0.2% [-0.1%, +0.4%]
BoltzmannWealth large 🔵 -0.4% [-0.9%, +0.2%] 🔵 +0.4% [-0.9%, +1.8%]
Schelling small 🔵 +0.8% [+0.4%, +1.3%] 🔵 +0.7% [-0.1%, +1.5%]
Schelling large 🔵 +1.0% [+0.4%, +1.4%] 🔵 +4.8% [+2.5%, +6.8%]
WolfSheep small 🔵 -0.2% [-0.6%, +0.2%] 🔵 +3.4% [-0.5%, +7.5%]
WolfSheep large 🔵 -0.3% [-0.7%, +0.2%] 🔵 +1.3% [+0.4%, +2.2%]
SugarscapeG1mt small 🔵 -0.2% [-0.5%, +0.1%] 🔵 -2.0% [-3.7%, -0.3%]
SugarscapeG1mt large 🔵 +0.3% [-0.3%, +0.9%] 🔵 +0.5% [-1.4%, +2.8%]
BoidFlockers small 🔵 +0.9% [+0.3%, +1.4%] 🔵 +1.2% [+0.7%, +1.6%]
BoidFlockers large 🔵 -0.1% [-0.6%, +0.5%] 🔵 +0.7% [+0.4%, +1.0%]
MultiLevelAllianceModel small 🔵 +1.4% [+0.5%, +2.4%] 🔵 -0.4% [-0.7%, -0.1%]
MultiLevelAllianceModel large 🔵 -0.3% [-0.9%, +0.4%] 🔵 -0.1% [-0.4%, +0.1%]

@rishiiicreates
rishiiicreates force-pushed the fix/cell-add-agent-empty-flag branch from 2019820 to dcf4a90 Compare September 29, 2026 03:11
@rishiiicreates

Copy link
Copy Markdown
Contributor Author

hey, just noting: the examples CI job failure (test_model_steps[VirusAntibodyModel]) is a pre-existing issue on main — the latest main build_lint run shows the same failure. the VirusAntibodyModel in mesa-examples passes a sequence of length 3 to an agentset expecting 20 agents. not related to this PR's changes — all actual build, coverage, and pre-commit checks pass. rebasing on latest main now to trigger a re-run.

Cell.add_agent previously set self.empty = False before verifying capacity,
leaving empty out of sync with is_empty when CellFullException was raised.
Now self.empty is set only after the agent is successfully appended.

Fixes mesa#3883
@quaquel
quaquel merged commit 8b35f01 into mesa:main Oct 24, 2026
15 of 16 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
#3889)

Cell.add_agent previously set self.empty = False before verifying capacity,
leaving empty out of sync with is_empty when CellFullException was raised.
Now self.empty is set only after the agent is successfully appended.

Fixes #3883

(cherry picked from commit 8b35f01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cell.add_agent flips the empty flag even when the add is rejected

3 participants