Untitled

 avatar
unknown
plain_text
2 years ago
2.6 kB
4
Indexable
def foodLogicPlan(problem) -> List:
    """
    Given an instance of a FoodPlanningProblem, return a list of actions that help Pacman
    eat all of the food.
    Available actions are ['North', 'East', 'South', 'West']
    Note that STOP is not an available action.
    Overview: add knowledge incrementally, and query for a model each timestep. Do NOT use pacphysicsAxioms.
    """
    walls = problem.walls
    width, height = problem.getWidth(), problem.getHeight()
    walls_list = walls.asList()
    (x0, y0), food = problem.start
    food = food.asList()

    # Get lists of possible locations (i.e. without walls) and possible actions
    all_coords = list(itertools.product(range(width + 2), range(height + 2)))

    non_wall_coords = [loc for loc in all_coords if loc not in walls_list]
    actions = ["North", "South", "East", "West"]

    KB = []

    KB.append(PropSymbolExpr(pacman_str, x0, y0, time=0))

    # Initialize Food[x,y]_t variables with the code
    # PropSymbolExpr(food_str, x, y, time=t), where each variable is
    # true if and only if there is a food at (x, y) at time t.
    for x, y in food:
        KB.append(PropSymbolExpr(food_str, x, y, time=0))

    for t in range(0, 50):
        print("t = ", t)

        possibleCoords = []
        for x, y in non_wall_coords:
            possibleCoords.append(PropSymbolExpr(pacman_str, x, y, time=t))
        KB.append(exactlyOne(possibleCoords))

        allFood = []
        for x, y in non_wall_coords:
            allFood.append(PropSymbolExpr(food_str, x, y, time=t))

        # goal assertion sentence must be true if and only if all of
        # the food have been eaten. This happens when all Food[x,y]_t
        # are false.
        model = findModel(
            conjoin(conjoin(KB), ~disjoin(allFood)),
        )
        if model:
            return extractActionSequence(model, actions)

        actionlist = []
        for action in actions:
            actionlist.append(PropSymbolExpr(action, time=t))
        KB.append(exactlyOne(actionlist))

        transitionModelSentences = []
        for x, y in non_wall_coords:
            transitionModelSentences.append(
                pacmanSuccessorAxiomSingle(x, y, t + 1, walls)
            )
        KB += transitionModelSentences

        # food successor axiom
        for x, y in non_wall_coords:
            P1 = PropSymbolExpr(food_str, x, y, time=t)
            P2 = ~PropSymbolExpr(pacman_str, x, y, time=t)
            C = PropSymbolExpr(food_str, x, y, time=t + 1)
            KB.append(C % conjoin(P1, P2))
Editor is loading...
Leave a Comment