Sheepdog Round-Up
Ten sheep are grazing and they run away from the dog. You are the dog! Move your pointer over the field (or use the arrow keys) and push the flock through the gate into the pen on the right.
How the dogs did, generation by generation
Score out of 100 (average of the best half of the dogs)
Checkpoints — the champion dog saved along the way (its path on a test field). Tap one to watch it herd today's field.
How it works
Flock rules
Every sheep looks at its neighbours: move toward their middle, match their way, keep a little space. If the dog is inside the scare circle, run away from it.
Ten senses
The dog feels where the flock's middle is, where the sheep furthest out is, which way the gate is, how spread out the flock is, and where it is in the field. Everything is measured as "toward the gate" and "to the side".
A tiny brain
The ten numbers go through ten thinking neurons. Two answers come out: how much to run toward the gate, and how much to run sideways.
Score each dog
Thirty dogs each try the same fields. Points for every sheep in the pen, a bonus for being quick, and some credit for moving the flock closer to the gate.
Copy with changes
The best dogs stay; the rest are replaced by babies of good dogs, each with small random changes to its brain. Repeat, generation after generation.
What just happened?
- The sheep never learn anything. Each one follows three simple flocking rules: stay near friends, go the same way as them, don't bump into them. Plus one more: run from the dog. Those few rules make a whole flock move like one animal.
- You probably found out that chasing straight at the sheep scatters them. Good herding means getting behind the flock and fetching the sheep that wander off.
- The first robot dogs had random brains. They ran in circles, into corners, or nowhere at all, and penned zero sheep.
- Each generation, the dogs that got the flock closer to the gate (and penned more, faster) became the parents. Their babies got slightly changed copies of their brains. Nobody ever told them "go behind the flock".
- When you switched on a challenge, the old tricks worked less well. The score dropped, and then the dogs that coped best had the babies, so the population adapted.
For grown-ups: what's really going on
Two classic ideas meet here: boids (Reynolds, 1987) for the flock, and neuroevolution (a genetic algorithm over the weights of a fixed-topology neural network) for the dog. There are no gradients and no labelled examples; the only signal is a fitness number per dog.
- Sheep (boids): for each free sheep, neighbours within 70 px give cohesion (acceleration 0.0035 × (neighbour centroid − position)), alignment (0.04 × (mean neighbour velocity − velocity)) and separation (a linear push inside 15 px). Within the flight zone (95 px) the dog adds a repulsion growing linearly as the dog gets closer. A small random heading drift (grazing), fence repulsion near the field edge, velocity damping 0.9 and a speed cap of 2.1 px/step complete the model. The pen fence is solid except for the gate.
- Dog: top speed 3 px/step with first-order velocity smoothing (it cannot turn on the spot) and it may not enter the pen.
- Network:
[10 → 10 → 2], tanh, 132 weights. Inputs are expressed in a task frame whose x-axis points from the flock centroid to a target point just outside the gate (switching to just inside once the flock reaches the gate): flock centroid relative to the dog (along, across), the outermost sheep relative to the dog (along, across), the target direction in screen coordinates (2), centroid-to-target distance, flock spread (max distance from centroid) and the dog's own position (2). The two outputs are a velocity command in that same frame, rotated back to screen coordinates. Using an egocentric task frame makes the policy largely rotation-invariant, which is why a small net can learn it. - Fitness: 100 × (fraction penned) + 40 × (mean time bonus of penned sheep, 1 − t/T) + 40 × (1 − mean free-sheep distance to the gate ÷ the starting distance). The last term is reward shaping: without it, random dogs all score 0 and selection has nothing to work with. The kid-facing score is 50 points per penned sheep share plus a 50-point share for speed.
- Genetic algorithm: population 30, each dog tested on the same 1 to 3 random fields per generation (common random numbers, so comparisons are fair). The top 8% survive unchanged (elitism); the rest are children of the top quarter (rank-biased choice, 30% uniform crossover) with Gaussian mutation on 30% of genes, the step size spread from 0.25× to 2× the slider value.
- Measured in Node (population 30, two fields per dog, three seeds): generations 1 to 3 score about 0 to 1 out of 100 and pen no sheep; by generation 35 the best half scores 77 to 85 (about 0.5 s of computing per generation on a laptop). A dog that stands still scores 0. A hand-written "collect the straggler, otherwise drive from behind" controller (the classic rule from shepherding research, e.g. Strömbom et al., 2014) scores about 90 and pens every sheep on the default field, so evolution gets close to a strong engineered rule. Wind and the bigger flock with a narrow gate cause a dip that recovers within 20 to 30 generations; stubborn sheep are the hardest, and scores stay noisier.
- Why it matters: herding is a real testbed for swarm robotics and multi-agent control (drones guiding animals, robots moving crowds of other robots). The same neuroevolution recipe, scaled up, is used for game agents and robot controllers where gradients are unavailable.