I've recently been having a lot of fun with different machine learning algorithms, and one of my favorites has been genetic algorithms.
For the sake of completeness, here is a quick overview of the genetic algorithm:
- Individual - Each individual is some sort of recipe of how the problem of interest might be solved. Could be a string of bits, a list of integers, etc.
- Allele - A single piece of information, of which many make up an individual. Could be a single bit, an integer, etc.
- Population - A collection of individuals.
- Fitness Function - A function that tests and scores each individual.
The basic idea is that the algorithm starts with a (usually) randomly generated population. Each individual is passed to the fitness function and its score is saved. Once all individuals have been rated they are passed to some form of selection routine, which, depending on their scores, creates a new population with some combination of the best individuals and their offspring, which are creating using some sort of combining routine.
As the vagueness of my description may imply, I see this as an extremely general-purpose algorithm, and I think that the majority of the task-specific details can be handled through clever fitness functions. I created Simple GA with this in mind.
The idea was to have a straight-forward Python API that could be easily used both in programs and at the interactive prompt. It is quite versatile, allowing for any length individuals of bit-string, integer or floating-point types, any number of random crossovers during combination, and offering a couple different selection routines and the option of random mutation during selection. It is also very simple to directly save and load populations (using RawConfigParser).
Using Simple GA:
ga = Simple_GA(fitness_function, 10, 100, selection=BEST_HALF)
# Creates a population of 100 bit-string individuals of 10 alleles each
ga.evolve(2, verbose=True) # See the verbose output
ga.evolve(100) # evolve 100 times or until correct answer reached
This is still a work in progress; I have plans to add more options for selection and combination routines, and it needs some cleaning up and further testing. It's quite usable as is though.
You can find Simple GA here:
(This was my first time using the Epydoc Python documentation generator; worked great! Check it out if you haven't before: http://epydoc.sourceforge.net/)