Add continuous predator prey
Description
This PR introduces a new ContinuousSpace Predator-Prey model to the examples directory.
As discussed by the maintainers in the Mesa 4 goals (Discussion #2972), there is a need for a model with dynamic adding and removing of agents in continuous space to properly benchmark the performance of the new ContinuousSpaceAgent and the experimental ContinuousSpace.
This model provides exactly that testing ground. It includes:
ContinuousSpaceAgentimplementations for both Predator and Prey.- Dynamic runtime additions and removals via reproduction, hunting, and starvation.
- Proper usage of the new Mesa 4.0 agent-centric
get_neighbors_in_radiusAPI. - A headless run script (
run.py) ready to be hooked into the benchmark suite.
Related Issue / Discussion
Addresses the benchmarking gap mentioned by @quaquel in Discussion #2972.
Type of Change
- [x] ✨ New feature / enhancement
How Has This Been Tested?
- [x] Local headless testing via
python run.py, confirming populations fluctuate dynamically and agents are successfully added/removed from the continuous space and scheduler during runtime. - [x] Formatted using
ruff.
Thanks for the PR. We have certain standards to keep the quality of our examples high, please see the Mesa Examples self- and peer-review guidelines. If you have any questions let us know.
I beg to differ here
Good fit for Mesa 4.0: Using ContinuousSpaceAgent and get_neighbors_in_radius aligns with the new API. This fills a real gap in mesa-examples.
These are not new Mesa 4.0 APIs, see https://github.com/mesa/mesa/pull/2584
especially checking for a metadata.toml, consistent README format, and making sure the model follows the latest mesa-examples contribution template
I really don't know what you meant by metadata.toml
I would really appreciate if you'll actually dig into the code and I'm sure you'll enjoy it once familiar :) And try to post review comment in the format mentioned in #390
Hi @Ishwarpatra You can go through https://github.com/mesa/mesa/tree/main/mesa/examples/basic/boid_flockers to see ContinuousSpace and Agent in work
For eg. You need not to assign unique_id in your logic, This was also mentioned in #390
Uses current Mesa APIs (no deprecated schedulers, manual unique_id, dict portrayals, etc.
Hi @codebreaker32 and @B2prakash,
Thank you both so much for your patience and incredible guidance! The boid_flockers reference was the perfect blueprint, and Ved's suggestion to add a Solara visualization completely transformed this model.
I have just pushed the final round of updates to the PR:
Folder Restructure: Moved the model into a clean prey_predator directory.
Biological Realism: I noticed the base logic resulted in infinite exponential growth, so I added biological constraints: agents now have lifespans (max_age), Prey have a spatial carrying capacity (overcrowding checks), and Predators require a high energy threshold to reproduce.
Solara Dashboard: Added app.py which now renders both the continuous spatial map and a live Lotka-Volterra population line chart!
Documentation: I also just updated the README.md to follow the "mini-paper" structure discussed by Ewout in #417.
I am really proud of how this turned out. I'm ready for a final review whenever you have the time also open recommendation as their are many variables which can affect this!
Hi @Ishwarpatra, I’ve been following the Mesa 4.0 "Clean Slate" evolution and just did a deep-dive into your latest refactor. Moving toward the "mini-paper" README format is exactly the direction @EwoutH suggested in #417.
Technical Observations:
-
Agent Lifecycle: Excellent job removing the manual unique_id assignment. It now correctly relies on the base class, which is the standard for the newer agent-centric API.
-
Biological Constraints: The addition of max_age and energy thresholds for reproduction provides the necessary negative feedback loops to stabilize the population dynamics. It makes the model much more robust for benchmarking.
-
Solara Integration: Verified the app.py structure. The combination of the continuous spatial map and the live Lotka-Volterra chart significantly improves the "User Experience"
-
Code Architecture: Since you've transitioned away from the deprecated scheduler, you might consider using the self.agents.do("step") pattern in your model.step(). This is a clean way to handle batch agent execution in Mesa 4.0.
Really strong work. This is a solid blueprint for others looking to build Continuous Space models!
Hey,great work on this! I just have one small suggestion :predator_gain_from_food not exposed as a slider in app.py — it's a key parameter affecting dynamics but can't be tuned interactively.
Hi @EwoutH and @codebreaker32!
Just a quick follow-up: based on some excellent peer feedback , this PR adds the predator_gain_from_food slider to the Solara dashboard so users can interactively tweak that critical Lotka-Volterra parameter.
I also caught and fixed a minor local import bug in app.py and run.py while I was in there, so the visualization boots up perfectly out of the box now.
Let me know if this looks good to merge!
One last minor addition: I was reading through Discussion #417 regarding metadata standards, so I went ahead and added the YAML frontmatter block to the top of the README.md to help with future automated discoverability!