Simple Genetic Algorithm in Python

After a long break from the world of AI, I’ve finally decided to devote a few cycles here and there on some “first principles” genetic algorithm coding. The basics of Genetic Algorithms (GAs) are described here. There are a number of existing libraries, an example of which is pygene.

Regardless, I’ve decided to have a crack at one from first principles, mainly to get a better understanding of the various pieces that make up a GA solution. The main parts are:

– fitness function

– altering the population based on the fitness function (optimisation / mutation / cross-over )

– population creation

I’m curious to do it from first principles, as part of the above is you want the population to converge to the “best” answer based on your fitness function. This may take a “long time” ™, so a bound on the number of iterations is required. I can see a case where a local minima for the fitness function is reached and the iterations expire before a true minimum of the fitness function (typically we are trying to find the minimum Hamming distance between a population member and the target solution, or minimise the fitness function).

My first foray is to solve a simple problem. Given an arbitrary amount of money below $10, what are the minimum number of coins needed to make that amount (in US denominated coins, ie 1-pennies, 5-nickels, 10-dimes, 25-quarters).