The Deep Dive
How the Cars Learn to Drive
1 · The Challenge
Driving Without Being Taught
Most software follows rules a programmer wrote. Here, nobody writes the rule “turn left when the wall on the right gets close.” Instead, each car gets a small neural network with random settings, and the only feedback is a score for how far it drove.
That is the idea behind neuroevolution: treat each network like a living creature, keep the ones that do well, and let them have slightly different children. Repeat for enough generations and good driving emerges on its own.
2 · Senses
Eight Rays and a Speedometer
Each car casts 8 rays every 45°, reaching up to 200 units. For every ray the engine finds the closest point where it crosses a road edge.
The distance becomes a closeness value: 0 when nothing is in range and close to 1 when a wall is right next to the car. The ninth input is the car's speed divided by its top speed, so all inputs sit between 0 and 1.
3 · The Brain
A Tiny Neural Network
The 9 inputs feed into a network with 4 outputs. Every connection multiplies the signal by a weight. Each neuron adds up what arrives, adds its bias, and passes the total through a scaled tanh function, which squeezes any number into the range −1 to 1.
Every network starts with each input wired straight to each output and no hidden neurons. Hidden neurons only appear later, when mutation adds them and they turn out to help.
4 · Actions
From Four Numbers to Four Pedals
The outputs map to Accelerate, Brake, Turn Left, and Turn Right. An action only fires when its output is above 0.5 and higher than its opposite, so a car can never press both pedals or steer both ways at once.
Physics is kept deliberately simple. Turning rotates the car 2° per tick, acceleration adds 0.2 to the speed, braking removes 1, friction removes 0.1, and speed is capped at 10 units per tick.
5 · Fitness
Scoring Every Driver
Each tick, a car earns fitness equal to how far it moved up the track, divided by 100. A car is taken off the road if it hits a wall, falls more than 200 units behind the leader, drives backward, or stalls. That costs it 1 point.
A generation ends when every car has stopped, crossed the finish line, or run out of time. A run is solved when any car reaches the finish line.
6 · Evolution
How NEAT Breeds Better Drivers
The simulator uses NEAT, short for NeuroEvolution of Augmenting Topologies. Between generations it does five things:
- Speciate. Group similar networks into species, so a new idea competes with its own kind first.
- Keep the elite. The best 2 networks of each species are copied into the next generation unchanged.
- Select parents. Only the top 20% of each species may reproduce.
- Crossover. A child inherits connections from two parents, preferring the fitter parent.
- Mutate. Weights are nudged (80% chance by default), and new connections (30%) or neurons (20%) may appear. Connections and neurons can also be removed.
Species that fail to improve for 20 generations are removed, which frees up room for new approaches.
7 · Experiments
Settings Worth Trying
Change one setting at a time and compare runs on your dashboard.
| Setting | What It Changes | Try This |
|---|---|---|
| Cars per Generation | How many different networks are tried each generation. | Compare 20 cars with 100 cars. More cars usually finds a good driver in fewer generations. |
| Track Variety | Whether each generation drives a new road or the same one. | Use Same Track, then watch a champion fail on a new track. That is overfitting. |
| Track Width and Curves | How hard the road is to follow. | Narrow and Twisty tracks need more generations and often grow hidden neurons. |
| Weight Mutation Chance | How often a child's connection weights are nudged. | Set it very low and progress stalls, because children barely differ from parents. |
| New Connection and Neuron Chance | How quickly networks grow new structure. | Raise the neuron chance and watch the brain diagram get more complex. |
8 · Glossary
Words You'll See in the Simulator
- Tick
- One step of the simulation. There are 30 ticks per second at normal speed.
- Neural Network
- A web of neurons joined by weighted connections that turns inputs into outputs.
- Weight
- A number on a connection that strengthens, weakens, or flips the signal passing through it.
- Bias
- A number added inside a neuron before its activation function. It shifts when the neuron fires.
- Activation Function
- The squashing function inside each neuron. This project uses a scaled tanh, which outputs −1 to 1.
- Genome
- The genetic description of one network: its neurons, its connections, and their weights.
- Fitness
- The score a genome earns. Here it is roughly the distance driven, in hundreds of units.
- Generation
- One round in which every car drives, gets scored, and is replaced by the next population.
- Species
- A group of genomes with similar structure that mostly compete among themselves.
- Elitism
- Copying the very best genomes of each species into the next generation unchanged.
- Crossover
- Building a child genome by combining the genes of two parents.
- Mutation
- A random change to a genome, like nudging a weight or adding a neuron.
- Stagnation
- When a species stops improving. NEAT removes species that stagnate for 20 generations.
Want the original sources?
Try It in the Simulator