Advertisement
thibthibaut

Egreedy

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