The purpose of the following demonstration is to make an easy to understand application of Artificial Neural Networks and Genetic Algorithms.
The demonstration is about pandas that have to eat carrots in order to survive and avoid the spikes that kill them under the following rules:
- Each panda has 100 life points
- At each frame the life of a panda is reduced by 0.2 points
- If a panda eats a carrot its life is increased by 30 points
- If a panda touches a spike, or its health is reduced to 0, it dies
The spikes, the pandas and the carrots are all randomly placed, if a carrot is eaten it appears in another random position.
Each panda has a Multilayer Perceptron consisting of 14 inputs, 2 hidden layers of 12 and 6 neurons respectively, and 2 outputs. All the layers are fully connected and the activation function is the hyperbolic tangent.
The weights of the net are generated using a Genetic Algorithm, the population is selected with the roulette wheel method and the crossover method is a two point list.
Each panda has a sight of range that is divided in 7 frustums, each frustum represents 2 inputs for the net, the first input is a normalized distance in the range of 0 and 1 between the panda and an object inside the frustum, and the second input depends on the type of object, -1 for a spike, 0 for nothing and 1 for carrot.
The following libraries are required:
In order to work properly a small change has to be made in PyEvolve:
- In file GenomeBase.py
- In def copy(self, g):
- add the line
- g.internalParams = self.internalParams.copy()
- comment the line
- #g.internalParams = self.internalParams
- add the line
- In def copy(self, g):
- In file GSimpleGA.py
- In def step(self):
- comment the lines
- #newPop.evaluate()
- #self.internalPop.sort()
- add the following line at the beginning of the function
- self.internalPop.evaluate()
- self.internalPop.sort()
- comment the lines
- In def step(self):