mc.py
author Eugen Sawin <sawine@me73.com>
Wed, 18 May 2011 01:51:18 +0200
changeset 0 33930f88de07
permissions -rw-r--r--
Missionaries and Cannibals.
     1 """
     2 Name:
     3 Author: Eugen Sawin <sawine@informatik.uni-freiburg.de>
     4 """
     5 
     6 def main():    
     7     goal = ((0, 0, 0), (3, 3, 1))
     8     node = Node(((3, 3, 1), (0, 0, 0)))
     9     frontier = [node]
    10     explored = set()
    11     solutions = set()
    12     while len(frontier):
    13         node = frontier.pop()
    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:
    18                 print child
    19                 return
    20             if child.state not in explored and child not in frontier:
    21                 if child.state == goal:
    22                     solutions.add(child)
    23                 else:
    24                     frontier.append(child) 
    25     for i, s in enumerate(solutions):
    26         print "solution ", i
    27         parent = s.parent
    28         parents = []
    29         while parent:
    30             parents.append(parent)            
    31             parent = parent.parent
    32         print [p.state for p in reversed(parents)]
    33             
    34 def negate(action):
    35     return (-action[0], -action[1], -action[2]) 
    36 
    37 def apply(state, action):
    38     if len(state) == 2:
    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])
    41 
    42 def actions(state):      
    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]
    52 
    53 class Node(object):
    54     def __init__(self, state, parent=None, action=None, cost=None):
    55         self.state = state
    56         self.parent = parent
    57         self.action = action
    58         self.cost = cost
    59     
    60 
    61 from argparse import ArgumentParser
    62 
    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()
    67 
    68 if __name__ == "__main__":
    69     main()