Exemple #1
0
    def findPathToClosestDot(self, gameState: pacman.GameState):
        """
        Returns a path (a list of actions) to the closest dot, starting from
        gameState.
        """
        # Here are some useful elements of the startState
        startPosition = gameState.getPacmanPosition()
        food = gameState.getFood()
        walls = gameState.getWalls()
        problem = AnyFoodSearchProblem(gameState)

        # we don't know where the closest dot is, so let's estimate the closest dot
        import time
        start_time = time.time()

        # print("food (%s): %s" %(type(food), food))

        # getting the closest dot to set it as the goal
        problem.goal = __getClosestGoal__(
            startPosition,
            food.asList())  # so that the heuristic knows the goal

        import search
        astar = search.astar(problem, heuristic=euclideanHeuristic)
        print("findPathToClosestDot() took %2.5f seconds" %
              (time.time() - start_time))
        return astar
Exemple #2
0
def betterEvaluationFunction(currentGameState: GameState):
    """
    Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
    evaluation function (question 5).

    DESCRIPTION: <write something here so we know what you did>
    """
    "*** YOUR CODE HERE ***"
    pos = currentGameState.getPacmanPosition()
    foods = currentGameState.getFood()
    walls = currentGameState.getWalls()
    capsules = currentGameState.getCapsules()
    ghosts = currentGameState.getGhostStates()

    score = currentGameState.getScore()
    foods_cost = _food_cost(foods.asList(), pos, walls)
    capsules_cost = _food_cost(capsules, pos, walls)

    ghosts_dis, s_ghosts_dis = _ghost_cost(ghosts, pos, walls)

    def d(x):
        if x == 0:
            return float('inf')
        return 9 / (x**2)

    ghosts_cost = sum(map(d, ghosts_dis))
    s_ghosts_cost = sum(map(lambda x: x[0], filter(lambda x: x[0] < x[1], s_ghosts_dis)))

    return score - (2 * foods_cost) - capsules_cost - s_ghosts_cost - ghosts_cost
Exemple #3
0
 def __init__(self, startingGameState: pacman.GameState):
     self.start = (startingGameState.getPacmanPosition(),
                   startingGameState.getFood())
     self.walls = startingGameState.getWalls()
     self.startingGameState = startingGameState
     self._expanded = 0  # DO NOT CHANGE
     self.heuristicInfo = {
     }  # A dictionary for the heuristic to store information
Exemple #4
0
    def __init__(self, startingGameState: pacman.GameState):
        """
        Stores the walls, pacman's starting position and corners.
        """
        self.walls = startingGameState.getWalls()
        self.startingPosition = startingGameState.getPacmanPosition()
        top, right = self.walls.height - 2, self.walls.width - 2
        self.corners = ((1, 1), (1, top), (right, 1), (right, top))
        for corner in self.corners:
            if not startingGameState.hasFood(*corner):
                print('Warning: no food in corner ' + str(corner))
        self._expanded = 0  # DO NOT CHANGE; Number of search nodes expanded
        # Please add any code here which you would like to use
        # in initializing the problem
        "*** YOUR CODE HERE ***"
        self.startState = PositionWithFoods(self.startingPosition,
                                            self.corners)

        self.to_foods: Dict[Coordinate, Dict[Coordinate, float]] = {}
        for c in self.corners:
            queue = util.Queue()
            queue.push((c, 0))
            distances = {}
            while not queue.isEmpty():
                pos, dis = queue.pop()
                x, y = pos
                if pos in distances:
                    continue
                distances[pos] = dis
                for action in DIRECTIONS:
                    dx, dy = Actions.directionToVector(action)
                    next_pos = int(x + dx), int(y + dy)
                    nextx, nexty = next_pos
                    if next_pos not in distances and not self.walls[nextx][
                            nexty]:
                        queue.push((next_pos, dis + 1))
            self.to_foods[c] = distances

        self.to_other_foods = {}

        def tmp(foods, curr):
            key = (curr, tuple(sorted(foods)))
            if len(foods) == 1:
                self.to_other_foods[key] = 0
                return 0

            if key in self.to_other_foods:
                return self.to_other_foods[key]

            left_overs = list(foods)
            left_overs.remove(curr)
            self.to_other_foods[key] = min(
                map(lambda c: self.to_foods[curr][c] + tmp(left_overs, c),
                    left_overs))
            return self.to_other_foods[key]

        for c in self.corners:
            tmp(self.corners, c)
Exemple #5
0
    def __init__(self, gameState: pacman.GameState):
        "Stores information from the gameState.  You don't need to change this."
        # Store the food for later reference
        self.food = gameState.getFood()

        # Store info for the PositionSearchProblem (no need to change this)
        self.walls = gameState.getWalls()
        self.startState = gameState.getPacmanPosition()
        self.costFn = lambda x: 1
        self._visited, self._visitedlist, self._expanded = {}, [], 0  # DO NOT CHANGE
Exemple #6
0
    def __init__(self, start_game_state: GameState):
        super().__init__(start_game_state)
        self._expanded = 0  # DO NOT CHANGE; Number of search nodes expanded
        self.startingPosition = start_game_state.getPacmanPosition()

        self.capsules = tuple(start_game_state.getCapsules())
        self.foods = start_game_state.getFood()
        self.walls = start_game_state.getWalls()

        self.costFn = lambda x: 1
        self.start_game_state = start_game_state

        self.is_eating_capsule = True
Exemple #7
0
    def findPathToClosestDot(self, gameState: pacman.GameState):
        """
        Returns a path (a list of actions) to the closest dot, starting from
        gameState.
        """
        # Here are some useful elements of the startState
        startPosition = gameState.getPacmanPosition()
        food = gameState.getFood()
        walls = gameState.getWalls()
        problem = AnyFoodSearchProblem(gameState)

        "*** YOUR CODE HERE ***"
        return search.bfs(problem)
Exemple #8
0
def mazeDistance(point1, point2, gameState: pacman.GameState):
    """
    Returns the maze distance between any two points, using the search functions
    you have already built. The gameState can be any game state -- Pacman's
    position in that state is ignored.

    Example usage: mazeDistance( (2,4), (5,6), gameState)

    This might be a useful helper function for your ApproximateSearchAgent.
    """
    x1, y1 = point1
    x2, y2 = point2
    walls = gameState.getWalls()
    assert not walls[x1][y1], 'point1 is a wall: ' + str(point1)
    assert not walls[x2][y2], 'point2 is a wall: ' + str(point2)
    prob = PositionSearchProblem(gameState,
                                 start=point1,
                                 goal=point2,
                                 warn=False,
                                 visualize=False)
    return len(search.bfs(prob))