This is a neural network
It’s job is to train itself and learn how
to drive a car on a randomly generated path,
avoiding collisions with the road borders
and trying to go as fast as possible
But how can it do it?
Very quickly, a neural network is a collection
of nodes, called “neurons”, linked together
by connections with a weight between -1 and
1
These neurons are organized in layers, where
the first is called the input layer and the
last the output layer, and every one of them
can have a value between 0 and 1
8 of the 9 input neurons represent the sensors
of the car, which detect the distance from
the road borders in a specific direction.
They have value 1 if the wall is touching
the car, decreasing to 0 if it goes out
of the sensor range
The ninth instead is the velocity of the car
itself, ranging from 0 to it’s maximum speed
These nodes can be connected to neurons in
the hidden layers or in the output layer,
and the information carried across the connection
is the product of the node value by the connection
weight
The receiving neurons combine all the input
values into an output using a specific function,
which then is sent forward in the network
At the end of the chain reaction the 4 output
nodes have 4 different values that can trigger
an action if they go over a certain threshold,
such as 0.5
These actions tell the car to accelerate,
brake, turn left or right
To create a network capable of driving the
car we need to create the right number of
nodes in the hidden layers and assign to the
connections the appropriate weights
And here we are to neat part, because to do
that I used the NEAT module in Python
(dumb joke right?)
NEAT stands for NeuroEvolution of Augmented
Topologies and it works like this:
At the starting line many simple neural networks
are created,
each of them driving it’s own car
These networks are randomly generated, so
they have absolutely no idea of what they are doing
Yea, the results speak for themselves, they
are trash
Since we already spoke about the “Neuro”ns,
here we are the to “Evolution” part!
To each car is given a score, called "fitness":
if they start wandering and crash into a wall
at supersonic speed, they are going to have
what they deserve, but if they can stay on
track longer than their rivals, their fitness
will be higher
At the end of the run, when all the cars are
dead, the best networks are selected and are
allowed to generate children, which will be
the pilots of the cars in the next run
These cars will race again, and they will
perform better than before, I mean, not much better,
but better nonetheless, so, let’s make some
more babies and run it again, over, and over, and over
And here is where we get to the “of Augmented
Topologies” bit: a child will have some
of the characteristics of the parent, but
also new traits, which can be a different
value in a connection here, a new connection
there, or a new neuron in the hidden layers
Going down the generations, as the new networks
get more complicated, their structure (so
their topology) is going to get bigger and
bigger to achieve the best fitness possible
As you can probably (maybe) imagine, this
is going to take a looooong time: if you don’t
want to wait hours of training just to see
some cars move on a screen without crashing,
which, by the way, is the concept of any racing
videogame ever, you can adjust the settings
giving them a lower maximum speed and a tighter
turning radius
They will learn very soon to keep the foot
on the gas and never brake, just turning left
or right
Again, like any noob on any racing videogame
ever
…right...
If instead you have nothing better to do,
you can make the road tighter or introduce
new features like bicycles, intersections,
traffic lights, and congratulation nerd, you
just got a job at Tesla
One thing to note is that these networks are
just a bunch of numbers, so they have no morals
and they loooove cheating….
Wait, wat?
Oh, you dumb...
