My first neural network in Unity3d
Reinforcement Network & What it does
In the video you can see how my very first neural network got applied.
These small blobs are craving for food - otherwise they will die. Their goal is to move towards the yellow food and consume it by colliding with their tongue. The amount of energy they currently have is express by a value called "fitness", which gets reduced per frame and increased when they eat a piece of food.
The goal is that they learn on their own to move towards food to increase their fitness. In the so-called "input neurons" they get the angle that they need to turn in order to look at the nearest food directly and the other on is a factor that scales their z-Movement (forward-backward)..
Nowhere is written, how they should interprete these informations. There is no if statement which tells them what to do.
This is where the magic begins. Each blob got their own "brain" / neural network, which now mutates random values, which lead to random behaviour - as you can see, most blobs are rotating or moving randomly at the start.
Meanwhile their fitness value gets reduced every frame to simulate energy-loss. If they consume food, their fitness will increase. After 10 seconds all blobs get killed, but the most fittest 50% get duplicated and respawned again.
When creating a new generation there is a 0.8% chance that a connection between new neurons might change it's value randomly. This does sound fairly unlikely, but the amount of connections in every blobs brains is 140. (layer neuron count: 2-10-10-2)
By chance one of them will eventually mutate values that will convert the input values (angle towards food, movement speed) to and output which makes them steer towards a food. Its fitness increases, it will be duplicated. And generation per generation they will learn how to eat food to survive.
These small blobs are craving for food - otherwise they will die. Their goal is to move towards the yellow food and consume it by colliding with their tongue. The amount of energy they currently have is express by a value called "fitness", which gets reduced per frame and increased when they eat a piece of food.
The goal is that they learn on their own to move towards food to increase their fitness. In the so-called "input neurons" they get the angle that they need to turn in order to look at the nearest food directly and the other on is a factor that scales their z-Movement (forward-backward)..
Nowhere is written, how they should interprete these informations. There is no if statement which tells them what to do.
This is where the magic begins. Each blob got their own "brain" / neural network, which now mutates random values, which lead to random behaviour - as you can see, most blobs are rotating or moving randomly at the start.
Meanwhile their fitness value gets reduced every frame to simulate energy-loss. If they consume food, their fitness will increase. After 10 seconds all blobs get killed, but the most fittest 50% get duplicated and respawned again.
When creating a new generation there is a 0.8% chance that a connection between new neurons might change it's value randomly. This does sound fairly unlikely, but the amount of connections in every blobs brains is 140. (layer neuron count: 2-10-10-2)
By chance one of them will eventually mutate values that will convert the input values (angle towards food, movement speed) to and output which makes them steer towards a food. Its fitness increases, it will be duplicated. And generation per generation they will learn how to eat food to survive.
Code
This project got created with the help of a tutorial, which can be found here.
Below you can see the class NeuralNetwork. It created a "bone" structure of the network. What this network actually does will be managed in the NetworkManager below.
It initiates the structure (input layer, hidde layers, output layer and their connections), executes the mutations, compares networks and sets their fitness value
It initiates the structure (input layer, hidde layers, output layer and their connections), executes the mutations, compares networks and sets their fitness value
This class is called NetworkManager. It gives orders to each instance of a NeuralNetwork to mutate, to duplicate, which gets deleted and controlls how long each training sessions lasts and initiates new networks.
Here is the pygmy/blob script. It implements an instance of a neural network, gives the input values and received the output values which influence their behaviour.