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, the final part, we finish our genetic algorithm (that we started here) by applying a mutation to the newly evolved networks and then we write a bit of code to output the average time alive for each generation. We can use this to make sure that our eyes are not deceiving us and the UFOs are actually evolving to become better at avoiding each other with each new generation.
Mutation is a genetic operator used to maintain genetic diversity from one generation of a population of genetic algorithm chromosomes to the next.
I’ve used a specific type of mutation called ‘swap mutation’ which gains its name from the fact that we swap weights at two locations. There are other mutation variants that you can play around with, others may work better than swap mutation but I know from previous experience that swap mutation does a decent job. It works like this:
Now for the last part: testing. We could just watch the UFOs but I like to be able to prove with numbers that we’ve actually achieved our original goal. We can do this quickly and simply by logging the average time alive for each generation to a text file. We’ll do this in our GeneticAlgorithm class.
We calculate the average fitness by dividing the total fitness score (the sum of all UFOs time alive in the pool) by the number of UFOs in the pool. If you look in the resources folder for the part in the GitHub page you’ll find an example log file of around 370 generations called ‘ga_log.txt’. As you’ll see the average time alive does increase with each generation and it does so quite drastically at times. By generation 1 the average time alive is just over 1 second but by generation 371 the average is 778 seconds, which means that the UFOs, on average, survive for nearly 13 minutes!
Have 60+ UFOs onscreen that have taught themselves to avoid each other and the sides of their environment.
Do let me know if you need help with anything, had a great idea of how to use this in a real game, or have spotted some obvious bug in my code that I’ve missed. I think next time I’ll try a more traditional form of AI, possibly goal-oriented action planning. Maybe we could combine our neural networks with the more structured actions in goap? Whatever I chose to do, it won’t be too long before I start another ‘Experiments in AI’ series, as I had a great time writing this one!