Skip to content

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:

  • downloading and extracting the examples archive from the neworder releases page, or
  • pulling the docker image virgesmith/neworder:latest (more here

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

python examples/boids/run.py 2d
or

python examples/boids/run.py 3d

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


  1. in this context "vectorisation" merely means the avoidance of explicit loops in an interpreted language. The actual implementation may be compiled to assembly language, vectorised in the true (SIMD) sense, parallelised, optimised in other ways, or any combination thereof.