本文整理汇总了Python中util.sample函数的典型用法代码示例。如果您正苦于以下问题:Python sample函数的具体用法?Python sample怎么用?Python sample使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sample函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given its
previous position (oldPos) as well as Pacman's current position.
util.sample(Counter object) is a helper method to generate a sample from
a belief distribution.
"""
"*** YOUR CODE HERE ***"
samples = []
for parPos in self.parDist:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, parPos))
samples.append(util.sample(newPosDist))
self.parDist = samples
return self.getBeliefDistribution()
util.raiseNotDefined()
开发者ID:hkmangla,项目名称:Artificial-Intelligence,代码行数:25,代码来源:inference.py
示例2: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given
its previous position (oldPos) as well as Pacman's current
position.
util.sample(Counter object) is a helper method to generate a sample from a
belief distribution
"""
"*** YOUR CODE HERE ***"
#util.raiseNotDefined()
newParticles = []
self.newPosDistMemo = util.Counter()
for oldPos in self.particles:
if oldPos in self.newPosDistMemo.keys():
newPosDist = self.newPosDistMemo[oldPos]
else:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
self.newPosDistMemo[oldPos] = newPosDist # memoize for speed
newParticles.append(util.sample(newPosDist))
self.particles = newParticles
开发者ID:brianvo,项目名称:cs188-tracking,代码行数:28,代码来源:inference.py
示例3: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given
its previous position (oldPos) as well as Pacman's current
position.
util.sample(Counter object) is a helper method to generate a sample from a
belief distribution
"""
"*** YOUR CODE HERE ***"
#util.raiseNotDefined()
beliefs = self.getBeliefDistribution()
for particle in self.particles:
oldPos=particle[0]
newPosDist=self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
#update particle based on newPosDist
particle[0] = util.sample(newPosDist);
开发者ID:Jwonsever,项目名称:AI,代码行数:27,代码来源:inference.py
示例4: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given
its previous position (oldPos) as well as Pacman's current
position.
util.sample(Counter object) is a helper method to generate a sample from a
belief distribution
"""
"*** YOUR CODE HERE ***"
#1) go through each particle
#2) generate the transition probability distribution at the particle's position
#3) sample from the distribution to get the new position of the particle
#4) add the particle to the new list
#counter = util.Counter()
newParticles = []
for particle in self.particles:
#for oldPos in self.legalPositions:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, particle))
newParticles.append(util.sample(newPosDist))
self.particles = newParticles
开发者ID:shubhamsinha92,项目名称:Pacman-Projects,代码行数:28,代码来源:inference.py
示例5: observe
def observe(self, observation, gameState):
"""
Update beliefs based on the given distance observation. Make
sure to handle the special case where all particles have weight
0 after reweighting based on observation. If this happens,
resample particles uniformly at random from the set of legal
positions (self.legalPositions).
A correct implementation will handle two special cases:
1) When a ghost is captured by Pacman, **all** particles should be updated so
that the ghost appears in its prison cell, 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).
2) When all particles receive 0 weight, they should be recreated from the
prior distribution by calling initializeUniformly. The total weight
for a belief distribution can be found by calling totalCount on
a Counter object
util.sample(Counter object) is a helper method to generate a sample from
a belief distribution
You may also want to use util.manhattanDistance to calculate the distance
between a particle and pacman's position.
"""
noisyDistance = observation
emissionModel = busters.getObservationDistribution(noisyDistance)
pacmanPosition = gameState.getPacmanPosition()
"*** YOUR CODE HERE ***"
'''
print "Noisy Distance: ", noisyDistance
print "Emission Model: ", emissionModel
print "pacman Position: ", pacmanPosition
print "self.particles: ", self.particles
'''
allPossible = util.Counter()
belief = self.getBeliefDistribution()
#ghost has been captured
if noisyDistance is None:
self.particles = []
for index in range(0, self.numParticles):
self.particles.append(self.getJailPosition())
else:
#weighting and testing if the weights are 0
for position in self.legalPositions:
trueDistance = util.manhattanDistance(position, pacmanPosition)
allPossible[position] = emissionModel[trueDistance] * belief[position]
if allPossible.totalCount() == 0:
self.initializeUniformly(gameState)
return
#resampling
allPossible.normalize()
self.particles = []
for index in range(0, self.numParticles):
newP = util.sample(allPossible)
self.particles.append(newP)
开发者ID:gunderjt,项目名称:artificial_intelligence,代码行数:60,代码来源:inference.py
示例6: observe
def observe(self, observation, gameState):
noisyDistance = observation
emissionModel = busters.getObservationDistribution(noisyDistance)
pacmanPosition = gameState.getPacmanPosition()
"*** YOUR CODE HERE ***"
weights = util.Counter()
i = 0
if noisyDistance == None:
self.particle_positions = []
while i != self.numParticles:
self.particle_positions.append(self.getJailPosition())
i += 1
else:
for p in self.particle_positions:
trueDistance = util.manhattanDistance(p, pacmanPosition)
#maybe more than one particle per position
weights[p] += emissionModel[trueDistance]
if weights.totalCount() != 0:
self.particle_positions = []
while i != self.numParticles:
self.particle_positions.append(util.sample(weights))
i += 1
else:
self.initializeUniformly(gameState)
开发者ID:MangoDreams,项目名称:cs151,代码行数:29,代码来源:super_deep_backup.py
示例7: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given
its previous position (oldPos) as well as Pacman's current
position.
util.sample(Counter object) is a helper method to generate a sample from a
belief distribution
"""
"*** YOUR CODE HERE ***"
# Updated: Bharadwaj Tanikella 2014
temp = self.particles
i = 0
while i < len(temp):
# newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
#print temp[i], Old Position
newPostDict = self.getPositionDistribution(self.setGhostPosition(gameState,temp[i]))
temp[i]=util.sample(newPostDict)
i+=1
self.particles= temp
开发者ID:btanikella,项目名称:AI-Pacman-Project,代码行数:27,代码来源:inference.py
示例8: observeState
def observeState(self, gameState):
"""
Resamples the set of particles using the likelihood of the noisy observations.
As in elapseTime, to loop over the ghosts, use:
for i in range(self.numGhosts):
...
A correct implementation will handle two special cases:
1) When a ghost is captured by Pacman, all particles should be updated so
that the ghost appears in its prison cell, position (2 * i + 1, 1),
where "i" is the 0-based index of the ghost.
You can check if a ghost has been captured by Pacman by
checking if it has a noisyDistance of 999 (a noisy distance
of 999 will be returned if, and only if, the ghost is
captured).
2) When all particles receive 0 weight, they should be recreated from the
prior distribution by calling initializeParticles.
"""
pacmanPos = gameState.getPacmanPosition()
noisyDistances = gameState.getNoisyGhostDistances()
if len(noisyDistances) < self.numGhosts: return
emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]
jailed = [ noisy == 999 for noisy in noisyDistances ]
partials = [ tuple() ] * self.numParticles
for g in xrange(self.numGhosts):
weighted = util.Counter()
if jailed[g]:
# handle the jailed ghost
jailLocation = (2 * g + 1, 1)
for i in xrange(self.numParticles):
partials[i] += (jailLocation, )
continue
for oldAssign, counts in self.sampledCounts.iteritems():
for assign, oldCount in counts.iteritems():
if oldCount <= 0:
continue
trueDistance = util.manhattanDistance(pacmanPos, assign[g])
delta = abs(trueDistance - noisyDistances[g])
if emissionModels[g][trueDistance] > 0 and delta <= MAX_DIST_DELTA:
# no need to normalize by constant
pTrue = math.exp( -delta )
weighted[assign[g]] = oldCount * emissionModels[g][trueDistance] * pTrue / self.proposals[oldAssign][assign]
totalWeight = weighted.totalCount()
if totalWeight != 0: weighted.normalize()
for i in xrange(self.numParticles):
if totalWeight == 0:
# handle the zero weights case
partials[i] += (random.choice(self.legalPositions), )
else:
partials[i] += (util.sample(weighted), )
self.particles = CounterFromIterable(partials)
开发者ID:zahanm,项目名称:ai-projects,代码行数:60,代码来源:inference.py
示例9: elapseTime
def elapseTime(self, gameState):
"Update beliefs for a time step elapsing."
"*** YOUR CODE HERE ***"
temp = util.Counter()
#print 'len of self.beliefs at the start of elapse time ',len(self.beliefs.keys())
#print 'number of particles: ',self.numParticles
for pos in self.beliefs.keys():
#if self.beliefs[pos]==0: continue
#print 'Ghost position ',pos
#print 'Position distribution ', self.getPositionDistribution(gameState)
#import time
#print 'position ', pos
state = self.setGhostPosition(gameState,pos)
#if not pos in self.legalPositions:
#print 'the position is not a legal position!! KERNEL PANIC'
#time.sleep(20)
#if self.getPositionDistribution
#if state is None:
#print 'state was none'
#import time
#if len(self.getPositionDistribution(state)) is 0:
#print 'the class of state is ', state.__class__.__name__
#print 'position distribution ', self.getPositionDistribution(state)
#time.sleep(10000000)
#print self.beliefs[pos]
for i in range(self.beliefs[pos]):
newSample = util.sample(self.getPositionDistribution(state))
temp[newSample]+=1
self.beliefs=temp
开发者ID:lyeechong,项目名称:ai,代码行数:33,代码来源:inference.py
示例10: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given
its previous position (oldPos) as well as Pacman's current
position.
util.sample(Counter object) is a helper method to generate a sample from a
belief distribution
"""
"*** YOUR CODE HERE ***"
allPossible = util.Counter()
newPosDist = util.Counter()
currParticle = 0
newParticleList = []
for i in range(0, len(self.particleList)):
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, self.particleList[i]))
newParticleList.append(util.sample(newPosDist))
self.particleList = newParticleList
开发者ID:thomaschow,项目名称:cs188,代码行数:25,代码来源:inference.py
示例11: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given its
previous position (oldPos) as well as Pacman's current position.
util.sample(Counter object) is a helper method to generate a sample from
a belief distribution.
"""
"*** YOUR CODE HERE ***"
newParticleList = []
allPossibleGhostDist = util.Counter()
if self.particles[0] == self.getJailPosition():
return
'''
for p in self.legalPositions:
allPossibleGhostDist[p] = self.getPositionDistribution(self.setGhostPosition(gameState, p))
'''
for particle in self.particles:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, particle))
newParticleList.append(util.sample(newPosDist))
self.particles = newParticleList
开发者ID:williamseto,项目名称:ai-class,代码行数:33,代码来源:inference.py
示例12: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given
its previous position (oldPos) as well as Pacman's current
position.
"""
"*** YOUR CODE HERE ***"
temporaryPos = []
temporaryWeight = []
cnt = 0
for oldPos in self.particles:
oldWeight = self.particlesWeight[cnt]
cnt += 1
if not oldWeight > 0: continue
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
newProb = 0
while(newProb == 0):
newPos = util.sample(newPosDist)
newProb = newPosDist[newPos]
temporaryPos.append(newPos)
temporaryWeight.append(oldWeight)
self.particles = temporaryPos
self.particlesWeight = temporaryWeight
开发者ID:iceNuts,项目名称:MY-AI-Labs,代码行数:29,代码来源:inference.py
示例13: observe
def observe(self, observation, gameState):
"""
Update beliefs based on the given distance observation. Make
sure to handle the special case where all particles have weight
0 after reweighting based on observation. If this happens,
resample particles uniformly at random from the set of legal
positions (self.legalPositions).
A correct implementation will handle two special cases:
1) When a ghost is captured by Pacman, all particles should be updated so
that the ghost appears in its prison cell, 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).
2) When all particles receive 0 weight, they should be recreated from the
prior distribution by calling initializeUniformly. Remember to
change particles to jail if called for.
"""
noisyDistance = observation
emissionModel = busters.getObservationDistribution(noisyDistance)
pacmanPosition = gameState.getPacmanPosition()
# check if all weights are zero
def zeroWeights(weights):
return all(w == 0 for w in weights.values())
prevBelief = self.getBeliefDistribution()
allPossible = util.Counter()
nextParticles = []
# ghost captured
if noisyDistance is None:
jailPosition = self.getJailPosition()
# put ghost to jail
for i in range(self.numParticles):
nextParticles.append(jailPosition)
self.particles = nextParticles
else:
# update beliefs
for pos in self.legalPositions:
trueDistance = util.manhattanDistance(pos, pacmanPosition)
allPossible[pos] += emissionModel[trueDistance] * prevBelief[pos]
# weights all zero
if zeroWeights(allPossible):
self.initializeUniformly(gameState)
else:
# resample particles
for i in range(self.numParticles):
nextParticles.append(util.sample(allPossible))
self.particles = nextParticles
开发者ID:startupjing,项目名称:Artificial-Intelligence,代码行数:60,代码来源:inference.py
示例14: observeState
def observeState(self, gameState):
"""
Resamples the set of particles using the likelihood of the noisy
observations.
To loop over the ghosts, use:
for i in range(self.numGhosts):
...
A correct implementation will handle two special cases:
1) When a ghost is captured by Pacman, all particles should be updated
so that the ghost appears in its prison cell, position
self.getJailPosition(i) where `i` is the index of the ghost.
As before, you can check if a ghost has been captured by Pacman by
checking if it has a noisyDistance of None.
2) When all particles receive 0 weight, they should be recreated from
the prior distribution by calling initializeParticles. After all
particles are generated randomly, any ghosts that are eaten (have
noisyDistance of None) must be changed to the jail Position. This
will involve changing each particle if a ghost has been eaten.
self.getParticleWithGhostInJail is a helper method to edit a specific
particle. Since we store particles as tuples, they must be converted to
a list, edited, and then converted back to a tuple. This is a common
operation when placing a ghost in jail.
"""
pacmanPosition = gameState.getPacmanPosition()
noisyDistances = gameState.getNoisyGhostDistances()
if len(noisyDistances) < self.numGhosts:
return
emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]
"*** YOUR CODE HERE ***"
#first special case
for i in xrange(self.numGhosts):
if noisyDistances[i]==None:
for x, y in enumerate(self.particles):
self.particles[x] = self.getParticleWithGhostInJail(y, i)
#create a weighted particle distribution
weightedParticleDistri = util.Counter()
for particle in self.particles:
product = 1
for i in xrange(self.numGhosts):
if noisyDistances[i]!=None:
trueDistance = util.manhattanDistance(particle[i], pacmanPosition)
product *= emissionModels[i][trueDistance]
weightedParticleDistri[particle] += product
# second special case
if weightedParticleDistri.totalCount()==0:
self.initializeParticles()
#change all particles with all the eaten ghosts' positions changed to their respective jail positions
for i in xrange(self.numGhosts):
if noisyDistances[i]==None:
for x, y in enumerate(self.particles):
self.particles[x] = self.getParticleWithGhostInJail(y, i)
else: # resampling
self.particles = [util.sample(weightedParticleDistri) for particle in self.particles]
开发者ID:SanityL,项目名称:Projects,代码行数:60,代码来源:inference.py
示例15: elapseTime
def elapseTime(self, gameState):
"""
Samples each particle's next state based on its current state and the gameState.
You will need to use two helper methods provided below:
1) setGhostPositions(gameState, ghostPositions)
This method alters the gameState by placing the ghosts in the supplied positions.
2) getPositionDistributionForGhost(gameState, ghostIndex, agent)
This method uses the supplied ghost agent to determine what positions
a ghost (ghostIndex) controlled by a particular agent (ghostAgent)
will move to in the supplied gameState. All ghosts
must first be placed in the gameState using setGhostPositions above.
Remember: ghosts start at index 1 (Pacman is agent 0).
The ghost agent you are meant to supply is self.enemyAgents[ghostIndex-1],
but in this project all ghost agents are always the same.
"""
newParticles = []
for oldParticle in self.particles:
newParticle = list(oldParticle) # A list of ghost positions
for enemyIndex in range(len(self.enemyIndices)):
tmpState = setEnemyPositions(gameState, newParticle, self.enemyIndices)
updatedParticle = util.sample(getPositionDistributionForEnemy(tmpState, self.enemyIndices[enemyIndex], self.enemyIndices[enemyIndex]))
newParticle[enemyIndex] = updatedParticle
newParticles.append(tuple(newParticle))
self.particles = newParticles
开发者ID:yifeng96,项目名称:188searchproject,代码行数:27,代码来源:munchAgents.py
示例16: resample
def resample(self, noisyDistances):
print "resample"
dist = util.Counter()
for i in range(self.numParticles):
newParticle = []
particle = self.particles[i]
weight = self.weights[i]
for ghost in range(self.numGhosts):
if (noisyDistances[ghost] == None):
newParticle.append(self.getJailPosition(ghost))
else:
newParticle.append(particle[ghost])
dist[tuple(newParticle)] += weight
dist.normalize()
resampleParticles = []
resampleWeights = []
for particle in range(self.numParticles):
sample = util.sample(dist)
resampleParticles.append(sample)
resampleWeights.append(1)
self.particles = resampleParticles
self.weights = resampleWeights
开发者ID:kevintchan,项目名称:tracking,代码行数:26,代码来源:inference.py
示例17: observeState
def observeState(self, gameState):
"""
Resamples the set of particles using the likelihood of the noisy observations.
To loop over the ghosts, use:
for i in range(self.numGhosts):
...
A correct implementation will handle two special cases:
1) When a ghost is captured by Pacman, all particles should be updated so
that the ghost appears in its prison cell, position self.getJailPosition(i)
where "i" is the index of the ghost.
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).
2) When all particles receive 0 weight, they should be recreated from the
prior distribution by calling initializeParticles. After all particles
are generated randomly, any ghosts that are eaten (have noisyDistance of 0)
must be changed to the jail Position. This will involve changing each
particle if a ghost has been eaten.
** Remember ** We store particles as tuples, but to edit a specific particle,
it must be converted to a list, edited, and then converted back to a tuple. Since
this is a common operation when placing a ghost in the jail for a particle, we have
provided a helper method named self.getParticleWithGhostInJail(particle, ghostIndex)
that performs these three operations for you.
"""
pacmanPosition = gameState.getPacmanPosition()
noisyDistances = gameState.getNoisyGhostDistances() # before was a single noisyDistance
if len(noisyDistances) < self.numGhosts: return
emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]
# before was a single one: emissionModel = busters.getObservationDistribution(noisyDistance)
"*** YOUR CODE HERE ***"
newWeightCounter = util.Counter()
for particle in self.particleList:
newWeightForParticle = 1
for index in range(0, self.numGhosts):
if(noisyDistances[index] == None):
particle = self.getParticleWithGhostInJail(particle, index)
else:
ghostsDistanceProduct = util.manhattanDistance(particle[index], pacmanPosition)
newWeightForParticle *= emissionModels[index][ghostsDistanceProduct]
newWeightCounter[particle] = newWeightCounter[particle] + newWeightForParticle
if(not newWeightCounter.totalCount() == 0):
newWeightCounter.normalize()
for x in range(0, self.numParticles):
self.particleList[x] = util.sample(newWeightCounter)
else: #all particles receive 0 weight
self.initializeParticles()
for x in range(0, len(self.particleList)):
for index in range(0, self.numGhosts):
if(noisyDistances[index] == None):
self.particleList[x] = self.getParticleWithGhostInJail(self.particleList[x], index)
开发者ID:mosheleon,项目名称:CS188_Proj_4,代码行数:60,代码来源:inference.py
示例18: elapseTime
def elapseTime(self, gameState):
"""
Update beliefs for a time step elapsing.
As in the elapseTime method of ExactInference, you should use:
newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
to obtain the distribution over new positions for the ghost, given
its previous position (oldPos) as well as Pacman's current
position.
util.sample(Counter object) is a helper method to generate a sample from a
belief distribution
"""
allParticles = self.particles
# Create an empty list
particles = []
# Iterate through every particle and add sample to list
for particle in allParticles:
self.setGhostPosition(gameState, particle)
currentDistribution = self.getPositionDistribution(gameState)
particles.append(util.sample(currentDistribution))
self.particles = particles
开发者ID:reicruz,项目名称:pacman,代码行数:26,代码来源:inference.py
示例19: observeState
def observeState(self, gameState):
pacmanPosition = gameState.getPacmanPosition()
noisyDistances = gameState.getNoisyGhostDistances()
if len(noisyDistances) < self.numGhosts: return
emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]
"*** YOUR CODE HERE ***"
weights = util.Counter()
for i in range(len(self.particle_positions)):
temp = 1
for ghost_idx in range(self.numGhosts):
if noisyDistances[ghost_idx] == None:
new_particle = self.getParticleWithGhostInJail(self.particle_positions[i],ghost_idx)
self.particle_positions[i] = new_particle
else:
trueDistance = util.manhattanDistance(self.particle_positions[i][ghost_idx],pacmanPosition)
model = emissionModels[ghost_idx]
temp *= model[trueDistance]
weights[self.particle_positions[i]] += temp
i = 0
if weights.totalCount() != 0:
self.particle_positions = []
while i != self.numParticles:
self.particle_positions.append(util.sample(weights))
i += 1
else:
self.initializeParticles()
开发者ID:MangoDreams,项目名称:cs151,代码行数:29,代码来源:super_deep_backup.py
示例20: observe
def observe(self, observation, gameState):
"""
Update beliefs based on the given distance observation. Make
sure to handle the special case where all particles have weight
0 after reweighting based on observation. If this happens,
resample particles uniformly at random from the set of legal
positions (self.legalPositions).
A correct implementation will handle two special cases:
1) When a ghost is captured by Pacman, **all** particles should be updated so
that the ghost appears in its prison cell, 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).
2) When all particles receive 0 weight, they should be recreated from the
prior distribution by calling initializeUniformly. The total weight
for a belief distribution can be found by calling totalCount on
a Counter object
util.sample(Counter object) is a helper method to generate a sample from
a belief distribution
You may also want to use util.manhattanDistance to calculate the distance
between a particle and pacman's position.
"""
noisyDistance = observation
emissionModel = busters.getObservationDistribution(noisyDistance)
pacmanPosition = gameState.getPacmanPosition()
"*** YOUR CODE HERE ***"
newBeliefs = util.Counter()
beliefs = self.getBeliefDistribution()
# Special Case #1
# this will handle the case when pacman captures a ghost, it will update the
# particles to reflect this change
if noisyDistance == None:
newBeliefs[self.getJailPosition()] = 1
# go through all the legal positions and calculate their manhattan distance
# to pacman's position
else:
for p in self.legalPositions:
distance = util.manhattanDistance(p, pacmanPosition)
# if the probability of that distance is greater than zero, than recalculate
# the new beliefs as accounting for that probability
if emissionModel[distance] > 0:
newBeliefs[p] = emissionModel[distance] * beliefs[p]
newBeliefs.normalize()
# Special Case #2
# when all the particles have a weight of 0, we reinitialize
if newBeliefs.totalCount() == 0:
self.initializeUniformly(self.numParticles)
return
self.particles = [util.sample(newBeliefs) for _ in range(self.numParticles)]
开发者ID:HannahTuell,项目名称:CSCI3202-Project4,代码行数:60,代码来源:inference.py
注:本文中的util.sample函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论