dooooooduo

CI_ASS5_Reinforcement Learning_C

Feb 22nd, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include "rl_homework_lib.h"
  2.  
  3. // Q1
  4. State getNextState(State s_t, Action a_t) {
  5.     if      (a_t == DOWN  && s_t.y != world_height - 2) s_t.y += 1;
  6.     else if (a_t == UP    && s_t.y != 1               ) s_t.y -= 1;
  7.     else if (a_t == RIGHT && s_t.x != world_width  - 2) s_t.x += 1;
  8.     else if (a_t == LEFT  && s_t.x != 1               ) s_t.x -= 1;
  9.     return s_t;
  10. }
  11.  
  12. float getNextReward(State s_t) {
  13.     if (s_t.y == world_height - 2) {
  14.         if (s_t.x >= 2 && s_t.x <= world_width - 3)
  15.             return dropOffPenalty;
  16.         else if (s_t.x == world_width - 2)
  17.             return goalReward;
  18.     }
  19.     return stepPenalty;
  20. }
  21.  
  22. int isTerminalState(State s_t) {
  23.     return s_t.x >= 2 && s_t.y == world_height - 2;
  24. }
  25.  
  26.  
  27. // Q2
  28. Action getMax (float QValues[], State s_t) {
  29.     int i;
  30.     float maxQValue    = -9999999.0;
  31.     int   maxDirection = 0;
  32.     for (i = 0; i < 4; i++) {
  33.         float qvalue = QValues[stateAction2idx(s_t, (Action) i)];
  34.         if (maxQValue >= qvalue) continue;
  35.         maxQValue    = qvalue;
  36.         maxDirection = i;
  37.     }
  38.     return (Action) maxDirection;
  39. }
  40.  
  41. Action getNextAction (Agent* a, State s_t) {
  42.     return ((float) rand() / (float) RAND_MAX) >= a->epsilon ? getGreedyAction(a, s_t) : getRandomAction();
  43. }
  44.  
  45. Action getRandomAction () {
  46.     return (Action) (rand() % 4);
  47. }
  48.  
  49. Action getGreedyAction(Agent* a, State s_t) {
  50.     return getMax(a->QValues, s_t);
  51. }
  52.  
  53.  
  54. // Q3 & Q4
  55. State currentState;
  56. Action nextAction;
  57.  
  58. Agent* trainAgent(int nIter, int verbose, Agent* a) {
  59.     int i;
  60.     float accumReward = 0;
  61.  
  62.     for (i = 0; i < nIter; i++) {
  63.         // initialize the current state to left bottom and
  64.         // the accumulated reward for current iteration
  65.         currentState   = (State) { .x = 1, .y = world_height - 2 };
  66.         a->accumReward = 0;
  67.  
  68.         // start the episode
  69.         playEpisode(0, verbose, a);
  70.  
  71.         // now add the accumalted reward to the total space
  72.         accumReward += a->accumReward;
  73.     }
  74.  
  75.     // calculate the average reward use (total / n)
  76.     a->avgReward = accumReward / (float) nIter;
  77.  
  78.     return a;
  79. }
  80.  
  81. Agent* playEpisode(int nEpisode, int verbose, Agent* a) {
  82.     // for SARSA, only generate an action in updating process
  83.     if ((a->type == SARSA && nEpisode == 0) || a->type == QLearning)
  84.         nextAction = getNextAction(a, currentState);
  85.  
  86.     // retrieve all the essential data
  87.     State nextState   = getNextState(currentState, nextAction);
  88.     int isGoal        = nextState.x == world_width - 2 && nextState.y == world_height - 2;
  89.     int isTerminal    = isTerminalState(nextState);
  90.     float reward      = isGoal ? goalReward : (isTerminal ? dropOffPenalty : stepPenalty);
  91.  
  92.     // here we accumaltes the local reward for current iteration
  93.     a->accumReward   += reward;
  94.  
  95.     // now applying the updating rule according to the training type
  96.     if   (a->type == SARSA) sarsaUpdate    (a, currentState, nextState, nextAction, reward);
  97.     else                    qLearningUpdate(a, currentState, nextState, nextAction, reward);
  98.  
  99.     // save the current state
  100.     currentState = nextState;
  101.  
  102.     return isTerminal ? a : playEpisode(++nEpisode, verbose, a);
  103. }
  104.  
  105. // SARSA: Q(s_t1, a_t1) = Q(s_t1, a_t1) + alpha * (r_t + gamma * Q(s_t2, a_t2)) - Q(s_t1, a_t1))
  106. Agent* sarsaUpdate(Agent* a, State s_t1, State s_t2, Action a_t1, float r_t) {
  107.     int index          = stateAction2idx(s_t1, a_t1);
  108.     Action a_t2        = getNextAction(a, s_t2);
  109.     a->QValues[index] += a->alpha * (r_t + a->gamma * a->QValues[stateAction2idx(s_t2, a_t2)] - a->QValues[index]);
  110.     nextAction         = a_t2;
  111.     return a;
  112. }
  113.  
  114. // QLearning: Q(s_t1, a_t1) = r_t + gamma * MAX(Q(s_t2, A))
  115. Agent* qLearningUpdate(Agent* a, State s_t1, State s_t2, Action a_t1, float r_t) {
  116.     int i;
  117.     float maxQValue  = -9999999.0;
  118.     int maxDirection = 0;
  119.     for (i = 0; i < 4; i++) {
  120.         float qValue = a->QValues[stateAction2idx(s_t2, (Action) i)];
  121.         if (qValue <= maxQValue) continue;
  122.         maxQValue    = qValue;
  123.         maxDirection = i;
  124.     }
  125.     a->QValues[stateAction2idx(s_t1, a_t1)] = r_t + a->gamma * a->QValues[stateAction2idx(s_t2, (Action) maxDirection)];
  126.     return a;
  127. }
Add Comment
Please, Sign In to add comment