def evaluate(self, gstate: gamestate.Gamestate, move: util.Move):
        """
        This method is used by the reflex agent to determine
        the value of a given move if it would be used in a given gamestate.
        """
        closed_set = []
        open_set = [gstate]
        came_from = parent
        gScore = float("inf")
        gScore[0] = 0
        fScore = float("inf")
        fScore[0] = heurtistic(gstate.pacman, gstate.win)
        while open_set:
            current = min(fScore)
            if current == gstate.win:
                return path(came_from, current)

            open_set.remove(current)
            closed_set.append(current)
            for neighbor in current:
                if neighbor in closed_set:
                    continue
                if neighbor not in open_set:
                    open_set.append(neighbor)
                tentative_gScore = gScore[current] + util.manhattan(
                    current, neighbor)
                if tentative_gScore >= gScore[neighbor]:
                    continue
                came_from[neighbor] = current
                gScore[neighbor] = tentative_gScore
                fScore[neighbor] = gScore[neighbor] + heuristic(neighbor, goal)
        return failure
Example #2
0
def manhattan_path_cost(path):
    position = path[0]
    cost = 0
    for next_position in path[1:]:
        cost += util.manhattan(next_position, position)
        position = next_position
    return cost
Example #3
0
def better_evaluate(gstate):
    evaluate = 0
    if gstate.win:
        evaluate = float("inf")
    if gstate.loss:
        evaluate = float("-inf")

    dist_dots = 0
    for dot in gstate.dots:
        dist_dots += util.manhattan(dot, gstate.pacman)
    evaluate += dist_dots * 5
    dist_pellets = 0
    for pellet in gstate.pellets:
        dist_pellets += util.manhattan(pellet, gstate.pacman)
    evaluate += dist_pellets * 8
    dist_ghosts = 0
    for ghost in gstate.ghosts:
        dist_ghosts += util.manhattan(ghost, gstate.pacman)
    evaluate -= dist_ghosts * 10
    return evaluate
Example #4
0
    def evaluate(self, gstate, move):
        dots = gstate.dots.list()
        gstate.apply_move(0, move)

        if gstate.win:
            return float("inf")
        if gstate.loss:
            return float("-inf")
        eval = 0
        pacpos = gstate.pacman
        if move != util.Move.stop:
            if pacpos in gstate.ghosts:
                eval -= 1500
            if pacpos != None:
                for ghost in gstate.ghosts:
                    if gstate.timers[0] > 15:
                        if util.manhattan(pacpos, ghost) < 3:
                            eval += 100
                        if pacpos == gstate.ghosts:
                            eval += 200
                    else:
                        if util.manhattan(pacpos, ghost) > 2:
                            eval += 1000
            if pacpos == gstate.pellets:
                eval += 1500
            if pacpos != None:
                for dot in dots:
                    if util.manhattan(pacpos, dot) < 3:
                        eval += 500
                        if util.manhattan(pacpos, dot) < 2:
                            eval += 300
                            if util.manhattan(pacpos, dot) < 1:
                                eval += 100
                    if pacpos == dot:
                        eval += 900
            return eval
        else:
            eval -= 500
            return eval
Example #5
0
def dots_heuristic(state, representation):
    if state.dots:
        left, bottom = 1, 1
        right, top = representation.walls.shape - 2 * util.Vector.unit
        corners = frozenset([util.Vector(left, bottom), util.Vector(left, top),
                             util.Vector(right, bottom), util.Vector(right, top)])
        closest_to_corners = {min(state.dots, key=lambda dot: sum(abs(dot - corner))) for corner in corners}
        return min(manhattan_path_cost((state.vector,) + corner_path) for corner_path in itertools.permutations(closest_to_corners))
    else:
        return 0
    
    # Some easier alternatives:
    return max(util.manhattan(state.vector, dot) for dot in state.dots)
    return len(state.dots.list())
Example #6
0
    def evaluate(self, gstate, move):
        newState = gstate.successor(0, move)
        # copyState = gstate.copy
        # copyState.apply_move(0,move)
        score = 0
        # if successor state is a win -> high score
        if newState.win:
            return math.inf
        # if successor state is a loss -> low sore
        if newState.loss:
            return -math.inf
        #Do not stop
        if move == util.Move.stop:
            score -= 200
        #More distance to ghost is better
        minDistGhost = 10000
        closestGhost = None
        for x in newState.ghosts:
            ghostDist = util.manhattan(newState.pacman, x)
            if ghostDist < minDistGhost:
                minDistGhost = ghostDist
                closestGhost = x
        if gstate.timers[0] == 0:
            if util.manhattan(newState.pacman, closestGhost) > 5:
                score += 500
            else:
                score -= 500
        if gstate.timers[0] > 0:
            if util.manhattan(gstate.pacman, closestGhost) < 3:
                if util.manhattan(newState.pacman,
                                  closestGhost) < util.manhattan(
                                      gstate.pacman, closestGhost):
                    score += 500

        minFood = 10000
        closestDot = None
        for x in newState.dots.list() + newState.pellets.list():
            foodDist = util.manhattan(newState.pacman, x)
            if foodDist < minFood:
                minFood = foodDist
                closestDot = x
        # Less distance to closest dot or less food left -> higher score
        if util.manhattan(newState.pacman, closestDot) < util.manhattan(
                gstate.pacman, closestDot):
            score += 200
        return score