Cars with brains: Messing around with small neural networks

A long time ago, I tried to make a project with simple neural networks, where a bunch of “cars” would drive around a track. I could never get this to work unfortunately, and I lost interest. Recently, after watching 3Blue1Brown’s YouTube video on neural networks, I realized that I had been doing it really wrong, and I pulled this project out of the depths of the “view all” menu and into the light. I spent the past few days fixing my implementation and fiddling with values until these “cars” could actually train enough to make it all the way around the track!

Controls:
A - run simulation at 20x speed
B - slow simulation to 1x speed (runs at 5x speed normally)
Up Arrow - force new training round (use this if a car is going around forever, but you still want to train better cars)
The number in the top right is the number of cars still alive.

How this works:

There are two images. The background image (the track you see), and the “distance” image. The distance image is in the shape of the track, but changes color every once in a while. When a car drives over a new color, its “distance” variable is increased. This is used to tell which car traveled the farthest.

We then find out which cars went the furthest and create many copies of them, changing the neural net values slightly, and keeping a copy of the original car as well. Slowly, the cars that do best will go further, and they will create more “children”, so this kind of training is similar to how evolution works, in a way.

Each car has 3 inputs, and those inputs are the distance from it to the walls in 3 directions, relative to its movement direction. The output is a number from -128 to +128 that controls the change in the movement direction of the car. Each car has 1 layer of input, a hidden layer, and an output layer. The number of nodes (neurons) per layer is determined by a “nodes” variable. Right now I have that set to 4. Each car has 3 buffers representing the weights and stuff of each neuron.

If you have any questions just ask! I learned a lot while making this, so hopefully I’ll have answers. :smile:

If the screen is black for a long time at the beginning, that’s because the first round creates 40000 random-brain cars to start with. After that, each round is just 400 cars.

5 Likes

I’ll add that another thing I changed was that, in the old project, I had the speed and rotation of the car as inputs, but I think the cars were training to use them as a sort of timer for when to turn, instead of using the distance inputs, which meant they would train very slowly. When I removed those inputs, and made each car move at a constant speed, they started training much faster.

2 Likes

for 10 seconds straight I read this as cat with brains and I was so confused until I clicked it

3 Likes

lol