Thursday, January 6, 2011

Genetic Hello World

A recent article called Genetic Algorithm for Hello World describes the basic concepts involved in programming genetic algorithms. The original implementation is in Javascript, and has a nice online simulator. I thought it would be fun to contribute an implementation in Factor.

Genetic algorithms are generally comprised of the following concepts:

A target chromosome which expresses a possible solution to the problem

A fitness function which takes a chromosome as input and returns a higher value for better solutions

A population which is just a set of many chromosomes

A selection method which determines how parents are selected for breeding from the population

A crossover operation which determines how parents combine to produce offspring

A mutation operation which determines how random deviations manifest themselves

First, we need to define the vocabularies that we will use and a namespace:

Selection

We select two parents to survive or breed into the next generation by "taking two members of the population at complete random and keep the fittest as the first parent, then do the same with another two members and keep the fittest as the other parent".

Crossover

After choosing two parents, we want to "ensure their genes survive in the next generation". Sometimes (10% chance) the parents survive into the next generation, but most frequently, we mix the parents by picking a split point and then making one child from the head of parent1 plus tail of parent2 and another from the head of parent2 plus tail of parent1.