Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ApproximateQAgent(PacmanQAgent):
- """
- ApproximateQLearningAgent
- You should only have to overwrite getQValue
- and update. All other QLearningAgent functions
- should work as is.
- """
- def __init__(self, extractor='SimpleExtractor', **args):
- self.featExtractor = lookup(extractor, globals())()
- PacmanQAgent.__init__(self, **args)
- # self.weights = CustomCounter()
- # self.weights = {'eats-food': 302.5172688564452, 'closest-food': -42.752010792473826, 'bias': 89.99750164854542, '#-of-ghosts-1-step-away': -3503.8012028767084}
- self.weights = {'bias': -277096.33365697827, '#-of-ghosts-1-step-away': -104598.07945904322, 'eats-food': 399786.0660682465, 'closest-food': -365.4721024438045}
- def getWeights(self):
- return self.weights
- def getQValue(self, state, action):
- """
- Should return Q(state,action) = w * featureVector
- where * is the dotProduct operator
- """
- "*** YOUR CODE HERE ***"
- features = self.featExtractor.getFeatures(state, action)
- result = 0
- for feature in features:
- result += self.weights[feature] * features[feature]
- return result
- def update(self, state, action, nextState, reward):
- """
- Should update your weights based on transition
- """
- "*** YOUR CODE HERE ***"
- features = self.featExtractor.getFeatures(state, action)
- correction = reward + self.discount * self.getValue(nextState) - self.getQValue(state, action)
- for feature in features:
- self.weights[feature] += self.alpha * correction * features[feature]
- def final(self, state):
- "Called at the end of each game."
- # call the super-class final method
- PacmanQAgent.final(self, state)
- # did we finish training?
- if self.episodesSoFar == self.numTraining:
- # you might want to print your weights here for debugging
- "*** YOUR CODE HERE ***"
- print(self.weights)
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement