Neural networks · Evolution

The car that learns to drive

Press Play! Lots of baby cars will try to drive. None of them know how… yet.

The road

Best car's brain

Finish line
0%

Best distance each round

Checkpoints — the champion brain from each round. Tap one to watch it drive.

Each finished round saves its champion here.

How it works

Eyes

Each car has invisible feelers (the yellow lines). They turn darker when they touch a wall or another car.

A tiny brain

The feelers send numbers into a pretend brain. It decides: gas, brake, left or right?

Random at first

In round 1 every brain is random, so cars wobble and bonk into things.

Pick the best

The car that got the furthest wins. Its brain is kept.

Copy with tiny changes

The winner's brain is copied into all new babies, each with a little random tweak.

Again and again

Good tweaks survive, bad ones don't. After a few rounds, a car can drive!

Or copy a person

When you drive, the car writes down what its eyes saw and which keys you pressed. Then it practises until it does the same.

What just happened?

For grown-ups: what's really going on

This is neuroevolution: a neural network trained by a genetic algorithm instead of backpropagation. Nobody labels the right action; the only signal is how far a car got.

  • Sensors: N rays spread over 90° are ray-cast against the road borders and nearby traffic polygons. Input i = 1 − d/L (closer = bigger, 0 if nothing is hit).
  • Network: a fully-connected feed-forward net [rays → hidden → 4] with a step activation: a neuron outputs 1 if Σ wᵢxᵢ > b, else 0. The 4 outputs drive forward / left / right / reverse.
  • Fitness: distance gained relative to the slowly moving traffic. Cars die on collision or after ~4 seconds without progress.
  • Selection & mutation: elitism with a single parent. Car #0 is an exact copy of the champion (so the record never gets worse, because the simulation is deterministic); every other car is w ← lerp(w, U(−1,1), α) for every weight and bias, where α is the “how different” slider.
  • Copy my driving: this is behaviour cloning (imitation learning), which is supervised learning. Each recorded frame is a pair (sensor readings + speed → the 4 keys you held). A sigmoid network [rays+1 → 8 → 4] is trained by backpropagation on binary cross-entropy. The copycat then seeds the genetic algorithm, much like an LLM is first trained to imitate text and then improved with rewards (RLHF).
  • Curvy roads: the road centre is a sum of sines; borders are short segments, so the same ray-casting works on bends.
  • Generalisation: a champion evolved on one road often fails on a new one (overfitting!). The “trained driver” was evolved in Node.js across several random roads, so it copes better, though still not perfectly.

Inspired by Radu Mariescu-Istodor's free course Self-driving car with JavaScript (freeCodeCamp) and its source code.