Finish project 4.

This commit is contained in:
Felix Martin 2021-12-14 11:44:09 -05:00
parent 5f66554cdb
commit 2fd09bb7b8

View File

@ -478,18 +478,17 @@ class JointParticleFilter:
distribution, samples = [], [] distribution, samples = [], []
for particle in self.particles: for particle in self.particles:
weight = 1 # weight is likelihood over whole particle weight = 1.0 # weight is likelihood over whole particle
particle = tuple(particle)
for ghostIndex in range(self.numGhosts): for ghostIndex in range(self.numGhosts):
noisyDistance = noisyDistances[ghostIndex] if noisyDistances[ghostIndex] is None:
emissionModel = emissionModels[ghostIndex]
if noisyDistance is None:
# Ghost is in jail. # Ghost is in jail.
particle = self.getParticleWithGhostInJail(particle, ghostIndex) particle = self.getParticleWithGhostInJail(particle, ghostIndex)
else: else:
# Find probability and update weight. # Find probability and update weight.
ghostPosition = particle[ghostIndex] ghostPosition = particle[ghostIndex]
trueDistance = util.manhattanDistance(ghostPosition, pacmanPosition) trueDistance = util.manhattanDistance(ghostPosition, pacmanPosition)
probability = emissionModel[trueDistance] probability = emissionModels[ghostIndex][trueDistance]
weight = weight * probability weight = weight * probability
distribution.append(weight) distribution.append(weight)
samples.append(particle) samples.append(particle)
@ -497,14 +496,17 @@ class JointParticleFilter:
if not [p for p in distribution if p > 0.0]: if not [p for p in distribution if p > 0.0]:
# All probabilities are zero so we have to reinitialize. # All probabilities are zero so we have to reinitialize.
self.initializeParticles() self.initializeParticles()
newParticles = []
# But then make sure that we move jailed ghosts into jail. # But then make sure that we move jailed ghosts into jail.
for particle in self.particles: jailedGhostIndices = [i
for ghostIndex in range(self.numGhosts): for i, noisyDistance in enumerate(noisyDistances)
if noisyDistances[ghostIndex] is None: if noisyDistance is None]
if jailedGhostIndices:
newParticles = []
for particle in self.particles:
for ghostIndex in jailedGhostIndices:
particle = self.getParticleWithGhostInJail(particle, ghostIndex) particle = self.getParticleWithGhostInJail(particle, ghostIndex)
newParticles.append(particle) newParticles.append(particle)
self.particles = newParticles self.particles = newParticles
else: else:
self.particles = util.nSample(distribution, samples, self.numParticles) self.particles = util.nSample(distribution, samples, self.numParticles)
@ -563,12 +565,17 @@ class JointParticleFilter:
""" """
newParticles = [] newParticles = []
for oldParticle in self.particles: for oldParticle in self.particles:
prevGhostPositions = oldParticle
newParticle = list(oldParticle) # A list of ghost positions newParticle = list(oldParticle) # A list of ghost positions
# now loop through and update each entry in newParticle... # now loop through and update each entry in newParticle...
for ghostIndex in range(self.numGhosts):
"*** YOUR CODE HERE ***" ghostPosition = newParticle[ghostIndex]
newPosDist = getPositionDistributionForGhost(
"*** END YOUR CODE HERE ***" setGhostPositions(gameState, prevGhostPositions),
ghostIndex,
self.ghostAgents[ghostIndex])
newPos = util.sample(newPosDist)
newParticle[ghostIndex] = newPos
newParticles.append(tuple(newParticle)) newParticles.append(tuple(newParticle))
self.particles = newParticles self.particles = newParticles