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 ***"
        # update the particles to include the distributions over the new positions for
        # the ghost with a sample
        newParticles = []
        for oldPos in self.particles:
            newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
            new = util.sampleFromCounter(newPosDist)
            newParticles.append(new)
        self.particles = newParticles
        "*** END YOUR CODE HERE ***"
示例#2
0
文件: inference.py 项目: zbard/StanAI
  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.
    """
    particles = [] 
    #particleWeights = util.Counter()

    for i in range(self.numParticles):
        # <Should I resample ? Or just use particle translation>
        j = random.choice(self.particles)
        newPosDist = self.getPositionDistribution(
                self.setGhostPosition(gameState, j))
        p = util.sampleFromCounter(newPosDist)
        particles.append(p)
        # Calculate w = P(z|p) in observe() - I think ..
        #particleWeights[p] = self.particleWeights[j]

    self.particles = particles
示例#3
0
    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 ***"
        # particlesToReturn = []
        # for oldPos in self.particles:

        #     newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
        #     # print newPosDist
        #     # print "values"
        #     # print newPosDist.values()
        #     # print "keys"
        #     # print newPosDist.keys()
        #     values = [(p) for p in newPosDist.values()]
        #     keys = list(newPosDist.keys())
        #     print values

        #     particlesToReturn.append(util.nSample(values, keys, 1))
        # self.particles = particlesToReturn 
        particles = []
        for oldPos in self.particles:
            newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, oldPos))
            particles.append(util.sampleFromCounter(newPosDist))
        self.particles = particles
示例#4
0
  def observeState(self, gameState):
    """
    Resamples the set of particles using the likelihood of the noisy observations.
    """
    pacmanPosition = gameState.getPacmanPosition()
    noisyDistances = gameState.getNoisyGhostDistances()
    if len(noisyDistances) < self.numGhosts: return
    emissionModels = [getObservationDistribution(dist) for dist in noisyDistances]

    "*** YOUR CODE HERE ***"
    WEIGHT = -1
    self.particles = self.sendToJail(noisyDistances)
    newParticles = []
    for par in self.particles:
        newParticle = list(par)
        newParticle[WEIGHT] = reduce(mul, [emissionModels[i][util.manhattanDistance(pacmanPosition, newParticle[i])]
                                       for i in range(len(emissionModels)) if noisyDistances[i] != None])
        newParticles.append(tuple(newParticle))
    newDistribution = util.Counter()
    for par in newParticles:
        newDistribution[par[:WEIGHT]] = sum(newParticle[WEIGHT] for newParticle in newParticles if newParticle[:WEIGHT] == par[:WEIGHT])
    if len([par for par in newParticles if newDistribution[par[:WEIGHT]] > 0]) == 0:
        self.initializeParticles()
        self.particles = self.sendToJail(noisyDistances)
        newDistribution = util.Counter()
        for par in self.particles:
            newDistribution[par[:WEIGHT]] = sum(newParticle[WEIGHT] for newParticle in self.particles if newParticle[:WEIGHT] == par[:WEIGHT])
    else:
        self.particles = [tuple(list(util.sampleFromCounter(newDistribution)) + [1]) for _  in range(self.numParticles)]
示例#5
0
  def observe(self, observation, gameState):
    "Update beliefs based on the given distance observation."
    emissionModel = busters.getObservationDistribution(observation)
    pacmanPosition = gameState.getPacmanPosition()
    "*** YOUR CODE HERE ***"
    noisyDistance = observation

    newBeliefs = util.Counter()
    beliefs = self.getBeliefDistribution()

    for pos in self.legalPositions:
        trueDistance = util.manhattanDistance(pacmanPosition, pos)
        if emissionModel[trueDistance] > 0:
            newBeliefs[pos] = emissionModel[trueDistance] * beliefs[pos]
    newBeliefs.normalize()

    if newBeliefs.totalCount() == 0:
        self.initializeUniformly(gameState, self.numParticles)
    else:
        self.particles = []
        for i in range (0, self.numParticles):
            self.particles.append(util.sampleFromCounter(newBeliefs))

    if noisyDistance == 999:
        self.particles = []
        for i in range (0, self.numParticles):
            self.particles.append(self.getJailPosition())
示例#6
0
    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()
        if len(noisyDistances) < self.numGhosts: return
        emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]

        "*** YOUR CODE HERE ***"
        weighted, particles = util.Counter(), list()
        for particle in self.particles:
            ghostPositions, weight = list(particle), 1
            for ghost in range(self.numGhosts):
                manhattan = util.manhattanDistance(ghostPositions[ghost], pacmanPosition)
                if noisyDistances[ghost] is None:
                    particle = self.getParticleWithGhostInJail(particle, ghost)
                else:
                    weight *= emissionModels[ghost][manhattan]
            weighted[particle] += weight
        weighted.normalize()
        if weighted.totalCount() != 0:
            for i in range(self.numParticles):
                particles.append(util.sampleFromCounter(weighted))
        else:
            self.initializeParticles()
            particles = self.particles
            for ghost in range(self.numGhosts):
                    if noisyDistances[ghost] is None:
                        for i in range(self.numParticles):
                            particles[i] = self.getParticleWithGhostInJail(self.particles[i], ghost)
        self.particles = particles
示例#7
0
    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]
        newBeliefs = util.Counter()
        particles = self.particles
        for i, particle in enumerate(particles):
            prob = 1.0
            for index in range(self.numGhosts):
                emissionModel = emissionModels[index]
                if noisyDistances[index] is None: # we have captured the ghost
                    particle = self.getParticleWithGhostInJail(particle, index)
                else:
                    trueDistance = util.manhattanDistance(particle[index], pacmanPosition)
                    prob *= emissionModel[trueDistance]
            newBeliefs[particle] += prob
        if all(v == 0 for v in newBeliefs.values()):
            self.initializeParticles()
            for particle in self.particles:
                for i in range(self.numGhosts):
                    if noisyDistances[i] is None:
                        particle = self.getParticleWithGhostInJail(particle, i)
        else:
            newBeliefs.normalize()
            newParticles = []
            while len(newParticles) < self.numParticles:
                newParticle = util.sampleFromCounter(newBeliefs)
                newParticles.append(newParticle)
            self.particles = newParticles
示例#8
0
文件: inference.py 项目: zbard/StanAI
  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.
    """ 
    pacmanPosition = gameState.getPacmanPosition()
    noisyDistances = gameState.getNoisyGhostDistances()
    if len(noisyDistances) < self.numGhosts: return
    emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]
    particleWeights = util.Counter()

    for particle in self.particles:
        #particleWeights[particle] = 1
        oldPos = list(particle)
        for i in range(self.numGhosts):
            p = oldPos[i]
            trueDistance = util.manhattanDistance(p, pacmanPosition)
            # To handle zero probabilities when in jail
            #particleWeights[particle] *= emissionModels[i][trueDistance]
            particleWeights[particle] += emissionModels[i][trueDistance]
    particleWeights.normalize()
   
    # Handle case for all weights are 0
    if not particleWeights.totalCount():
        print "Re init"
        self.initializeParticles()
    else :
        # Resample
        self.particles = []
        for i in range(self.numParticles):
            p = util.sampleFromCounter(particleWeights)
            self.particles.append(p)

    # Handle jail cells
    for i in range(self.numGhosts):
        if noisyDistances[i] == 999:
            newParticles = []
            for particle in self.particles:
                oldPos = list(particle)
                oldPos[i] = (2*i + 1, 1)
                newParticles.append(tuple(oldPos))
            self.particles = newParticles
示例#9
0
  def elapseTime(self, gameState):
    """
    Samples each particle's next state based on its current state and the gameState.

    To loop over the ghosts, use:

      for i in range(self.numGhosts):
        ...

    Then, assuming that "i" refers to the index of the
    ghost, to obtain the distributions over new positions for that
    single ghost, given the list (prevGhostPositions) of previous
    positions of ALL of the ghosts, use this line of code:

      newPosDist = getPositionDistributionForGhost(setGhostPositions(gameState, prevGhostPositions),
                                                   i, self.ghostAgents[i])

    Note that you may need to replace "prevGhostPositions" with the
    correct name of the variable that you have used to refer to the
    list of the previous positions of all of the ghosts, and you may
    need to replace "i" with the variable you have used to refer to
    the index of the ghost for which you are computing the new
    position distribution.

    As an implementation detail (with which you need not concern
    yourself), the line of code above for obtaining newPosDist makes
    use of two helper functions defined below in this file:

      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.
          
          The ghost agent you are meant to supply is self.ghostAgents[ghostIndex-1],
          but in this project all ghost agents are always the same.
    """
    '''
    POSITION, WEIGHT = 0, 1
    newParticles = []
    for par in self.particles:
        newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, par[POSITION]))
        newParticles.append((util.sampleFromCounter(newPosDist), par[WEIGHT]))
    self.particles = newParticles
    '''
    newParticles = []
    for oldParticle in self.particles:
      newParticle = list(oldParticle) # A list of ghost positions
      "*** YOUR CODE HERE ***"
      for i in range(self.numGhosts):
        prevGhostPositions = newParticle[:-1]
        newPosDist = getPositionDistributionForGhost(setGhostPositions(gameState, prevGhostPositions),
                                                     i, self.ghostAgents[i])
        newParticle[i] = util.sampleFromCounter(newPosDist)
      newParticles.append(tuple(newParticle))
    self.particles = newParticles
示例#10
0
  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 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. Remember to
          change ghosts' positions to jail if called for.
    """ 
    pacmanPosition = gameState.getPacmanPosition()
    noisyDistances = gameState.getNoisyGhostDistances()
    if len(noisyDistances) < self.numGhosts: return
    emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]

    "*** YOUR CODE HERE ***"
    allPossible = util.Counter()
    #for x in xrange(0,self.numParticles):
    #  p = util.sampleFromCounter(self.particles)
    for p in xrange(self.numParticles):
      weight = 1
      for i in xrange(self.numGhosts):
          if noisyDistances[i]:
              trueDistance = util.manhattanDistance(self.particles[p][i], pacmanPosition)
              emissionModel = emissionModels[i]
              #if emissionModel[trueDistance] > 0:
              weight *= emissionModel[trueDistance]
      allPossible[self.particles[p]] += weight
    allPossible.normalize()

    self.particles = []
    if allPossible.totalCount() == 0:
        self.initializeUniformly(self.numParticles)
    else:
        for i in xrange(self.numParticles):
            self.particles.append(util.sampleFromCounter(allPossible))
    
    self.newParticles = []
    for p in self.particles:
        p = list(p)
        for i in range(self.numGhosts):
            if noisyDistances[i] == None:
                p[i] = self.getJailPosition(i)
        self.newParticles.append(tuple(p))
    self.particles = self.newParticles        
示例#11
0
  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 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. Remember to
          change ghosts' positions to jail if called for.
    """ 
    pacmanPosition = gameState.getPacmanPosition()
    noisyDistances = gameState.getNoisyGhostDistances()
    if len(noisyDistances) < self.numGhosts: return
    emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]

    "*** YOUR CODE HERE ***"
    weight =util.Counter()
    for c in range(self.numParticles):
        oldPos=list(self.particles[c])
        weights = util.Counter()
        for i in range(self.numGhosts):
            if (noisyDistances[i]==None):
                oldPos[i]=self.getJailPosition(i)
                self.particles[c]=tuple(oldPos)
                weights[i] = 1.0
            else:
                trueDistance = util.manhattanDistance(oldPos[i], pacmanPosition)
                if emissionModels[i][trueDistance] > 0: 
                    weights[i]+= emissionModels[i][trueDistance]
        product=1.00
        
        for j in range(self.numGhosts):
            product*=weights[j]
        weight[self.particles[c]]+=product
    if weight.totalCount()==0:
        self.initializeParticles()
        return
    resampledParticle =[]
    i=0
    while(i<self.numParticles):
        sample=util.sampleFromCounter(weight)
        resampledParticle+=[sample]
        i+=1
    self.particles = resampledParticle
  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 ***"
#    if self.particles.totalCount()==0:
#        self.initializeUniformly(self.numParticles)
    flag = True   
    if noisyDistance == None:
        belief = util.Counter()
        belief[self.getJailPosition()] = 1
        self.beliefs = belief
        return   
    allPossible = util.Counter()
    for elem in self.legalPositions:
      trueDistance = util.manhattanDistance(elem, pacmanPosition)
      if emissionModel[trueDistance] > 0: 
          Flag = False
          allPossible[elem] = emissionModel[trueDistance] * self.particles[elem]       
    allPossible.normalize()
    if Flag:
        self.initializeUniformly(self.numParticles)
    i = 0
    self.particles = util.Counter()
    while i!=self.numParticles:
        choose = util.sampleFromCounter(allPossible)
        self.particles[choose]+=1 
        i+=1
    self.particles.normalize()
示例#13
0
    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()
        if len(noisyDistances) < self.numGhosts: return
        emissionModels = [
            busters.getObservationDistribution(dist) for dist in noisyDistances
        ]

        "*** YOUR CODE HERE ***"
        weighted, particles = util.Counter(), list()
        for particle in self.particles:
            ghostPositions, weight = list(particle), 1
            for ghost in range(self.numGhosts):
                manhattan = util.manhattanDistance(ghostPositions[ghost],
                                                   pacmanPosition)
                if noisyDistances[ghost] is None:
                    particle = self.getParticleWithGhostInJail(particle, ghost)
                else:
                    weight *= emissionModels[ghost][manhattan]
            weighted[particle] += weight
        weighted.normalize()
        if weighted.totalCount() != 0:
            for i in range(self.numParticles):
                particles.append(util.sampleFromCounter(weighted))
        else:
            self.initializeParticles()
            particles = self.particles
            for ghost in range(self.numGhosts):
                if noisyDistances[ghost] is None:
                    for i in range(self.numParticles):
                        particles[i] = self.getParticleWithGhostInJail(
                            self.particles[i], ghost)
        self.particles = particles
示例#14
0
  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.
    """ 
    pacmanPosition = gameState.getPacmanPosition()
    noisyDistances = gameState.getNoisyGhostDistances()
    if len(noisyDistances) < self.numGhosts: return
    emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]

    "*** YOUR CODE HERE ***"
    allWeight = util.Counter();
    for p in range(self.numParticles):
      weight = 0;
      for i in range(self.numGhosts):
        trueDistance = util.manhattanDistance(self.particles[p][i], pacmanPosition);
        if emissionModels[i][trueDistance] > 0: 
          weight += emissionModels[i][trueDistance];
      allWeight[self.particles[p]] = weight;
    #allWeight.normalize();
    
    if allWeight.totalCount() == 0:
      self.initializeParticles();
    else:
      self.particles = [];
      for j in range(self.numParticles):
        self.particles.append(tuple(util.sampleFromCounter(allWeight)));

    checkedParticles = [];    
    ## check for ghost eaten by the Pacman
    for p in range(self.numParticles):
      particle = list(self.particles[p]);
      for i in range(self.numGhosts):
        if noisyDistances[i] == 999:
          particle[i] = (2 * i + 1, i);
      checkedParticles.append(tuple(particle));
    self.particles = checkedParticles;
示例#15
0
    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()

             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 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()
        "*** Q4 CODE STARTS HERE ***"
        # util.raiseNotDefined()

        jailedGhostBeliefs = util.Counter()
        beliefs = self.getBeliefDistribution()

        for posn in self.legalPositions:
            distance = util.manhattanDistance(pacmanPosition, posn)
            if emissionModel[distance] > 0:
                jailedGhostBeliefs[posn] = emissionModel[distance] * beliefs[posn]
        jailedGhostBeliefs.normalize()

        if jailedGhostBeliefs.totalCount() == 0:
            self.initializeUniformly(gameState)
        else:
            self.particles = []
            for i in range(0, self.numParticles):
                self.particles.append(util.sampleFromCounter(jailedGhostBeliefs))

        if noisyDistance is None:
            self.particles = []
            for i in range(0, self.numParticles):
                self.particles.append(self.getJailPosition())
示例#16
0
    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()
        "*** YOUR CODE HERE ***"

        value = 0
        allPossible = util.Counter()
        for p in self.legalPositions:
            trueDistance = util.manhattanDistance(p, pacmanPosition)
            if emissionModel[trueDistance] > 0:
                value = 1
                allPossible[p] = self.particles[p] * emissionModel[trueDistance]
        allPossible.normalize()

        # all weights for particles == 0
        if value == 0:
            self.initializeUniformly(self.numParticles)

        # If ghost in jail
        if noisyDistance == None:
            for p in self.beliefs:
                if p == self.getJailPosition():
                    allPossible[p] = 1.0
                else:
                    allPossible[p] = 0.0
        # Resample:
        # Use the probability distribution from allPossible {Pos: probability, Pos: probability, ...}
        self.particles = util.Counter()
        for i in range(self.numParticles):
            samplePos = util.sampleFromCounter(allPossible)
            self.particles[samplePos] = self.particles[samplePos] + 1
        self.particles.normalize()
示例#17
0
    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()
        "*** YOUR CODE HERE ***"
        """ compute weights, i.e. P(noise_t+1 | dist(pac, si) ), si \in self.particles """
        if noisyDistance == None:
            return

        wts = util.Counter()
        all_zero = True
        for si in self.particles:
            d = util.manhattanDistance(si, pacmanPosition)
            wts[si] += emissionModel[d]
            if wts[si] > 0.0:
                all_zero = False
        wts.normalize()
        # print( "wts: ", wts )
        # print( wts.totalCount() )
        """ resample uniformly or according to wts """
        if all_zero:
            self.initializeUniformly(gameState)
        else:
            self.particles = []
            for i in range(self.numParticles):
                self.particles.append(util.sampleFromCounter(wts))

        # print( "after resampling: ", self.particles )
        return
示例#18
0
 def elapseTime(self, gameState):
   """
   Samples each particle's next state based on its current state and the gameState.
   """
   newParticles = []
   for oldParticle in self.particles:
     newParticle = list(oldParticle) # A list of ghost positions
     "*** YOUR CODE HERE ***"
     for i in range(self.numGhosts):
       prevGhostPositions = newParticle[:-1]
       newPosDist = getPositionDistributionForGhost(setGhostPositions(gameState, prevGhostPositions),
                                                    i, self.ghostAgents[i])
       newParticle[i] = util.sampleFromCounter(newPosDist)
     newParticles.append(tuple(newParticle))
   self.particles = newParticles
示例#19
0
  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 = getObservationDistribution(noisyDistance)
    pacmanPosition = gameState.getAgentPosition(self.myIndex)
    "*** YOUR CODE HERE ***"
    POSITION, WEIGHT = 0, 1
    if noisyDistance == None:
        self.particles = [(self.getJailPosition(), 1) for _ in range(self.numParticles)]
    else:
        newParticles = []
        for par in self.particles:
              newPar = (par[POSITION], emissionModel[util.manhattanDistance(pacmanPosition, par[POSITION])])
              newParticles.append(newPar)
        newDistribution = util.Counter()
        for p in self.particles:
            newDistribution[p[POSITION]] = sum([par[WEIGHT] for par in newParticles if par[POSITION] == p[POSITION]])
        if len([p for p in self.legalPositions if newDistribution[p] > 0]) == 0:
            self.initializeUniformly(gameState)
            if noisyDistance == None:
                self.particles = [(self.getJailPosition(), 1) for _ in range(self.numParticles)]
            newDistribution = util.Counter()
            for p in self.legalPositions:
                newDistribution[p] = sum([par[WEIGHT] for par in newParticles if par[POSITION] == p])
        else:
            self.particles = [(util.sampleFromCounter(newDistribution), 1) for _ in range(self.numParticles)]
示例#20
0
  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()
    "*** YOUR CODE HERE ***"
    if pacmanPosition in self.getJailPosition():
        allPossible[pacmanPosition]=1
        self.particles=allPossible
    else:
        weights = util.Counter()
        for p in self.particles:
          trueDistance = util.manhattanDistance(p, pacmanPosition)
          if emissionModel[trueDistance] > 0: 
              weights[p] += emissionModel[trueDistance]
        weights.normalize()
        if weights.totalCount()==0:
            self.initializeUniformly(gameState)
            return
        resampledParticle =[]
        i=0
        while(i<self.numParticles):
            sample=util.sampleFromCounter(weights)
            resampledParticle+=[sample]
            i+=1
        self.particles = resampledParticle
示例#21
0
  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.
    """
    newParticlesList = []
    for particle in self.particles:
        newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, particle.state))
        newParticle = Particle(util.sampleFromCounter(newPosDist),1.0)
        newParticlesList.append(newParticle)
    self.particles = newParticlesList
示例#22
0
  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 ***"
    newParticles = [];
    for p in self.particles:
      newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, p));
      newParticles.append(util.sampleFromCounter(newPosDist)); 
    self.particles = newParticles;
示例#23
0
    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 ***"
        for i in range(self.numParticles):
            new_p_distr = self.getPositionDistribution(
                self.setGhostPosition(gameState, self.particles[i]))
            self.particles[i] = util.sampleFromCounter(new_p_distr)
        # print( "update for time: ", self.particles )
        return
示例#24
0
 def observeState(self, gameState, friendlyIndex):
   """
   Resamples the set of particles using the likelihood of the noisy observations.
   
   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 cell, position (2 * ghostIndex - 1, 1).
         Captured ghosts always have a noisyDistance of 999.
        
     2) When all particles receive 0 weight, they should be recreated from the
         prior distribution by calling initializeParticles.  
   """ 
   friendlyPosition = gameState.getAgentPosition(friendlyIndex)   # Should NEVER be 'None'
   noisyDistances = list()
   #for i in range(gameState.getNumAgents()):
   #  print gameState.getAgentPosition(i)
   for i in range(len(gameState.getAgentDistances())):
     if i in self.enemyIndices:
       noisyDistances.append(gameState.getAgentDistances()[i])
   emissionModels = [getObservationDistribution(dist) for dist in noisyDistances]
   particleWeightsCounter = util.Counter()
   if (len(emissionModels) != 0):
     for particleArray in self.particles:
         particleArrayList = list(particleArray)
         particle_weights = util.Counter()
         for enemyIndex in range(self.numEnemy):
           trueDist = util.manhattanDistance(friendlyPosition, particleArrayList[enemyIndex])
           weight = (float) (emissionModels[enemyIndex][trueDist])
           particle_weights[enemyIndex] += weight
         weightProduct = 1
         for particle in particle_weights:
           weightProduct *= particle_weights[particle]
         particleArrayList = tuple(particleArrayList)
         particleWeightsCounter[particleArrayList] = weightProduct + particleWeightsCounter[particleArrayList]
     if (sum(particleWeightsCounter.values()) == 0):
       self.initializeParticles()
       return
     newParticles = []
     for n in range(len(self.particles)):
       resample = util.sampleFromCounter(particleWeightsCounter)
       newParticles.append(resample)
     self.particles = newParticles;
示例#25
0
    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 ***"
        updatedParticlesList = []
        for oldPosition in self.particles:
            updatedPositionDistance = self.getPositionDistribution(self.setGhostPosition(gameState, oldPosition))
            updatedParticlesList.append(util.sampleFromCounter(updatedPositionDistance))
        self.particles = updatedParticlesList
示例#26
0
    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.
        """
        particles = self.particles
        newParticles = []
        for particle in particles:
            newPosDist = self.getPositionDistribution(self.setGhostPosition(gameState, particle))
            newParticle = util.sampleFromCounter(newPosDist)
            newParticles.append(newParticle)
        self.particles = newParticles
示例#27
0
    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.
        """
        "*** Q5 CODE STARTS HERE ***"
        # util.raiseNotDefined()

        listOfNewParticles = []

        for posn in self.particles:
            newParticlePos = self.getPositionDistribution(self.setGhostPosition(gameState, posn))
            listOfNewParticles.append(util.sampleFromCounter(newParticlePos))

        self.particles = listOfNewParticles
示例#28
0
文件: inference.py 项目: zbard/StanAI
  def observe(self, observation, gameState):
    "Update beliefs based on the given distance observation."
    emissionModel = busters.getObservationDistribution(observation)
    pacmanPosition = gameState.getPacmanPosition()
    particleWeights = util.Counter()

    # Because the evidence itself is dependent on position;
    # I am also resampling here.

    for p in self.legalPositions:
        # Noise - is it helping ?
        particleWeights[p] = 1 
    particleWeights.normalize()
    for p in self.particles:
        trueDistance = util.manhattanDistance(p, pacmanPosition)
        # Hardcoded : uggh. priority to current particles
        particleWeights[p] += 10 * emissionModel[trueDistance]
    particleWeights.normalize()

    self.particles = []
    #self.particleWeights = util.Counter()
    for i in range(self.numParticles):
        p = util.sampleFromCounter(particleWeights)
        self.particles.append(p)
示例#29
0
    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 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. Remember to
          change ghosts' positions to jail if called for.
    """
        pacmanPosition = gameState.getPacmanPosition()
        noisyDistances = gameState.getNoisyGhostDistances()
        if len(noisyDistances) < self.numGhosts:
            return
        emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]

        "*** YOUR CODE HERE ***"

        allPossible = util.Counter()
        value = 0
        for p in range(self.numParticles):
            accumulatedWeight = 1.0
            for g in range(self.numGhosts):
                if noisyDistances[g] == None:
                    continue
                elif noisyDistances[g] != None:
                    pos = self.particles[p][g]
                    trueDistance = util.manhattanDistance(pos, pacmanPosition)
                    # if emissionModels[g][trueDistance] > 0:
                    value = 1
                    accumulatedWeight = accumulatedWeight * emissionModels[g][trueDistance]
                # allPossible[p].normalize()
            allPossible[self.particles[p]] = allPossible[self.particles[p]] + accumulatedWeight
        allPossible.normalize()
        self.particles = []
        newParticles = []
        # all weights for particles == 0
        if value == 0:
            self.initializeParticles()
        else:
            for i in range(self.numParticles):
                samplePos = util.sampleFromCounter(allPossible)
                # self.particles[samplePos] = self.particles[samplePos] + 1
                self.particles.append(samplePos)
        # Resample:
        # Use the probability distribution from allPossible {Pos: probability, Pos: probability, ...}
        # self.particles = util.Counter()

        for p in self.particles:
            p = list(p)
            for g in range(self.numGhosts):
                if noisyDistances[g] == None:
                    p[g] = self.getJailPosition(g)
            newParticles.append(tuple(p))
        self.particles = newParticles
        """
示例#30
0
  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 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. Remember to
          change ghosts' positions to jail if called for.
    """ 
    pacmanPosition = gameState.getPacmanPosition()
    noisyDistances = gameState.getNoisyGhostDistances()
    if len(noisyDistances) < self.numGhosts: return
    emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]

    "*** YOUR CODE HERE ***"

    allPossible = util.Counter()

    #new weights
    for particle in self.particles:
      #variables
        pList = list(particle)
        pweights = util.Counter()
        weightProduct = 1
        resampledParticles = []


        for i in range(self.numGhosts):
          if (noisyDistances[i] == 999):
            pList[i] = (2 * (i+1) - 1, 1)
            pweights[i] = 1.0
          else:
            weight = (float) (emissionModels[i][util.manhattanDistance(pacmanPosition, pList[i])])
            pweights[i] += weight   #multiple of the weights of each ghost
        for particle in pweights:
          weightProduct *= pweights[particle]
        pList = tuple(pList) #initialized as a tuple
        allPossible[pList] += weightProduct

    if (sum(allPossible.values()) == 0):
      self.initializeParticles()
      return

    #resampling with the new weights
    for n in range(len(self.particles)):
      resample = util.sampleFromCounter(allPossible)
      resampledParticles.append(resample)
    self.particles = resampledParticles
示例#31
0
    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 ***"
        # here
        # if noisyDistance is None:
        #     self.particles[p] = self.getParticleWithGhostInJail
        # allPossible = util.Counter()
        # for i in range(self.numGhosts):
        # if allPossible.totalCount() == 0:
           # self.initializeParticles()


        # noneList = []
        # for i in range(len(noisyDistances)):
        #     if noisyDistances[i] is None:
        #         noneList.append(i)
        # allPossible = util.Counter()
        # for i in range(len(self.particles)):
        #     p = self.particles[i]
        #     for j in noneList: 
        #         p = self.getParticleWithGhostInJail(p,j)
        #     curr = 1 
        #     for j in range(self.numGhosts): 
        #         if noisyDistances[j] is not None: 
        #             dist = util.manhattanDistance(p[j], pacmanPosition)
        #             curr *= emissionModels[j][dist]
        #     allPossible[p] += curr 
        # if allPossible.totalCount() != 0:
        #     self.particles = [util.sample(allPossible) for i in range(len(self.particles))]
        # else:
        #     self.initializeParticles()
        allPossible = util.Counter()
        for particle in self.particles:
            product = 1.0
            for i in range(self.numGhosts):
                if noisyDistances[i] is not None:
                    distance = util.manhattanDistance(particle[i], pacmanPosition)
                    product *= emissionModels[i][distance]
                else:
                    particle = self.getParticleWithGhostInJail(particle, i)
            allPossible[particle] += product
        if allPossible.totalCount() == 0:
            self.initializeParticles()
            for particle in self.particles:
                for i in range(self.numGhosts):
                    if noisyDistances[i] == None:
                        particle = self.getParticleWithGhostInJail(particle, i)
        else:
            dist = []
            for particle in range(self.numParticles):
                dist.append(util.sampleFromCounter(allPossible))
            self.particles = dist
示例#32
0
    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]

        "*** Q6 CODE STARTS HERE ***"

        distribution = util.Counter()
        beliefs = self.getBeliefDistribution()

        for posn in self.particles:
            particle = list(posn)
            # print "particle :::: ", particle
            particleWeight = 1
            for i in range(self.numGhosts):
                if not noisyDistances[i] is None:
                    distance = util.manhattanDistance(pacmanPosition, particle[i])
                    # particleWeight *= emissionModels[i][distance] * beliefs[i]
                    particleWeight *= emissionModels[i][distance]
            distribution[tuple(posn)] += particleWeight

        # distribution.normalize()

        if sum(distribution.values()) == 0:
            self.initializeParticles()
        else:
            particleList = []
            for p in range(self.numParticles):
                particle = []
                for g in range(self.numGhosts):
                    if noisyDistances[g] is None:
                        # 5-JointParticleObserve.test failed as it returned tuple instead of list
                        # particle.append(self.getParticleWithGhostInJail(self.particles[p], g))
                        # particle.append((2*g + 1, 1))
                        particle.append(self.getJailPosition(g))
                    else:
                        sample = util.sampleFromCounter(distribution)
                        particle.append(sample[g])
                particleList.append(tuple(particle))
            self.particles = particleList
示例#33
0
    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 ***"
        allPossible = util.Counter()
        beliefs = self.getBeliefDistribution()
        ghost = 0
        totalWeights = 0
        for nd in noisyDistances:
            if nd == None:
                for particle, prob in beliefs.items():
                    allPossible[self.getParticleWithGhostInJail(particle, ghost)] += prob
                beliefs = allPossible
                beliefs.normalize()
                allPossible = util.Counter()
            ghost += 1
        for particle, prob in beliefs.items():
            allPossible[particle] = prob
            for ghost in range(0, self.numGhosts):
                if noisyDistances[ghost] != None:
                    allPossible[particle] *= emissionModels[ghost][
                        util.manhattanDistance(particle[ghost], pacmanPosition)
                    ]
        beliefs = allPossible
        beliefs.normalize()
        for belief, prob in beliefs.items():
            totalWeights += prob
            if totalWeights != 0:
                break
        if totalWeights == 0:
            self.initializeParticles()
        else:
            self.particles = list()
            while len(self.particles) < self.numParticles:
                self.particles.append(util.sampleFromCounter(beliefs))
示例#34
0
  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 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. Remember to
          change ghosts' positions to jail if called for.
    """
    '''
    noisyDistance = observation
    emissionModel = busters.getObservationDistribution(noisyDistance)
    pacmanPosition = gameState.getPacmanPosition()
    "*** YOUR CODE HERE ***"
    POSITION, WEIGHT = 0, 1
    if noisyDistance == None:
        self.particles = [(self.getJailPosition(), 1) for _ in range(self.numParticles)]
    else:
        newParticles = []
        for par in self.particles:
              newPar = (par[POSITION], emissionModel[util.manhattanDistance(pacmanPosition, par[POSITION])])
              newParticles.append(newPar)
        newDistribution = util.Counter()
        for p in self.legalPositions:
            newDistribution[p] = sum([par[WEIGHT] for par in newParticles if par[POSITION] == p])
        if len([p for p in self.legalPositions if newDistribution[p] > 0]) == 0:
            self.initializeUniformly(gameState)
            newDistribution = util.Counter()
            for p in self.legalPositions:
                newDistribution[p] = sum([par[WEIGHT] for par in newParticles if par[POSITION] == p])
        else:
            self.particles = [(util.sampleFromCounter(newDistribution), 1) for _ in range(self.numParticles)]
    '''
    pacmanPosition = gameState.getPacmanPosition()
    noisyDistances = gameState.getNoisyGhostDistances()
    if len(noisyDistances) < self.numGhosts: return
    emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]

    "*** YOUR CODE HERE ***"
    WEIGHT = -1
    #validGhostIndices = range(self.numGhosts)
    #for i in validGhostIndices:
    #    if noisyDistances[i] == None:
    #        validGhostIndices.remove(i)
    self.particles = self.sendToJail(noisyDistances)
    newParticles = []
    for par in self.particles:
        newParticle = list(par)
        #print [emissionModels[i][util.manhattanDistance(pacmanPosition, newParticle[i])]
        #                               for i in range(len(emissionModels)) if newParticle[i] != self.getJailPosition(i)]
        newParticle[WEIGHT] = reduce(mul, [emissionModels[i][util.manhattanDistance(pacmanPosition, newParticle[i])]
                                       for i in range(len(emissionModels)) if noisyDistances[i] != None])
        newParticles.append(tuple(newParticle))
    newDistribution = util.Counter()
    for par in newParticles:
        newDistribution[par[:WEIGHT]] = sum(newParticle[WEIGHT] for newParticle in newParticles if newParticle[:WEIGHT] == par[:WEIGHT])
    #print "Number of nonzero-weight particles:", len([par for par in newParticles if newDistribution[par[:WEIGHT]] > 0])
    if len([par for par in newParticles if newDistribution[par[:WEIGHT]] > 0]) == 0:
        #print "Need to re-initialize"
        self.initializeParticles()
        self.particles = self.sendToJail(noisyDistances)
        newDistribution = util.Counter()
        for par in self.particles:
            newDistribution[par[:WEIGHT]] = sum(newParticle[WEIGHT] for newParticle in self.particles if newParticle[:WEIGHT] == par[:WEIGHT])
    else:
        #print "Everything is fine here"
        self.particles = [tuple(list(util.sampleFromCounter(newDistribution)) + [1]) for _  in range(self.numParticles)]