Fish School vs Shark
Welcome to the tank! You are the yellow fish. Steer with the arrow keys (or press on the water) to gobble green plankton and stay away from the shark. How long can you last?
How long the fish survive, generation by generation
Percent (1 generation = 20 pretend seconds in the tank)
Checkpoints — the champion fish of each generation. Tap one to watch a school of 40 copies of it face today's tank.
How it works
Six eyes
Each fish has six eyes all around its body. Every eye tells the brain how close the nearest plankton, shark, wall and fish friend is in its direction. Plus: how full its tummy is.
A tiny brain
Those 25 numbers flow through 8 thinking neurons to 2 answers: turn left or right, and swim fast or slow. Fast swimming makes you hungry quicker!
Score
Each fish scores points for every moment it stays alive, a little for each plankton snack, and a bonus for still swimming at the end.
Babies
The top fish are copied unchanged. The rest of the new school are babies of the top quarter, sometimes mixing two parents, always with a few random changes.
The shark
The shark is not learning. It just chases the nearest fish. But when several fish crowd together it can lose track and miss its bite.
What just happened?
- In generation 1 every fish had a random brain. Most swam in circles, bumped the glass, starved or got eaten.
- After each generation, the fish that lived longest and ate most became the parents. Their babies got copies of their brains with small random changes.
- Nobody told any fish "plankton is food" or "sharks are scary". Turning toward green dots and away from the shark simply survived better, so those brains spread.
- Fish that swim near friends are harder to catch: the shark gets confused by a crowd. So school-like swimming can appear on its own, without a rule saying "stay together".
- When you made the shark faster, added a second shark or clouded the water, survival dipped. The school kept evolving and adapted to the new tank.
For grown-ups: what's really going on
This is neuroevolution: a population of 40 small fixed-topology networks [25 → 8 → 2] (tanh) whose 226 weights are optimised by a genetic algorithm with no gradients, against a hand-written pursuit predator. Group behaviour is not programmed; any schooling is an emergent side effect of individual selection.
- Senses (25 inputs): six 64° sensor wedges centred at 0°, ±55°, ±115° and 180° from the heading. For each wedge and each of four object classes (plankton, shark, wall, other fish) the input is
1 − d/Rfor the nearest object of that class in the wedge (0 if none within range R = 130 px; 45 px in murky water). Walls use one exact ray-cast per wedge; objects use the nearest-angle wedge. The 25th input is energy, mapped to [−1, 1]. - Outputs (2): turn rate (tanh, up to 0.14 rad/step) and thrust in [0, 1], mapped to a target speed of 0.35–2.1 px/step with first-order lag.
- Energy: drains by 0.0009 + 0.0008·thrust² per step; each plankton adds 0.3 (capped at 1). Zero energy = starved. Plankton respawns around three slowly drifting patches, so food is clumped.
- Predator: pursuit with turn rate 0.05 rad/step at 1.6 px/step (fast shark: 2.25 px/step, 0.07 rad/step). Targets the nearest fish (fish behind it count as 1.8× further), re-targets every 40 steps. After a catch it is slowed for 20 steps. Confusion effect: if k other fish are within 30 px of the bitten fish the bite succeeds with probability
1/(1 + 0.3k); crowds around its target also make it switch targets at random. This is a simplified version of the predator-confusion hypothesis studied in real fish shoals. - Fitness:
t/T + 0.02·snacks + 0.5·[alive at T], T = 1200 steps. Every fish in a generation shares one tank, so fitness is noisy and depends on the others (a co-evolutionary, frequency-dependent setting). - GA: top 10% elites copied; the rest are children of the top 25% (rank-biased pick), 30% uniform crossover, Gaussian mutation on 25% of genes with step size drawn from 0.25–2× the slider value. Seeded RNG (mulberry32) so runs repeat exactly.
- Metric on the chart: mean fraction of the generation each fish survived, and the share still alive at the end.
- Measured in Node (seeded, 2 seeds, 50 generations): random brains survive about 67–69% of a generation on average, eat 2–5 snacks each and 19–28% are alive at the end. After 25 generations: 79–85% survival, 11–24 snacks, 51–65% alive at the end, and the average number of other fish within 40 px roughly doubles (1.1 → 2.1). Re-tested on fresh tanks, the evolved school beats its own random starting brains (about 80% vs 70% survival, 13–27 vs 3 snacks). Switching a challenge on after generation 25 drops survival to about 69% (two sharks), 70% (murky) or 77% (faster shark); the school then claws part of it back. Single generations are noisy (±5 points) because all 40 fish share one tank.
- Related work: evolved prey/predator swarms (e.g. Olson et al. 2013 on predator confusion and the evolution of swarming), boids (Reynolds 1987) as the hand-written alternative, and NEAT, which also evolves the network shape.