Ejemplo n.º 1
0
 def _computeNoisyPositions(self, state):
     """
         Compute a noisy position from true ghosts positions.
         XXX: DO NOT MODIFY THAT FUNCTION !!!
         Doing so will result in a 0 grade.
     """
     positions = state.getGhostPositions()
     w = self.args.w
     w2 = 2 * w + 1
     div = float(w2 * w2)
     new_positions = []
     for p in positions:
         (x, y) = p
         dist = util.Counter()
         for i in range(x - w, x + w + 1):
             for j in range(y - w, y + w + 1):
                 dist[(i, j)] = 1.0 / div
         dist.normalize()
         new_positions.append(util.chooseFromDistribution(dist))
     return new_positions
    def update_belief_state(self, evidences, pacman_position):
        """
        Given a list of (noised) distances from pacman to ghosts,
        returns a list of belief states about ghosts positions

        Arguments:
        ----------
        - `evidences`: list of distances between
          pacman and ghosts at state x_{t}
          where 't' is the current time step
        - `pacman_position`: 2D coordinates position
          of pacman at state x_{t}
          where 't' is the current time step

        Return:
        -------
        - A list of Z belief states at state x_{t}
          as N*M numpy mass probability matrices
          where N and M are respectively width and height
          of the maze layout and Z is the number of ghosts.

        N.B. : [0,0] is the bottom left corner of the maze
        """
        beliefStates = self.beliefGhostStates

        # XXX: Your code here

        height = self.walls.height
        width = self.walls.width

        z = 0
        while z < len(beliefStates):

            for m in range(0, width):

                for n in range(0, height):

                    if (not self.walls[m][n]) and (not pacman_position
                                                   == (m, n)):

                        currDistance = util.manhattanDistance(
                            pacman_position, (m, n))
                        sigma = np.sqrt(self.sensor_variance)
                        mu = currDistance
                        sModel = (1 / (np.sqrt(2 * np.pi) * sigma)) * np.exp(
                            -np.power(evidences[z] - mu, 2.) /
                            (2 * np.power(sigma, 2.)))
                        tModel = util.Counter()

                        if self.ghost_type == "scared": k = 3
                        if self.ghost_type == "afraid": k = 1
                        if self.ghost_type == "confused": k = 0

                        for i in range(0, width):
                            for j in range(0, height):

                                succDistance = util.manhattanDistance(
                                    pacman_position, (i, j))

                                if ((m + 1 == i and n == j) or
                                    (m - 1 == i and n == j) or
                                    (m == i and n + 1 == j) or
                                    (m == i and n - 1 == j)) and (
                                        not self.walls[i][j]):

                                    if succDistance > currDistance:
                                        tModel[(i, j)] = np.power(2, k)
                                    else:
                                        tModel[(i, j)] = 1

                                else:
                                    tModel[(i, j)] = 0

                        tModel.normalize()

                        epsilon = 0

                        for i in range(0, width):
                            for j in range(0, height):

                                epsilon = epsilon + tModel[
                                    (i, j)] * beliefStates[z][i][j]

                        beliefStates[z][m][n] = sModel * epsilon

                    else:
                        beliefStates[z][m][n] = 0
            #On peut mtn calculer alpha.

            alpha = np.sum(beliefStates[z])
            beliefStates[z] = np.divide(beliefStates[z], alpha)

            z = z + 1

        # XXX: End of your code

        self.beliefGhostStates = beliefStates

        return beliefStates