thenamelessunicorn

Evlutionary Algorithm for finding out string

Jul 21st, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: quark
  4.  *
  5.  * Created on July 18, 2015, 2:53 PM
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. #define ALPHABET_CHAR 27
  14. #define offspring 10
  15. #define mutation_rate 0.9
  16.  
  17. char ALPHABET[27] = "abcdefghijklmnopqrstuvxyz ";
  18.  
  19. /**
  20.  * Number between 0 and 1
  21.  * @return
  22.  */
  23. double
  24. random0to1 ()
  25. {
  26.   return ((double) rand () / (double) RAND_MAX);
  27. }
  28.  
  29. /**
  30.  * Returns a measure of how close the "string" is to the "target"
  31.  *
  32.  * @param string string to be compared
  33.  * @param target target string
  34.  * @param n number of characters in string
  35.  * @return Fitness. the closer to 1 the better
  36.  */
  37. int
  38. closeness (char* string, char* target, int n)
  39. {
  40.   int fit = 0, i, j;
  41.  
  42.   if (n == 0) return 0;
  43.  
  44.   if (!strcmp (string, target)) return 99999999; //if strings match, no need to iterate
  45.  
  46.     for (i = n - 1; i > 0; i--)
  47.       {
  48.         if ((string[i] == target[i])) fit++; //fit gets increased by each matching letter.
  49.       }
  50.  
  51.   return fit;
  52.  
  53. }
  54.  
  55. /**
  56.  * Mutates randomly a random number of characters from a supplied string
  57.  * without changing the original one;
  58.  *
  59.  * @param original The string to be mutated
  60.  * @param mut_rate How often should a mutation occur
  61.  * @param n Number of characters in the string
  62.  * @return mutated string.
  63.  */
  64. char*
  65. mutate (char* original, double mut_rate, int n)
  66. {
  67.   double mutates = random0to1 ();
  68.   char* mutated = original;
  69.   int* pos;
  70.   int num_mutations, i;
  71.   if (mutates > mut_rate)
  72.     {
  73.  
  74.       mutated[rand () % n] = ALPHABET[rand () % ALPHABET_CHAR];
  75.       //      num_mutations = rand () % n; //how many mutations are going to occur
  76.       //      for (i = num_mutations ; i > 0; i--)
  77.       //        {
  78.       //          mutated[rand()%n] = ALPHABET[rand () % ALPHABET_CHAR];
  79.       //        }
  80.       //      return mutated;
  81.     }
  82.   return original;
  83. }
  84.  
  85. char*
  86. random_string (int n)
  87. {
  88.   int i;
  89.   char* string = (char*) malloc (n * sizeof (char) + 1);
  90.   for (i = 0; i < n; i++)
  91.     {
  92.       int pos = rand () % ALPHABET_CHAR;
  93.       string[i] = ALPHABET[pos];
  94.     }
  95.   string[n] = 0;
  96.   return string;
  97. }
  98.  
  99. /*
  100.  *
  101.  */
  102. int
  103. main (int argc, char** argv)
  104. {
  105.  
  106.   srand (time (NULL));
  107.  
  108.   char target[] = "methinks";
  109.   char* parent = NULL;
  110.   int fitness[offspring];
  111.   int i, best_fit_spec = 0, generation;
  112.   int best_fit = 0;
  113.   //  char[offspring][sizeof(target)];
  114.   char** specimen = (char**) malloc (offspring * sizeof (char*));
  115.   for (i = 0; i < offspring; i++)
  116.     {
  117.       specimen[i] = (char*) malloc (sizeof (target) * sizeof (char) + 1);
  118.     }
  119.   parent = random_string (sizeof (target));
  120.  
  121.   int n = offspring;
  122.   int size = sizeof (target);
  123.   char* old_parent = NULL;
  124.   while (strcmp (parent, target))
  125.     {
  126.       generation++;
  127.       for (i = 0; i < n; i++)
  128.         {
  129.           specimen[i] = mutate (parent, mutation_rate, size);
  130.           fitness[i] = closeness (specimen[i], target, size);
  131.           if (best_fit < fitness[i])
  132.             {
  133.               best_fit = fitness[i];
  134.               best_fit_spec = i;
  135.             }
  136.         }
  137.       parent = specimen[best_fit_spec];
  138.       printf ("parent: %s best match: %s in generation %d, specimen %d with fitness %d\n", old_parent, parent, generation, best_fit_spec, best_fit);
  139.     }
  140.  
  141.   return (EXIT_SUCCESS);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment