Dino Runner
A dino learns to play an endless runner, jumping cacti and ducking flying obstacles. The genetic algorithm evolves a tiny neural network from tripping at the first hurdle to long clean runs.
Go to the live page Open the Dino Runner demoThe Dino Runner demo is a take on the offline dinosaur game. A dino runs along the ground while obstacles scroll toward it, and the genetic algorithm evolves a small neural network to keep it alive. Early on the dino trips at the very first hurdle. After a few generations it learns to read each obstacle and react correctly, and the runs get long.
It lives alongside Flappy Bird, Snake, and the Self-Driving Car under the same Live Agent demo. Use the tabs at the top of the page to switch between them.
The problem¶
Two kinds of obstacle come at the dino, and they need opposite reactions:
- Cactus on the ground. The dino has to jump it. Ducking still hits it.
- Flying obstacle at body height. The dino has to duck under it. Jumping rises straight into it, and running hits it standing.
So there is a real choice to make, not just one button to press. The network reads seven inputs: how far ahead the next obstacle is, whether it is on the ground or in the air, how tall it is, the dino's own height and rise or fall, and the current speed. One hidden layer of eight neurons feeds three outputs: run, jump, or duck. The largest output wins.
Fitness rewards how long the dino survives plus a bonus for each obstacle it clears. The course speeds up the longer a run lasts, so later obstacles arrive with less time to react. A network that reacts a touch too early or too late clips the obstacle and the run ends.
How a run looks¶
Click Start. The current best network plays a run live, and a panel in the corner shows the three action scores each step with the chosen one highlighted. The chart on the right tracks the best score for each cycle.
Generation zero is clumsy: the dino jumps into birds, runs into cacti, or ducks when it should jump. Within a few generations the population learns to tell the two obstacles apart and time each reaction, and the runs stretch out as the speed climbs.
Playing it yourself¶
Switch the mode toggle from Agent to You to take over. Press Space or the up arrow to jump, and hold the down arrow to duck. On a touch screen, use the on-screen up and down buttons. Switch back to Agent at any time and the evolved network picks up from where you left off.
Settings worth trying¶
Open Settings to tune the genetic algorithm for the next run:
- Population size. More candidates per generation means more variety to select from, at the cost of a slower cycle.
- Generations per cycle. How much evolving happens between each live run you watch.
- Mutation rate. How much the networks are randomly perturbed each generation. A little keeps the search exploring; too much stops it from settling on a good policy.
The dino records and shares like the other demos. See Sharing and recording for how to capture a clip of a good run.