Advertisement
Guest User

Untitled

a guest
Feb 16th, 2009
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <time.h>
  6. #include <math.h>
  7.  
  8. using namespace std;
  9. std::string target = std::string( "Hello World!" );
  10.  
  11. #define MUTATION_RATE RAND_MAX * 0.25f
  12. #define POPSIZE 2048
  13. #define MAXGEN 1024
  14.  
  15. struct cromosome {
  16.     string text;
  17.     unsigned int fit;
  18. };
  19. typedef vector<cromosome> population;
  20.  
  21. void create_population( population &pop, unsigned int size )
  22. {
  23.     unsigned int tsize = target.size();
  24.     for( unsigned int i=0; i<size; i++ )
  25.     {
  26.         cromosome node;
  27.         node.fit = 0;
  28.         for( unsigned int j=0; j<tsize; j++ )
  29.             node.text += (rand()%90)+32;
  30.         pop.push_back( node );
  31.     }
  32. }
  33.  
  34. void calc_fitness( population &pop )
  35. {
  36.     unsigned int fit, psize = pop.size(), tsize = target.size();
  37.     for( unsigned int i=0; i<psize; i++ )
  38.     {
  39.         fit = 0;
  40.         for( unsigned int j=0; j<tsize; j++ )
  41.             fit += abs( pop[i].text[j] - target[j] );
  42.         pop[i].fit = fit;
  43.     }
  44. }
  45.  
  46. bool cmp_cromosomes( cromosome a, cromosome b )
  47. {
  48.     return (a.fit < b.fit );
  49. }
  50.  
  51. inline void sort_population_by_fitness( population &pop )
  52. {
  53.     sort( pop.begin(), pop.end(), cmp_cromosomes );
  54. }
  55.  
  56. void print_population( population &pop )
  57. {
  58.     cout << "- Printing population -" << endl;
  59.  
  60.     population::iterator i;
  61.     for( i=pop.begin(); i != pop.end(); i++ )
  62.     {
  63.         if( !i->text.compare("") )
  64.             continue;
  65.  
  66.         cout << "Fitness: " << i->fit << " Text: " << i->text << endl;
  67.     }
  68. }
  69.  
  70. population survival_of_the_fittest( population &pop, unsigned int limit )
  71. {
  72.     population survivors( pop.size() );
  73.     for( unsigned int i=0; i<limit; i++ )
  74.         survivors[i] = pop[i];
  75.     return survivors;
  76. }
  77.  
  78. void print_best_cromosome( population &pop )
  79. {
  80.     cout << "Best cromosome with fitness " << pop[0].fit << ", " << pop[0].text << endl;
  81. }
  82.  
  83. cromosome mate( cromosome &a, cromosome &b, unsigned int xover, unsigned int tsize )
  84. {
  85.     cromosome offspring;
  86.     offspring.text = a.text.substr( 0, xover ) + b.text.substr( xover, tsize - xover );
  87.  
  88.     return offspring;
  89. }
  90.  
  91. void mutate( cromosome &a )
  92. {
  93.     int mpos = rand() % a.text.size();
  94.     a.text[mpos] = ((a.text[mpos] + ((rand()%90)+32)) % 122 );
  95. }
  96.  
  97. void advance_population( population &pop )
  98. {
  99.     unsigned int p1, p2, tsize = target.size(), psize = pop.size(), esize = psize * 0.1;
  100.  
  101.     population new_generation = survival_of_the_fittest( pop, esize );
  102.  
  103.     for( unsigned int i=esize; i<psize; i++ )
  104.     {
  105.         p1 = rand() % (psize/2);
  106.         p2 = rand() % (psize/2);
  107.  
  108.         new_generation[i] = mate( pop[p1], pop[p2], rand() % tsize + 1, tsize );
  109.         if( rand() < MUTATION_RATE )
  110.             mutate( new_generation[i] );
  111.     }
  112.     pop = new_generation;
  113. }
  114.  
  115. int main()
  116. {
  117.     int silent = 0;
  118.     srand( unsigned(time(NULL)) );
  119.  
  120.     population pop;
  121.     create_population( pop, POPSIZE );
  122.  
  123.     for( unsigned int i=0; i<MAXGEN; i++ )
  124.     {
  125.         calc_fitness( pop );
  126.         sort_population_by_fitness( pop );
  127.  
  128.         if( !silent )
  129.         {
  130.             cout << "Generation " << i << " ";
  131.             print_best_cromosome( pop );
  132.         }
  133.  
  134.         if( pop[0].fit == 0 )
  135.             break;
  136.  
  137.         advance_population( pop );
  138.     }
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement