This is part of an ongoing series in which we experiment with different methods of AI. We’ll look at the state of the art and the out of fashion; the practical and the (seemingly) impractical; to find what works and what doesn’t. In this part, we will be writing the genetic algorithm that will evolve our UFOs behaviour. You can read the first part in the series for more information on what we are trying to accomplish. You can also see my earlier article for more general information on genetic algorithms, however, I’ll make sure to include the important parts here.
So what is a genetic algorithm?
A Genetic Algorithm (GA) is an attempt to mimic our evolutionary processes. GA’s are a specific type of evolutionary computing paradigm common to artificial life simulations. Solutions to problems are encoded in a structure called a chromosome (or genome). The solution can be represented as binary digits (bits) or in any other computer-readable format; we’ll be using the floating-point weights of our UFOs neural networks. Similarly, to biological evolution, new generations are created by mating two chromosomes and (possibly) applying mutation to produce a new offspring and consequently a new solution. So in terms of our game, the ‘mating’ will occur when we create a new neural network by combining two previously created neural networks.
Therefore by implementing a GA in our game, we want our UFOs to become more successful over subsequent generations. Just as we (and everything that’s alive) evolve over many generations to become more successful at survival and reproduction.
Chromosomes A and B are mated to produce an offspring. The crossover point can be dynamically selected or pre-determined; in the diagram the first block of A is crossed over with the second and third block of B. This produces a new offspring with characteristics of both parents. Mutation is not usually performed after every crossover; rather it is performed based on a mutation rate. During mutation, one or more digits is selected and changed in some manner e.g. if the chromosome is encoded in binary digits the digits are reversed.
A general process for a GA is shown in the diagram below.
There are a number of methods used to select individuals for crossover and mutation; however, fitness proportionate selection is commonly used in artificial life simulations due to its parallels with nature. This is what we’ll be using as well. Using this method, the fitter an individual (based on their fitness) the higher the probability of selection. However, it does not guarantee that the fittest individual will be selected. We’ll go into more detail when we write the code in the next part.
A problem being solved by a GA is defined by its fitness function. Individuals are evolved to have the best fitness values possible. The fitness function is used to evaluate the relative performance of possible solutions and is normally defined as a numerical, problem-independent, value. There is no general function; it must be tailored for each application. For our game, we’ll use the maximum time a UFO is alive.
While there was a lot of information hopefully as we write the genetic algorithm (which we will start now and finish in the next part) it will become more clear how this will help our UFOs.
For our GA to work, we need a method of deciding when to remove a UFO from the game and therefore creating ‘generations’. To accomplish this we’ll give each UFO an energy level. This energy value will decrease whenever they collide with another UFO or a screen edge, the two things we do not want our UFOs to do. When the energy reaches zero the UFO will be removed from the simulation and added to a ‘pool’. This pool represents a number of solutions to our problem, it is a collection of a UFOs predecessors. I’ll go into more detail as we go along but the general steps for genetic algorithm are:
- Spawn number of UFOs with random weights.
- Add UFOs to game.
- UFOs energy reaches zero.
- UFO removed from the game and added to the pool.
- Pool sorted so that UFOs with the longest time alive are first in the pool.
- Select parents from the pool based on fitness (more on this soon).
- Crossover parents neural networks to create a new network.
- Possibly apply mutation to the neural network.
- Spawn new UFO.
- Add neural network to UFO.
- Add new UFO to the game.
The game loop will continue until we stop the game and you’ll notice it is very similar to the GA process I outlined earlier.
Create a new class called GeneticAlgorithm. This will be responsible for evolving our neural networks. Start by moving our SpawnUFO from SceneGame to our new class.
The method is mostly unchanged however it now returns a reference to the created UFOs neural network. We’ll use this later to evolve the newly created UFOs. Don’t forget to remove SpawnUFO from the SceneGame class, we only want our GeneticAlgorithm class spawning UFOs from now on. We need to instantiate our new GeneticAlgorithm class in SceneGame.
I’ve also updated SceneGames constructor so that it calls our GAs constructor.
Hopefully, when the game is run there should still be 80 UFOs onscreen. Now let’s add energy levels and a method of removing the UFOs from the game. We’ll do this by adding new components to our UFO in our GeneticAlgorithm class.
We add three new components in SpawnUFO:
- C_Energy: stores the UFOs current energy and allows us to modify it.
- C_DamageOnWallHit: reduces energy to 0 when a UFO touches the screen edge.
- C_DamageOnCollision: reduces energy whenever UFOs are colliding.
For more information on how these components work please see the classes in the Part 4 folder from the GitHub link at the top of the page.
Now our UFOs have an energy level we need to create a pool of UFOs to store the recently removed. To do this we’ll create a structure to store all of the information we require for each UFO in the pool.
We also need to provide the GA with an Update function (to loop through every UFO and check if their energy levels have reached 0) and a PoolSort function that will sort the pool so that the UFOs that were alive the longest are at the front of the pool. We’ll need them sorted in this way when we come to select parents from the pool in the next part.
We have to update the constructor to initialise our maximum pool size. The Update function loops through the UFOs in the game and gets their energy component. If their energy has reached zero we create a UFOGAData structure to store the information we need and add this to our pool collection. Lastly, the UFO is set to be removed from the pool by calling QueueForRemoval.
The AddToPool function is straightforward. It adds the data to the vector, sorts the vector, and then if the pool size is greater than the maxPoolSize the last UFO data is removed. As the pool was sorted before this removal, the last UFO in the vector is also the UFO with the lowest time alive and therefore the most unfit for our purposes.
The comparator function for the sort method uses the UFOs time alive.
We need to call our new Update function from our SceneGames Update function so that it is called each frame.
If you run the game now you’ll notice the UFOs being removed as their energy levels reach zero, however you’ll also notice that no new UFOs are being spawned so eventually you’ll end up with an empty screen.
Thats all for this part, next part we’ll spawn new UFOs using our Genetic Algorithm. As always, thank you for reading 🙂