Guest User

Untitled

a guest
Oct 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <string>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <cassert>
  5. #include <algorithm>
  6. #include <vector>
  7.  
  8. std::string allowed_chars = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  9.  
  10. class selection
  11. {
  12.     public:
  13.         static int fitness(std::string candidate)
  14.         {
  15.             assert(target.length() == candidate.length());
  16.  
  17.             int fitness_so_far = 0;
  18.  
  19.             for(int i = 0; i < target.length(); i++)
  20.             {
  21.                 int target_pos = allowed_chars.find(target[i]);
  22.                 int candidate_pos = allowed_chars.find(candidate[i]);
  23.                 int diff = std::abs(target_pos - candidate_pos);
  24.                 fitness_so_far -= std::min(diff, int(allowed_chars.length()) - diff);
  25.             }
  26.  
  27.             return fitness_so_far;
  28.         }
  29.  
  30.         static int target_length() { return target.length(); }
  31.  
  32.     private:
  33.         static std::string target;
  34. };
  35.  
  36. std::string selection::target = "Oracular is just mad he cannot make this epicness"; // Change this string to change what the program outputs
  37.  
  38. void move_char(char& c, int distance)
  39. {
  40.     while(distance < 0)
  41.         distance += allowed_chars.length();
  42.     int char_pos = allowed_chars.find(c);
  43.     c = allowed_chars[(char_pos + distance) % allowed_chars.length()];
  44. }
  45.  
  46. std::string mutate(std::string parent, double mutation_rate)
  47. {
  48.     for(int i = 0; i < parent.length(); ++i)
  49.         if(std::rand() / (RAND_MAX + 1.0) < mutation_rate)
  50.         {
  51.             int distance = std::rand() % 3 + 1;
  52.  
  53.             if(std::rand() % 2 == 0)
  54.                 move_char(parent[i], distance);
  55.             else
  56.                 move_char(parent[i], -distance);
  57.         }
  58.  
  59.         return parent;
  60. }
  61.  
  62. bool less_fit(std::string const& s1, std::string const& s2)
  63. {
  64.     return selection::fitness(s1) < selection::fitness(s2);
  65. }
  66.  
  67. int main()
  68. {
  69.     int const C = 100;
  70.  
  71.     std::string parent;
  72.  
  73.     for(int i = 0; i < selection::target_length(); ++i)
  74.     {
  75.         parent += allowed_chars[std::rand() % allowed_chars.length()];
  76.     }
  77.  
  78.     int const initial_fitness = selection::fitness(parent);
  79.  
  80.     for(int fitness = initial_fitness; fitness < 0; fitness = selection::fitness(parent))
  81.     {
  82.         std::cout << parent << ": " << fitness << "\n";
  83.         double const mutation_rate = 0.02 + (0.9 * fitness) / initial_fitness;
  84.         typedef std::vector<std::string> childvec;
  85.         childvec childs;
  86.         childs.reserve(C + 1);
  87.  
  88.         childs.push_back(parent);
  89.         for(int i = 0; i < C; ++i)
  90.             childs.push_back(mutate(parent, mutation_rate));
  91.         parent = *std::max_element(childs.begin(), childs.end(), less_fit);
  92.     }
  93.  
  94.     std::cout << "Final string: " << parent << "\n";
  95.  
  96.     system("pause");
  97. }
Add Comment
Please, Sign In to add comment