mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Our “canonical” implementation of flocking is wrong

Open EwoutH opened this issue 9 months ago • 6 comments

We claim to have canonical implementations, but in the case of flocking I believe our implementation might be wrong.

  • Our implementation move towards a “leader”
  • The canonical (or at least original) model move toward “the average position of local flockmates”

https://www.red3d.com/cwr/boids/

Image

A thirds options is to dynamically create flocks using meta-agents for this.

A fourth is to use distance weighted decay.

Even cooler would be comparing behaviour between the methods.

EwoutH avatar Feb 11 '26 22:02 EwoutH

I'd like to fix the boid cohesion behavior to match Reynolds' original model changing it from "follow a leader" to "move toward the average position of local flockmates". Can I work on this?

Krishsharma179 avatar Feb 12 '26 10:02 Krishsharma179

Hi @EwoutH! 👋 I've been studying this issue closely.

I see PR #3288 addresses the basic cohesion fix. I'd love to go further and tackle the extended ideas you mentioned:

  • Option 3: Implement dynamic flock formation using meta-agents
  • Option 4: Distance-weighted decay for cohesion (closer neighbours = stronger pull)
  • Option 5: A side-by-side behavioural comparison of all methods

I've already set up the mesa repo locally, read through 'agents.py', and studied Reynolds (1986) at red3d.com/cwr/boids. I also have a working interactive demo comparing the wrong vs canonical behaviour.

I would love to contribute. Thanks! 🙂

Harshini2411 avatar Feb 24 '26 02:02 Harshini2411

I was wrong here.

Mesa-examples is open to alternative, more experimental implementations!

EwoutH avatar Feb 24 '26 06:02 EwoutH

Hi @EwoutH! 👋 I've been looking at this issue and noticed you mentioned Option 4 (distance-weighted decay) as an interesting extension. I also read through #3288 and understood why it was closed. I've implemented it locally with all 12 tests passing on Python 3.13. Here's what I added:

A decay parameter on each Boid (default 2.0 using inverse square law) 1.Closer neighbours have stronger influence than far ones, producing more realistic flocking behaviour 2.decay=0.0 available for comparison against original equal-weight Reynolds (1986) behaviour 3.Fixed separation to correctly average only close neighbours 4.Added safe normalization to prevent division by zero(to escape from math errors and producing NAN values)

Would you be open to a PR for this? Happy to adjust anything based on your feedback before submitting.

Harshini2411 avatar Feb 25 '26 03:02 Harshini2411

In Mesa we would like to keep a simple, canonical example. Mesa-examples is open to new and more explorative implementations. It might be quite interesting to have a single model with different flocking behavior options there.

EwoutH avatar Feb 25 '26 11:02 EwoutH

Thanks for the clarification @EwoutH! I'll submit the PR to mesa-examples. I've already implemented it as a single model with a decay parameter as decay=2.0 for distance-weighted behaviour and decay=0.0 for canonical Reynolds behaviour, so users can compare both with just one parameter change. I've already forked mesa-examples and will submit a PR there. Does that sound good?

Harshini2411 avatar Feb 25 '26 20:02 Harshini2411