Boids flocking model¶
Example of how simple interaction rules can give rise to collective behaviours, based on the Netlogo model.
2-d simulation with two predators
3-d simulation with one predator
Examples Source Code
The code for all the examples can be obtained by either:
Implementation¶
Each entity travels at a fixed speed in a 2- or 3-dimensional constrained universe, and interacts with the other entities in four ways:
- separation: turns to avoid contact with other entities in close range, or
- evasion: avoids boids that are predators (which are faster than other boids), and
- alignment: turns towards the mean heading of nearby entities, and
- cohesion: turns towards the centre of gravity of nearby entities
- reversion (3d only): tend to return to the centre of the domain
(if a separation is required, the boid will not attempt to align or cohere)
The entities are stored in a pandas DataFrame and use neworder.Space to update positions. There are no explicit for loops in the model - all position and velocity calculations are "vectorised"1 for efficiency.
Run like so
orThe 2d version utilises a wrap-around domain and so does not require the reversion step.
Outputs¶
The output is an animation of the boid trajectories, as illustrated above.