def test_dpll4(self): clauses = [ 'a || b', '!a || b || e', 'a || !b', 'b || !e', 'd || !e', '!b || !c || !f', 'a || !e', '!b || f', '!b || c' ] result = dpll(cnf.sentence('\n'.join(clauses))) assert result == None result = dpll(cnf.sentence('\n'.join(clauses[1:]))) assert result == { 'a': False, 'b': False, 'c': False, 'd': False, 'e': False, 'f': False } result = dpll(cnf.sentence('\n'.join(clauses[:-1]))) assert result == { 'a': True, 'b': True, 'c': False, 'd': False, 'e': False, 'f': True }
def test_dpll_contrastive(self): clauses = [ 'a || b', '!a || b || e', 'a || !b', 'b || !e', 'd || !e', '!b || !c || !f', 'a || !e', '!b || f', '!b || c', 'g1 || !a || b', 'g2 || !a || b', 'g3 || !a || b', 'g4 || !a || b', 'g5 || !a || b', 'g6 || !a || b', 'g7 || !a || b', 'g8 || !a || b', 'g9 || !a || b', 'g10 || !a || b', 'g11 || !a || b' ] result = dpll(cnf.sentence('\n'.join(clauses))) assert result == None
def solve(self): def interpret_lit(l): negate = (l[0] == "!") if negate: l = l[2:] else: l = l[1:] d, i, j = l.split("_") return d, i, j, negate if len(self.contents() ) == 0: # short cut if nothing has been filled in return SudokuBoard([[4, 1, 2, 3], [2, 3, 4, 1], [3, 4, 1, 2], [1, 2, 3, 4]]) model = dpll(self.cnf()) if model is None: return None matrix = copy.deepcopy(self.matrix) positive_literals = [l for l in model if model[l] == 1] for l in positive_literals: d, i, j, _ = interpret_lit(l) matrix[int(i) - 1][int(j) - 1] = int(d) return SudokuBoard(matrix)
def test_dpll3(self): clauses = ['a || b', '!a || b', 'a || !b', '!a || !b'] result = dpll(cnf.sentence('\n'.join(clauses))) assert result == None
def test_dpll2(self): clauses = ['a || b', '!a || !b'] result = dpll(cnf.sentence('\n'.join(clauses))) assert result == {'a': False, 'b': True}