Exemple #1
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 #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.
  """

    # BEGIN_YOUR_CODE (our solution is 13 lines of code, but don't worry if you deviate from this)
    def getDistFromPacman(x, y):
        pacmanPos = currentGameState.getPacmanPosition()
        return abs(pacmanPos[0] - x) + abs(pacmanPos[1] - y)

    def exponentiallyWeightedScore(objectives):
        return sum(2**(-1 *
                       getDistFromPacman(objectivePos[0], objectivePos[1]))
                   for objectivePos in objectives)

    def getFoodScore(foodGrid, pacmanPosition):
        foodsPos = [(x, y) for y in range(foodGrid.height)
                    for x in range(foodGrid.width) if foodGrid[x][y] == True]
        return exponentiallyWeightedScore(foodsPos)

    food = 9 * getFoodScore(currentGameState.getFood(),
                            currentGameState.getPacmanPosition())
    ghostStates = currentGameState.getGhostStates()
    scary = sum(ghostState.scaredTimer for ghostState in ghostStates) > 0
    capsule = 100 if scary else 200 * exponentiallyWeightedScore(
        currentGameState.getCapsules())
    scaredGhostsPos = [
        ghostState.getPosition() for ghostState in ghostStates
        if ghostState.scaredTimer > 0
    ]
    ghost = 200 * exponentiallyWeightedScore(scaredGhostsPos)
    score = currentGameState.getScore()
    return food + capsule + ghost + 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>
    """
    features = [0.]*8
    weights = [20000,10,1,1,45,0.5,3,1]
    #1/number_food,1/cloest_food_dis,
    #1/num_pallets, 1/cloest_pallet, if_empty_pallet(encourage to eat the last pallet), scared_time(encourage the pacman to eat pallet to increase scared_time)
    #encourage to eat the ghost, some scared time becomes 0, some are not
    #1/distance_to_ghost
    #weights = []
    if currentGameState.isWin():
        #print(currentGameState.getFood().asList())
        return float("inf")
    if currentGameState.isLose():
        return float("-inf")
    pac_pos = currentGameState.getPacmanPosition()
    food_pos = currentGameState.getFood().asList()
    num_food = len(food_pos)
    ghost_states = currentGameState.getGhostStates()
    ghost_scared_times = [ghostState.scaredTimer for ghostState in ghost_states]
    ghost_pos = [g.getPosition() for g in ghost_states]
    pallets = currentGameState.getCapsules()
    features[0] = 1/num_food
    food_dis = sorted([util.manhattanDistance(fp,pac_pos) for fp in food_pos])
    features[1] = 1/food_dis[0]
    pallets_dis = sorted([util.manhattanDistance(p,pac_pos) for p in pallets])
    if pallets_dis:
        features[2] = 1/len(pallets_dis)
        features[3] = 1/pallets_dis[0]
    features[4] = int(not pallets) # if pallets become zero, we need to encourage this
    features[5] = sum(ghost_scared_times)
    features[6] = 1 if 0 in ghost_scared_times and sum(ghost_scared_times)>0 else 0
    # if some ghost's scared time becomes zero, some are not, then some ghost is eaten
    # for those states after the ghost is eaten, this will always be 1, but does not afftect the action selection, because we compare the relative relation
    ghost_dis = [util.manhattanDistance(g, pac_pos) for g in ghost_pos]
    minDis = min(ghost_dis)
    min_dis_ghost_inds = [i for i in range(len(ghost_dis)) if ghost_dis[i] == minDis]
    min_dis_ghost_scared_times = [ghost_scared_times[i] for i in min_dis_ghost_inds]
    if any(st < minDis for st in min_dis_ghost_scared_times):
        features[7] = -1.0 / minDis  # if any closet ghost has no enough scared time, then the agent is in danger
    else:
        features[7] = 1.0 / minDis  # otherwise, there is no danger
    return sum(w*f for w,f in zip(weights,features))