#Try out pycosat with something really simple. #pycosat is just a Python interface to the PicoSAT #SAT solver. #Our problem. For lunch you have to order soup or salad, but not both. #You only eat soup on cold days. What should you order? #p--you order soup #q--you order salad #r--it's cold. #The conditions are (p XOR q) and (q-->r) #We tranlate to CNF as # (p or q) and (not p or not q) and (not q or r) #Numbering variables 1,2,3 gives clauses #1 2 #-1 -2 #-2 3 import pycosat #return the first solution found def soup_or_salad(): formula=[[1,2],[-1,-2],[-2,3]] return pycosat.solve(formula) #return every solution def soup_or_salad2(): formula=[[1,2],[-1,-2],[-2,3]] u=list(pycosat.itersolve(formula)) return u #What if there's no solution? def soup_or_salad3(): formula=[[1,2],[-1,-2],[-2,3],[1],[2]] return pycosat.solve(formula)