Genetic Algorithm – Binary Encoded Example
The following example is a Ruby interpretation based on the idea illustrated here.
In this example, we will illustrate the effectiviness of genetic algorithms by simulating a population of chromosomes. Each generation, the chromosomes will be assessed and given a fitness score. The closer a given chromosome is to a predetermined target, the higher the fitness score. Then, by roulette wheel selection, 2 parent chromosomes will be chosen. Chromosomes with a higher fitness score are more likely to be selected. These parent chromosomes will produce 2 offspring chromosomes, with each offspring carrying certain combined traits of their parents, based on a crossover rate. Each offspring may also undergo a genetic mutation, also based on a fixed rate. This process of roulette wheel selection, crossover, and possible mutation is repeated until a new generation of chromosomes is created. This population of chromosomes is assessed for fitness and then the entire process is repeated until a chromosome is produced that matches the predetermined target.
Download the code
git clone email@example.com:kornypoet/genetic_algorithm.git
For most testing purposes the following string was used:
Broken up into its constituent genes with corresponding decoded value
'1010' '0110' '1100' '1000' '1111' '1010' '0010' '1011' '10' '+' '6' '*' '8' nil '+' '2' '-' drop