Hello Community,

here i'd like to give you a little peace of code, which you are able to calculate genetic operations.

What does the code do:

1. It generates an organism, which includes a genome-string (dna) with 4 genomes (simple numbers between 0-3)
2. Then it generate a random modell generation to compare a fitness value later.
3. After initializion the first generation of organisms will created. it compares the genome-string with the modell generation an sign the fittest organism.
4. if in the first run the fittest organism was found it stopps the code and print the best generation.
5. if not, it re-evaluate the next generation with mutation (breaks the genome-string at a random position in two peaces and stitch it with another broken string) and compare again.
6. it prozesses step 5 until the best generation was found.

In the #define section you could play with the settings.

It generate at default 100 organisms and a genome-string of 20 genes.
The DoOneRun() routine is a endless loop until it founds the fittest generation, so be carfull if you extend your genome-strings and/or the number of genes.
ALLELES is the gene-variations a genome has. Default 4 (Number between 0-3)
MUTATION_RATE ist the number which sets, if a new breed get a crossover or a new random generation. Default is 0.001. (1/1000th that a random string will be produced). the bigger the number the more like to become a new random genome. 0.1 = 1/10th.


Yes, it is very simple, but shows how simple genetic algorithms work. You could extend it and use it for AI in your own game, simulator, or whatever you like.

have fun and contribute your extension if you like.

PriNova

P.S.: please rename file-extension from .txt to .c to use it with SED.

Attached Files
GA.txt (91 downloads)