Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
6
Indexable
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))
        KB.append(exactlyOne(inp_wall))

        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))
        KB.append(exactlyOne(inp_dir))

        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