Skip to content

Fix #3597: safe Sheep.feed() when grass=False, add regression test - #3627

Merged
quaquel merged 2 commits into
mesa:mainfrom
DipayanDasgupta:fix/3597-sheep-feed-grass-false
Mar 24, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
DipayanDasgupta:fix/3597-sheep-feed-grass-false

Conversation

@DipayanDasgupta

Copy link
Copy Markdown
Contributor

Summary

Closes #3597 (partial — agent logic fix)

Sheep.feed() passed a bare generator to next() with no default.
When grass=False, no GrassPatch agents are created, so the generator
is always empty and next() raises StopIteration on step 1.

# Before — crashes when grass=False:
grass_patch = next(
    obj for obj in self.cell.agents if isinstance(obj, GrassPatch)
)
if grass_patch.fully_grown:

# After — safe default:
grass_patch = next(
    (obj for obj in self.cell.agents if isinstance(obj, GrassPatch)), None
)
if grass_patch is not None and grass_patch.fully_grown:

Zero impact on the grass=True path.

Tests added

test_wolf_sheep_grass_disabled — runs 10 steps with grass=False,
asserts no crash, confirms Wolves/Sheep in dataframe and Grass absent.


GSoC contributor checklist

Context & motivation

I was exploring the WolfSheep model to study predator-prey dynamics without the grass mechanic — a common variant in ABM literature. The model crashed immediately on the first step. Tracing the stack I found next() with no default — a silent landmine that only explodes on non-default execution paths.

What I learned

next(gen) with no default is dangerous on any code path that isn't the happy path — always pass a default. I also learned that Mesa's DataCollector is intentionally dynamic: reporters are only registered conditionally, so downstream code can never assume every key exists.

Learning repo

🔗 My learning repo: https://github.com/DipayanDasgupta/GSoC-learning-space
🔗 Relevant model: https://github.com/DipayanDasgupta/GSoC-learning-space/tree/main/models/wolf_sheep_investigation

Readiness checks

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.2% [-2.6%, -1.8%] 🔵 -0.7% [-1.0%, -0.4%]
BoltzmannWealth large 🔵 -2.7% [-2.9%, -2.5%] 🔵 -1.2% [-1.6%, -0.7%]
Schelling small 🔵 -1.4% [-1.8%, -1.0%] 🔵 -0.2% [-1.0%, +0.5%]
Schelling large 🔵 -0.2% [-0.8%, +0.3%] 🔵 +1.8% [+0.7%, +2.8%]
WolfSheep small 🔵 -1.0% [-1.4%, -0.6%] 🔵 -1.6% [-5.6%, +2.6%]
WolfSheep large 🔵 -0.3% [-1.0%, +0.4%] 🔵 -0.9% [-1.6%, -0.2%]
SugarscapeG1mt small 🔵 -0.6% [-0.9%, -0.3%] 🔵 +1.5% [+0.1%, +3.0%]
SugarscapeG1mt large 🔵 -1.1% [-1.6%, -0.7%] 🔵 -1.7% [-3.9%, +0.7%]
BoidFlockers small 🔵 -2.3% [-2.7%, -1.9%] 🔵 -2.4% [-2.9%, -1.8%]
BoidFlockers large 🔵 -1.5% [-2.0%, -0.9%] 🔵 -2.9% [-3.2%, -2.6%]

@quaquel quaquel added bug Release notes label example Changes the examples or adds to them. labels Mar 24, 2026
@quaquel
quaquel merged commit 101b49c into mesa:main Mar 24, 2026
12 of 13 checks passed
souro26 pushed a commit to souro26/mesa that referenced this pull request Mar 28, 2026
codebreaker32 pushed a commit to codebreaker32/mesa that referenced this pull request Apr 1, 2026
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label example Changes the examples or adds to them.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wolf sheep example crashes when i try to run it with grass regrowth enabled=False

2 participants