本文整理汇总了Python中util.sample函数的典型用法代码示例。如果您正苦于以下问题:Python sample函数的具体用法?Python sample怎么用?Python sample使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sample函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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()
示例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
示例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);
示例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
示例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)
示例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)
示例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
示例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)
示例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
示例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
示例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
示例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
示例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
示例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]
示例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