daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Dec 6th, 2017 143 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     def initializeParticles(self):
  2.         """
  3.        Initialize particles to be consistent with a uniform prior.
  4.  
  5.        Each particle is a tuple of ghost positions. Use self.numParticles for
  6.        the number of particles. You may find the `itertools` package helpful.
  7.        Specifically, you will need to think about permutations of legal ghost
  8.        positions, with the additional understanding that ghosts may occupy the
  9.        same space. Look at the `itertools.product` function to get an
  10.        implementation of the Cartesian product.
  11.  
  12.        Note: If you use itertools, keep in mind that permutations are not
  13.        returned in a random order; you must shuffle the list of permutations in
  14.        order to ensure even placement of particles across the board. Use
  15.        self.legalPositions to obtain a list of positions a ghost may occupy.
  16.  
  17.        Note: the variable you store your particles in must be a list; a list is
  18.        simply a collection of unweighted variables (positions in this case).
  19.        Storing your particles as a Counter (where there could be an associated
  20.        weight with each position) is incorrect and may produce errors.
  21.        """
  22.         "*** YOUR CODE HERE ***"
  23.         self.particles = []
  24.         for i in range(0, self.numParticles):
  25.             self.particles += (random.choice(self.legalPositions) for j in range(0, self.numGhosts))
  26.  
  27.     def addGhostAgent(self, agent):
  28.         """
  29.        Each ghost agent is registered separately and stored (in case they are
  30.        different).
  31.        """
  32.         self.ghostAgents.append(agent)
  33.  
  34.     def getJailPosition(self, i):
  35.         return (2 * i + 1, 1);
  36.  
  37.     def observeState(self, gameState):
  38.         """
  39.        Resamples the set of particles using the likelihood of the noisy
  40.        observations.
  41.  
  42.        To loop over the ghosts, use:
  43.  
  44.          for i in range(self.numGhosts):
  45.            ...
  46.  
  47.        A correct implementation will handle two special cases:
  48.          1) When a ghost is captured by Pacman, all particles should be updated
  49.             so that the ghost appears in its prison cell, position
  50.             self.getJailPosition(i) where `i` is the index of the ghost.
  51.  
  52.             As before, you can check if a ghost has been captured by Pacman by
  53.             checking if it has a noisyDistance of None.
  54.  
  55.          2) When all particles receive 0 weight, they should be recreated from
  56.             the prior distribution by calling initializeParticles. After all
  57.             particles are generated randomly, any ghosts that are eaten (have
  58.             noisyDistance of None) must be changed to the jail Position. This
  59.             will involve changing each particle if a ghost has been eaten.
  60.  
  61.        self.getParticleWithGhostInJail is a helper method to edit a specific
  62.        particle. Since we store particles as tuples, they must be converted to
  63.        a list, edited, and then converted back to a tuple. This is a common
  64.        operation when placing a ghost in jail.
  65.        """
  66.         pacmanPosition = gameState.getPacmanPosition()
  67.         noisyDistances = gameState.getNoisyGhostDistances()
  68.         if len(noisyDistances) < self.numGhosts:
  69.             return
  70.         emissionModels = [busters.getObservationDistribution(dist) for dist in noisyDistances]
  71.  
  72.         "*** YOUR CODE HERE ***"
  73.         for i in range(0, self.numGhosts):
  74.             dis = noisyDistances[i]
  75.             if dis == None:
  76.                 self.particles = [getParticleWithGhostInJail(self.particles[j], i) for j in range(0, self.numParticles)]
  77.  
  78.         particleWeights = util.Counter()
  79.         isPositive = False
  80.  
  81.         for p in self.particles:
  82.             weight = 1.0
  83.             for i in range(0, self.numGhosts):
  84.                 dis = noisyDistances[i]
  85.                 if dis != None:
  86.                     realDistance = util.manhattanDistance(pacmanPosition, p[i])
  87.                     weight *= emissionModels[i][realDistance]
  88.  
  89.             if weight > 0:
  90.                 isPositive = True
  91.             particleWeights[p] = weight
  92.  
  93.         particleWeights.normalize()
  94.  
  95.         if isPositive:
  96.             self.particles = [util.sample(particleWeights) for i in range(0, self.numParticles)]
  97.         else:
  98.             self.initializeParticles()
  99.             for i in range(0, self.numGhosts):
  100.                 if noisyDistance[i] == None:
  101.                         self.particles = [getParticleWithGhostInJail(self.particles[j], i) for j in range(0, self.numParticles)]
  102.  
  103.  
  104.     def getParticleWithGhostInJail(self, particle, ghostIndex):
  105.         """
  106.        Takes a particle (as a tuple of ghost positions) and returns a particle
  107.        with the ghostIndex'th ghost in jail.
  108.        """
  109.         particle = list(particle)
  110.         particle[ghostIndex] = self.getJailPosition(ghostIndex)
  111.         return tuple(particle)
  112.  
  113.     def elapseTime(self, gameState):
  114.         """
  115.        Samples each particle's next state based on its current state and the
  116.        gameState.
  117.  
  118.        To loop over the ghosts, use:
  119.  
  120.          for i in range(self.numGhosts):
  121.            ...
  122.  
  123.        Then, assuming that `i` refers to the index of the ghost, to obtain the
  124.        distributions over new positions for that single ghost, given the list
  125.        (prevGhostPositions) of previous positions of ALL of the ghosts, use
  126.        this line of code:
  127.  
  128.          newPosDist = getPositionDistributionForGhost(
  129.             setGhostPositions(gameState, prevGhostPositions), i, self.ghostAgents[i]
  130.          )
  131.  
  132.        Note that you may need to replace `prevGhostPositions` with the correct
  133.        name of the variable that you have used to refer to the list of the
  134.        previous positions of all of the ghosts, and you may need to replace `i`
  135.        with the variable you have used to refer to the index of the ghost for
  136.        which you are computing the new position distribution.
  137.  
  138.        As an implementation detail (with which you need not concern yourself),
  139.        the line of code above for obtaining newPosDist makes use of two helper
  140.        functions defined below in this file:
  141.  
  142.          1) setGhostPositions(gameState, ghostPositions)
  143.              This method alters the gameState by placing the ghosts in the
  144.              supplied positions.
  145.  
  146.          2) getPositionDistributionForGhost(gameState, ghostIndex, agent)
  147.              This method uses the supplied ghost agent to determine what
  148.              positions a ghost (ghostIndex) controlled by a particular agent
  149.              (ghostAgent) will move to in the supplied gameState.  All ghosts
  150.              must first be placed in the gameState using setGhostPositions
  151.              above.
  152.  
  153.              The ghost agent you are meant to supply is
  154.              self.ghostAgents[ghostIndex-1], but in this project all ghost
  155.              agents are always the same.
  156.        """
  157.         newParticles = []
  158.         for oldParticle in self.particles:
  159.             newParticle = list(oldParticle) # A list of ghost positions
  160.             # now loop through and update each entry in newParticle...
  161.  
  162.             "*** YOUR CODE HERE ***"
  163.  
  164.             "*** END YOUR CODE HERE ***"
  165.             newParticles.append(tuple(newParticle))
  166.         self.particles = newParticles
  167.  
  168.     def getBeliefDistribution(self):
  169.         "*** YOUR CODE HERE ***"
  170.         beliefs = util.Counter()
  171.         for p in self.particles:
  172.             beliefs[p] += 1.0
  173.         beliefs.normalize()
  174.         return beliefs
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top