Success! I’m actually quite surprised that it worked, well not surprised that it worked but surprised that I got it to work with as few errors as I got. It was pretty confusing, but I did manage to logic it out with the help of the book and my head. Some parts of it were ommited in the book and some parts didn’t really make sense, so I had to do it with reference to the previous genetic algorithm that I made.
For example, I did the fitness calculation, the cross-over and mutation in the Ship class instead of creating another class. I also found that the fitness equation which the book gave (1/d)^2 was too small to work with, and had to replace it with (1/d) and alter the way the mating pool was generated. However, some of the code for the gA was remarkably similar to the previous one and could easily be translated, so that helped. I added a text through Pygame that counted the number of generations (or I called it a cycle).
Here is the code (that I’m quite proud of), tidied up with a smattering of comments:
I made a gif (in very low fps and resolution, sorry) of it simulating and in the beginning, the ships are accelerating in random directions with apparently no goal. However by cycle 3-4, the ships are mostly all moving towards the depot, with a few stragglers. By cycle 5-10, the ships are pretty homogenised with no noticeable difference in cycles 10-20, other than random mutations and most reach the depot.