Skip to content

Instantly share code, notes, and snippets.

@rioleo
Created October 13, 2011 18:55
Show Gist options
  • Save rioleo/1285140 to your computer and use it in GitHub Desktop.
Save rioleo/1285140 to your computer and use it in GitHub Desktop.
221-ex1
def observe(self, observation, gameState):
"""
Updates beliefs based on the distance observation and Pacman's position.
The noisyDistance is the estimated manhattan distance to the ghost you are tracking.
The emissionModel below stores the probability of the noisyDistance for any true
distance you supply. That is, it stores P(noisyDistance | TrueDistance).
self.legalPositions is a list of the possible ghost positions (you
should only consider positions that are in self.legalPositions).
A correct implementation will handle the following special case:
* When a ghost is captured by Pacman, all beliefs should be updated so
that the ghost appears in its prison cell, position self.getJailPosition()
You can check if a ghost has been captured by Pacman by
checking if it has a noisyDistance of None (a noisy distance
of None will be returned if, and only if, the ghost is
captured).
"""
noisyDistance = observation
emissionModel = busters.getObservationDistribution(noisyDistance)
pacmanPosition = gameState.getPacmanPosition()
#print "Noisy Distance", noisyDistance
#print "Emission Model", emissionModel
#print "Pacman position", pacmanPosition
#print "Beliefs", self.beliefs
"*** YOUR CODE HERE ***"
# Replace this code with a correct observation update
# Be sure to handle the jail.
allPossible = util.Counter()
for p in self.legalPositions:
trueDistance = util.manhattanDistance(p, pacmanPosition)
if trueDistance == noisyDistance:
allPossible[p] = 1
else:
allPossible[p] = 0
#print allPossible
allPossible.normalize()
for p in self.legalPositions:
self.beliefs[p] += allPossible[p]
self.beliefs.normalize()
print "Max likely", max(self.beliefs.iteritems(), key=operator.itemgetter(1))[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment