Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 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='SimpleExtractor', **args):
  10.         self.featExtractor = lookup(extractor, globals())()
  11.         PacmanQAgent.__init__(self, **args)
  12.         # self.weights = CustomCounter()
  13.         # self.weights = {'eats-food': 302.5172688564452, 'closest-food': -42.752010792473826, 'bias': 89.99750164854542, '#-of-ghosts-1-step-away': -3503.8012028767084}
  14.         self.weights = {'bias': -277096.33365697827, '#-of-ghosts-1-step-away': -104598.07945904322, 'eats-food': 399786.0660682465, 'closest-food': -365.4721024438045}
  15.     def getWeights(self):
  16.         return self.weights
  17.  
  18.     def getQValue(self, state, action):
  19.         """
  20.          Should return Q(state,action) = w * featureVector
  21.          where * is the dotProduct operator
  22.        """
  23.         "*** YOUR CODE HERE ***"
  24.         features = self.featExtractor.getFeatures(state, action)
  25.         result = 0
  26.         for feature in features:
  27.             result += self.weights[feature] * features[feature]
  28.         return result
  29.  
  30.     def update(self, state, action, nextState, reward):
  31.         """
  32.           Should update your weights based on transition
  33.        """
  34.         "*** YOUR CODE HERE ***"
  35.         features = self.featExtractor.getFeatures(state, action)
  36.         correction = reward + self.discount * self.getValue(nextState) - self.getQValue(state, action)
  37.         for feature in features:
  38.             self.weights[feature] += self.alpha * correction * features[feature]
  39.  
  40.     def final(self, state):
  41.         "Called at the end of each game."
  42.         # call the super-class final method
  43.         PacmanQAgent.final(self, state)
  44.  
  45.         # did we finish training?
  46.         if self.episodesSoFar == self.numTraining:
  47.             # you might want to print your weights here for debugging
  48.             "*** YOUR CODE HERE ***"
  49.             print(self.weights)
  50.             pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement