Skip to content

abmind-community/Wolf-Sheep-Model-Comparison

Repository files navigation

Wolf-Sheep Model Comparison

中文 README

This repository compares the classic Wolf-Sheep predator–prey model across three implementations—ABSESpy, a stripped Mesa version, and a pure Python baseline—highlighting runtime performance and code size.

Project Layout

wolf_sheep/
├── absespy/       # ABSESpy implementation & docs
├── pure_python/   # Pure Python implementation & docs
├── mesa_impl/     # Mesa-inspired lightweight implementation
├── benchmark.py   # Comparison script
└── __init__.py
tests/
└── test_pure_python_model.py

Implementations

  • wolf_sheep/absespy: demonstrates ABSESpy’s scheduling, batch operations, and data collection.
  • wolf_sheep/pure_python: uses only the standard library—grid, scheduling, and metrics are handwritten.
  • wolf_sheep/mesa_impl: concise reimplementation based on Mesa’s example (comments/docstrings removed for line-count parity).

Each implementation exposes the same metrics (n_sheep, n_wolves, population_ratio, grass_coverage) so benchmarks align.

Setup

uv sync --all-extras && uv run pytest

Optional extras:

  • ABSESpy: uv add abses matplotlib
  • Mesa: uv add mesa

Run Individually

uv run python - <<'PY'
from wolf_sheep.pure_python import ModelParams, Simulation
history = Simulation(ModelParams(seed=42, max_steps=200)).run()
print(history[-1])
PY
uv run python - <<'PY'
from wolf_sheep.absespy import WolfSheepModel
cfg = {
    "model": {"shape": [30, 30], "n_sheep": 50, "n_wolves": 10, "rep_rate": 0.01},
    "time": {"end": 200},
}
model = WolfSheepModel(parameters=cfg, seed=42)
model.run_model(steps=200)
print(model.n_sheep, model.n_wolves, model.population_ratio, model.grass_coverage)
PY
uv run python - <<'PY'
from wolf_sheep import mesa_run_steps
print(mesa_run_steps(steps=200, seed=42).tail(1))
PY

Benchmark Script

uv run python wolf_sheep/benchmark.py --steps 200 --repeats 3
  • Avg Time (s): mean runtime across repeats.
  • LOC raw/log: raw lines vs. logical lines (comments & docstrings removed).

Example output:

Framework Avg Time (s) LOC raw/log
pure_python 0.0437 341 / 304
mesa 0.0441 200 / 200
absespy 2.4238 237 / 99

Metrics

  • n_sheep, n_wolves: population counts.
  • population_ratio: sheep share of total population.
  • grass_coverage: proportion of cells with regrown grass.

License

MIT License — see LICENSE.

About

Implementing the Wolf-Sheep-Grass model using different frameworks

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

 
 
 

Contributors