Exemple #1
0
    def evaluationFunction(self, currentGameState: GameState, action: str) -> float:
        """
        Design a better evaluation function here.

        The evaluation function takes in the current and proposed successor
        GameStates (pacman.py) and returns a number, where higher numbers are better.

        The code below extracts some useful information from the state, like the
        remaining food (newFood) and Pacman position after moving (newPos).
        newScaredTimes holds the number of moves that each ghost will remain
        scared because of Pacman having eaten a power pellet.

        Print out these variables to see what you're getting, then combine them
        to create a masterful evaluation function.
        """
        # Useful information you can extract from a GameState (pacman.py)
        successorGameState = currentGameState.generatePacmanSuccessor(action)
        walls = successorGameState.getWalls()
        width = walls.width
        height = walls.height
        newPos = successorGameState.getPacmanPosition()
        newFood = successorGameState.getFood()
        newGhostStates = successorGameState.getGhostStates()
        newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]

        "*** YOUR CODE HERE ***"
        ghosts = Grid(width, height)
        for i in range(len(newGhostStates)):
            if newScaredTimes[i] <= 0:
                x, y = newGhostStates[i].getPosition()
                ghosts[int(x)][int(y)] = True

        queue = util.Queue()
        queue.push((newPos, 0))
        visited = set()
        shortest = float('inf')
        ghosts_dis = []
        while not queue.isEmpty():
            cur, dis = queue.pop()
            x, y = cur
            if in_range(cur, width, height) and not walls[x][y] and cur not in visited:
                visited.add(cur)
                if newFood[x][y]:
                    shortest = min(dis, shortest)
                if ghosts[x][y]:
                    ghosts_dis.append(dis)
                for d in DIRECTIONS:
                    queue.push(((x + d[0], y + d[1]), dis + 1))
        if shortest == float('inf'):
            shortest = 0
            
        score = successorGameState.getScore()
        def d(x):
            if x == 0:
                return float('inf')
            return 9 / (x**2)
        score -= shortest + sum(map(d, ghosts_dis))
        if action == 'Stop':
            score -= 10
        return score
Exemple #2
0
    def evaluationFunction(self, currentGameState: GameState,
                           action: str) -> float:
        """
        The evaluation function takes in the current and proposed successor
        GameStates (pacman.py) and returns a number, where higher numbers are better.

        The code below extracts some useful information from the state, like the
        remaining food (oldFood) and Pacman position after moving (newPos).
        newScaredTimes holds the number of moves that each ghost will remain
        scared because of Pacman having eaten a power pellet.
        """
        # Useful information you can extract from a GameState (pacman.py)
        successorGameState = currentGameState.generatePacmanSuccessor(action)
        newPos = successorGameState.getPacmanPosition()
        oldFood = currentGameState.getFood()
        newGhostStates = successorGameState.getGhostStates()
        newScaredTimes = [
            ghostState.scaredTimer for ghostState in newGhostStates
        ]

        return successorGameState.getScore()