Missionaries and Cannibals.
3 Author: Eugen Sawin <sawine@informatik.uni-freiburg.de>
7 goal = ((0, 0, 0), (3, 3, 1))
8 node = Node(((3, 3, 1), (0, 0, 0)))
14 explored.add(node.state)
15 for action in actions(node.state):
16 child = Node(apply(node.state, action), node, action)
17 if child.state == child.parent.state:
20 if child.state not in explored and child not in frontier:
21 if child.state == goal:
24 frontier.append(child)
25 for i, s in enumerate(solutions):
30 parents.append(parent)
31 parent = parent.parent
32 print [p.state for p in reversed(parents)]
35 return (-action[0], -action[1], -action[2])
37 def apply(state, action):
39 return (apply(state[0], action), apply(state[1], negate(action)))
40 return (state[0] + action[0], state[1] + action[1], state[2] + action[2])
43 actions = [(-2, 0, -1), (-1, 0, -1), (-1, -1, -1), (0, -2, -1), (0, -1, -1)]
44 actions.extend([negate(a)for a in actions])
45 states = [apply(state, a) for a in actions]
46 good_states = [s[0] for s in states if s[0][2] >= 0 and s[1][2] >= 0
47 and (s[0][0] >= s[0][1] or not s[0][0])
48 and (s[1][0] >= s[1][1] or not s[1][0])
49 and s[0][1] >= 0 and s[1][1] >= 0
50 and s[0][2] >= 0 and s[1][2] >= 0]
51 return [a for a in actions if apply(state[0], a) in good_states]
54 def __init__(self, state, parent=None, action=None, cost=None):
61 from argparse import ArgumentParser
63 def parse_arguments():
64 parser = ArgumentParser(description="Parses simple modal logic syntax.")
65 parser.add_argument("-f", "--formula", help="your formula")
66 return parser.parse_args()
68 if __name__ == "__main__":