Advertisement
Guest User

Untitled

a guest
May 27th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. class ApproximateQAgent(PacmanQAgent):
  2. """
  3. ApproximateQLearningAgent
  4.  
  5. You should only have to overwrite getQValue
  6. and update. All other QLearningAgent functions
  7. should work as is.
  8. """
  9. def __init__(self, extractor='IdentityExtractor', **args):
  10. self.featExtractor = util.lookup(extractor, globals())()
  11. PacmanQAgent.__init__(self, **args)
  12. self.weights = util.Counter()
  13.  
  14. def getWeights(self):
  15. return self.weights
  16.  
  17. def getQValue(self, state, action):
  18. """
  19. Should return Q(state,action) = w * featureVector
  20. where * is the dotProduct operator
  21. """
  22. "*** YOUR CODE HERE ***"
  23. Qsa = 0
  24. features = self.featExtractor.getFeatures(state, action)
  25. for key, value in features.items():
  26. Qsa += self.weights[key]*value
  27.  
  28. return Qsa
  29.  
  30. def update(self, state, action, nextState, reward):
  31. """
  32. Should update your weights based on transition
  33. """
  34. "*** YOUR CODE HERE ***"
  35. Qsa = self.getQValue(state, action)
  36. nextActions = self.getLegalActions(nextState)
  37. nextMax = []
  38. for actionNext in nextActions:
  39. nextMax.append(self.getQValue(nextState, actionNext))
  40. if nextMax:
  41. QNextSNextA = max(nextMax)
  42. else:
  43. QNextSNextA = 0.0
  44. diff = (reward + self.discount * QNextSNextA) - Qsa
  45. features = self.featExtractor.getFeatures(state, action)
  46.  
  47. for key, value in features.items():
  48. self.weights[key] += self.alpha * diff * value
  49.  
  50. def final(self, state):
  51. "Called at the end of each game."
  52. # call the super-class final method
  53. PacmanQAgent.final(self, state)
  54.  
  55. # did we finish training?
  56. if self.episodesSoFar == self.numTraining:
  57. # you might want to print your weights here for debugging
  58. "*** YOUR CODE HERE ***"
  59. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement