The Computational Beauty of Nature
Source code

gastring

breed strings with a genetic algorithm

gastring -help
  or
gastring
       [-target  string]  [-size integer] [-steps integer]
       [-seed integer]  [-crate  double]  [-mrate  double]
       [-pbase double]

Uses a genetic algorithm to breed strings that match a user-specified target string. This program illustrates how GAs can perform a type of stochastic search in a space of discrete objects. Reproduction of strings entails crossover and mutation with strings being selected based on fitness.

Use a genetic algorithm to breed strings that match a user-specified target string. This program illustrates how GAs can perform a type of stochastic search in a space of discrete objects. Reproduction of strings entails crossover and mutation with strings being selected based on fitness.

Miscellany

The fitness function works in three steps. First, the number of correct characters is tallied and denoted the raw fitness. The scaled fitness is then set to pow(PBASE, raw fitness - string len). The normalized fitness is then set to the scaled fitness divided by the sum of the scaled fitnesses. Thus the sum of the normalized fitnesses must be equal to one while a string with one letter more correct than another string is PBASE times as likely to reproduce, where PBASE is the value supplied with the -pbase option.

Bugs

No sanity checks are performed to make sure that any of the options make sense.

Options

-targetTarget string.furious green ideas sweat profusely
-sizePopulation size.500
-stepsNumber of generations.50
-seedRandom seed.0
-crateCrossover rate.0.75
-mrateMutation rate.0.01
-pbasePower base for fitness.2.0

gastring.c on GitHub