Chrysolites Tech Academy

How Neural Networks Work · Course book

Chapter 3: Teaching a Car to Drive

Pairs with Lesson 3: A Self-Driving Car from Scratch

Reading about neural networks is one thing; watching one learn to steer a car through traffic is another. This chapter follows a project that builds a small self-driving car simulation in plain JavaScript, with no libraries at all, drawn on an HTML canvas in the browser. Every component is written by hand: the physics that moves the car, the road it drives on, the sensors it sees with, the geometry that detects crashes, the traffic it must avoid, the neural network that makes its decisions and the genetic algorithm that improves that network without any labelled data. Each section explains the idea, shows the key code and works a number through by hand, so that by the end you understand every line of a working, learning system.

Car physics and the road

The car is a rectangle with a position, an angle and a speed. Each animation frame, about sixty times a second, the program reads the controls and updates these values. Pressing forward adds a fixed acceleration to the speed; reversing subtracts it. A maximum speed caps how fast the car can go, and friction removes a little speed every frame, so a car left alone rolls to a halt. Steering changes the angle, and the position moves by the speed in the direction of the angle, using the sine and cosine of the angle to split the motion into x and y parts.

update(controls) {
  if (controls.forward) this.speed += this.acceleration;
  if (this.speed > this.maxSpeed) this.speed = this.maxSpeed;
  if (this.speed > 0) this.speed -= this.friction;
  this.x -= Math.sin(this.angle) * this.speed;
  this.y -= Math.cos(this.angle) * this.speed;
}

On a canvas the y coordinate grows downwards, so subtracting moves the car up the screen. With an acceleration of 0.2, friction of 0.05 and a maximum of 3 pixels per frame, the speed rises by a net 0.15 each frame. After 19 frames it reaches 2.85; on the next frame the acceleration takes it to 3.05, the cap cuts it to 3 and friction leaves 2.95, the true top speed. Releasing the pedal then removes 0.05 per frame, so the car takes 59 frames, about one second, to stop.

pedal released 30 020306090 framespeed (pixels per frame)
Figure 3.1 — Speed over time with acceleration 0.2, friction 0.05 and a cap of 3. The car reaches its real top speed of 2.95 at frame 20 and coasts to a stop 59 frames after the pedal is released.

The road is a vertical strip divided into lanes. Its left and right borders are stored as line segments, and the centre of each lane can be computed with linear interpolation, or lerp: lerp(a, b, t) = a + (b − a) × t returns the point a fraction t of the way from a to b. For a road from x = 10 to x = 190 with three lanes, the lane boundaries are at t = 0, 1/3, 2/3 and 1, giving x = 10, 70, 130 and 190, and the centre of the middle lane is at 100. The same small function turns up again in the sensors and in the genetic algorithm.

Sensors and collision detection

The car sees the world through rays, straight lines cast outwards from its centre in a fan, for example five rays spread across a 90-degree arc ahead of the car, each 150 pixels long. For every ray the program checks whether it crosses a road border or another car, and records the nearest crossing. That needs a test for where two line segments intersect.

For a segment from A to B and another from C to D, the intersection can be written as A + t(B − A) = C + u(D − C), and solving gives t and u from three cross-product style expressions. If both t and u lie between 0 and 1, the segments really cross, and t tells you how far along the first segment the crossing is.

Worked example. A ray runs from the car at A (0, 0) straight up to B (0, −150). A road edge runs from C (−50, −90) to D (50, −90). The bottom term is (Dy − Cy)(Bx − Ax) − (Dx − Cx)(By − Ay) = 0 × 0 − 100 × (−150) = 15,000. The top term for t is (Dx − Cx)(Ay − Cy) − (Dy − Cy)(Ax − Cx) = 100 × 90 − 0 = 9,000, so t = 0.6. The top term for u works out to 7,500, so u = 0.5. Both lie between 0 and 1, so the ray hits the edge at 60 percent of its length, the point (0, −90), which is halfway along the edge.

The value t is called the offset. A ray that hits nothing has no reading. Before feeding the readings to the network, each is converted to 1 − offset, so a nearby obstacle gives a value near 1 and a missing or distant one gives 0. In the example the input would be 1 − 0.6 = 0.4.

Collision detection uses the same test. The car's body is a polygon of four corners, computed from its position, angle, width and height. If any edge of the car's polygon intersects any road border or any edge of another car's polygon, the car is damaged and stops moving. Drawing damaged cars in grey makes it easy to see which ones crashed.

Traffic and the neural network

Traffic consists of other cars placed in various lanes ahead, driving forward at a steady speed with no intelligence of their own. They give the self-driving car something to overtake and avoid. The sensors detect their polygons just like road borders, and a collision with one damages the car.

The brain is a small feedforward neural network built from levels. Each level connects every input to every output through a weight between −1 and 1, and each output has a bias. An output switches on, producing 1, when the weighted sum of its inputs is greater than its bias, and otherwise gives 0. The network has one input per sensor ray, a hidden level, and four outputs: forward, left, right and reverse. Those outputs drive the same controls a human would press.

PartSizeWeightsBiasesTotal values
Inputs (sensor readings)5–––
Level 1: inputs to hidden6 hidden neurons5 × 6 = 30636
Level 2: hidden to outputs4 outputs6 × 4 = 24428
Whole network–541064

For example, suppose an output neuron receives three inputs 0.4, 0 and 0.9 through weights 0.5, −0.3 and −0.8, and has a bias of −0.6. Its sum is 0.2 + 0 − 0.72 = −0.52. Because −0.52 is greater than −0.6, the output switches on. Drawing the network next to the road, with weights coloured by sign and strength and outputs lit when active, turns the brain into something you can watch thinking.

Parallel cars and the genetic algorithm

There is no labelled dataset of correct steering, so the network cannot be trained by backpropagation in the usual way. Instead it is trained by evolution. The first step is parallelisation: instead of one car, the simulation runs hundreds at once, each with its own randomly weighted brain. They do not collide with each other, only with the road and the traffic. Most crash almost immediately; by pure luck, a few drive further.

The program needs a fitness measure to choose the best. A simple one is distance travelled: since the road runs upward, the best car is the one with the smallest y coordinate. The camera follows it, and its brain can be saved to the browser's storage with a button press.

The genetic algorithm then improves on the best brain. On the next run, one car gets an exact copy of it, and every other car gets a mutated copy in which each weight and bias is nudged towards a random value. The nudge uses lerp again: new value = lerp(old value, random value between −1 and 1, amount). With an old weight of 0.6, a random value of −0.4 and an amount of 0.1, the new weight is 0.6 + (−0.4 − 0.6) × 0.1 = 0.5. A small amount explores close variations of a good brain; an amount of 1 replaces it with a completely random one.

  • Run many cars with brains that are slight variations of the best one so far.
  • Pick the car that drove furthest without crashing and save its brain.
  • Reload, mutate from the saved brain, and repeat.

After several rounds, the best car weaves through the traffic, changing lanes to pass slower cars. Testing it on a fresh traffic pattern is an important check: a brain that only learned one layout has overfitted, just like a model that memorised its training data. Adding varied traffic to the training runs makes the learned behaviour more general.

Practice tasks

  1. With acceleration 0.3, friction 0.05 and a maximum speed of 4, what is the car's true top speed, and on which frame is it first reached?
  2. Use lerp to find the lane centres of a road from x = 0 to x = 240 with four lanes.
  3. A ray from (0, 0) to (100, 0) and a segment from (70, −20) to (70, 20) cross. Compute t and the network input value 1 − t.
  4. Count the weights and biases in a network with 7 sensor inputs, a hidden level of 8 neurons and 4 outputs.
  5. An output neuron has inputs 0.2, 0.7 and 0, weights −0.4, 0.6 and 0.9, and bias 0.3. Does it switch on?
  6. Explain why a genetic algorithm can train this network when backpropagation cannot easily be used, and suggest a better fitness measure than distance alone.

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.