def mapping(problem, agent): ''' problem: a MappingProblem instance agent: a MappingLogicAgent instance ''' debug = False pac_x_0, pac_y_0 = problem.startState KB = [] all_coords = list( itertools.product(range(problem.getWidth() + 2), range(problem.getHeight() + 2))) non_outer_wall_coords = list( itertools.product(range(1, problem.getWidth() + 1), range(1, problem.getHeight() + 1))) # map describes what we know, for GUI rendering purposes. -1 is unknown, 0 is open, 1 is wall known_map = [[-1 for y in range(problem.getHeight() + 2)] for x in range(problem.getWidth() + 2)] known_map_by_timestep = [] # Pacman knows that the outer border of squares are all walls outer_wall_sent = [] for x, y in all_coords: if ((x == 0 or x == problem.getWidth() + 1) or (y == 0 or y == problem.getHeight() + 1)): known_map[x][y] = 1 outer_wall_sent.append(PropSymbolExpr(wall_str, x, y)) KB.append(conjoin(outer_wall_sent)) KB.append(PropSymbolExpr(pacman_str, pac_x_0, pac_y_0, 0)) for t in range(agent.num_timesteps): KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(agent.actions[t], t)) KB.append(sensorAxioms(t, non_outer_wall_coords)) percept_rules = four_bit_percept_rules(t, agent.getPercepts()) KB.append(percept_rules) for xy in non_outer_wall_coords: wall_present = PropSymbolExpr(wall_str, xy[0], xy[1]) model1 = findModel(conjoin(conjoin(KB), wall_present)) model2 = findModel(conjoin(conjoin(KB), ~wall_present)) if model2 is False: known_map[xy[0]][xy[1]] = 1 KB.append(wall_present) if model1 is False: known_map[xy[0]][xy[1]] = 0 KB.append(~wall_present) if model1 and model2: known_map[xy[0]][xy[1]] = -1 known_map_by_timestep.append(copy.deepcopy(known_map)) agent.moveToNextState(agent.actions[t]) KB.append( allLegalSuccessorAxioms(t + 1, known_map_by_timestep[t], non_outer_wall_coords)) return known_map_by_timestep
def pacphysics_axioms(t, all_coords, non_outer_wall_coords): """ Given: t: timestep all_coords: list of (x, y) coordinates of the entire problem non_outer_wall_coords: list of (x, y) coordinates of the entire problem, excluding the outer border (these are the actual squares pacman can possibly be in) Return a logic sentence containing all of the following: - for all (x, y) in all_coords: If a wall is at (x, y) --> Pacman is not at (x, y) - Pacman is at exactly one of the squares at timestep t. - Pacman takes exactly one action at timestep t. """ pacphysics_sentences = [] "*** BEGIN YOUR CODE HERE ***" for x, y in all_coords: s = PropSymbolExpr(wall_str, x, y) >> ~PropSymbolExpr(pacman_str, x, y, t) pacphysics_sentences.append(s) inp_wall = [] for x, y in non_outer_wall_coords: inp_wall.append(PropSymbolExpr(pacman_str, x, y, t)) pacphysics_sentences.append(exactlyOne(inp_wall)) inp_dir = [] for dir in DIRECTIONS: inp_dir.append(PropSymbolExpr(dir, t)) pacphysics_sentences.append(exactlyOne(inp_dir)) return conjoin(pacphysics_sentences) "*** END YOUR CODE HERE ***" return conjoin(pacphysics_sentences)
def positionLogicPlan(problem): """ Given an instance of a PositionPlanningProblem, return a list of actions that lead to the goal. 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 = problem.startState xg, yg = problem.goal # 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 = [] pos_t0 = PropSymbolExpr(pacman_str, x0, y0, 0) KB.append(pos_t0) for t in range(50): nonwallpos = [ PropSymbolExpr(pacman_str, xy[0], xy[1], t) for xy in non_wall_coords ] KB.append(exactlyOne(nonwallpos)) goal_assertion = PropSymbolExpr(pacman_str, xg, yg, t) model = findModel(conjoin(conjoin(KB), goal_assertion)) if model: return extractActionSequence(model, actions) action_list = [PropSymbolExpr(action, t) for action in actions] KB.append(exactlyOne(action_list)) for xy in non_wall_coords: KB.append(pacmanSuccessorStateAxioms(xy[0], xy[1], t + 1, walls))
def pacphysics_axioms(t, all_coords, non_outer_wall_coords): """ Given: t: timestep all_coords: list of (x, y) coordinates of the entire problem non_outer_wall_coords: list of (x, y) coordinates of the entire problem, excluding the outer border (these are the actual squares pacman can possibly be in) Return a logic sentence containing all of the following: - for all (x, y) in all_coords: If a wall is at (x, y) --> Pacman is not at (x, y) - Pacman is at one of the non_outer_wall_coords. - Pacman is at exactly one of the squares at timestep t. - Pacman takes one of the four actions in DIRECTIONS - Pacman takes exactly one action at timestep t. """ pacphysics_sentences = [] "*** BEGIN YOUR CODE HERE ***" all_coords_elements = [] for i in all_coords: x = i[0] y = i[1] all_coords_elements.append(PropSymbolExpr(wall_str, x, y) >> ~PropSymbolExpr(pacman_str, x, y ,t)) first = conjoin(all_coords_elements) second = exactlyOne([PropSymbolExpr(pacman_str, i[0], i[1], t) for i in non_outer_wall_coords]) third = exactlyOne([PropSymbolExpr(action, t) for action in DIRECTIONS]) pacphysics_sentences.append(first) pacphysics_sentences.append(second) pacphysics_sentences.append(third) "*** END YOUR CODE HERE ***" return conjoin(pacphysics_sentences)
def SLAMSensorAxioms(t, non_outer_wall_coords): all_percept_exprs = [] combo_var_def_exprs = [] for direction in DIRECTIONS: percept_exprs = [] dx, dy = DIR_TO_DXDY_MAP[direction] for x, y in non_outer_wall_coords: combo_var = PropSymbolExpr(pacman_wall_str, x, y, t, x + dx, y + dy) percept_exprs.append(combo_var) combo_var_def_exprs.append(combo_var % (PropSymbolExpr(pacman_str, x, y, t) & PropSymbolExpr(wall_str, x + dx, y + dy))) blocked_dir_clause = PropSymbolExpr(blocked_str_map[direction], t) all_percept_exprs.append(blocked_dir_clause % disjoin(percept_exprs)) percept_to_blocked_sent = [] for n in range(1, 4): wall_combos_size_n = itertools.combinations(blocked_str_map.values(), n) n_walls_blocked_sent = disjoin([ conjoin([PropSymbolExpr(blocked_str, t) for blocked_str in wall_combo]) for wall_combo in wall_combos_size_n]) # n_walls_blocked_sent is of form: (N & S) | (N & E) | ... percept_to_blocked_sent.append( PropSymbolExpr(geq_num_adj_wall_str_map[n], t) % n_walls_blocked_sent) return conjoin(all_percept_exprs + combo_var_def_exprs + percept_to_blocked_sent)
def positionLogicPlan(problem): """ Given an instance of a PositionPlanningProblem, return a list of actions that lead to the goal. 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 = problem.startState xg, yg = problem.goal # 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 = [] "*** BEGIN YOUR CODE HERE ***" KB.append(PropSymbolExpr(pacman_str, x0, y0, 0)) for t in range(50): KB.append( exactlyOne([ PropSymbolExpr(pacman_str, x, y, t) for x, y in non_wall_coords ])) model = findModel( conjoin(conjoin(KB), PropSymbolExpr(pacman_str, xg, yg, t))) if (model != False): return extractActionSequence(model, actions) KB.append(exactlyOne([PropSymbolExpr(action, t) for action in actions])) KB.append(allLegalSuccessorAxioms(t + 1, walls, non_wall_coords)) "*** END YOUR CODE HERE ***"
def pacmanSuccessorStateAxioms(x, y, t, walls_grid, var_str=pacman_str): """ Successor state axiom for state (x,y,t) (from t-1), given the board (as a grid representing the wall locations). Current <==> (previous position at time t-1) & (took action to move to x, y) Available actions are ['North', 'East', 'South', 'West'] Note that STOP is not an available action. """ possibilities = [] if not walls_grid[x][y + 1]: possibilities.append( PropSymbolExpr(var_str, x, y + 1, t - 1) & PropSymbolExpr('South', t - 1)) if not walls_grid[x][y - 1]: possibilities.append( PropSymbolExpr(var_str, x, y - 1, t - 1) & PropSymbolExpr('North', t - 1)) if not walls_grid[x + 1][y]: possibilities.append( PropSymbolExpr(var_str, x + 1, y, t - 1) & PropSymbolExpr('West', t - 1)) if not walls_grid[x - 1][y]: possibilities.append( PropSymbolExpr(var_str, x - 1, y, t - 1) & PropSymbolExpr('East', t - 1)) if not possibilities: return None return PropSymbolExpr(var_str, x, y, t) % disjoin(possibilities)
def four_bit_percept_rules(t, percepts): """ Localization and Mapping both use the 4 bit sensor, which tells us True/False whether a wall is to pacman's north, south, east, and west. """ percept_unit_clauses = [] for wall_present, direction in zip(percepts, DIRECTIONS): percept_unit_clause = PropSymbolExpr(blocked_str_map[direction], t) if not wall_present: percept_unit_clause = ~PropSymbolExpr(blocked_str_map[direction], t) percept_unit_clauses.append(percept_unit_clause) # The actual sensor readings return conjoin(percept_unit_clauses)
def localization(problem, agent): ''' problem: a LocalizationProblem instance agent: a LocalizationLogicAgent instance ''' debug = False walls_grid = problem.walls walls_list = walls_grid.asList() all_coords = list( itertools.product(range(problem.getWidth() + 2), range(problem.getHeight() + 2))) non_outer_wall_coords = list( itertools.product(range(1, problem.getWidth() + 1), range(1, problem.getHeight() + 1))) possible_locs_by_timestep = [] KB = [] for xy in all_coords: if xy in walls_list: KB.append(PropSymbolExpr(wall_str, xy[0], xy[1])) else: KB.append(~PropSymbolExpr(wall_str, xy[0], xy[1])) for t in range(agent.num_timesteps): KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(agent.actions[t], t)) KB.append(sensorAxioms(t, non_outer_wall_coords)) percept_rules = four_bit_percept_rules(t, agent.getPercepts()) KB.append(percept_rules) possible_locations_t = [] for xy in non_outer_wall_coords: pacman_present = PropSymbolExpr(pacman_str, xy[0], xy[1], t) model1 = findModel(conjoin(conjoin(KB), pacman_present)) model2 = findModel(conjoin(conjoin(KB), ~pacman_present)) if model2 is False: possible_locations_t.append(xy) KB.append(pacman_present) if model1 is False: KB.append(~pacman_present) if model1 and model2: possible_locations_t.append(xy) possible_locs_by_timestep.append(possible_locations_t) agent.moveToNextState(agent.actions[t]) KB.append( allLegalSuccessorAxioms(t + 1, walls_grid, non_outer_wall_coords)) return possible_locs_by_timestep
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 ***"
def sensorAxioms(t, non_outer_wall_coords): all_percept_exprs = [] combo_var_def_exprs = [] for direction in DIRECTIONS: percept_exprs = [] dx, dy = DIR_TO_DXDY_MAP[direction] for x, y in non_outer_wall_coords: combo_var = PropSymbolExpr(pacman_wall_str, x, y, t, x + dx, y + dy) percept_exprs.append(combo_var) combo_var_def_exprs.append(combo_var % ( PropSymbolExpr(pacman_str, x, y, t) & PropSymbolExpr(wall_str, x + dx, y + dy))) percept_unit_clause = PropSymbolExpr(blocked_str_map[direction], t) all_percept_exprs.append(percept_unit_clause % disjoin(percept_exprs)) return conjoin(all_percept_exprs + combo_var_def_exprs)
def check_location_satisfiability(x1_y1, x0_y0, action0, action1, problem): """ Given: - x1_y1 = (x1, y1), a potential location at time t = 1 - x0_y0 = (x0, y0), Pacman's location at time t = 0 - action0 = one of the four items in DIRECTIONS, Pacman's action at time t = 0 - problem = An instance of logicAgents.LocMapProblem Return: - a model proving whether Pacman is at (x1, y1) at time t = 1 - a model proving whether Pacman is not at (x1, y1) at time t = 1 """ walls_grid = problem.walls walls_list = walls_grid.asList() all_coords = list( itertools.product(range(problem.getWidth() + 2), range(problem.getHeight() + 2))) non_outer_wall_coords = list( itertools.product(range(1, problem.getWidth() + 1), range(1, problem.getHeight() + 1))) KB = [] x0, y0 = x0_y0 x1, y1 = x1_y1 # We know which coords are walls: map_sent = [PropSymbolExpr(wall_str, x, y) for x, y in walls_list] KB.append(conjoin(map_sent)) "*** BEGIN YOUR CODE HERE ***" raise NotImplementedError "*** END YOUR CODE HERE ***"
def mapping(problem, agent): ''' problem: a MappingProblem instance agent: a MappingLogicAgent instance ''' debug = False pac_x_0, pac_y_0 = problem.startState KB = [] all_coords = list(itertools.product(range(problem.getWidth()+2), range(problem.getHeight()+2))) non_outer_wall_coords = list(itertools.product(range(1, problem.getWidth()+1), range(1, problem.getHeight()+1))) #map describes what we know, for GUI rendering purposes. -1 is unknown, 0 is open, 1 is wall known_map = [[-1 for y in range(problem.getHeight()+2)] for x in range(problem.getWidth()+2)] known_map_by_timestep = [] # Pacman knows that the outer border of squares are all walls outer_wall_sent = [] for x, y in all_coords: if ((x == 0 or x == problem.getWidth() + 1) or (y == 0 or y == problem.getHeight() + 1)): known_map[x][y] = 1 outer_wall_sent.append(PropSymbolExpr(wall_str, x, y)) KB.append(conjoin(outer_wall_sent)) "*** BEGIN YOUR CODE HERE ***" KB.append(PropSymbolExpr(pacman_str, pac_x_0, pac_y_0, 0)) for t in range(agent.num_timesteps): # Add pacphysics, action, sensor, and percept information to KB KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(agent.actions[t], t)) KB.append(sensorAxioms(t, non_outer_wall_coords)) KB.append(four_bit_percept_rules(t, agent.getPercepts())) # Find provable wall locations with updated KB for i in non_outer_wall_coords: x, y = i # find a model such that (x,y) is wall model1 = findModel(conjoin(KB) & PropSymbolExpr(wall_str, x, y)) # find a model such that (x,y) is not wall model2 = findModel(conjoin(KB) & ~PropSymbolExpr(wall_str, x, y)) if not model2: KB.append(PropSymbolExpr(wall_str, x, y)) known_map[x][y] = 1 elif not model1: KB.append(~PropSymbolExpr(wall_str, x, y)) known_map[x][y] = 0 map_copy = copy.deepcopy(known_map) known_map_by_timestep.append(map_copy) agent.moveToNextState(agent.actions[t]) KB.append(allLegalSuccessorAxioms(t + 1, known_map, non_outer_wall_coords)) "*** END YOUR CODE HERE ***" return known_map_by_timestep
def check_location_satisfiability(x1_y1, x0_y0, action0, action1, problem): """ Given: - x1_y1 = (x1, y1), a potential location at time t = 1 - x0_y0 = (x0, y0), Pacman's location at time t = 0 - action0 = one of the four items in DIRECTIONS, Pacman's action at time t = 0 - problem = An instance of logicAgents.LocMapProblem Return: - a model proving whether Pacman is at (x1, y1) at time t = 1 - a model proving whether Pacman is not at (x1, y1) at time t = 1 """ walls_grid = problem.walls walls_list = walls_grid.asList() all_coords = list( itertools.product(range(problem.getWidth() + 2), range(problem.getHeight() + 2))) non_outer_wall_coords = list( itertools.product(range(1, problem.getWidth() + 1), range(1, problem.getHeight() + 1))) KB = [] x0, y0 = x0_y0 x1, y1 = x1_y1 # We know which coords are walls: map_sent = [PropSymbolExpr(wall_str, x, y) for x, y in walls_list] KB.append(conjoin(map_sent)) "*** BEGIN YOUR CODE HERE ***" KB.append(PropSymbolExpr(pacman_str, x0, y0, 0)) KB.append(pacphysics_axioms(0, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(action0, 0)) KB.append(allLegalSuccessorAxioms(1, walls_grid, non_outer_wall_coords)) KB.append(pacphysics_axioms(1, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(action1, 1)) pcc = PropSymbolExpr(pacman_str, x1, y1, 1) return (findModel(conjoin(conjoin(KB), ~pcc)), findModel(conjoin(conjoin(KB), pcc))) "*** END YOUR CODE HERE ***"
def sentence3(): """Using the symbols PacmanAlive[1], PacmanAlive[0], PacmanBorn[0], and PacmanKilled[0], created using the PropSymbolExpr constructor, return a PropSymbolExpr instance that encodes the following English sentences (in this order): Pacman is alive at time 1 if and only if Pacman was alive at time 0 and it was not killed at time 0 or it was not alive at time 0 and it was born at time 0. Pacman cannot both be alive at time 0 and be born at time 0. Pacman is born at time 0. """ "*** BEGIN YOUR CODE HERE ***" alive0 = PropSymbolExpr("PacmanAlive", 0) alive1 = PropSymbolExpr("PacmanAlive", 1) born = PropSymbolExpr("PacmanBorn", 0) kill = PropSymbolExpr("PacmanKilled", 0) s1 = alive1 % disjoin(conjoin(alive0, ~kill), conjoin(~alive0, born)) return conjoin(s1, ~conjoin(alive0, born), born) "*** END YOUR CODE HERE ***"
def sentence3(): """Using the symbols PacmanAlive[1], PacmanAlive[0], PacmanBorn[0], and PacmanKilled[0], created using the PropSymbolExpr constructor, return a PropSymbolExpr instance that encodes the following English sentences (in this order): Pacman is alive at time 1 if and only if Pacman was alive at time 0 and it was not killed at time 0 or it was not alive at time 0 and it was born at time 0. Pacman cannot both be alive at time 0 and be born at time 0. Pacman is born at time 0. """ "*** BEGIN YOUR CODE HERE ***" a0 = PropSymbolExpr("PacmanAlive", 0) a1 = PropSymbolExpr("PacmanAlive", 1) b0 = PropSymbolExpr("PacmanBorn", 0) k0 = PropSymbolExpr("PacmanKilled", 0) first = a1 % disjoin(a0 & ~k0, ~a0 & b0) second = ~(a0 & b0) third = b0 return conjoin(first, second, third) "*** END YOUR CODE HERE ***"
def localization(problem, agent): ''' problem: a LocalizationProblem instance agent: a LocalizationLogicAgent instance ''' debug = False walls_grid = problem.walls walls_list = walls_grid.asList() all_coords = list( itertools.product(range(problem.getWidth() + 2), range(problem.getHeight() + 2))) non_outer_wall_coords = list( itertools.product(range(1, problem.getWidth() + 1), range(1, problem.getHeight() + 1))) possible_locs_by_timestep = [] KB = [] "*** BEGIN YOUR CODE HERE ***" for x, y in all_coords: if (x, y) in walls_list: KB.append(PropSymbolExpr(wall_str, x, y)) else: KB.append(~PropSymbolExpr(wall_str, x, y)) for t in range(agent.num_timesteps): KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(agent.actions[t], t)) KB.append(sensorAxioms(t, non_outer_wall_coords)) percepts = agent.getPercepts() KB.append(four_bit_percept_rules(t, percepts)) possible_locations_t = [] for x, y in non_outer_wall_coords: res1 = findModel( conjoin(KB) & ~PropSymbolExpr(pacman_str, x, y, t)) res2 = findModel(conjoin(KB) & PropSymbolExpr(pacman_str, x, y, t)) if not res1: KB.append(PropSymbolExpr(pacman_str, x, y, t)) elif not res2: KB.append(~PropSymbolExpr(pacman_str, x, y, t)) if res2: possible_locations_t.append((x, y)) possible_locs_by_timestep.append(possible_locations_t) agent.moveToNextState(agent.actions[t]) KB.append( allLegalSuccessorAxioms(t + 1, walls_grid, non_outer_wall_coords)) "*** END YOUR CODE HERE ***" print(possible_locs_by_timestep) return possible_locs_by_timestep
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 ***" KB.append(PropSymbolExpr(pacman_str, x0, y0, 0)) for i in food: expression = PropSymbolExpr(food_str, i[0], i[1], 0) KB.append(expression) for t in range(50): goal_check = conjoin([~PropSymbolExpr(food_str, f[0], f[1], t) for f in food]) KB.append(exactlyOne([PropSymbolExpr(pacman_str, i[0], i[1], t) for i in non_wall_coords])) model = findModel(conjoin(KB) & goal_check) if model is not False: return extractActionSequence(model, actions) KB.append(exactlyOne([PropSymbolExpr(action, t) for action in actions])) for i in non_wall_coords: KB.append(pacmanSuccessorStateAxioms(i[0], i[1], t + 1, walls)) for f in food: KB.append(PropSymbolExpr(food_str, f[0], f[1], t + 1) % (~PropSymbolExpr(pacman_str, f[0], f[1], t) & PropSymbolExpr(food_str, f[0], f[1], t))) return None "*** END YOUR CODE HERE ***"
def pacphysics_axioms(t, all_coords, non_outer_wall_coords): """ Given: t: timestep all_coords: list of (x, y) coordinates of the entire problem non_outer_wall_coords: list of (x, y) coordinates of the entire problem, excluding the outer border (these are the actual squares pacman can possibly be in) Return a logic sentence containing all of the following: - for all (x, y) in all_coords: If a wall is at (x, y) --> Pacman is not at (x, y) - Pacman is at one of the non_outer_wall_coords. - Pacman is at exactly one of the squares at timestep t. - Pacman takes one of the four actions in DIRECTIONS - Pacman takes exactly one action at timestep t. """ pacphysics_sentences = [] "*** BEGIN YOUR CODE HERE ***" #Pacman at x,y at time t: #PropSymbolExpr(pacman_str, x, y, t) #Whether or not a wall is at x,y #PropSymbolExpr(wall_str, x, y) #Whether or not pacman takes action a at time t #PropSymbolExpr(action, t) #if a wall at x,y >> pacman not at x,y implications = [] for (x, y) in all_coords: implications.append( PropSymbolExpr(wall_str, x, y) >> ~PropSymbolExpr(pacman_str, x, y, t)) pacphysics_sentences.append(conjoin(implications)) #pacman is at exactly one of the non_outer_wall_coords props = [] for (x, y) in non_outer_wall_coords: props.append(PropSymbolExpr(pacman_str, x, y, t)) is_somewhere = exactlyOne(props) pacphysics_sentences.append(is_somewhere) #Pacman takes exactly one of four directions one_direction = exactlyOne([ PropSymbolExpr('North', t), PropSymbolExpr('South', t), PropSymbolExpr('East', t), PropSymbolExpr('West', t) ]) pacphysics_sentences.append(one_direction) "*** END YOUR CODE HERE ***" return conjoin(pacphysics_sentences)
def sentence3(): """Using the symbols PacmanAlive[1], PacmanAlive[0], PacmanBorn[0], and PacmanKilled[0], created using the PropSymbolExpr constructor, return a PropSymbolExpr instance that encodes the following English sentences (in this order): The Wumpus is alive at time 1 if and only if the Wumpus was alive at time 0 and it was not killed at time 0 or it was not alive at time 0 and it was born at time 0. The Wumpus cannot both be alive at time 0 and be born at time 0. The Wumpus is born at time 0. """ "*** BEGIN YOUR CODE HERE ***" alive_0 = PropSymbolExpr('PacmanAlive', 0) alive_1 = PropSymbolExpr('PacmanAlive', 1) born_0 = PropSymbolExpr('PacmanBorn', 0) killed_0 = PropSymbolExpr('PacmanKilled', 0) temp = (alive_0 & ~killed_0) | (~alive_0 & born_0) first = alive_1 % temp second = ~(alive_0 & born_0) third = born_0 return conjoin(first, second, third) "*** END YOUR CODE HERE ***"
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'] food_vars = [PropSymbolExpr(food_str, f[0], f[1], 0) for f in food] KB = [] pos_t0 = PropSymbolExpr(pacman_str, x0, y0, 0) KB.append(pos_t0) KB.append(conjoin(food_vars)) for t in range(50): nonwallpos = [ PropSymbolExpr(pacman_str, xy[0], xy[1], t) for xy in non_wall_coords ] KB.append(exactlyOne(nonwallpos)) goal = [] for coords in non_wall_coords: goal.append(~PropSymbolExpr(food_str, coords[0], coords[1], t)) goal_assertion = conjoin(goal) model = findModel(conjoin(conjoin(KB), goal_assertion)) if model: return extractActionSequence(model, actions) action_list = [PropSymbolExpr(action, t) for action in actions] KB.append(exactlyOne(action_list)) for xy in non_wall_coords: KB.append(pacmanSuccessorStateAxioms(xy[0], xy[1], t + 1, walls)) food_present = PropSymbolExpr(food_str, xy[0], xy[1], t) food_t1 = PropSymbolExpr(food_str, xy[0], xy[1], t + 1) pacman_present = PropSymbolExpr(pacman_str, xy[0], xy[1], t + 1) KB.append(conjoin(food_present, ~pacman_present) % food_t1)
def pacphysics_axioms(t, all_coords, non_outer_wall_coords): """ Given: t: timestep all_coords: list of (x, y) coordinates of the entire problem non_outer_wall_coords: list of (x, y) coordinates of the entire problem, excluding the outer border (these are the actual squares pacman can possibly be in) Return a logic sentence containing all of the following: - for all (x, y) in all_coords: If a wall is at (x, y) --> Pacman is not at (x, y) - Pacman is at exactly one of the squares at timestep t. - Pacman takes exactly one action at timestep t. """ s = [] walls = [] for xy in all_coords: walls.append( PropSymbolExpr(wall_str, xy[0], xy[1]) >> ~PropSymbolExpr(pacman_str, xy[0], xy[1], t)) s.append(conjoin(walls)) non_wall_literals = [] for non_walls in non_outer_wall_coords: is_at = PropSymbolExpr(pacman_str, non_walls[0], non_walls[1], t) non_wall_literals.append(is_at) s.append(exactlyOne(non_wall_literals)) action_literals = [] for action in DIRECTIONS: chosen_action = PropSymbolExpr(action, t) action_literals.append(chosen_action) s.append(exactlyOne(action_literals)) return conjoin(s)
def localization(problem, agent): ''' problem: a LocalizationProblem instance agent: a LocalizationLogicAgent instance ''' debug = False walls_grid = problem.walls walls_list = walls_grid.asList() all_coords = list(itertools.product(range(problem.getWidth()+2), range(problem.getHeight()+2))) non_outer_wall_coords = list(itertools.product(range(1, problem.getWidth()+1), range(1, problem.getHeight()+1))) possible_locs_by_timestep = [] KB = [] "*** BEGIN YOUR CODE HERE ***" for i in all_coords: if i in walls_list: KB.append(PropSymbolExpr(wall_str, i[0], i[1])) else: KB.append(~PropSymbolExpr(wall_str, i[0], i[1])) for t in range(agent.num_timesteps): # Add pacphysics, action, sensor, and percept information to KB KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(agent.actions[t], t)) KB.append(sensorAxioms(t, non_outer_wall_coords)) KB.append(four_bit_percept_rules(t, agent.getPercepts())) #Find possible pacman locations with updated KB possible_locations_t = [] for i in non_outer_wall_coords: model1 = findModel(conjoin(KB) & PropSymbolExpr(pacman_str, i[0], i[1], t)) model2 = findModel(conjoin(KB) & ~PropSymbolExpr(pacman_str, i[0], i[1], t)) if model1: possible_locations_t.append(i) elif not model2: KB.append(~PropSymbolExpr(pacman_str, i[0], i[1], t)) elif not model1: KB.append(~PropSymbolExpr(pacman_str, i[0], i[1], t)) possible_locs_by_timestep.append(possible_locations_t) agent.moveToNextState(agent.actions[t]) KB.append(allLegalSuccessorAxioms(t + 1, walls_grid, non_outer_wall_coords)) return possible_locs_by_timestep #print(KB) "*** END YOUR CODE HERE ***" return possible_locs_by_timestep
def positionLogicPlan(problem): """ Given an instance of a PositionPlanningProblem, return a list of actions that lead to the goal. 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 = problem.startState xg, yg = problem.goal # 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 = [] "*** BEGIN YOUR CODE HERE ***" KB.append(PropSymbolExpr(pacman_str, x0, y0, 0)) for t in range(50): #pacman can only be at one location props = [] for (x, y) in non_wall_coords: props.append(PropSymbolExpr(pacman_str, x, y, t)) KB.append(exactlyOne(props)) #is there a satisfying model assignment? knowledge_base = conjoin(KB) model = findModel(knowledge_base & PropSymbolExpr(pacman_str, xg, yg, t)) if model: return extractActionSequence(model, actions) #pacman takes exactly one position per time step KB.append( exactlyOne([ PropSymbolExpr('North', t), PropSymbolExpr('South', t), PropSymbolExpr('East', t), PropSymbolExpr('West', t) ])) #add transition models to KB for (x, y) in non_wall_coords: KB.append( pacmanSuccessorStateAxioms(x, y, t + 1, walls, pacman_str)) "*** END YOUR CODE HERE ***"
def num_adj_walls_percept_rules(t, percepts): """ SLAM uses a weaker num_adj_walls sensor, which tells us how many walls pacman is adjacent to in its four directions. 000 = 0 adj walls. 100 = 1 adj wall. 110 = 2 adj walls. 111 = 3 adj walls. """ percept_unit_clauses = [] num_adj_walls = sum(percepts) for i, percept in enumerate(percepts): n = i + 1 percept_literal_n = PropSymbolExpr(geq_num_adj_wall_str_map[n], t) if not percept: percept_literal_n = ~percept_literal_n percept_unit_clauses.append(percept_literal_n) return conjoin(percept_unit_clauses)
def slam(problem, agent): ''' problem: a SLAMProblem instance agent: a SLAMLogicAgent instance ''' debug = False pac_x_0, pac_y_0 = problem.startState KB = [] all_coords = list( itertools.product(range(problem.getWidth() + 2), range(problem.getHeight() + 2))) non_outer_wall_coords = list( itertools.product(range(1, problem.getWidth() + 1), range(1, problem.getHeight() + 1))) # map describes what we know, for GUI rendering purposes. -1 is unknown, 0 is open, 1 is wall known_map = [[-1 for y in range(problem.getHeight() + 2)] for x in range(problem.getWidth() + 2)] known_map_by_timestep = [] possible_locs_by_timestep = [] # We know that the outer_coords are all walls. outer_wall_sent = [] for x, y in all_coords: if ((x == 0 or x == problem.getWidth() + 1) or (y == 0 or y == problem.getHeight() + 1)): known_map[x][y] = 1 outer_wall_sent.append(PropSymbolExpr(wall_str, x, y)) KB.append(conjoin(outer_wall_sent)) "*** BEGIN YOUR CODE HERE ***" raise NotImplementedError "*** END YOUR CODE HERE ***" return known_map_by_timestep, possible_locs_by_timestep
def pacmanSLAMSuccessorStateAxioms(x, y, t, walls_grid, var_str=pacman_str): """ Similar to `pacmanSuccessorStateAxioms` but accounts for illegal actions where the pacman might not move timestep to timestep. Available actions are ['North', 'East', 'South', 'West'] """ moved_tm1_possibilities = [] if not walls_grid[x][y + 1]: moved_tm1_possibilities.append( PropSymbolExpr(var_str, x, y + 1, t - 1) & PropSymbolExpr('South', t - 1)) if not walls_grid[x][y - 1]: moved_tm1_possibilities.append( PropSymbolExpr(var_str, x, y - 1, t - 1) & PropSymbolExpr('North', t - 1)) if not walls_grid[x + 1][y]: moved_tm1_possibilities.append( PropSymbolExpr(var_str, x + 1, y, t - 1) & PropSymbolExpr('West', t - 1)) if not walls_grid[x - 1][y]: moved_tm1_possibilities.append( PropSymbolExpr(var_str, x - 1, y, t - 1) & PropSymbolExpr('East', t - 1)) if not moved_tm1_possibilities: return None moved_tm1_sent = conjoin([ ~PropSymbolExpr(var_str, x, y, t - 1), ~PropSymbolExpr(wall_str, x, y), disjoin(moved_tm1_possibilities) ]) unmoved_tm1_possibilities_aux_exprs = [] # merged variables aux_expr_defs = [] for direction in DIRECTIONS: dx, dy = DIR_TO_DXDY_MAP[direction] wall_dir_clause = PropSymbolExpr( wall_str, x + dx, y + dy) & PropSymbolExpr(direction, t - 1) wall_dir_combined_literal = PropSymbolExpr(wall_str + direction, x + dx, y + dy, t - 1) unmoved_tm1_possibilities_aux_exprs.append(wall_dir_combined_literal) aux_expr_defs.append(wall_dir_combined_literal % wall_dir_clause) unmoved_tm1_sent = conjoin([ PropSymbolExpr(var_str, x, y, t - 1), disjoin(unmoved_tm1_possibilities_aux_exprs) ]) return conjoin([ PropSymbolExpr(var_str, x, y, t) % disjoin([moved_tm1_sent, unmoved_tm1_sent]) ] + aux_expr_defs)
def slam(problem, agent): ''' problem: a SLAMProblem instance agent: a SLAMLogicAgent instance ''' debug = False pac_x_0, pac_y_0 = problem.startState KB = [] all_coords = list(itertools.product(range(problem.getWidth()+2), range(problem.getHeight()+2))) non_outer_wall_coords = list(itertools.product(range(1, problem.getWidth()+1), range(1, problem.getHeight()+1))) # map describes what we know, for GUI rendering purposes. -1 is unknown, 0 is open, 1 is wall known_map = [[-1 for y in range(problem.getHeight()+2)] for x in range(problem.getWidth()+2)] known_map_by_timestep = [] possible_locs_by_timestep = [] # We know that the outer_coords are all walls. outer_wall_sent = [] for x, y in all_coords: if ((x == 0 or x == problem.getWidth() + 1) or (y == 0 or y == problem.getHeight() + 1)): known_map[x][y] = 1 outer_wall_sent.append(PropSymbolExpr(wall_str, x, y)) KB.append(conjoin(outer_wall_sent)) "*** BEGIN YOUR CODE HERE ***" KB.append(PropSymbolExpr(pacman_str, pac_x_0, pac_y_0, 0)) for t in range(agent.num_timesteps): # Add pacphysics, action, sensor, and percept information to KB KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(agent.actions[t], t)) KB.append(SLAMSensorAxioms(t, non_outer_wall_coords)) KB.append(num_adj_walls_percept_rules(t, agent.getPercepts())) # Find provable wall locations with updated KB for i in non_outer_wall_coords: x, y = i # find a model such that (x,y) is wall model1 = findModel(conjoin(KB) & PropSymbolExpr(wall_str, x, y)) # find a model such that (x,y) is not wall model2 = findModel(conjoin(KB) & ~PropSymbolExpr(wall_str, x, y)) if known_map[x][y] == -1: if not model2: KB.append(PropSymbolExpr(wall_str, x, y)) known_map[x][y] = 1 elif not model1: KB.append(~PropSymbolExpr(wall_str, x, y)) known_map[x][y] = 0 map_copy = copy.deepcopy(known_map) known_map_by_timestep.append(map_copy) # Find possible pacman locations with updated KB possible_locations_t = [] for i in non_outer_wall_coords: x, y = i model1 = findModel(conjoin(KB) & PropSymbolExpr(pacman_str, x, y, t)) model2 = findModel(conjoin(KB) & ~PropSymbolExpr(pacman_str, x, y, t)) if model1: possible_locations_t.append(i) else: KB.append(~PropSymbolExpr(pacman_str, x, y, t)) if not model2: KB.append(PropSymbolExpr(pacman_str, x, y, t)) possible_locs_by_timestep.append(possible_locations_t) agent.moveToNextState(agent.actions[t]) temp = [] for i in range(len(known_map)): temp.append([]) for j in range(len(known_map[i])): if known_map[i][j] == 1: temp[i].append(1) else: temp[i].append(0) print(temp) KB.append(SLAMSuccessorAxioms(t + 1, temp, non_outer_wall_coords)) "*** END YOUR CODE HERE ***" return known_map_by_timestep, possible_locs_by_timestep
def slam(problem, agent): ''' problem: a SLAMProblem instance agent: a SLAMLogicAgent instance ''' debug = False pac_x_0, pac_y_0 = problem.startState KB = [] all_coords = list( itertools.product(range(problem.getWidth() + 2), range(problem.getHeight() + 2))) non_outer_wall_coords = list( itertools.product(range(1, problem.getWidth() + 1), range(1, problem.getHeight() + 1))) # map describes what we know, for GUI rendering purposes. -1 is unknown, 0 is open, 1 is wall known_map = [[-1 for y in range(problem.getHeight() + 2)] for x in range(problem.getWidth() + 2)] known_map_by_timestep = [] possible_locs_by_timestep = [] # We know that the outer_coords are all walls. outer_wall_sent = [] for x, y in all_coords: if ((x == 0 or x == problem.getWidth() + 1) or (y == 0 or y == problem.getHeight() + 1)): known_map[x][y] = 1 outer_wall_sent.append(PropSymbolExpr(wall_str, x, y)) KB.append(conjoin(outer_wall_sent)) KB.append(PropSymbolExpr(pacman_str, pac_x_0, pac_y_0, 0)) for t in range(agent.num_timesteps): KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(agent.actions[t], t)) KB.append(SLAMSensorAxioms(t, non_outer_wall_coords)) percept_rules = num_adj_walls_percept_rules(t, agent.getPercepts()) KB.append(percept_rules) possible_locations_t = [] for xy in non_outer_wall_coords: wall_present = PropSymbolExpr(wall_str, xy[0], xy[1]) wall_model1 = findModel(conjoin(conjoin(KB), wall_present)) wall_model2 = findModel(conjoin(conjoin(KB), ~wall_present)) pacman_present = PropSymbolExpr(pacman_str, xy[0], xy[1], t) pm_model1 = findModel(conjoin(conjoin(KB), pacman_present)) pm_model2 = findModel(conjoin(conjoin(KB), ~pacman_present)) # evaluate wall models if wall_model2 is False: known_map[xy[0]][xy[1]] = 1 KB.append(wall_present) if wall_model1 is False: known_map[xy[0]][xy[1]] = 0 KB.append(~wall_present) if wall_model1 and wall_model2: known_map[xy[0]][xy[1]] = -1 # evaluate pacman positional models if pm_model2 is False: possible_locations_t.append(xy) KB.append(pacman_present) if pm_model1 is False: KB.append(~pacman_present) if pm_model1 and pm_model2: possible_locations_t.append(xy) map_copy = copy.deepcopy(known_map) for x in map_copy: for k in x: if k == -1: x[x.index(k)] = 0 known_map_by_timestep.append(copy.deepcopy(known_map)) possible_locs_by_timestep.append(possible_locations_t) agent.moveToNextState(agent.actions[t]) KB.append(SLAMSuccessorAxioms(t + 1, map_copy, non_outer_wall_coords)) return known_map_by_timestep, possible_locs_by_timestep
def slam(problem, agent): ''' problem: a SLAMProblem instance agent: a SLAMLogicAgent instance ''' debug = True pac_x_0, pac_y_0 = problem.startState KB = [] all_coords = list( itertools.product(range(problem.getWidth() + 2), range(problem.getHeight() + 2))) non_outer_wall_coords = list( itertools.product(range(1, problem.getWidth() + 1), range(1, problem.getHeight() + 1))) # map describes what we know, for GUI rendering purposes. -1 is unknown, 0 is open, 1 is wall known_map = [[-1 for y in range(problem.getHeight() + 2)] for x in range(problem.getWidth() + 2)] known_map_by_timestep = [] possible_locs_by_timestep = [] # We know that the outer_coords are all walls. outer_wall_sent = [] for x, y in all_coords: if ((x == 0 or x == problem.getWidth() + 1) or (y == 0 or y == problem.getHeight() + 1)): known_map[x][y] = 1 outer_wall_sent.append(PropSymbolExpr(wall_str, x, y)) KB.append(conjoin(outer_wall_sent)) "*** BEGIN YOUR CODE HERE ***" KB.append(PropSymbolExpr(pacman_str, pac_x_0, pac_y_0, 0)) # 1 for t in range(agent.num_timesteps): KB.append(pacphysics_axioms(t, all_coords, non_outer_wall_coords)) KB.append(PropSymbolExpr(agent.actions[t], t)) KB.append(SLAMSensorAxioms(t, non_outer_wall_coords)) KB.append(num_adj_walls_percept_rules(t, agent.getPercepts())) # 2 wall for x, y in non_outer_wall_coords: model_xy = findModel( conjoin(conjoin(KB), PropSymbolExpr(wall_str, x, y))) model_not_xy = findModel( conjoin(conjoin(KB), ~PropSymbolExpr(wall_str, x, y))) if (model_not_xy == False): KB.append(PropSymbolExpr(wall_str, x, y)) known_map[x][y] = 1 elif (model_xy == False): KB.append(~PropSymbolExpr(wall_str, x, y)) known_map[x][y] = 0 known_map_by_timestep.append(copy.deepcopy(known_map)) # 3 pacman possible_locations_t = [] for x, y in non_outer_wall_coords: model_xy = findModel( conjoin(conjoin(KB), PropSymbolExpr(pacman_str, x, y, t))) model_not_xy = findModel( conjoin(conjoin(KB), ~PropSymbolExpr(pacman_str, x, y, t))) if (model_xy != False): possible_locations_t.append((x, y)) elif (model_not_xy == False): KB.append(PropSymbolExpr(pacman_str, x, y, t)) elif (model_xy == False): KB.append(~PropSymbolExpr(pacman_str, x, y, t)) possible_locs_by_timestep.append(possible_locations_t) agent.moveToNextState(agent.actions[t]) KB.append( SLAMSuccessorAxioms( t + 1, list( map(lambda i: list(map(lambda j: 0 if j == -1 else j, i)), known_map)), non_outer_wall_coords)) "*** END YOUR CODE HERE ***" return known_map_by_timestep, possible_locs_by_timestep