Self-Driving Car
A car learns to steer around a race track using five distance sensors. The genetic algorithm evolves a tiny neural network from crashing into walls to completing clean laps.
Go to the live page Open the Self-Driving Car demoThe Self-Driving Car demo gives the genetic algorithm a steering problem. A car drives around a closed race track of your choice at a steady speed, and the only thing it can choose is which way to turn. Early on the cars veer straight into the first wall. After a few generations they learn to read the track and string together clean laps.
It lives alongside the other live-agent games (Flappy Bird, Snake, Dino Runner, Breakout, Pong, Gem Collector and Frog Crossing) under the same Live Agent demo. Use the tabs at the top of the page to switch between them.
The Problem¶
Each track is a loop with an inner and an outer wall. The car always rolls forward, so it never has to decide when to go or how fast. It only decides how to steer: turn left, keep straight, or turn right.
To see where it is going, the car casts five distance rays fanned out around its heading, like a small lidar. Each ray reports how far the wall is in that direction. Those five readings, plus the car's speed, are the six inputs to a small neural network. The network has one hidden layer of eight neurons and three outputs, one per steering choice. The largest output wins, and that is the turn the car makes this step.
Fitness rewards staying alive, how far the car travels forward along the track, each checkpoint it clears and each full lap. Touching a wall ends the run with a penalty, and a car that stops making forward progress is retired, so a car that crashes early or circles in place scores far less than one that keeps going.
Choosing a Track¶
Pick a track from the row of cards above the canvas before you click Start. Each card shows the layout and how hard it is:
- Classic Loop (Medium). Five even bends that turn both ways. The original track.
- Speedway (Easy). A wide road with two long straights and a single kink. A good first run.
- Grand Prix (Medium). An uneven circuit that mixes fast sweeping bends with slower corners.
- Hairpin Pass (Hard). Three tight switchbacks, and the road narrows right where you turn.
- The Narrows (Hard). Gentle bends, but the road pinches down to little more than a car's width at each one.
The whole run uses the track you picked, so the population learns that layout. To try another track, click Reset, pick a new card and press Start again. Comparing tracks is a quick way to see how the same algorithm and settings cope with an easier or harder world. On the easier tracks the best car usually manages a clean lap within the first cycle or two. The two hard tracks often take several cycles, and now and then a run needs most of its cycles, or a bigger population, to get there.
The page remembers your choice in its address, so you can bookmark or share a link that opens straight onto a track.
How a Run Looks¶
Click Start. The current best network drives a lap live, with its sensor rays drawn on the track so you can see what it is reacting to. The rest of the generation drives the same track at the same time as faint cars, so you can see where most of them go off, and Still in play in the Training panel counts the cars still driving. The Show all cars switch next to the Start button turns them off. A panel in the corner shows the three steering scores each step and highlights the one the car picked. The chart on the right tracks the best score for each cycle.
Generation zero is rough. Most cars turn the wrong way and clip a wall within a second or two. Over the next several cycles the population shifts toward networks that ease off their turns before a corner and hold the racing line, and the cars start completing full laps.
Driving It Yourself¶
Switch the mode toggle from Agent to You to take the wheel. Steer with the left and right arrow keys, or drag the joystick over the game left or right. A quick push nudges the wheel and holding keeps turning. The joystick appears wherever you touch or click the game, so you can play with your thumb in whichever spot is comfortable, and it stays there for next time. When the game ends, tap the game (or press Space, or Play again) to start a new round straight away. The car keeps rolling forward on its own, so all you do is steer. There is no step limit in You mode, so you can keep lapping until you touch a wall. 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 cars 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 lap you watch. Higher values learn faster but make Pause and Reset feel less immediate.
- 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 driver.
The car records and shares just like the other demos. See Sharing and recording for how to capture a clip of a good lap.