Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: quark
- *
- * Created on July 18, 2015, 2:53 PM
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define ALPHABET_CHAR 27
- #define offspring 10
- #define mutation_rate 0.9
- char ALPHABET[27] = "abcdefghijklmnopqrstuvxyz ";
- /**
- * Number between 0 and 1
- * @return
- */
- double
- random0to1 ()
- {
- return ((double) rand () / (double) RAND_MAX);
- }
- /**
- * Returns a measure of how close the "string" is to the "target"
- *
- * @param string string to be compared
- * @param target target string
- * @param n number of characters in string
- * @return Fitness. the closer to 1 the better
- */
- int
- closeness (char* string, char* target, int n)
- {
- int fit = 0, i, j;
- if (n == 0) return 0;
- if (!strcmp (string, target)) return 99999999; //if strings match, no need to iterate
- for (i = n - 1; i > 0; i--)
- {
- if ((string[i] == target[i])) fit++; //fit gets increased by each matching letter.
- }
- return fit;
- }
- /**
- * Mutates randomly a random number of characters from a supplied string
- * without changing the original one;
- *
- * @param original The string to be mutated
- * @param mut_rate How often should a mutation occur
- * @param n Number of characters in the string
- * @return mutated string.
- */
- char*
- mutate (char* original, double mut_rate, int n)
- {
- double mutates = random0to1 ();
- char* mutated = original;
- int* pos;
- int num_mutations, i;
- if (mutates > mut_rate)
- {
- mutated[rand () % n] = ALPHABET[rand () % ALPHABET_CHAR];
- // num_mutations = rand () % n; //how many mutations are going to occur
- // for (i = num_mutations ; i > 0; i--)
- // {
- // mutated[rand()%n] = ALPHABET[rand () % ALPHABET_CHAR];
- // }
- // return mutated;
- }
- return original;
- }
- char*
- random_string (int n)
- {
- int i;
- char* string = (char*) malloc (n * sizeof (char) + 1);
- for (i = 0; i < n; i++)
- {
- int pos = rand () % ALPHABET_CHAR;
- string[i] = ALPHABET[pos];
- }
- string[n] = 0;
- return string;
- }
- /*
- *
- */
- int
- main (int argc, char** argv)
- {
- srand (time (NULL));
- char target[] = "methinks";
- char* parent = NULL;
- int fitness[offspring];
- int i, best_fit_spec = 0, generation;
- int best_fit = 0;
- // char[offspring][sizeof(target)];
- char** specimen = (char**) malloc (offspring * sizeof (char*));
- for (i = 0; i < offspring; i++)
- {
- specimen[i] = (char*) malloc (sizeof (target) * sizeof (char) + 1);
- }
- parent = random_string (sizeof (target));
- int n = offspring;
- int size = sizeof (target);
- char* old_parent = NULL;
- while (strcmp (parent, target))
- {
- generation++;
- for (i = 0; i < n; i++)
- {
- specimen[i] = mutate (parent, mutation_rate, size);
- fitness[i] = closeness (specimen[i], target, size);
- if (best_fit < fitness[i])
- {
- best_fit = fitness[i];
- best_fit_spec = i;
- }
- }
- parent = specimen[best_fit_spec];
- printf ("parent: %s best match: %s in generation %d, specimen %d with fitness %d\n", old_parent, parent, generation, best_fit_spec, best_fit);
- }
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment