def enhancedPacmanFeatures(state, action):
    """
    For each state, this function is called with each legal action.
    It should return a counter with { <feature name> : <feature value>, ... }
    """
    features = util.Counter()
    # *** YOUR CODE HERE ***
    succGameState = state.generateSuccessor(0, action)

    dist = 0

    for n in range(len(GameState.getGhostPositions(succGameState))):

        pac_location = GameState.getPacmanPosition(succGameState)
        ghost_loc = GameState.getGhostPositions(succGameState)
        dist += util.manhattanDistance(pac_location, ghost_loc[n])
        feat = 'dist'+str(n)
        features[feat] = util.manhattanDistance(pac_location, ghost_loc[n])



    if action == 'Stop':
        features['stopped'] += 1

    features['dist'] = dist
    features['foodCount'] = GameState.getNumFood(succGameState)
    features['power_pellet'] = len(GameState.getCapsules(succGameState))

    return features
Exemple #2
0
def betterEvaluationFunction(currentGameState: GameState) -> float:
    """
      Your extreme, unstoppable evaluation function (problem 4). Note that you can't fix a seed in this function.
    """
    score = currentGameState.getScore()
    numFoods = currentGameState.getNumFood()
    score -= 4.5 * numFoods
    pacPos = currentGameState.getPacmanPosition()
    numCapsules = len(currentGameState.getCapsules())
    ghostStates = [
        manhattanDistance(ghost, pacPos)
        for ghost in currentGameState.getGhostPositions()
    ]
    minGhost = min(ghostStates)
    index = ghostStates.index(minGhost)
    minGhost = 1.0 / minGhost
    if currentGameState.getGhostState(index + 1).scaredTimer > 5:
        minGhost *= -200
    score -= minGhost
    score -= numCapsules * 30
    if numFoods >= 1:
        foods = currentGameState.getFood().data
        num = 0
        arr = []
        for i in range(len(foods)):
            for j in range(len(foods[0])):
                if foods[i][j]:
                    arr.append((i, j))
                    num += 1
                    if num == numFoods:
                        break
            if num == numFoods:
                break
        nearest = min([util.manhattanDistance(pacPos, food) for food in arr])
        if numFoods > 1:
            score += 3.0 * pow(nearest, -1.0)
        else:
            score += 5.0 * pow(nearest, -1.0)
    return score
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 ***"
    # 首先判断当前是否已经结束
    if currentGameState.isWin():
        return float('inf')
    if currentGameState.isLose():
        return -float('inf')
    score = currentGameState.getScore()
    # 考虑food
    foods = currentGameState.getFood().asList()
    foodDis = [
        util.manhattanDistance(currentGameState.getPacmanPosition(), food)
        for food in foods
    ]
    foodDis.sort(reverse=True)
    numFoods = 3
    if currentGameState.getNumFood() < 3:
        numFoods = currentGameState.getNumFood()
    score -= foodDis[0] * 1.5
    # for i in range(numFoods):
    #     score-=(numFoods-i)*foodDis[i]
    # 如果附近有food 最好可以吃到
    score -= (currentGameState.getNumFood() * 4)
    # 考虑ghost
    ghostDis = [
        util.manhattanDistance(currentGameState.getPacmanPosition(), ghost)
        for ghost in currentGameState.getGhostPositions()
    ]
    score += max(3, min(ghostDis)) * 2
    # 考虑capsule
    score -= len(currentGameState.getCapsules()) * 4
    return score
Exemple #4
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>
    From the state we pulled pacman's position, ghost positions, food positions and capsule positions.
     We calculated the distance to all the food dots and the ghosts and used food dots as positive
     incentive and ghosts as negative incentive. We used number of capsules left as negative incentive
     to try to get pacman to eat capsules.
    """
    "*** YOUR CODE HERE ***"
    ghostScore : float = 1
    nearGhosts : float = 0
    foodScore : float = 0
    curScore = currentGameState.getScore()

    nearestFood = [(0, 0), float('inf')]
    pacPos = currentGameState.getPacmanPosition()
    foodPoss= currentGameState.getFood().asList()
    capsulePoss = currentGameState.getCapsules()
    ghostPoss = currentGameState.getGhostPositions()

    for foodPos in foodPoss:
        val = manhattanDistance(foodPos, pacPos)
        if val < nearestFood[1]:
            nearestFood[1] = val
            nearestFood[0] = foodPos
    foodScore = nearestFood[1]
    
    for gpos in ghostPoss:
        val = manhattanDistance(pacPos, gpos)
        if val <= 1:
            nearGhosts += (1-val)
        ghostScore += val

    return curScore - (1/ghostScore) + (1/foodScore) - nearGhosts - len(capsulePoss)