Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1.  
  2. class QLearningAgent(ReinforcementAgent):
  3. """
  4. Q-Learning Agent
  5.  
  6. Functions you should fill in:
  7. - computeValueFromQValues
  8. - computeActionFromQValues
  9. - getQValue
  10. - getAction
  11. - update
  12.  
  13. Instance variables you have access to
  14. - self.epsilon (exploration prob)
  15. - self.alpha (learning rate)
  16. - self.discount (discount rate)
  17.  
  18. Functions you should use
  19. - self.getLegalActions(state)
  20. which returns legal actions for a state
  21. """
  22. def __init__(self, **args):
  23. "You can initialize Q-values here..."
  24. ReinforcementAgent.__init__(self, **args)
  25. self.qValues = util.Counter()
  26.  
  27.  
  28. "*** YOUR CODE HERE ***"
  29.  
  30. def getQValue(self, state, action):
  31. """
  32. Returns Q(state,action)
  33. Should return 0.0 if we have never seen a state
  34. or the Q node value otherwise
  35. """
  36. return self.qValues[(state, action)]
  37.  
  38.  
  39. def computeValueFromQValues(self, state):
  40. """
  41. Returns max_action Q(state,action)
  42. where the max is over legal actions. Note that if
  43. there are no legal actions, which is the case at the
  44. terminal state, you should return a value of 0.0.
  45. """
  46. "*** YOUR CODE HERE ***"
  47. possibleStateQValues = util.Counter()
  48. for action in self.getLegalActions(state):
  49. possibleStateQValues[action] = self.getQValue(state, action)
  50. if len(possibleStateQValues) > 0:
  51. return possibleStateQValues[possibleStateQValues.argMax()]
  52. return 0.0
  53.  
  54. def computeActionFromQValues(self, state):
  55. """
  56. Compute the best action to take in a state. Note that if there
  57. are no legal actions, which is the case at the terminal state,
  58. you should return None.
  59. """
  60. possibleStateQValues = util.Counter()
  61. for action in self.getLegalActions(state):
  62. possibleStateQValues[action] = self.getQValue(state, action)
  63. if len(possibleStateQValues) == 0:
  64. return None
  65. best_actions = []
  66. best_value = possibleStateQValues[possibleStateQValues.argMax()]
  67.  
  68. for action,value in possibleStateQValues.items():
  69. if (value == best_value):
  70. best_actions.append(action)
  71. "*** YOUR CODE HERE ***"
  72. return random.choice(best_actions)
  73.  
  74. def getAction(self, state):
  75. """
  76. Compute the action to take in the current state. With
  77. probability self.epsilon, we should take a random action and
  78. take the best policy action otherwise. Note that if there are
  79. no legal actions, which is the case at the terminal state, you
  80. should choose None as the action.
  81.  
  82. HINT: You might want to use util.flipCoin(prob)
  83. HINT: To pick randomly from a list, use random.choice(list)
  84. """
  85. # Pick Action
  86. legalActions = self.getLegalActions(state)
  87. action = None
  88.  
  89. if len(legalActions) > 0:
  90. if util.flipCoin(self.epsilon):
  91. action = random.choice(legalActions)
  92. else:
  93. action = self.getPolicy(state)
  94. return action
  95. "*** YOUR CODE HERE ***"
  96.  
  97. return action
  98.  
  99. def update(self, state, action, nextState, reward):
  100. """
  101. The parent class calls this to observe a
  102. state = action => nextState and reward transition.
  103. You should do your Q-Value update here
  104.  
  105. NOTE: You should never call this function,
  106. it will be called on your behalf
  107. """
  108. "*** YOUR CODE HERE ***"
  109. self.qValues[(state, action)] = self.getQValue(state, action) + self.alpha * (reward + self.discount * self.getValue(nextState) - self.getQValue(state,action))
  110.  
  111. util.raiseNotDefined()
  112.  
  113. def getPolicy(self, state):
  114. return self.computeActionFromQValues(state)
  115.  
  116. def getValue(self, state):
  117. return self.computeValueFromQValues(state)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement