/*------------------------------------------------------------- Copyright (C) 2000 Peter Clote. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. THE AUTHOR AND PUBLISHER MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. -------------------------------------------------------------*/ /************************************************* Program: evolution.c Implementation of Dawkin's program, described in his book "The Blind Watchmaker", to simulate evolution of strings and count the number of generations necessary to converge to a given target string. Start with some string s, and form a number of mutant copies of s as follows. Each character of s is changed, with probability r, to a randomly chosen character from the alphabet of size A. From the copies of s, choose one that agrees with the target t in the greatest number of positions, and replace s by this string. The process stops when s is the same as t. Erich Bach gave an interesting probabilistic analysis of Dawkin's program. *************************************************/ #define PROB 0.1 /* probability to change character */ #define LETTERS 10 /* number of letters in string + 1 (for EOS) */ #define OFFSPRING 40 /* number of offspring */ #define ALPHABET 27 /* alphabet size */ #define EOS '\0' /* end-of-string symbol */ #include #include #include #include main() { char target[LETTERS] = "lamark "; char current[LETTERS] = "lenark "; char words[OFFSPRING][LETTERS]; /* array for the offspring strings */ int time = 0, i, minindex, min, current_errors, num; int num_errors(char target[],char current[]); int mutate(char current[], char word[]); int print(char target[LETTERS], char words[OFFSPRING][LETTERS]); /* initialize last char of the strings in array words with EOS */ for (i=0;i