Docs


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.

Last updated June 3, 2026

Go to the live page Open the Self-Driving Car demo

The Self-Driving Car demo gives the genetic algorithm a steering problem. A car drives around a closed race track 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 Flappy Bird and Snake under the same Live Agent demo. Use the tabs at the top of the page to switch between the three.

The problem

The track is a fixed 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 two things: how far the car travels forward along the track, and how many checkpoints it clears on the way around. Touching a wall ends the run, so a car that crashes early scores far less than one that keeps going. There is a gentle nudge toward the middle of the road so a fresh network has something to aim for before it has learned anything, and a small penalty for spinning in place that goes nowhere.

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. 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 tap the on-screen left and right buttons on a touch screen. The car keeps rolling forward on its own, so all you do is steer. 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.

Thanks for the feedback!