You drag one loop and it becomes a road. Fifty cars line up, each with a small brain full of random numbers, and drive into the walls. The ones that got furthest are kept and bred, and within a few generations one laps cleanly. Then you race the driver you trained, and the link you share carries both the track and its brain.
Cars per generation
50
First clean lap
Median generation 6
Size
About 58 kB
TypeScript · neuroevolution from scratch · 2D canvas
A car never sees the track. It gets seven distances to the wall, along lines fanned from 75 degrees left to 75 degrees right and capped at 300 units, plus its own speed: eight numbers between 0 and 1. These go into a feed-forward network with 6 hidden neurons and 2 outputs, steering and pedal, joined by 68 weights. Each neuron sums its weighted inputs, adds a bias and squashes the total with tanh. It runs sixty times a second.
02 · How it works
How a generation is bred
After each generation the fifty cars are ranked by the furthest point they reached. The best four go through unchanged, so the best can never get worse. Every other seat is filled by tournament selection: three picked at random from the better half, the best of them kept, and half the time a second parent mixed in weight by weight. About a fifth of the child's weights are then nudged. When progress stalls the nudges grow, and after three stuck generations six newcomers join.
03 · How it works
Why I chose evolution over gradients
I used neuroevolution because it is honest and visible. There is no gradient, no reward during a drive and no training data. Every car on screen is a different network, and the only thing separating a good one from a bad one is how far it got. It learns fast because the problem is small on purpose: a network that merely turns away from the nearer wall already survives the first bend, and every improvement after that is a small step.
04 · How it works
From a finger stroke to a road
The hard part was making any scribble a drivable track. The stroke is resampled and smoothed, an overshoot is trimmed, and if the loop crosses itself the longer loop is kept. A relaxation pass then eases open any corner tighter than 42 units and pushes apart stretches closer than 82 units. The result becomes control points 28 units apart and a closed cubic B-spline. Sensor rays walk a uniform grid of 48-unit cells, testing only the walls in the cells they pass.
05 · How it works
A link that carries the driver
The engine steps at a fixed 1/60 of a second and takes every random number from one seeded generator, so the same seed gives the same evolution, weight for weight. That is what lets a shared rookie drive the same race for anyone. The track is about a hundred whole-number control points and the driver is 68 numbers, so both fit after the # of a link, in about 750 characters, and never reach a server.
06 · What it teaches
The idea underneath
Rookie shows that nobody has to program driving. Only the scoring was written: how far round the road a car got. Selection and small random changes do the rest. It is also honest about the limit: evolution works here because the network has 68 weights, and networks with millions of weights need gradients.
How it works, in one picture
How Rookie works
Your track → Fifty cars → Sense and steer → Breeding → A driver. Open the picture to read it at full size.
1600 × 1000 · JPEG
Film · 63 seconds
I taught fifty cars to drive
A one-minute film of Rookie, a page by Pratyush Shivam where you draw any race track with your finger and fifty AI cars learn to drive it from nothing, shot on the live page. Generation 1 is a pile-up; in this run the first clean lap comes in generation 5. Diagrams drawn from the real numbers in the code show what a car sees (seven distance sensors and its own speed), its brain (a neural network of 8 inputs, 6 hidden neurons and 2 outputs, 68 weights in all), and how each generation is ranked, crossed over and mutated. No training data and no machine-learning library: everything is written from scratch and runs on your own device.
How many generations does it take to learn a track?
On 200 random drawings, every one reached a clean lap within 40 generations. The median was generation 6, nine in ten made it by generation 12, and the slowest took 21. At the default speed that is usually 20 to 40 seconds.
02
Can the trained car drive a different track?
Not well. A rookie has learned one road by trial and error. On a new track it has to learn again, though usually more quickly than starting from a brain full of random numbers.
03
Does Rookie use a machine learning library?
No. The network, the car physics and the genetic algorithm are written from scratch, with no dependencies at run time. There is no server and no model download, and the learning happens live while you watch.