Neuroevolution: Cars Learn to Drive

Learning without a teacher — it works when you don't mourn the bad attempts.

What is Neuroevolution?

AnalogyDefinition

Imagine a driving school where nobody teaches the students how to drive. Instead, hundreds of students get into cars simultaneously and just try their best.

After each round, the students who got the farthest are allowed to continue. Their driving strategies are combined and slightly modified for the next generation of students:

The best drivers pass on their instincts to the next generation

By combining strategies from two good drivers, even better approaches emerge

Small random changes sometimes lead to surprisingly clever maneuvers

Overview

This interactive demo simulates neuroevolution: a population of cars, each controlled by its own neural network, learns to navigate a track. Over successive generations, the cars improve their driving through evolutionary selection, crossover, and mutation of their neural network weights.

Key Concepts
  • Sensors:Each car has distance sensors that detect how far away the track walls are. These measurements serve as the inputs to the neural network.
  • Neural Network:A small feedforward network that takes sensor readings and current speed as inputs and outputs steering angle and acceleration values.
  • Fitness:A car's fitness counts the checkpoints it reaches, plus a small bonus for how close it is to the next one. Time does not count directly – but fast cars reach more checkpoints within a generation's time.
  • Selection:After each generation, the cars with the highest fitness are selected as parents. Their neural network weights form the basis for the next generation.
  • Mutation:Small random changes are applied to the neural network weights of offspring. This introduces variation and helps discover new driving strategies.
  • Crossover:The weights of two parent networks are combined to produce a child network. This merges successful strategies from different parents.
  • Elitism:The top-performing cars are copied unchanged into the next generation. This ensures the best solutions are never lost.

Interactive Neuroevolution

Generation 0. Best fitness 0. Diversity 0 percent.

Generation
0
Best Fitness
0
Alive
0 / 30
Elapsed
20.0s

What happens on the track

This demo teaches cars to drive — not by programming them, but through trial and inheritance. Here is what you see on screen.

What you see
A race track seen from above, with many cars driving at once. Each car feels its way forward with thin sensor whiskers that measure the walls. A small diagram in the bottom right shows the neural network of the car you are watching.
What happens
At first almost every car drives straight into a wall. The few that get furthest pass their brain on to the next generation — slightly mutated. Over many generations, more and more cars take the corners cleanly. In the champions race, the best cars from every generation compete at once.
What you can do
Start and pause the evolution, change the speed, follow a single car or click on it. In manual mode you drive a lap yourself and compare your time with the AI.
What to watch for
Nobody programmed the cars with rules for driving. The driving behaviour emerges from chance, selection and inheritance alone — generation after generation.
Click a car to follow it
Controls
Settings
10×
15%
0.30
🏆 Hall of Champions

No champions yet. Run the evolution — every 5 generations the best brain is saved.

Neuroevolution Step by Step

  1. Create Population

    A population of cars is created, each equipped with a randomly initialized neural network. These networks have random connection weights, so the cars initially drive erratically in all directions.

  2. Measure Fitness

    Each car drives the track until it crashes or times out. Its fitness counts the checkpoints it passes, plus a small bonus for being close to the next one. Time does not count directly – but faster cars pass more checkpoints before time runs out.

  3. Selection

    The cars with the highest fitness scores are selected as parents for the next generation. This mimics natural selection: only the fittest survive and reproduce.

  4. Crossover

    The neural network weights of two parent cars are combined to create offspring. Some weights come from one parent, others from the second. This merges successful driving strategies.

  5. Mutation

    Small random changes are applied to the weights of the offspring's neural networks. This introduces diversity and can produce novel behaviors that neither parent exhibited.

  6. Repeat

    Steps 2 through 5 are repeated for each new generation. Over time, the population converges toward neural networks that drive the track effectively, sometimes discovering surprisingly creative strategies.

The Sensor System

Each car is equipped with five distance sensors pointing in different directions: front, left 45 degrees, right 45 degrees, left 90 degrees, and right 90 degrees. These sensors cast rays from the car toward the track walls and measure the distance to the nearest wall. Short distances mean the wall is close, signaling the car to steer away. The sensor readings are the primary inputs to the neural network, giving it spatial awareness of the track ahead and to the sides.

The Neural Network

The neural network is a simple feedforward network with an input layer (sensor readings plus current speed and steering angle), one hidden layer, and an output layer with two neurons: one for steering and one for acceleration. Each connection has a weight that determines how strongly one neuron influences another. During evolution, these weights are what gets optimized - the network architecture stays fixed. The visualization shows active connections in brighter colors and thicker lines when they carry stronger signals.

The Fitness Function

Fitness is the measure of how well a car performs on the track. It counts the checkpoints the car passes (invisible lines across the track) and adds a small bonus for being close to the next one. There is no time bonus: fast cars simply pass more checkpoints before the generation ends. Cars that crash early receive low fitness scores. This fitness value determines which cars are selected as parents for the next generation - creating evolutionary pressure toward better driving.

Selection Pressure

Selection pressure determines how strongly fitness differences influence reproduction. High selection pressure means only the very best cars get to reproduce, leading to fast convergence but risking the loss of genetic diversity. Low selection pressure allows more cars to contribute to the next generation, maintaining diversity but slowing improvement. The balance is crucial: too much pressure leads to premature convergence on a suboptimal strategy, while too little pressure makes evolution inefficiently slow. In this demo, tournament selection is used - a small group of cars is randomly chosen, and the best one from that group becomes a parent.

Quiz: Understanding Neuroevolution

Question 1 / 5
Not completed

What do the car's sensors measure?

Select one answer
Answer Key: 1) B · 2) C · 3) A · 4) B · 5) C
TheoryHistoryPseudocodeStep by StepFlow Diagram

Learning without a teacher, without a gradient

Neuroevolution trains neural networks using the principles of biological evolution: reproduction, selection and mutation. Instead of computing a gradient from an error and adjusting the weights via backpropagation, a whole population of networks is tried out. The most successful get to reproduce, the weak ones disappear. Over many generations, ever better networks emerge — without a gradient ever being computed.

The network in the car

In this demo, each network steers a car. The inputs are five distance sensors (rays that measure how far away the nearest wall is) plus speed and steering angle. A hidden layer with ReLU activation processes them, and two outputs steer the car: steering (via tanh, from left to right) and gas (via sigmoid, from zero to full). The network structure is the same and fixed for all cars.

Fitness instead of a loss function

Because there are no predefined correct answers, neuroevolution needs no differentiable loss function, just a fitness — a single number that measures success. Here, what counts is how far a car gets: every checkpoint passed and every completed lap increases the fitness. That is enough to act as evolution's compass; it never needs to know why a car drives better, only that it drives better.

The evolutionary cycle

  • Selection (tournament): For each parent, three random networks are drawn and the fittest wins. Good networks get more offspring, weak ones keep a remaining chance.
  • Elitism: The best networks (default: 2) are carried over unchanged, so a good driving style that has been found is never lost.
  • Crossover: The child inherits each individual weight by a coin flip from one of the two parents.
  • Mutation: Each weight is randomly nudged slightly with a small probability — the source of genuine novelty.

Weights, not structure

Important for classification: this demo evolves only the weights of a network with a fixed structure (classical neuroevolution). More advanced methods like NEAT additionally grow the topology — they add new neurons and connections over the course of evolution. Both are neuroevolution; the difference lies in whether only the weights evolve or the network's blueprint as well.

Strengths and limits

  • No gradient needed: works even when the reward is non-differentiable or only known at the end.
  • Highly parallelizable: all networks in a generation can be evaluated independently.
  • Metaheuristic: there is no guarantee of finding the best possible network — usually a good, not the optimal, solution emerges.
  • Noisy and slow: the fitness is only a sample, and without diversity the population can get stuck in a local optimum early.

Play with the demo! Turn the mutation rate up and watch how restless the population becomes; set the elite count to 0 and observe how good driving styles can get lost again. Watch the diversity indicator — if it drops toward zero, all networks resemble each other and evolution barely progresses anymore.