def ghostDirectionSuccessorStateAxioms(t, ghost_num, blocked_west_positions, blocked_east_positions): """ Successor state axiom for patrolling ghost direction state (t) (from t-1). west or east walls. Current <==> (causes to stay) | (causes of current) """ pos_str = ghost_pos_str + str(ghost_num) east_str = ghost_east_str + str(ghost_num) west_positions = list() east_positions = list() blocked_west_positions_copy = blocked_west_positions[:] blocked_east_positions_copy = blocked_east_positions[:] while blocked_west_positions_copy: position = blocked_west_positions_copy.pop() west_positions.append( logic.PropSymbolExpr(pos_str, position[0], position[1], t)) while blocked_east_positions_copy: position = blocked_east_positions_copy.pop() east_positions.append( logic.PropSymbolExpr(pos_str, position[0], position[1], t)) west_positions = logic.disjoin(west_positions) east_positions = logic.disjoin(east_positions) conditions = (west_positions & ~logic.PropSymbolExpr(east_str, t - 1)) | ( ~east_positions & logic.PropSymbolExpr(east_str, t - 1)) final_axiom = logic.PropSymbolExpr(east_str, t) % conditions return final_axiom
def ghostDirectionSuccessorStateAxioms(t, ghost_num, blocked_west_positions, blocked_east_positions): """ Successor state axiom for patrolling ghost direction state (t) (from t-1). west or east walls. Current <==> (causes to stay) | (causes of current) """ pos_str = ghost_pos_str + str(ghost_num) east_str = ghost_east_str + str(ghost_num) "*** YOUR CODE HERE ***" west_action = ~(logic.PropSymbolExpr(east_str, t - 1)) east_action = logic.PropSymbolExpr(east_str, t - 1) west_list = [] east_list = [] for pos in blocked_east_positions: x, y = pos not_blocked_east = ~logic.PropSymbolExpr(pos_str, x, y, t) lst = logic.conjoin(east_action, not_blocked_east) east_list.append(lst) for pos in blocked_west_positions: x, y = pos blocked_west = logic.PropSymbolExpr(pos_str, x, y, t) lst = logic.conjoin(west_action, blocked_west) west_list.append(lst) east_list = logic.conjoin(east_list) west_list = logic.disjoin(west_list) return logic.PropSymbolExpr(east_str, t) % logic.disjoin( east_list, west_list)
def exactlyOne(literals): """ Given a list of logic.Expr literals, return a single logic.Expr instance in CNF (conjunctive normal form)that represents the logic that exactly one of the expressions in the list is true. """ # print literals conjunctions = [] one_must_be_true_list = [] for literal in literals: not_literal = ~literal one_must_be_true_list.append(literal) # Disjoin literal with NOT(literal) for every other element besides this literal # and add it to the list to be conjoined reached_literal = False for inner_literal in literals: if (reached_literal): not_inner_literal = ~inner_literal disjunction = logic.disjoin(not_literal, not_inner_literal) conjunctions.append(disjunction) if literal == inner_literal: reached_literal = True # Add the expression that states at least one of the literals must be true one_must_be_true = logic.disjoin(one_must_be_true_list) conjunctions.append(one_must_be_true) return logic.conjoin(conjunctions)
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 exactlyOne(literals): """ Given a list of logic.Expr literals, return a single logic.Expr instance in CNF (conjunctive normal form)that represents the logic that exactly one of the inits in the list is true. """ "*** YOUR CODE HERE ***" conjunctions = [] true_list = [] for literal in literals: true_list.append(literal) reached = False for inner_literal in literals: if reached: not_inner_literal = ~inner_literal disjunction = logic.disjoin(~literal, not_inner_literal) conjunctions.append(disjunction) if literal == inner_literal: reached = True true_one = logic.disjoin(true_list) conjunctions.append(true_one) return logic.conjoin(conjunctions)
def sentence1(): """Returns a Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ s1 = disjoin(A, B) s2 = ~A % disjoin(~B, C) s3 = disjoin(~A, ~B, C) return conjoin(s1, s2, s3)
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 sentence1(): """Returns a logic.Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ "*** YOUR CODE HERE ***" ans1 = logic.Expr('A') | logic.Expr('B') ans2 = ~logic.Expr('A') % logic.disjoin( [~logic.Expr('B'), logic.Expr('C')]) ans3 = logic.disjoin([~logic.Expr('A'), ~logic.Expr('B'), logic.Expr('C')]) return logic.conjoin([ans1, ans2, ans3])
def sentence1(): """Returns a Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ "*** BEGIN YOUR CODE HERE ***" A = Expr('A') B = Expr('B') C = Expr('C') return conjoin(disjoin(A, B), ~A % disjoin(~B, C), disjoin(~A, ~B, C)) "*** END YOUR CODE HERE ***"
def sentence1(): """Returns a logic.Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ A = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') aaa = logic.disjoin(A, B) a_or_b = ~A % (~B | C) not_a = logic.disjoin(~A, ~B, C) return logic.conjoin(aaa, a_or_b, not_a)
def sentence2(): """Returns a logic.Expr instance that encodes that the following expressions are all true. C if and only if (B or D) A implies ((not B) and (not D)) (not (B and (not C))) implies A (not D) implies C """ "*** YOUR CODE HERE ***" A = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') D = logic.Expr('D') notB = ~B notD = ~D notC = ~C B_or_D = logic.disjoin((B), (D)) notB_and_notD = logic.conjoin((notB), (notD)) C_iff_B_or_D = C % B_or_D A_implies_notB_and_notD = A >> notB_and_notD B_and_notC = logic.conjoin((B), (notC)) not_B_and_notC_implies_A = ~B_and_notC >> A notD_implies_C = notD >> C return logic.conjoin((C_iff_B_or_D), (A_implies_notB_and_notD), (not_B_and_notC_implies_A), (notD_implies_C))
def exactlyOne(literals) : """ Given a list of logic.Expr literals, return a single logic.Expr instance in CNF (conjunctive normal form)that represents the logic that exactly one of the expressions in the list is true. """ "*** YOUR CODE HERE ***" n = len(literals) clause_brev = [] clauses = [] for i in range(2**n): bstr = str(bin(i)) clause_brev.append(bstr[2:].zfill(n)) for brev in clause_brev: clause = [] if (bin(int(brev,2)).count('1') == 1): continue for i in range(n): if (brev[i] == '1'): clause.append(~literals[i]) elif (brev[i] == '0'): clause.append(literals[i]) temp=clause[0] for i in range(n-1): temp = logic.disjoin(temp,clause[i+1]) clauses.append(temp) expression = clauses[0] for i in range(2**n-n-1): expression = logic.conjoin(expression,clauses[i+1]) return expression
def atLeastOne(literals) : """ Given a list of logic.Expr literals (i.e. in the form A or ~A), return a single logic.Expr instance in CNF (conjunctive normal form) that represents the logic that at least one of the literals in the list is true. >>> A = logic.PropSymbolExpr('A'); >>> B = logic.PropSymbolExpr('B'); >>> symbols = [A, B] >>> atleast1 = atLeastOne(symbols) >>> model1 = {A:False, B:False} >>> print logic.pl_true(atleast1,model1) False >>> model2 = {A:False, B:True} >>> print logic.pl_true(atleast1,model2) True >>> model3 = {A:True, B:True} >>> print logic.pl_true(atleast1,model2) True """ "*** YOUR CODE HERE ***" expression = literals[0] for i in range(len(literals)-1): expression = logic.disjoin(expression, literals[i+1]) return expression
def sentence1(): """Returns a logic.Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ "*** YOUR CODE HERE ***" A = logic.Expr("A") B = logic.Expr("B") C = logic.Expr("C") first = logic.disjoin(A, B) second = (~A) % logic.disjoin(~B, C) third = logic.disjoin(~A, ~B, C) return logic.conjoin(first, second, third)
def pacmanAliveSuccessorStateAxioms(x, y, t, num_ghosts): """ Successor state axiom for patrolling ghost state (x,y,t) (from t-1). Current <==> (causes to stay) | (causes of current) """ ghost_strs = [ ghost_pos_str + str(ghost_num) for ghost_num in xrange(num_ghosts) ] current = logic.PropSymbolExpr(pacman_str, x, y, t) ghosts = ghost_strs[:] neighbors = [] k = [] l = [] while num_ghosts != 0: k += [ logic.PropSymbolExpr(ghost_strs[num_ghosts - 1], x, y, t - 1) | logic.PropSymbolExpr(ghost_strs[num_ghosts - 1], x, y, t) ] num_ghosts -= 1 m = ~logic.PropSymbolExpr(pacman_alive_str, t - 1) prev_states = logic.disjoin(k) prev_states = logic.conjoin(logic.PropSymbolExpr(pacman_str, x, y, t), prev_states) | m final_axiom = ~logic.PropSymbolExpr(pacman_alive_str, t) % prev_states return final_axiom
def ghostPositionSuccessorStateAxioms(x, y, t, ghost_num, walls_grid): """ Successor state axiom for patrolling ghost state (x,y,t) (from t-1). Current <==> (causes to stay) | (causes of current) GE is going east, ~GE is going west """ pos_str = ghost_pos_str+str(ghost_num) east_str = ghost_east_str+str(ghost_num) pos = logic.PropSymbolExpr(pos_str, x, y, t) listAdj = [(x-1, y), (x+1, y)] lst = [] for l in listAdj: if not walls_grid[l[0]][l[1]]: if l == (x-1, y): e = logic.conjoin([logic.PropSymbolExpr(pos_str, x-1, y, t-1), logic.PropSymbolExpr(east_str, t-1)]) else: e = logic.conjoin([logic.PropSymbolExpr(pos_str, x+1, y, t-1), ~(logic.PropSymbolExpr(east_str, t-1))]) lst.append(e) if walls_grid[x-1][y] & walls_grid[x+1][y]: #if there are walls on both sides and ghost cannot move lst.append(logic.PropSymbolExpr(pos_str, x, y, t-1)) if (len(lst) == 0): return pos % pos r = logic.disjoin(lst) return pos % r
def ghostPositionSuccessorStateAxioms(x, y, t, ghost_num, walls_grid): """ Successor state axiom for patrolling ghost state (x,y,t) (from t-1). Current <==> (causes to stay) | (causes of current) GE is going east, ~GE is going west """ pos_str = ghost_pos_str+str(ghost_num) east_str = ghost_east_str+str(ghost_num) pos = logic.PropSymbolExpr(pos_str, x, y, t) listAdj = [(x-1, y), (x+1, y)] lst = [] for l in listAdj: if not walls_grid[l[0]][l[1]]: if l == (x-1, y): e = logic.conjoin([logic.PropSymbolExpr(pacman_str, x-1, y, t-1), logic.PropSymbolExpr(east_str, t-1)]) else: e = logic.conjoin([logic.PropSymbolExpr(pacman_str, x+1, y, t-1), ~(logic.PropSymbolExpr(east_str, t-1))]) lst.append(e) if (len(lst) == 0): return pos % pos r = logic.disjoin(lst) return pos % r "*** YOUR CODE HERE ***" return logic.Expr('A') # Replace this with your expression
def pacmanSuccessorStateAxioms(x, y, t, walls_grid): """ 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) """ "*** YOUR CODE HERE ***" current_pos = logic.PropSymbolExpr(pacman_str, x, y, t) pre = [] if not walls_grid[x][y - 1]: pre_pos = logic.PropSymbolExpr(pacman_str, x, y - 1, t - 1) move = logic.PropSymbolExpr("North", t - 1) pre.append(move & pre_pos) if not walls_grid[x][y + 1]: pre_pos = logic.PropSymbolExpr(pacman_str, x, y + 1, t - 1) move = logic.PropSymbolExpr("South", t - 1) pre.append(move & pre_pos) if not walls_grid[x - 1][y]: pre_pos = logic.PropSymbolExpr(pacman_str, x - 1, y, t - 1) move = logic.PropSymbolExpr("East", t - 1) pre.append(move & pre_pos) if not walls_grid[x + 1][y]: pre_pos = logic.PropSymbolExpr(pacman_str, x + 1, y, t - 1) move = logic.PropSymbolExpr("West", t - 1) pre.append(move & pre_pos) all_pre = logic.disjoin(pre) return current_pos % all_pre
def atLeastOne(literals): """ Given a list of Expr literals (i.e. in the form A or ~A), return a single Expr instance in CNF (conjunctive normal form) that represents the logic that at least one of the literals in the list is true. >>> A = PropSymbolExpr('A'); >>> B = PropSymbolExpr('B'); >>> symbols = [A, B] >>> atleast1 = atLeastOne(symbols) >>> model1 = {A:False, B:False} >>> print(pl_true(atleast1,model1)) False >>> model2 = {A:False, B:True} >>> print(pl_true(atleast1,model2)) True >>> model3 = {A:True, B:True} >>> print(pl_true(atleast1,model2)) True """ "*** BEGIN YOUR CODE HERE ***" conj = literals[0] for i in literals[1:]: conj = disjoin(conj, i) return conj "*** END YOUR CODE HERE ***"
def pacmanSuccessorStateAxioms(x, y, t, walls_grid): """ 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) """ "*** YOUR CODE HERE ***" def noWallHere(coord): return not walls_grid[coord[0]][coord[1]] listPrev = [] directions = ["East", "West", "South", "North"] at = [(x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1)] for i in xrange(len(directions)): direc = directions[i] coord = at[i] if noWallHere(coord): prevAtHere = logic.PropSymbolExpr(pacman_str, coord[0], coord[1], t - 1) prevGoThere = logic.PropSymbolExpr(direc, t - 1) listPrev.append(logic.conjoin(prevAtHere, prevGoThere)) pacmanNow = logic.PropSymbolExpr(pacman_str, x, y, t) return pacmanNow % logic.disjoin(i for i in listPrev)
def pacmanSuccessorStateAxioms(x, y, t, walls_grid): """ 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) """ moves = [] if not walls_grid[x + 1][y]: moves.append( logic.PropSymbolExpr(pacman_str, x + 1, y, t - 1) & logic.PropSymbolExpr('West', t - 1)) if not walls_grid[x - 1][y]: moves.append( logic.PropSymbolExpr(pacman_str, x - 1, y, t - 1) & logic.PropSymbolExpr('East', t - 1)) if not walls_grid[x][y + 1]: moves.append( logic.PropSymbolExpr(pacman_str, x, y + 1, t - 1) & logic.PropSymbolExpr('South', t - 1)) if not walls_grid[x][y - 1]: moves.append( logic.PropSymbolExpr(pacman_str, x, y - 1, t - 1) & logic.PropSymbolExpr('North', t - 1)) return logic.PropSymbolExpr(pacman_str, x, y, t) % (logic.disjoin(moves))
def atLeastOne(literals): """ Given a list of logic.Expr literals (i.e. in the form A or ~A), return a single logic.Expr instance in CNF (conjunctive normal form) that represents the logic that at least one of the literals in the list is true. >>> A = logic.PropSymbolExpr('A'); >>> B = logic.PropSymbolExpr('B'); >>> symbols = [A, B] >>> atleast1 = atLeastOne(symbols) >>> model1 = {A:False, B:False} >>> print logic.pl_true(atleast1,model1) False >>> model2 = {A:False, B:True} >>> print logic.pl_true(atleast1,model2) True >>> model3 = {A:True, B:True} >>> print logic.pl_true(atleast1,model2) True """ "*** YOUR CODE HERE ***" # symbols=list(combinations(literals,2)) y = logic.disjoin(literals) # print(symbols) # for i in symbols: # x=logic.conjoin(i) # y=logic.disjoin(y,x) return y util.raiseNotDefined()
def sentence1(): """Returns a logic.Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ "*** YOUR CODE HERE ***" A = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') A_or_B = logic.disjoin(A, B) Two = ~A % logic.disjoin(~B, C) Three = logic.disjoin(~A, ~B, C) return logic.conjoin(A_or_B, Two, Three) util.raiseNotDefined()
def ghostPositionSuccessorStateAxioms(x, y, t, ghost_num, walls_grid): """ Successor state axiom for patrolling ghost state (x,y,t) (from t-1). Current <==> (causes to stay) | (causes of current) GE is going east, ~GE is going west """ pos_str = ghost_pos_str + str(ghost_num) east_str = ghost_east_str + str(ghost_num) "*** YOUR CODE HERE ***" left = walls_grid[x - 1][y] right = walls_grid[x + 1][y] possible_locations = [] left_prev = logic.PropSymbolExpr(pos_str, x - 1, y, t - 1) right_prev = logic.PropSymbolExpr(pos_str, x + 1, y, t - 1) west_action = ~(logic.PropSymbolExpr(east_str, t - 1)) east_action = logic.PropSymbolExpr(east_str, t - 1) if left and right: return logic.PropSymbolExpr(pos_str, x, y, t) % logic.PropSymbolExpr( pos_str, x, y, t - 1) if not left: possible_locations.append(logic.conjoin(left_prev, east_action)) if not right: possible_locations.append(logic.conjoin(right_prev, west_action)) return logic.PropSymbolExpr(pos_str, x, y, t) % logic.disjoin(possible_locations)
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 sentence3(): """Using the symbols WumpusAlive[1], WumpusAlive[0], WumpusBorn[0], and WumpusKilled[0], created using the logic.PropSymbolExpr constructor, return a logic.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 and 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. """ "*** YOUR CODE HERE ***" #know the use of the PropSymbolExpr from logic.py Alive_1 = logic.PropSymbolExpr("WumpusAlive", 1) #print (Alive_1) Alive_0 = logic.PropSymbolExpr("WumpusAlive", 0) Born_0 = logic.PropSymbolExpr("WumpusBorn", 0) Killed_0 = logic.PropSymbolExpr("WumpusKilled", 0) One = Alive_1 % logic.disjoin(logic.conjoin(Alive_0, ~Killed_0), logic.conjoin(~Alive_0, Born_0)) Two = ~logic.conjoin(Alive_0, Born_0) Three = Born_0 return logic.conjoin(One, Two, Three) util.raiseNotDefined()
def pacmanSuccessorStateAxioms(x, y, t, walls_grid): """ 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) """ "*** YOUR CODE HERE ***" expr = [] if (not walls_grid[x][y - 1]): expr += [ logic.PropSymbolExpr(pacman_str, x, y - 1, t - 1) & logic.PropSymbolExpr('North', t - 1) ] if (not walls_grid[x][y + 1]): expr += [ logic.PropSymbolExpr(pacman_str, x, y + 1, t - 1) & logic.PropSymbolExpr('South', t - 1) ] if (not walls_grid[x - 1][y]): expr += [ logic.PropSymbolExpr(pacman_str, x - 1, y, t - 1) & logic.PropSymbolExpr('East', t - 1) ] if (not walls_grid[x + 1][y]): expr += [ logic.PropSymbolExpr(pacman_str, x + 1, y, t - 1) & logic.PropSymbolExpr('West', t - 1) ] return logic.PropSymbolExpr(pacman_str, x, y, t) % logic.disjoin( expr) # Replace this with your expression
def pacmanSuccessorStateAxioms(x, y, t, walls_grid): """ 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) """ "*** YOUR CODE HERE ***" # init current/final position current = logic.PropSymbolExpr(pacman_str, x, y, t) test = [] if walls_grid[x+1][y] != True: action = logic.PropSymbolExpr(pacman_str, x+1, y, t-1) & logic.PropSymbolExpr('West', t-1) test.append(action) successor_nodes.append((x+1, y, t)) if walls_grid[x-1][y] != True: action2 = logic.PropSymbolExpr(pacman_str, x-1, y, t-1) & logic.PropSymbolExpr('East', t-1) test.append(action2) successor_nodes.append((x+1, y, t)) if walls_grid[x][y+1] != True: action3 = logic.PropSymbolExpr(pacman_str, x, y+1, t-1) & logic.PropSymbolExpr('South', t-1) test.append(action3) successor_nodes.append((x+1, y, t)) if walls_grid[x][y-1] != True: action4 = logic.PropSymbolExpr(pacman_str, x, y-1, t-1) & logic.PropSymbolExpr('North', t-1) test.append(action4) successor_nodes.append((x+1, y, t)) return current % logic.disjoin(test)
def sentence1(): """Returns a logic.Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ "*** YOUR CODE HERE ***" a = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') sentence1 = logic.disjoin(a,B) sentence2 = logic.disjoin(~a%(~B|C)) sentence3 = logic.disjoin(~a,~B,C) return logic.conjoin(sentence1,sentence2,sentence3) util.raiseNotDefined()
def exactlyOne(literals): """ Given a list of logic.Expr literals, return a single logic.Expr instance in CNF (conjunctive normal form)that represents the logic that exactly one of the expressions in the list is true. """ "*** YOUR CODE HERE ***" result = [] for value in literals: for other in literals: if (other != value): result.append(logic.disjoin(~value, ~other)) result.append(logic.disjoin(literals)) return logic.conjoin(result)
def exactlyOne(literals): """ Given a list of logic.Expr literals, return a single logic.Expr instance in CNF (conjunctive normal form)that represents the logic that exactly one of the expressions in the list is true. """ conj_list = [] for literal in literals: # could set a variable to indicate if reach the result, to remove repetitive pair for pair_literal in literals: if literal != pair_literal: pair = logic.disjoin(~literal, ~pair_literal) conj_list.append(pair) at_least_one = logic.disjoin(literals) conj_list.append(at_least_one) return logic.conjoin(conj_list)
def exactlyOne(literals) : """ Given a list of logic.Expr literals, return a single logic.Expr instance in CNF (conjunctive normal form)that represents the logic that exactly one of the expressions in the list is true. """ if len(literals) == 1: return literals[0] return logic.conjoin([logic.disjoin(literals),atMostOne(literals)])
def sentence1(): """Returns a logic.Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ "*** YOUR CODE HERE ***" A, B, C = logic.Expr('A'), logic.Expr('B'), logic.Expr('C') line1, line2, line3 = A | B, ~A % (~B | C), logic.disjoin(~A, ~B, C) return logic.conjoin(line1, line2, line3)
def ghostDirectionSuccessorStateAxioms(t, ghost_num, blocked_west_positions, blocked_east_positions): """ Successor state axiom for patrolling ghost direction state (t) (from t-1). west or east walls. Current <==> (causes to stay) | (causes of current) """ # iterate over # blocked east and blocked west disjoin pos_str = ghost_pos_str+str(ghost_num) east_str = ghost_east_str+str(ghost_num) lst = [] lst3 = [] for (x, y) in blocked_east_positions: lst.append(logic.PropSymbolExpr(pos_str, x, y, t)) east = ~(logic.disjoin(lst)) east = logic.PropSymbolExpr(east_str, t-1) & east lst2 = [] for (x, y) in blocked_west_positions: lst2.append(logic.PropSymbolExpr(pos_str, x, y, t)) west = logic.disjoin(lst2) west = ~(logic.PropSymbolExpr(east_str, t-1)) & west return logic.PropSymbolExpr(east_str, t) % (east | west)
def atMostOne(literals) : """ Given a list of logic.Expr literals, return a single logic.Expr instance in CNF (conjunctive normal form) that represents the logic that at most one of the expressions in the list is true. """ notliterals = [~literal for literal in literals] clauses = [] for i in range(0,len(literals)-1): for j in range(i+1, len(literals)): clause = [notliterals[i]] + [notliterals[j]] clauses.append(logic.disjoin(clause)) return logic.conjoin(clauses)
def sentence1(): """Returns a logic.Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ A = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') expr1 = A | B expr2 = (~A) % ((~B)|C) expr3 = logic.disjoin([~A,~B,C]) return logic.conjoin([expr1,expr2,expr3])
def pacmanSuccessorStateAxioms(x, y, t, walls_grid): """ 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) """ "*** YOUR CODE HERE ***" lst = [] for i in [(-1, 0, "East"), (1, 0, "West"), (0, -1, "North"), (0, 1, "South")]: xt, yt = x + i[0], y + i[1] if not walls_grid[xt][yt]: lst.append(logic.PropSymbolExpr(pacman_str, xt, yt, t - 1) & logic.PropSymbolExpr(i[2], t - 1)) if lst: return logic.PropSymbolExpr(pacman_str, x, y, t) % logic.disjoin(lst) else: return logic.PropSymbolExpr(pacman_str, x, y, t) % logic.PropSymbolExpr(pacman_str, x, y, t - 1)
def ghostPositionSuccessorStateAxioms(x, y, t, ghost_num, walls_grid): """ Successor state axiom for patrolling ghost state (x,y,t) (from t-1). Current <==> (causes to stay) | (causes of current) GE is going east, ~GE is going west """ pos_str = ghost_pos_str+str(ghost_num) east_str = ghost_east_str+str(ghost_num) disjoin = [] if not walls_grid[x-1][y]: disjoin.append(logic.PropSymbolExpr(pos_str,x - 1,y, t-1) & logic.PropSymbolExpr(east_str,t-1)) if not walls_grid[x+1][y]: disjoin.append(logic.PropSymbolExpr(pos_str,x + 1,y, t-1) & ~logic.PropSymbolExpr(east_str,t-1)) if walls_grid[x-1][y] and walls_grid[x+1][y]: disjoin.append(logic.PropSymbolExpr(pos_str, x, y, t-1)) disjoin = logic.disjoin(disjoin) return logic.to_cnf(~logic.PropSymbolExpr(pos_str, x, y, t) | disjoin) & logic.to_cnf(~disjoin|logic.PropSymbolExpr(pos_str, x, y, t))
def sentence2(): """Returns a logic.Expr instance that encodes that the following expressions are all true. C if and only if (B or D) A implies ((not B) and (not D)) (not (B and (not C))) implies A (not D) implies C """ "*** YOUR CODE HERE ***" A, B, C, D = logic.Expr('A'), logic.Expr('B'), logic.Expr('C'), logic.Expr('D') C_iff = C % logic.disjoin([B,D]) A_imp = A >> logic.conjoin([~B, ~D]) nots = ~(logic.conjoin([B, ~C])) >> A notD = ~D >> C return logic.conjoin([C_iff, A_imp, nots, notD]) util.raiseNotDefined()
def pacmanSuccessorStateAxioms(x, y, t, walls_grid): """ 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) """ literals = [] if not walls_grid[x][y + 1]: literals.append(logic.PropSymbolExpr(pacman_str, x, y + 1, t-1)&logic.PropSymbolExpr('South', t-1)) if not walls_grid[x][y - 1]: literals.append(logic.PropSymbolExpr(pacman_str, x, y - 1, t-1)&logic.PropSymbolExpr('North', t-1)) if not walls_grid[x + 1][y]: literals.append(logic.PropSymbolExpr(pacman_str, x + 1, y, t-1)&logic.PropSymbolExpr('West', t-1)) if not walls_grid[x - 1][y]: literals.append(logic.PropSymbolExpr(pacman_str, x - 1, y, t-1)&logic.PropSymbolExpr('East', t-1)) expression = logic.PropSymbolExpr(pacman_str, x, y, t) % logic.disjoin(literals) return logic.to_cnf(expression)
def pacmanAliveSuccessorStateAxioms(x, y, t, num_ghosts): """ Successor state axiom for patrolling ghost state (x,y,t) (from t-1). Current <==> (causes to stay) | (causes of current) """ ghost_strs = [ghost_pos_str+str(ghost_num) for ghost_num in xrange(num_ghosts)] alive = logic.PropSymbolExpr(pacman_alive_str, t) lst = [] e = [] for g in ghost_strs: e.append(~logic.PropSymbolExpr(g, x, y, t-1)) e.append(~logic.PropSymbolExpr(g, x, y, t)) q = logic.conjoin(e) lst.append(q) lst.append(~logic.PropSymbolExpr(pacman_str, x, y, t)) res = logic.disjoin(lst) if len(lst) == 0: return alive % alive return alive % (logic.conjoin([logic.PropSymbolExpr(pacman_alive_str, t-1), res]))
def sentence1(): """Returns a logic.Expr instance that encodes that the following expressions are all true. A or B (not A) if and only if ((not B) or C) (not A) or (not B) or C """ "*** YOUR CODE HERE ***" A = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') a_or_b = A | B notA, notB = ~A, ~B notB_or_C = notB | C notA_iff = notA % notB_or_C notA_or_else = logic.disjoin([notA,notB,C]) return logic.conjoin([(a_or_b),(notA_iff),(notA_or_else)]) util.raiseNotDefined()
def atLeastOne(literals) : """ Given a list of logic.Expr literals (i.e. in the form A or ~A), return a single logic.Expr instance in CNF (conjunctive normal form) that represents the logic that at least one of the literals in the list is true. >>> A = logic.PropSymbolExpr('A'); >>> B = logic.PropSymbolExpr('B'); >>> symbols = [A, B] >>> atleast1 = atLeastOne(symbols) >>> model1 = {A:False, B:False} >>> print logic.pl_true(atleast1,model1) False >>> model2 = {A:False, B:True} >>> print logic.pl_true(atleast1,model2) True >>> model3 = {A:True, B:True} >>> print logic.pl_true(atleast1,model2) True """ return logic.disjoin(literals)
def pacmanAliveSuccessorStateAxioms(x, y, t, num_ghosts): """ Successor state axiom for patrolling ghost state (x,y,t) (from t-1). Current <==> (causes to stay) | (causes of current) """ ghost_strs = [ghost_pos_str+str(ghost_num) for ghost_num in xrange(num_ghosts)] "*** YOUR CODE HERE ***" lst1, lst2 = [], [] for g in ghost_strs: lst1.append(logic.PropSymbolExpr(g, x, y, t)) lst2.append(logic.PropSymbolExpr(g, x, y, t - 1)) if len(ghost_strs): causes = (logic.PropSymbolExpr(pacman_str, x, y, t) & logic.disjoin(lst2)) | logic.conjoin((logic.PropSymbolExpr(pacman_str, x, y, t), ~logic.disjoin(lst2), logic.disjoin(lst1))) else: return ~logic.PropSymbolExpr(pacman_alive_str, t) % ~logic.PropSymbolExpr(pacman_alive_str, t - 1) #print logic.PropSymbolExpr(pacman_alive_str, t) % logic.conjoin(logic.PropSymbolExpr(pacman_alive_str, t - 1), cause) return ~logic.PropSymbolExpr(pacman_alive_str, t) % ((logic.PropSymbolExpr(pacman_alive_str, t - 1) & causes) | ~logic.PropSymbolExpr(pacman_alive_str, t - 1))
def ghostPositionSuccessorStateAxioms(x, y, t, ghost_num, walls_grid): """ Successor state axiom for patrolling ghost state (x,y,t) (from t-1). Current <==> (causes to stay) | (causes of current) GE is going east, ~GE is going west """ pos_str = ghost_pos_str+str(ghost_num) east_str = ghost_east_str+str(ghost_num) "*** YOUR CODE HERE ***" lst = [] if not walls_grid[x - 1][y]: lst.append(logic.PropSymbolExpr(pos_str, x - 1, y, t - 1) & logic.PropSymbolExpr(east_str, t - 1)) if not walls_grid[x + 1][y]: lst.append(logic.PropSymbolExpr(pos_str, x + 1, y, t - 1) & ~logic.PropSymbolExpr(east_str, t - 1)) if lst: return logic.PropSymbolExpr(pos_str, x, y, t) % logic.disjoin(lst) else: return logic.PropSymbolExpr(pos_str, x, y, t) % logic.PropSymbolExpr(pos_str, x, y, t - 1)
def ghostDirectionSuccessorStateAxioms(t, ghost_num, blocked_west_positions, blocked_east_positions): """ Successor state axiom for patrolling ghost direction state (t) (from t-1). west or east walls. Current <==> (causes to stay) | (causes of current) """ pos_str = ghost_pos_str+str(ghost_num) east_str = ghost_east_str+str(ghost_num) "*** YOUR CODE HERE ***" wlst, elst = [], [] for (x, y) in blocked_east_positions: elst.append(~logic.PropSymbolExpr(pos_str, x, y, t)) for (x, y) in blocked_west_positions: wlst.append(logic.PropSymbolExpr(pos_str, x, y, t)) if not elst and wlst: return logic.PropSymbolExpr(east_str, t) % (logic.PropSymbolExpr(east_str, t - 1) | (~logic.PropSymbolExpr(east_str, t - 1) & logic.disjoin(wlst))) elif elst and not wlst: return logic.PropSymbolExpr(east_str, t) % (logic.PropSymbolExpr(east_str, t - 1) & logic.conjoin(elst)) elif not elst and not wlst: return logic.PropSymbolExpr(east_str, t) % logic.PropSymbolExpr(east_str, t - 1) else: return logic.PropSymbolExpr(east_str, t) % ((logic.PropSymbolExpr(east_str, t - 1) & logic.conjoin(elst)) | (~logic.PropSymbolExpr(east_str, t - 1) & logic.disjoin(wlst)))
def atMostOne(literals): """ Given a list of Expr literals, return a single Expr instance in CNF (conjunctive normal form) that represents the logic that at most one of the expressions in the list is true. """ "*** BEGIN YOUR CODE HERE ***" comb = list(itertools.combinations(literals, 2)) conj = disjoin(~comb[0][0], ~comb[0][1]) for i in comb[1:]: conj = conjoin(conj, disjoin(~i[0], ~i[1])) return conj "*** END YOUR CODE HERE ***"