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 positionLogicPlan(problem): """ Given an instance of a PositionSearchProblem, return a list of actions that lead to the goal. Available actions are game.Directions.{NORTH,SOUTH,EAST,WEST} Note that STOP is not an available action. """ actions = [ game.Directions.EAST, game.Directions.SOUTH, game.Directions.WEST, game.Directions.NORTH ] init_t = util.manhattanDistance(problem.getStartState(), problem.getGoalState()) goal_s = problem.getGoalState() preds = getPredecessors(problem) start_pos = problem.getStartState() init_state = [logic.Expr("&", logic.PropSymbolExpr("P", start_pos[0],start_pos[1],0),\ *[logic.Expr("~", logic.PropSymbolExpr("P", s[0],s[1],0)) for s in preds.keys() if s != start_pos])] for t in xrange(init_t, 51): goal = [logic.PropSymbolExpr("P", goal_s[0], goal_s[1]), \ logic.to_cnf(logic.Expr(">>", logic.PropSymbolExpr("P", goal_s[0], goal_s[1]),\ logic.Expr("|", *[logic.PropSymbolExpr("P", goal_s[0], goal_s[1], time) for time in xrange(1,t+1)])))] successors = generateSuccessorState(preds, t) exps = goal + successors + init_state model = logic.pycoSAT(exps) if model: return extractActionSequence(model, actions) return []
def exactlyOne(expressions): """ Given a list of logic.Expr instances, 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. """ return logic.Expr("|", *[logic.Expr("&", expr_i, \ *[logic.Expr("~", expr_j) for expr_j in expressions if expr_i != expr_j]) for expr_i in expressions])
def atMostOne(expressions): """ Given a list of logic.Expr instances, 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. """ exact = exactlyOne(expressions) none = logic.Expr("&", logic.Expr("~", *expressions)) return logic.Expr("|", none, exact)
def foodGhostLogicPlan(problem): """ Given an instance of a FoodGhostSearchProblem, return a list of actions that help Pacman eat all of the food and avoid patrolling ghosts. Ghosts only move east and west. They always start by moving East, unless they start next to an eastern wall. Available actions are game.Directions.{NORTH,SOUTH,EAST,WEST} Note that STOP is not an available action. """ actions = [ game.Directions.EAST, game.Directions.SOUTH, game.Directions.WEST, game.Directions.NORTH ] # this is my food grid as a list [(x,y), (x,y) ...] food_list = problem.getStartState()[1].asList() # this is a list of the distances from my start state to each food on the grid manhattan_food_distances = [ util.manhattanDistance(problem.getStartState()[0], food) for food in food_list ] # for the predecessors function extractState = lambda x: x[0][0] generateState = lambda x: (x, problem.getStartState()[1]) # return the food that is furthest away init_t = max(manhattan_food_distances) preds = getPredecessors(problem, extractState, generateState) start_pos = problem.getStartState()[0] init_state = [logic.Expr("&", logic.PropSymbolExpr("P", start_pos[0],start_pos[1],0),\ *[logic.Expr("~", logic.PropSymbolExpr("P", s[0],s[1],0)) for s in preds.keys() if s != start_pos])] ghost_pos_arrays = [ getGhostPositionArray(problem, ghost.getPosition()) for ghost in problem.getGhostStartStates() ] for t in xrange(init_t, 51): ghosts = reduce(lambda x,y: x + y, [[[~logic.PropSymbolExpr("P", g[i%len(g)][0],g[i%len(g)][1],i+1),\ ~logic.PropSymbolExpr("P", g[i%len(g)][0],g[i%len(g)][1],i)]\ for i in xrange(t+1)] for g in ghost_pos_arrays]) ghosts = reduce(lambda x, y: x + y, ghosts) goal_list = [] for food in food_list: # food is an (x, y) coordinate goal_list.append([logic.PropSymbolExpr("P", food[0], food[1]), \ logic.to_cnf(logic.Expr(">>", logic.PropSymbolExpr("P", food[0], food[1]),\ logic.Expr("|", *[logic.PropSymbolExpr("P", food[0], food[1], time) for time in xrange(t+1)])))]) successors = generateSuccessorState(preds, t) # makes goal_list a list, previously was a list of lists goal_list = reduce(lambda x, y: x + y, goal_list) exps = goal_list + successors + init_state + ghosts model = logic.pycoSAT(exps) if model: return extractActionSequence(model, actions) return []
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') return logic.conjoin([(A | B), ((~A) % ((~B) | C)), ((~A) | (~B) | C)])
def foodLogicPlan(problem): """ Given an instance of a FoodSearchProblem, return a list of actions that help Pacman eat all of the food. Available actions are game.Directions.{NORTH,SOUTH,EAST,WEST} Note that STOP is not an available action. """ # need a list where all the food is # go through with the logic phrase, for each food make sure at some time step t I will be there actions = [ game.Directions.EAST, game.Directions.SOUTH, game.Directions.WEST, game.Directions.NORTH ] # this is my food grid as a list [(x,y), (x,y) ...] food_list = problem.getStartState()[1].asList() # this is a list of the distances from my start state to each food on the grid manhattan_food_distances = [ util.manhattanDistance(problem.getStartState()[0], food) for food in food_list ] # for the predecessors function extractState = lambda x: x[0][0] generateState = lambda x: (x, problem.getStartState()[1]) # return the food that is furthest away init_t = max(manhattan_food_distances) preds = getPredecessors(problem, extractState, generateState) start_pos = problem.getStartState()[0] init_state = [logic.Expr("&", logic.PropSymbolExpr("P", start_pos[0],start_pos[1],0),\ *[logic.Expr("~", logic.PropSymbolExpr("P", s[0],s[1],0)) for s in preds.keys() if s != start_pos])] for t in xrange(init_t, 51): goal_list = [] for food in food_list: # food is an (x, y) coordinate goal_list.append([logic.PropSymbolExpr("P", food[0], food[1]), \ logic.to_cnf(logic.Expr(">>", logic.PropSymbolExpr("P", food[0], food[1]),\ logic.Expr("|", *[logic.PropSymbolExpr("P", food[0], food[1], time) for time in xrange(1,t+1)])))]) successors = generateSuccessorState(preds, t) # makes goal_list a list, previously was a list of lists goal_list = reduce(lambda x, y: x + y, goal_list) exps = goal_list + successors + init_state model = logic.pycoSAT(exps) if model: return extractActionSequence(model, actions) return []
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') one = A | B two = (~A) % ((~B) | C) three = logic.disjoin((~A), (~B), C) return logic.conjoin([one, two, three])
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') First = A | B Second = ~A % (~B | C) Third = logic.disjoin(~A, ~B, C) return logic.conjoin(First, Second, Third)
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') NOT_A, NOT_B = ~A, ~B E1 = A | B E2 = (NOT_A) % ((NOT_B) | C) E3 = logic.disjoin([NOT_A, NOT_B, C]) return logic.conjoin([E1, E2, E3])
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') r1 = A | B r2 = (~A) % ((~B) | C) r3 = logic.disjoin(~A, ~B, C) return logic.conjoin(r1, r2, r3)
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 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') L1 = A | B L2 = (~A) % ((~B) | C) L3 = logic.disjoin((~A), (~B), C) return logic.conjoin(L1, L2, L3)
def sentence1(): """Returns a logic.Expr instance that encodes that the following inits 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') s1 = A | B s2 = (~A) % (~B | C) s3 = logic.disjoin([~A, ~B, C]) return logic.conjoin([s1, s2, s3])
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') return logic.conjoin([(C % (B | D)), (A >> ((~B) & (~D))), (~(B & (~C)) >> A), ((~D) >> C)])
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') expr1 = A | B expr2 = (~A) % ((~B) | C) expr3 = logic.disjoin([~A, ~B, C]) return logic.conjoin([expr1, expr2, expr3])
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") s1 = A | B s2 = (~A) % ((~B) | C) s3 = logic.disjoin([(~A), (~B), C]) return logic.conjoin([s1, s2, s3])
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 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 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 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") p1 = A | B p2 = ~A % (~B | C) p3 = logic.disjoin(~A, ~B, C) return logic.conjoin(p1, p2, p3)
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') s1 = logic.to_cnf(A | B) s2 = ~A % (~B | C) s3 = logic.to_cnf(~A | ~B | C) return logic.conjoin(s1, s2, s3) util.raiseNotDefined()
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 """ A = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') D = logic.Expr('D') aaa = C % (B | D) a_or_b = A >> (~B & ~D) not_a = ~(B & ~C) >> A not_d = ~D >> C return logic.conjoin(aaa, a_or_b, not_a, not_d)
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 ***" return logic.Expr('A') # Replace this with your expression
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 """ A = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') D = logic.Expr('D') r1 = C % (B | D) r2 = A >> ((~B) & (~D)) r3 = (~(B & (~C))) >> A r4 = (~D) >> C return logic.conjoin(r1, r2, r3, r4)
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 not_A_iff_not_B_or_C = ~A % (~B | C) not_A_or_not_B_or_C = logic.disjoin(~A, ~B, C) sentence = logic.conjoin(A_or_B, not_A_iff_not_B_or_C, not_A_or_not_B_or_C) return sentence
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') NOT_A, NOT_B, NOT_C, NOT_D = ~A, ~B, ~C, ~D E1 = (C) % (B | D) E2 = A >> (NOT_B & NOT_D) E3 = (~(B & NOT_C)) >> A E4 = NOT_D >> C return logic.conjoin([E1, E2, E3, E4])
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 """ A = logic.Expr('A') B = logic.Expr('B') C = logic.Expr('C') D = logic.Expr('D') one = C % (B | D) two = A >> ((~B) & (~D)) three = (~(B & (~C))) >> A four = (~D) >> C return (logic.conjoin([one, two, three, four]))
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') L1 = C % (B | D) L2 = A >> ((~B) & (~D)) L3 = (~(B & (~C))) >> A L4 = (~D) >> C return logic.conjoin(L1, L2, L3, L4)
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") s1 = C % (B | D) s2 = A >> ((~B) & (~D)) s3 = (~(B & (~C))) >> A s4 = (~D) >> C return logic.conjoin([s1, s2, s3, s4])