Advertisement
thibthibaut

Untitled

Nov 25th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.15 KB | None | 0 0
  1. #QLEARNING
  2.  
  3. #ISIR IPMC
  4. Q = zeros(16,4);
  5.  
  6. g=0.2;
  7. nu = 0.4;
  8.  
  9. numberOfEpisodes = 100;
  10.  
  11.  
  12. trans = [ 2, 4, 5,13;
  13.  1, 3, 6,14;
  14.  4, 2, 7,15;
  15.  3, 1, 8,16;
  16.  6, 8, 1, 9;
  17.  5, 7, 2,10; 8, 6, 3,11; 7, 5, 4,12; 10,12,13, 5; 9,11,14, 6; 12,10,15, 7; 11, 9,16, 8; 14,16, 9, 1; 13,15,10, 2; 16,14,11, 3; 15,13,12, 4 ];
  18.  
  19. rew = [0,-1,0,-1;
  20.     0,0,0,-1;
  21.     0,0,0,-1;
  22.     0,-1,0,-1;
  23.     -1,-1,0,0;
  24.     0,0,0,0;
  25.     0,0,0,0;
  26.     0,1,0,0;
  27.     -1,-1,0,0;
  28.     0,0,0,0;
  29.     0,0,0,0;
  30.     0,4,0,0;
  31.     0,-1,0,-1;
  32.     0,0,0,1;
  33.     0,0,0,1;
  34.     -1,0,-1,0];
  35.  
  36.  
  37.  
  38.  
  39.  
  40. for n=1:numberOfEpisodes
  41.  
  42. s = ceil(rand*16); #Initial state
  43.  
  44.   for n=1:16
  45.  
  46.     a = ceil(rand*3); #choose random action
  47.     nextstate = trans(s, a); #Go to the next state
  48.    
  49.     [maxnext, dummy]= max( Q(nextstate,:) );
  50.    
  51.     #update Q
  52.     Q(s,a) = Q(s,a) + nu*( rew(s,a) + g*maxnext - Q(s,a) );
  53.    
  54.     s = nextstate;
  55.    
  56.  
  57.   end
  58.  
  59. end
  60.  
  61.  
  62. states = 1; #Choose random start value
  63.  
  64. [dummy, a] = max( Q(states,:) );
  65.  
  66. states = [states, trans(states,a)];
  67.  
  68. for r=2:16
  69.      
  70.    s = states(r);
  71.    
  72.   [dummy, a] = max( Q(s,:) );  
  73.  
  74.     states = [states, trans(s,a)];
  75.  
  76. end
  77.  
  78. walkshow(states)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement