Arusekk

e-bio

May 22nd, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef __WIN32
  3. #include <windows.h>
  4. #else
  5. #include <unistd.h>
  6. void Sleep(long ms) {
  7.   timespec t;
  8.   t.tv_sec = ms/1000;
  9.   t.tv_nsec = (ms%1000) * 1000000;
  10.   nanosleep(&t,NULL);
  11. }
  12. #endif
  13.  
  14. using namespace std;
  15. /*
  16. char labirynt[][64]={
  17.   "##########################",
  18.   "#         ################",
  19.   "# ####### ##   ###########",
  20.   "# ######     # ####      #",
  21.   "# ####### ###  ######### #",
  22.   "#  #####  #   ###        #",
  23.   "#########  # # ## ########",
  24.   "##########  #      #######",
  25.   "###########   ### ########",
  26.   "##########################",
  27.   "##########################"
  28. };
  29. */
  30. char labirynt[][64]={
  31.   "##########################",
  32.   "#   #     ################",
  33.   "### # ### ##   ###########",
  34.   "##  # ##     # ####      #",
  35.   "## ## ### ###  ######### #",
  36.   "##    ##  #   ###        #",
  37.   "#########  # # ## ########",
  38.   "##########  #      #######",
  39.   "###########   ### ########",
  40.   "##########################",
  41.   "##########################"
  42. };
  43.  
  44. short di[] = {0, 1, 0,-1};
  45. short dj[] = {1, 0,-1, 0};
  46.  
  47. int muta_rate = 18; // (0, 100)
  48.  
  49. typedef pair<pair<int,int>,int> situa;
  50. set<situa> odwiedzone;
  51.  
  52. const bool gumowe_sciany = false;
  53.  
  54. int test(const char* genom, int i, int j, int df) {
  55.   situa syt(make_pair(i,j),df*2+(labirynt[i][j]=='*'));
  56.   if (odwiedzone.find(syt)!=odwiedzone.end()) return 0;
  57.   odwiedzone.insert(syt);
  58.  
  59.   int reward = 1;
  60.   if (labirynt[i][j]=='#') {
  61.     if (gumowe_sciany) {
  62.       df ^= 2; // odbij
  63.       reward = -2; // -2: nadal bardziej oplaca sie nie wchodzic w sciany XD; -1: bez różnicy
  64.       return reward + test(genom, i+di[df], j+dj[df], df);
  65.     }
  66.     return 0;
  67.   }
  68.  
  69.   df ^= 2;
  70.   int sytuac=(labirynt[i][j]=='*')*2 + (labirynt[i+di[df&3]][j+dj[df&3]]=='*');
  71.   for (int d=1; d<4; d++) {
  72. #ifdef KUPA
  73.     sytuac *= 3;
  74. #else
  75.     sytuac *= 2;
  76. #endif
  77.     switch (labirynt[i+di[(d+df)&3]][j+dj[(d+df)&3]]) {
  78.       case ' ':
  79.     sytuac += 0;
  80.     break;
  81.       case '#':
  82.     sytuac += 1;
  83.     break;
  84.       case '*':
  85.     sytuac += 2;
  86.     break;
  87.       default:
  88.     break;
  89.     }
  90.   }
  91.  
  92.   sytuac = genom[sytuac]-'0';
  93.   sytuac += sytuac&4;
  94.   df += sytuac;
  95.   df &= 11;
  96.   df |= (df&8)>>1;
  97.   if (labirynt[i][j] == '-' || labirynt[i][j] == '*')
  98.     labirynt[i][j] = '+';
  99.   else if (labirynt[i][j] == '+')
  100.     labirynt[i][j] = '%';
  101.   else if (labirynt[i][j] == '%')
  102.     labirynt[i][j] = '&';
  103.   else {
  104.     labirynt[i][j] = '-';
  105.     reward += 60;
  106.   }
  107.   if (df&4)
  108.     labirynt[i][j] = '*';
  109.   return reward+test(genom, i+di[df&3], j+dj[df&3], df&7);
  110. }
  111.  
  112. char *oldgenome, *newgenome;
  113.  
  114. #ifdef KUPA
  115. const int genomesiz = 2*2 * 3*3*3;
  116. #else
  117. const int genomesiz = 2*2 * 2*2*2;
  118. #endif
  119.  
  120. int main(int argc, char* argv[]) {
  121.   oldgenome = (char*)malloc(genomesiz+3);
  122.   newgenome = (char*)malloc(genomesiz+3);
  123.   int maxfitness=1, fitness=0, newfitness, i, j, d, ile=0, last_ile=0;
  124.   srand(33);
  125.  
  126.   if (argc==2) {
  127.     ifstream ifile(argv[1]);
  128.     if (!ifile)
  129.       return!puts("Nie można otworzyć pliku");
  130.     else {
  131.       ifile.getline(oldgenome, genomesiz+3);
  132.       i=0;
  133.       while (i < sizeof(labirynt)/sizeof(*labirynt)-1 && !ifile.eof()) {
  134.     ifile.getline(newgenome, genomesiz+3);
  135.     if (newgenome[0]=='#')
  136.       strcpy(labirynt[i++], newgenome);
  137.       }
  138.     }
  139.     for (i=0; oldgenome[i]>=' '; i++);
  140.   }
  141.   else
  142.     for (i=0; i<genomesiz; i++)
  143.       oldgenome[i] = '0';
  144.   strcpy(oldgenome+i, "\r\n");
  145.  
  146.   // calculate max fitness and fix the box here
  147.   for (i=0; i<sizeof(labirynt)/sizeof(*labirynt); i++) {
  148.     for (j=0; labirynt[i][j]>=' '; j++)
  149.       if (labirynt[i][j] == ' ') {
  150.     for (d=0; d<4; d++)
  151.       if (labirynt[i+di[d]][j+dj[d]] == ' ')
  152.         maxfitness++;
  153.     maxfitness += 60;
  154.       }
  155.     strcpy(labirynt[i]+j, "\r\n");
  156.   }
  157.   // end: calculate max fitness
  158.  
  159.   for (int iter=0; fitness<maxfitness; iter++,last_ile++) {
  160. #ifdef __WIN32
  161.     system("cls");
  162. #else
  163.     fputs("\x1b[H\x1b[J",stdout);
  164. #endif
  165.     // XXX kinda cheating here:
  166.     if (last_ile>200) { muta_rate = rand()%(20-2) + 1; last_ile = 0; }
  167.  
  168.     strcpy(newgenome, oldgenome);
  169.  
  170.     // mutation here
  171.     if (iter) // ensure we do not modify initial genome
  172.       for (i=0; i<genomesiz; i++)
  173.     if (rand()%100 < muta_rate)
  174.       newgenome[i] = '0' + rand()%4; // %8 dla robienia kupy
  175.     // end: mutation
  176.  
  177.     odwiedzone.clear();
  178.     newfitness = test(newgenome, 1, 1, 0);
  179.     if (newfitness > fitness) {
  180.       ile++;
  181.       swap(oldgenome, newgenome);
  182.       fitness = newfitness;
  183.       last_ile = 0;
  184.     }
  185.  
  186.     printf("iteracja %d (%d mutacji ok, fitness=%d/%d[%d%%]); ostatnio=%d; muta_rate=%d%%\n",
  187.        iter, ile, fitness, maxfitness, fitness*100/maxfitness, last_ile, muta_rate);
  188.     for (i=0; i<sizeof(labirynt)/sizeof(*labirynt); i++) {
  189.       fputs(labirynt[i], stdout);
  190.       for (j=0; labirynt[i][j]; j++)
  191.     if (labirynt[i][j] != '#' && labirynt[i][j] > ' ')
  192.       labirynt[i][j] = ' ';
  193.     }
  194.     printf("\n%s%s", oldgenome, newgenome);
  195.     Sleep(200);
  196.   }
  197.   return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment