Guest User

Sokoban AI debugging

a guest
Sep 8th, 2020
501
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. You are going to fix a program that combines pattern learning with
  3. intelligent decision making. Having already implemented the Minimax
  4. algorithm with simple recursion, it turned out that it was not enough.
  5. The many unknown factors in the environment always require a heuristic
  6. for guessing probabilities by recognizing patterns and continuing them.
  7. Minimax is too rigid.
  8. The scheme of an randomly driven evolutionary algorithm appears appropriate for
  9. evaluating and extending input information and thus recognizing redundancy and figuring
  10. out likely future events. As you have probably noticed during your lifespan,
  11. a thought subjectively always consists of comparing two simultaneously perceived
  12. mental pictures with a certain criteria or 'operator' and taking them for relevant when there is
  13. congruency. Emoticons might represent emotions.
  14. Variation and recombination will generate new images to be compared and
  15. evaluated. The idea is now to inhibit or trigger actuators by linking them
  16. with certain sorts of images. With sometimes given input, like from a digital camera, the
  17. program should be able to condition itself so a promising Minimax tree will evolve by
  18. itself and will likely be carried out.
  19.  
  20. You not only believe in yourself, you know. You are determined, have some nerves,
  21. concentrate and simply try to imagine the truth.
  22. You see things from different angles and are aware that you are a victim of causality.
  23.  
  24. The problem is choice.
  25. Your task is to work out data structures that represent single mental images (probably mainly
  26. seven letter-sized bitmaps), operators for comparing them, and also define data structures for temporal connection matching between the images so the flow of the train of thoughts can be emulated over time ( ask yourself: how does putting things in sequence work, for instance).
  27. Furthermore, you have to adjust all parameters so the computer will get a demanded task done practically.
  28.  
  29. You could rotate pictures with a trigonometric formula, do a frequency transform by echoing and this way amplyfing the tone to be detected, do an unsharp masking for sharpening contrasts,
  30. do a central projection by dividing x and y through z, calculate the absolute sum of pixel differences for fuzzy logic picture similarity, translocate pictures with movement vectors before comparing... See what is relevant in your perceptions.
  31.  
  32. Just get the buggy program below working and then you're in the know.
  33. Hope and pray that the theory is true.
  34. It would be nice to see it work.
  35. */
  36.  
  37. #define NUM_BOXES 4
  38.  
  39.  
  40.   int player[2];
  41.    
  42.  unsigned char field[20][20],collide[80][80];
  43.  
  44.  
  45.   enum
  46.    move
  47.   {
  48.     left,
  49.     right,
  50.     up,
  51.     down,
  52.     none
  53.   }direc,queuemov;
  54.  
  55.  
  56. #define NUM_THOUGHTS 50
  57.  
  58. #define NUM_THOUGHTSTEPS 50
  59.  
  60. typedef
  61. struct
  62. {
  63.  unsigned char pic[16];
  64.  
  65.  int x,y;
  66.  
  67.  unsigned int ttrigger[2];
  68.  unsigned long int tcontext;
  69.  enum move impulse;
  70.  int paralyze;
  71.  
  72.  float eval;   
  73.    
  74. } Memory;
  75.  
  76. Memory Memories[NUM_THOUGHTS];
  77.  
  78. void combine()
  79. {
  80.    int n=0;
  81.   int n2=0;
  82.  
  83.   while ( n2 < 10)
  84.   {
  85.     // Kreuzung mit mehr als Zweien dasselbe?!
  86.     // selektieren wir NICHT je Individuum immer dieselben Chromosomen?!
  87.       // vorne haeufiger rekombinieren?!
  88.    Memories[NUM_THOUGHTS-1-n2].eval=1;
  89.    Memories[NUM_THOUGHTS-1-n2].impulse=Memories[rand()%NUM_THOUGHTS].impulse;
  90.    Memories[NUM_THOUGHTS-1-n2].paralyze=Memories[rand()%NUM_THOUGHTS].paralyze;
  91.    Memories[NUM_THOUGHTS-1-n2].tcontext=Memories[rand()%NUM_THOUGHTS].tcontext;
  92.    Memories[NUM_THOUGHTS-1-n2].x=Memories[rand()%NUM_THOUGHTS].x;
  93.    Memories[NUM_THOUGHTS-1-n2].y=Memories[rand()%NUM_THOUGHTS].y;
  94.  
  95.    int memselection;
  96.  
  97.    memselection =rand()%NUM_THOUGHTS;
  98.    
  99.    n=0;
  100.    while ( n < 16)Memories[NUM_THOUGHTS-1-n2].pic[n]=Memories[memselection].pic[n],n++;
  101.    
  102.    memselection =rand()%NUM_THOUGHTS;
  103.    
  104.    n=0;
  105.    while ( n < 4)Memories[NUM_THOUGHTS-1-n2].ttrigger[n]=Memories[memselection].ttrigger[n],n++;
  106.      
  107.    n2++;
  108.   }
  109.  
  110.    
  111. }
  112.  
  113.  
  114. void mutate()
  115. {
  116.  int n=0;
  117.   int n2=0;
  118.  
  119.   while ( n2 < 10)
  120.   {
  121.    
  122.  Memories[NUM_THOUGHTS-1-n2].eval=1,
  123.  
  124.   Memories[NUM_THOUGHTS-1-n2].impulse=rand()%5;
  125.   Memories[NUM_THOUGHTS-1-n2].paralyze=rand()%2;
  126.   Memories[NUM_THOUGHTS-1-n2].tcontext=rand();
  127.   Memories[NUM_THOUGHTS-1-n2].x=rand()%78+1;
  128.   Memories[NUM_THOUGHTS-1-n2].y=rand()%78+1;
  129.      
  130.    n=0;
  131.    while ( n < 16)Memories[NUM_THOUGHTS-1-n2].pic[n]=rand()%4,n++;
  132.    
  133.    n=0;
  134.    while ( n < 4)Memories[NUM_THOUGHTS-1-n2].ttrigger[n]=rand()%7,n++;
  135.    
  136.     n2++;
  137.   }
  138.    
  139. }
  140.  
  141.  
  142. int frontallap()
  143. {
  144.  int n=0;
  145.  signed int sum=0;
  146.  
  147.  while ( n < NUM_THOUGHTS) n++, (rand()%2==0? sum++ : 0 ); 
  148.  
  149.  sum-=NUM_THOUGHTS/2;
  150.  if ( sum < 0 ) sum=0;
  151.  
  152.  return sum;
  153.  
  154. }
  155.  
  156.  
  157. void insert_cut(int funcnr)
  158. {
  159.  int n=0, n2=0,n3=0;
  160.  
  161.  Memory buf;
  162.  unsigned char bufpic, bufttrigger;
  163.  
  164.  
  165.  
  166.  buf=Memories[funcnr];
  167.  n=0;
  168.  while ( n < 16) buf.pic[n]=Memories[funcnr].pic[n],n++;
  169.  n=0;
  170.  while ( n < 2) buf.ttrigger[n]=Memories[funcnr].ttrigger[n],n++;
  171.  
  172.  
  173.  float funceval;
  174.  
  175.  funceval=Memories[funcnr].eval;
  176.    
  177.      
  178.  while ( Memories[n3].eval >= funceval && n3 < NUM_THOUGHTS ) n3++;
  179.  
  180.  if ( n3 >= NUM_THOUGHTS) return;
  181.  if ( n3 >= funcnr) return;
  182.  n2=funcnr;
  183.  
  184.  while ( n2 > n3)
  185.  {
  186.     Memories[n2]=Memories[n2-1];
  187.    
  188.    n=0;
  189.    while ( n < 16) Memories[n2].pic[n]=Memories[n2-1].pic[n],n++;
  190.    n=0;
  191.    while ( n < 2) Memories[n2].ttrigger[n]=Memories[n2-1].ttrigger[n],n++;
  192.  
  193.     n2--;
  194.    
  195.  }
  196.  
  197.  
  198.  Memories[n3]=buf;
  199.  n=0;
  200.  while ( n < 16) Memories[n3].pic[n]=buf.pic[n],n++;
  201.  n=0;
  202.  while ( n < 2) Memories[n3].ttrigger[n]=buf.ttrigger[n],n++;
  203.  
  204.  
  205. }
  206.  
  207. int evomove(void) // this is the deciding function you can experiment with, aside from a few evo parameters
  208. {
  209.     int runs=0;
  210.     int funcnr,cmpfunc;
  211.    
  212.    
  213.   int n=0;
  214.  
  215.   while ( runs < NUM_THOUGHTSTEPS)
  216.   {
  217.         int x,y;
  218.  
  219.       cmpfunc=frontallap();
  220.     funcnr=rand()%NUM_THOUGHTS;
  221.    
  222.      if ( rand()%1==0){
  223.        
  224.         int selplus=0;
  225.         while ( selplus < 5 && n < NUM_THOUGHTS/2)
  226.         {
  227.             selplus=0;
  228.          funcnr=rand()%NUM_THOUGHTS;
  229.         y=0;
  230.         while ( y < 4)
  231.         {
  232.            x=0;
  233.            while ( x < 4)
  234.            {
  235.            if ( field[Memories[funcnr].x/4+x/4][Memories[funcnr].y/4+y/4]==Memories[funcnr].pic[x+y*16]||
  236.               collide[Memories[funcnr].x+x][Memories[funcnr].y+y]==Memories[funcnr].pic[x+y*16])
  237.          selplus++;
  238.             x++;
  239.            }
  240.            y++;
  241.           }
  242.          
  243.           n++;
  244.         }
  245.        
  246.      }
  247.  
  248.     n=0;
  249.     while ( n < NUM_THOUGHTS)(Memories[n].eval > 1 ? Memories[n].eval/=1.1: 0 ), n++;
  250.    
  251.    
  252.     if ( rand()%4==0)mutate();
  253.     if ( rand()%4==0)combine();
  254.    
  255.     int x_segment, y_segment, size;
  256.    
  257.    
  258.     x_segment=rand()%76+2;
  259.     y_segment=rand()%76+2;
  260.    
  261.     if ( rand()%4<3)
  262.     {
  263.      x_segment=Memories[funcnr].x-1, y_segment=Memories[funcnr].y-1;   
  264.       }
  265.    
  266.     size=rand()%4;
  267.     if ( rand()%4==0)x_segment=Memories[funcnr].x, y_segment=Memories[funcnr].y;
  268.        
  269.      unsigned char picbuf_sel[16],picbuf_cmp[256];
  270.    
  271.     signed int x_off,y_off;
  272.     x_off=0, y_off=0;
  273.    
  274.     if ( Memories[funcnr].paralyze > 0 )
  275.     if ( rand()%4==0)
  276.     {
  277.      if ( Memories[funcnr].impulse==right) x_off=1;
  278.      if ( Memories[funcnr].impulse==left) x_off=-1;
  279.      if ( Memories[funcnr].impulse==up) y_off=-1;
  280.      if ( Memories[funcnr].impulse==down) y_off=1;
  281.     }
  282.    
  283.     y=0;
  284.    
  285.     while ( y < 16)
  286.     {
  287.         x=0;
  288.         while ( x < 16)
  289.         {
  290.            picbuf_cmp[x+y*16]=0;
  291.          x++;
  292.         }
  293.        y++;
  294.       }
  295.    
  296.    
  297.       y=0;
  298.    
  299.     while ( y < 4)
  300.     {
  301.         x=0;
  302.         while ( x < 4)
  303.         {
  304.          
  305.             if ( field[(x_off*4+x_segment+x)/4][(y_off*4+y_segment+y)/4]=='0'&&collide[x_segment+x][y_segment+y]>=2)picbuf_cmp[x+4+(y+4)*4]=3;
  306.              else
  307.             if ( collide[x_segment+x][y_segment+y]>=2)
  308.              picbuf_cmp[x+4+(y+4)*4]=2;
  309.              else
  310.               if ( x_segment+x+1==player[0]&&y_segment+y+1==player[1])
  311.            picbuf_cmp[x+4+(y+4)*4]=1;
  312.    
  313.      
  314.          picbuf_sel[x+y*4]=picbuf_cmp[x+4+x_off+(y+4+y_off)*16];   
  315.    
  316.          
  317.            x++;
  318.         }
  319.        y++;
  320.       }
  321.      
  322.      y=0;
  323.      if ( rand()%3==0)
  324.      while ( y < 4)
  325.      {
  326.        x=0;
  327.        while ( x < 4)
  328.        {
  329.         picbuf_cmp[x+4-x_off+y+(4-y_off)*16]=Memories[cmpfunc].pic[x+y*4];
  330.         if ( Memories[funcnr].paralyze==1)
  331.            picbuf_sel[x+y*4]=picbuf_cmp[x+x_off+4+(y+y_off+4)*16]; 
  332.          else picbuf_sel[x+y*4]=picbuf_cmp[x+4+(y+4)*16];
  333.    
  334.         x++;   
  335.        }   
  336.        y++;
  337.      }
  338.      
  339.        
  340.     y=0;
  341.     if ( rand()%2==0)
  342.     while ( y < 4)
  343.     {
  344.         x=0;
  345.         while ( x < 4)
  346.         {
  347.          if ( //(field[x_segment/4+x/4][y_segment/4+y/4]=='0'||collide[x_segment+x][y_segment+y]>=2)&&
  348.               picbuf_sel[x+y*4]==3 ) Memories[funcnr].eval+=140;
  349.         x++;
  350.         }
  351.        y++;
  352.       }
  353.      
  354.      
  355.      
  356.     y=0;
  357.     if ( rand()%4==0)
  358.     while ( y < 4)
  359.     {
  360.         x=0;
  361.         while ( x < 4)
  362.         {
  363.           if ( picbuf_sel[x+y*4]==Memories[cmpfunc].pic[x+y*4] ) Memories[funcnr].eval+=40;
  364.          x++;
  365.         }
  366.        y++;
  367.       }
  368.      
  369.       if ( collide[Memories[funcnr].x][Memories[funcnr].y]>=2) Memories[funcnr].eval+=40;
  370.    if ( collide[Memories[funcnr].x+3][Memories[funcnr].y]>=2) Memories[funcnr].eval+=40;
  371.    if ( collide[Memories[funcnr].x][Memories[funcnr].y-1]>=2) Memories[funcnr].eval+=40;
  372.    if ( collide[Memories[funcnr].x][Memories[funcnr].y+3]>=2) Memories[funcnr].eval+=40;
  373.  
  374.       if ( Memories[funcnr].x==Memories[cmpfunc].x&&Memories[funcnr].y==Memories[cmpfunc].y) Memories[funcnr].eval+=4;
  375.  
  376.        if ( rand()%2==0)if ( Memories[funcnr].x+1==player[0]&&Memories[funcnr].y+1==player[1]) Memories[funcnr].eval+=40;
  377.    
  378.    Memories[funcnr].eval+=32;    
  379.     y=0;
  380.     if ( rand()%4==0)
  381.     while ( y < 4)
  382.     {
  383.         x=0;
  384.         while ( x < 4)
  385.         {
  386.           if ( field[x_segment/4+x][y_segment/4+y]!='3'&&
  387.               (field[x_segment/4+x][y_segment/4+y]=='X'||collide[x_segment+x][y_segment+y]>=2)&&
  388.              (picbuf_sel[x+y*4]==2||picbuf_sel[x+y*4]==1) &&Memories[funcnr].eval > 1) Memories[funcnr].eval-=2;
  389.          x++;
  390.         }
  391.        y++;
  392.       }
  393.  
  394.  
  395.     y=0;
  396.     if ( rand()%4==0)
  397.     while ( y < 4)
  398.     {
  399.         x=0;
  400.         while ( x < 4)
  401.         {
  402.           if ( Memories[cmpfunc].pic[x+y*4]==Memories[funcnr].pic[x+y*4]) Memories[funcnr].eval+=10;
  403.          x++;
  404.         }
  405.        y++;
  406.       }
  407.  
  408.      
  409.       if ( rand()%4==0) if (  Memories[funcnr].tcontext==Memories[cmpfunc].tcontext&&
  410.                               Memories[funcnr].ttrigger[1]==(Memories[cmpfunc].ttrigger[0]+1)%7 ) Memories[funcnr].eval++;
  411.      
  412.       if ( rand()%4==0) if (  Memories[funcnr].tcontext==runs/7&&
  413.                               Memories[funcnr].ttrigger[1]+1==(runs+1)%7 ) Memories[funcnr].eval++;
  414.      
  415.      
  416.    
  417.     insert_cut(funcnr);
  418.     runs++;
  419.    
  420.   }
  421.  
  422.   int sel=rand()%10;
  423.  
  424.   if ( Memories[sel].paralyze > 0) return Memories[sel].impulse;
  425.    
  426.     return none;
  427.    
  428.    
  429. }  
  430.  
  431.  
  432.  
  433. int main(void)
  434. {
  435.   unsigned char screen[80][80];
  436.  
  437.   struct
  438.   {
  439.    int x,y;
  440.   }boxes[NUM_BOXES];
  441.  
  442.   unsigned char level[400]="XXXXXXXXXXXXXXXXXXXX"
  443.                            "X                  X"
  444.                            "X           0      X"
  445.                            "X  0               X"
  446.                            "X               0  X"
  447.                            "X                  X"
  448.                            "X     0            X"
  449.                            "X                  X"
  450.                            "X                  X"
  451.                            "X     0      0     X"
  452.                            "X                  X"
  453.                            "X    0             X"
  454.                            "X                  X"
  455.                            "X           0      X"
  456.                            "X                  X"
  457.                            "XXXXXXXXXXXXXXXXXXXX"
  458.                            "X                  X"
  459.                            "X                  X"
  460.                            "X                  X"
  461.                            "XXXXXXXXXXXXXXXXXXXX";
  462.    
  463.   boxes[0].x=5*4, boxes[0].y=2*4;
  464.   boxes[1].x=7*4, boxes[1].y=3*4;
  465.   boxes[2].x=9*4, boxes[2].y=2*4;
  466.   boxes[3].x=14*4, boxes[3].y=7*4;
  467.   boxes[4].x=18*4, boxes[4].y=1*4;
  468.  
  469.       player[0]=10*4, player[1]=1*4;
  470.  
  471.  
  472.  
  473.    int x,y;
  474.    y=0;
  475.    while ( y < 20)
  476.    {
  477.     x=0;
  478.     while ( x < 20)
  479.     {
  480.      field[x][y]=level[x+y*20];
  481.      x++;
  482.     }
  483.     y++;
  484.    }
  485.  
  486.  
  487.  
  488.   while (1)
  489.   {
  490.    
  491.    direc=none;
  492.    
  493.    if ( kbhit())
  494.    {
  495.      switch(getch())
  496.      {
  497.       case 'h': direc=left;
  498.                  break;
  499.       case 'k': direc=right;
  500.                  break;
  501.       case 'u': direc=up;
  502.                  break;
  503.      case 'j': direc=down;
  504.                  break;
  505.      } 
  506.    
  507.    
  508.    }
  509.    
  510.    direc=evomove();
  511.    
  512.    int x,y;
  513.    
  514.    y=0;
  515.    while ( y < 80)
  516.    {
  517.     x=0;
  518.     while ( x < 80)
  519.     {
  520.      screen[x][y]=field[x/4][y/4];
  521.      collide[x][y]=( field[x/4][y/4]=='X' ? 1 : 0 );
  522.      x++;
  523.     }
  524.     y++;
  525.    }
  526.    
  527.    int n;
  528.    n=0;
  529.    while ( n < NUM_BOXES)
  530.    {
  531.      collide[boxes[n].x][boxes[n].y]=2+n;
  532.     collide[boxes[n].x+1][boxes[n].y]=2+n;
  533.     collide[boxes[n].x][boxes[n].y+1]=2+n;
  534.     collide[boxes[n].x+1][boxes[n].y+1]=2+n;
  535.  
  536.     screen[boxes[n].x][boxes[n].y]=219;
  537.     screen[boxes[n].x+1][boxes[n].y]=219;
  538.     screen[boxes[n].x][boxes[n].y+1]=219;
  539.     screen[boxes[n].x+1][boxes[n].y+1]=219;
  540.     n++;
  541.    }
  542.    
  543.    screen[player[0]][player[1]]=177;
  544.    screen[player[0]+1][player[1]]=177;
  545.    screen[player[0]][player[1]+1]=177;
  546.     screen[player[0]+1][player[1]+1]=177;
  547.    
  548.    
  549.    queuemov=none;
  550.    if ( direc==left)
  551.    {
  552.     if ( collide[player[0]-1][player[1]]==0&&collide[player[0]-1][player[1]+1]==0 ) player[0]--;
  553.     else
  554.      if ( collide[player[0]-1][player[1]]>=2 &&
  555.            collide[player[0]-1][player[1]]==collide[player[0]-1][player[1]+1] )
  556.      {
  557.        if ( collide[player[0]-3][player[1]]==0&&collide[player[0]-3][player[1]+1]==0)
  558.        {
  559.          boxes[collide[player[0]-1][player[1]]-2].x--;
  560.         queuemov=left;
  561.        }
  562.      }
  563.    
  564.    }
  565.    
  566.    
  567.    if ( direc==up)
  568.    {
  569.     if ( collide[player[0]][player[1]-1]==0&&collide[player[0]+1][player[1]-1]==0 ) player[1]--;
  570.     else
  571.      if ( collide[player[0]][player[1]-1]>=2&&
  572.            collide[player[0]][player[1]-1]==collide[player[0]+1][player[1]-1] )
  573.      {
  574.        if ( collide[player[0]][player[1]-3]==0&&collide[player[0]+1][player[1]-3]==0)
  575.        {
  576.          boxes[collide[player[0]][player[1]-1]-2].y--;
  577.         queuemov=up;
  578.        }
  579.      }
  580.    
  581.    }
  582.    
  583.    
  584.    if ( direc==down)
  585.    {
  586.     if ( collide[player[0]][player[1]+2]==0&&collide[player[0]+1][player[1]+2]==0 ) player[1]++;
  587.     else
  588.      if ( collide[player[0]][player[1]+2]>=2&&
  589.            collide[player[0]][player[1]+2]==collide[player[0]+1][player[1]+2] )
  590.      {
  591.        if ( collide[player[0]][player[1]+4]==0&&collide[player[0]+1][player[1]+4]==0)
  592.        {
  593.          boxes[collide[player[0]][player[1]+2]-2].y++;
  594.         queuemov=down;
  595.        }
  596.      }
  597.    
  598.    }
  599.    
  600.    
  601.    if ( direc==right)
  602.    {
  603.     if ( collide[player[0]+2][player[1]]==0&&collide[player[0]+2][player[1]+1]==0 ) player[0]++;
  604.     else
  605.      if ( collide[player[0]+2][player[1]]>=2 &&
  606.            collide[player[0]+2][player[1]]==collide[player[0]+2][player[1]+1])
  607.      {
  608.        if ( collide[player[0]+4][player[1]]==0&&collide[player[0]+4][player[1]+1]==0)
  609.        {
  610.          boxes[collide[player[0]+2][player[1]]-2].x++;
  611.         queuemov=right;
  612.        }
  613.      }
  614.    
  615.    }
  616.    
  617.    
  618.    
  619.    switch(queuemov)
  620.    {
  621.     case left: player[0]--;break;
  622.     case right: player[0]++;break;
  623.     case up: player[1]--;break;
  624.     case down: player[1]++;break;
  625.    }
  626.    
  627.    
  628.  
  629.    if ( rand()%1000==0) // turn the console resolution high
  630.    {
  631.    system("cls");
  632.    y=0;
  633.    while ( y < 80)
  634.    {
  635.     x=0;
  636.     while (x < 80)
  637.     {
  638.        
  639.       printf("%c%c%c%c%c%c%c%c",screen[x][y],screen[x+1][y],screen[x+2][y],screen[x+3][y],
  640.                                  screen[x+4][y],screen[x+5][y],screen[x+6][y],screen[x+7][y]); 
  641.        
  642. /*          
  643.         printf("%c%c%c%c",screen[x][y],screen[x+2][y],
  644.                                  screen[x+4][y],screen[x+6][y]);   
  645.   */      
  646.          x+=8;
  647.      }
  648.      printf("\n");
  649.    
  650.     y++;
  651.    }
  652.    
  653.    
  654.    }
  655.    
  656.    
  657.   }
  658.    
  659.    
  660.    
  661.    
  662.    
  663. }
  664.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×