Skip to content

Optimize wolf-sheep movement performance #2565

Open
@EwoutH

Description

@EwoutH

The current movement implementation in the wolf-sheep model performs redundant checks when agents search their neighborhood for valid moves, after #2503.

cells_without_wolves = self.cell.neighborhood.select(
lambda cell: not any(isinstance(obj, Wolf) for obj in cell.agents)
)

cells_with_grass = cells_without_wolves.select(
lambda cell: any(
isinstance(obj, GrassPatch) and obj.fully_grown for obj in cell.agents
)
)

cells_with_sheep = self.cell.neighborhood.select(
lambda cell: any(isinstance(obj, Sheep) for obj in cell.agents)
)

This could be performance optimized. Two promising optimization approaches:

  1. Early exit strategy
    Instead of checking every cell in the neighborhood for agent types, we can break out of loops early once a valid cell is found. For example, when a sheep checks for wolves, it can immediately skip a cell once a wolf is detected rather than continuing to check other agents.

  2. PropertyLayer-based vectorization
    We can leverage Mesa's PropertyLayer to track wolf and grass positions as boolean layers. By having wolves and grass patches update their position in the property layer during movement, we can perform vectorized operations to find valid moves instead of iterating through agent lists. This would allow expressing the entire movement logic using fast numpy operations.

Trade-offs:

  • Early exits: Simpler implementation but more limited gains
  • PropertyLayer: More complex but potentially much faster for large grids, at cost of extra memory

Worth benchmarking both approaches against current implementation with different model parameters.

Somewhat related:

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PerformanceexampleChanges the examples or adds to them.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions