Apps

Schooling fish

How a school emerges from three simple rules, with no leader

Fish 0 Step 0
Polarisation0.00
last 900 steps0 = random, 1 = aligned
New here?

About the model

What you are looking at

Each small shape is a fish. Every fish follows the same three steering rules, using only the fish it can sense nearby. There is no leader and no fish knows the shape of the school. Yet together they form a moving school that turns, splits and reforms. This is an agent-based model: complex group behaviour emerges from simple individual rules. The approach was introduced by Craig Reynolds (1987) to animate bird flocks ("boids") and is now widely used in the study of collective animal behaviour.

The three rules

Separation: steer away from neighbours that are inside your personal space. Closer neighbours count more when choosing which way to go.
Alignment: turn towards the average heading of your neighbours.
Cohesion: steer towards the average position (centre) of your neighbours.

A neighbour is any fish within the perception radius. Each rule gives a force whose direction and size depend on where the neighbours are at that moment. Separation is stronger the closer an intruder is, alignment is stronger the more out of step the fish is, and cohesion is stronger the further the fish is from the centre of its neighbours.

The forces are multiplied by weights (the sliders) and added to give the overall push, which changes the fish's velocity. The weights are fixed traits shared by every fish: how much it cares about each rule. Changing them is how you move between different kinds of collective behaviour.

Show the maths
Separation: F_sep = Σ_{j : r_ij < R_sep} (1 − r_ij/R_sep) · (x_i − x_j)/r_ij Alignment: F_ali = ( v̄ − v_i ) / v_0 v̄ = mean velocity of neighbours Cohesion: F_coh = ( x̄ − x_i ) / R x̄ = mean position of neighbours push_i = g · ( w_sep F_sep + w_ali F_ali + w_coh F_coh ) a_i = limit(push_i, F_max) + k (v_0 − |v_i|) v_i/|v_i| v_i ← v_i + a_i x_i ← x_i + v_i

Each separation term is a unit vector pointing directly away from neighbour j, scaled from 0 at the edge of personal space to 1 at contact. The three forces are dimensionless and of similar size (roughly 0 to 1), so the weights compare like with like. The gain g = 0.06 converts them into an acceleration. The cap Fmax = 0.1 limits how much a fish can change its velocity in one step, which acts like inertia and is why turns are gradual. The last term gently returns each fish to its cruising speed v0 = 3 units per step (k = 0.05). One body length is 8 units.

Distances are measured across the edges of the tank, which wraps around like a torus: a fish leaving on the right reappears on the left and can see fish on the other side.

This is a "social force" form of the boids rules. In Reynolds' original form, each rule steers towards a desired velocity at full strength, so arrow lengths mostly show the cap rather than how strongly a rule is triggered. The force form used here makes the geometry visible, and produces the same kinds of collective behaviour.

One time step

At each step, every fish first works out its steering from the same snapshot of positions and velocities, and only then do all fish move together. Updating fish one at a time would let later fish react to where earlier fish have already moved, a subtle but common bug in simple implementations. Use the step button next to Start (or press → while paused) to advance one step at a time.

Predators and startles

A predator picks a target within range, preferring fish that are close and have few neighbours. It reconsiders its choice every 45 steps and aims at where the target is heading, not where it is now. Predators are faster than cruising fish. After a catch, the predator pauses briefly to "handle" its prey, and the caught fish is replaced at a random spot away from the predators.

A fish within about 14 body lengths of a predator, or of a spot where you click, becomes alarmed. Its alarm level u rises from 0 to 1 as the threat gets closer. Its flocking forces are scaled down by (1 − 0.8u), and an escape force of up to three times the normal maximum steers it directly away. It can also briefly swim faster than usual.

Measuring order: polarisation

The plot shows the polarisation, a standard measure of how aligned a group is. It is the length of the average heading vector:

P = | (1/N) Σ_i v_i / |v_i| |

P = 1 when every fish swims in the same direction and is close to 0 when headings are random. With a finite number of fish, random headings still give a small value (about 0.08 for 120 fish), so values below about 0.15 mean "no shared direction". Physicists use the same quantity as the order parameter in the Vicsek model of collective motion, and it has been measured directly in real schools of fish.

What this model leaves out

The model is deliberately minimal, so it is worth being clear about its limits:

References