Example #1
0
def betterEvaluationFunction(currentGameState):
    """
      Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
      evaluation function.

      DESCRIPTION: <write something here so we know what you did>
    """
    from searchAgents import manhattan_distance

    import sys

    newPos             = currentGameState.getPacmanPosition()
    newFood            = currentGameState.getFood()
    newGhostStates     = currentGameState.getGhostStates()
    newScaredTimes     = [ghostState.scaredTimer for ghostState in newGhostStates]

    # Find the nearest ghost.
    ghost_distance = sys.maxint
    for ghost_state in newGhostStates:
        dist = manhattan_distance(ghost_state.getPosition(), newPos)
        ghost_distance = min(ghost_distance, dist)

    # Find the nearest food.
    food_distance = sys.maxint
    for x in range(newFood.width):
        for y in range(newFood.height):
            if newFood[x][y]:
                dist = manhattan_distance(newPos, (x,y))
                food_distance = min(food_distance, dist)

    # Make sure we can handle situations without any ghosts or food.
    if ghost_distance == sys.maxint: ghost_distance = 0
    if food_distance  == sys.maxint: food_distance  = 0

    # Start with the 'default' state score.
    score = currentGameState.getScore()

    # The ghost is nearby - penalize Pacman by slapping him across the face
    # with negative points. Since our evaluation need not be continuous (ie.
    # it's ok to have abrupt changes missing derivatives), using an if-
    # statement here is a cheap way to make this evaluation function pretty
    # much unbeatable. One could also imagine some kind of polynomial
    # function mimicking the if-statement without breaking continuity.
    if ghost_distance < 6:
        score -= 100.0 / (ghost_distance+1)

    # Help Pacman detect nearby food slightly.
    score += 1.0 / (food_distance+1)

    return score
Example #2
0
    def evaluationFunction(self, currentGameState, action):
        """
        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)
        newPos             = successorGameState.getPacmanPosition()
        newFood            = successorGameState.getFood()
        newGhostStates     = successorGameState.getGhostStates()
        newScaredTimes     = [ghostState.scaredTimer for ghostState in newGhostStates]

        """
        här är skitkoden som funkar dåligt.

        from searchAgents import manhattan_distance

        score = successorGameState.getScore()

        # gd = närmaste spöke
        gd = sys.maxint
        for ghost_state in newGhostStates:
            gd = min(gd, manhattan_distance(ghost_state.getPosition(), newPos))

        # nf = närmaste mat
        nf = sys.maxint
        for x in range(newFood.width):
            for y in range(newFood.height):
                if newFood[x][y]: nf = min(nf, manhattan_distance(newPos, (x,y)))

        if gd == sys.maxint: gd = 0
        if nf == sys.maxint: nf = 0

        score += 10.0/(nf+1)**0.25
        score += 10*gd**0.5*0.07
        """

        from searchAgents import manhattan_distance

        import sys

        # Find the nearest ghost.
        ghost_distance = sys.maxint
        for ghost_state in newGhostStates:
            dist = manhattan_distance(ghost_state.getPosition(), newPos)
            ghost_distance = min(ghost_distance, dist)

        # Find the nearest food.
        food_distance = sys.maxint
        for x in range(newFood.width):
            for y in range(newFood.height):
                if newFood[x][y]:
                    dist = manhattan_distance(newPos, (x,y))
                    food_distance = min(food_distance, dist)

        # Make sure we can handle situations without any ghosts or food.
        if ghost_distance == sys.maxint: ghost_distance = 0
        if food_distance  == sys.maxint: food_distance  = 0

        # Start with the 'default' state score.
        score = successorGameState.getScore()

        # The ghost is nearby - penalize Pacman by slapping him across the face
        # with negative points. Since our evaluation need not be continuous (ie.
        # it's ok to have abrupt changes missing derivatives), using an if-
        # statement here is a cheap way to make this evaluation function pretty
        # much unbeatable. One could also imagine some kind of polynomial
        # function mimicking the if-statement without breaking continuity.
        if ghost_distance < 6:
            score -= 100.0 / (ghost_distance+1)

        # Help Pacman detect nearby food slightly.
        score += 1.0 / (food_distance+1)

        return score