mail@pastecode.io avatar
16 days ago
2.0 kB
def foodLogicPlan(problem):
    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.
    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)))

    #locations = list(filter(lambda loc : loc not in walls_list, all_coords))
    non_wall_coords = [loc for loc in all_coords if loc not in walls_list]
    actions = [ 'North', 'South', 'East', 'West' ]

    KB = []

    "*** BEGIN YOUR CODE HERE ***"
    for x, y in food:
        KB.append(PropSymbolExpr(food_str, x, y, 0))

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

    for t in range(50):

        inp_wall = []
        for x, y in non_wall_coords:
            inp_wall.append(PropSymbolExpr(pacman_str, x, y, t))

        inp_food = []
        for x, y in food:
            inp_food.append(~PropSymbolExpr(food_str, x, y, t))
        food_goal = conjoin(inp_food)
        model = findModel(conjoin(KB) & food_goal)
        if model:
            return extractActionSequence(model, actions)

        inp_dir = []
        for dir in actions:
            inp_dir.append(PropSymbolExpr(dir, t))

        for x, y in non_wall_coords:
            KB.append(pacmanSuccessorStateAxioms(x, y, t+1, walls))
        for x, y in food:
            expr = (PropSymbolExpr(pacman_str, x, y, t) & PropSymbolExpr(food_str, x, y, t))
            expr2 = ~PropSymbolExpr(food_str, x, y, t+1)
            KB.append(expr2 % expr | ~PropSymbolExpr(food_str, x, y, t))
    "*** END YOUR CODE HERE ***"
Leave a Comment