Conversation
|
Performance benchmarks:
|
|
Performance benchmarks:
|
| # Among safe cells, pick those with grown grass | ||
| if has_grass: | ||
| cells_with_grass.append(cell) | ||
|
|
There was a problem hiding this comment.
I have been looking at this code myself before as well and agree with your improvement. I am in principle fine with merging this. However, I am wondering whether a further improvement might not be possible: what about implementing grass as a property layer rather than as agent?
There was a problem hiding this comment.
Thank you for the feedback! Honestly it also feels more conceptually correct to me, since grass is more a passive property of a cell rather than an active agent. Moving it to a PropertyLayer would eliminate the need to iterate over cell.agents entirely when checking for grass.
I've been looking into implementing this and i have one question: should the grass regrowth still be handled via model.schedule_event(), or it'll be better with a different approach?
There was a problem hiding this comment.
I would still ike to use scheduling, but if we use a property layer, there is no natural place for the scheduling. It would have to be done inside the sheep when its eating a patch. From an encapsulation point of view, this seems a bit strange.
There was a problem hiding this comment.
A potential solution could be to keep the scheduling part inside the GrassPatch agent and move the state to the model into a fully_grown PropertyLayer.
So GrassPatch becomes a pure behavior agent with no state of his own. That's what i'm currently implementing.
Would it make sense to merge the current early exit optimization as-is and open a separate issue to track this work?
* Early exit for sheeps * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Pietro Mondini <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Early exit for sheeps * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Pietro Mondini <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Approval Link
#2565
Summary
Optimizes the movement logic in the wolf-sheep example by replacing redundant CellCollection.select() passes with a single manual loop using an early exit strategy.
Motive
The previous implementation performed two separate .select() passes over the neighborhood on every agent step — one to filter wolf-free cells, and one to find grass. Each pass iterated over all agents in all neighboring cells. In dense simulations with many agents, this becomes a significant bottleneck.
Implementation
Replaced the two CellCollection.select() calls in Sheep.move() with a single manual loop over the neighborhood.
Added has_wolf / has_grass flags in order to break immediately upon finding a wolf, skipping remaining agents in that cell, and check both wolf and grass in the same loop.