Skip to content

Add continuous predator prey#347

Open
Ishwarpatra wants to merge 13 commits intomesa:mainfrom
Ishwarpatra:add-continuous-predator-prey
Open

Add continuous predator prey#347
Ishwarpatra wants to merge 13 commits intomesa:mainfrom
Ishwarpatra:add-continuous-predator-prey

Conversation

@Ishwarpatra
Copy link

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:

  • ContinuousSpaceAgent implementations 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_radius API.
  • 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

  • ✨ New feature / enhancement

How Has This Been Tested?

  • 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.
  • Formatted using ruff.

@Ishwarpatra Ishwarpatra force-pushed the add-continuous-predator-prey branch 4 times, most recently from fc5778f to fdc814d Compare March 2, 2026 12:59
@codebreaker32
Copy link
Collaborator

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.

Copy link

@B2prakash B2prakash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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.
  2. Dynamic agent management: The reproduction, hunting, and starvation mechanics properly test runtime agent addition/removal — which is exactly what the benchmarking discussion (#2972) called for.
  3. Suggestion: The maintainers have posted self- and peer-review guidelines (#390). It would be helpful to run through that checklist — especially checking for a metadata.toml, consistent README format, and making sure the model follows the latest mesa-examples contribution template.
  4. Minor: A headless run.py is included which is good for benchmarking. Consider also adding an app.py with SolaraViz visualization so users can see the model visually.

@codebreaker32
Copy link
Collaborator

codebreaker32 commented Mar 19, 2026

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 mesa/mesa#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

@codebreaker32
Copy link
Collaborator

codebreaker32 commented Mar 19, 2026

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.

@Ishwarpatra Ishwarpatra force-pushed the add-continuous-predator-prey branch from 6161bb5 to 61f4cc0 Compare March 20, 2026 11:06
@Ishwarpatra Ishwarpatra force-pushed the add-continuous-predator-prey branch from 76fa7a5 to f2ec8d5 Compare March 20, 2026 11:10
@Ishwarpatra
Copy link
Author

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!

@ApurvaPatil2401
Copy link

ApurvaPatil2401 commented Mar 21, 2026

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:

  1. 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.

  2. 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.

  3. 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"

  4. 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!

Copy link

@Vanya-kapoor Vanya-kapoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +76 to +82
self.datacollector.collect(
self
) # collect the data for the current step of the simulation
# Mesa 4.0 native agent activation (replaces the old scheduler)
self.agents.shuffle_do(
"step"
) # it step all agents during the scheduling process
Copy link
Collaborator

@codebreaker32 codebreaker32 Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are collecting data before the step logic will be encountered. In this way, wouldn't you always miss the last step?

new_pos = (new_x, new_y)

# to make sure it doesn't go off the map; use the new helper
if self.model.space.torus:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need not to do it, its already handled via position.setter

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants