def test_sudoku(self): cnf = [] cnf = cnf + one_hot(cnf) cnf = cnf + row_rules(cnf) cnf = cnf + column_rules(cnf) cnf = cnf + region_rules(cnf) #cnf = {frozenset(x) for x in cnf} #cnf = list(cnf) example = [(0, 0, 2), (0, 1, 5), (0, 4, 3), (0, 6, 9), (0, 8, 1), (1, 1, 1), (1, 5, 4), (2, 0, 4), (2, 2, 7), (2, 6, 2), (2, 8, 8), (3, 2, 5), (3, 3, 2), (4, 4, 9), (4, 5, 8), (4, 6, 1), (5, 1, 4), (5, 5, 3), (6, 3, 3), (6, 4, 6), (6, 7, 7), (6, 8, 2), (7, 1, 7), (7, 8, 3), (8, 0, 9), (8, 2, 3), (8, 6, 6), (8, 8, 4)] cnf = cnf + [[flatten_var(z[0], z[1], z[2]) - 1] for z in example] f = pysat.formula.CNF(from_clauses=cnf) solution = dpll.DPLL(formula=f).get_model_list( ) #model_dict_to_list(f.nv, dpll.dpll_solve(f, {})) self.assertNotEqual(0, len(solution)) X = [unflatten_var(v) for v in solution if v > 0] for i, cell in enumerate( sorted(X, key=lambda h: h[0] * N * N + h[1] * N)): print(cell[2] + 1, end=" ") if (i + 1) % M == 0: print("|", end="") # horizontal wall if (i + 1) % N == 0: print("") # newline if (i + 1) % (N * M) == 0: print("-" * 21) # vertical wall
def test_dpll_solve_satlib_uuf50_01(self): """ Tests the solver on a UNSATisfiable instance from satlib :return: """ self.assertEqual( 0, len(dpll.DPLL(cnf_file='instances/uuf50-01.cnf').get_model_list()))
def test_dpll_solve_satlib_uf50_040(self): """ Tests the solver on a SATisfiable instance from satlib that was problematic on the uf50 tests :return: """ self.assertGreater( len( dpll.DPLL( cnf_file='instances/uf50-040_clean.cnf').get_model_list()), 0)
def test_get_model_str(self): """ Same test as before, plus get_model_str :return: """ f = pysat.formula.CNF(from_clauses=[[1, -2], [1, 3], [-3, -2]]) solver = dpll.DPLL(formula=f) model = solver.solve() self.assertIsNotNone(model) self.assertEqual({1: 1, 3: -3}, model) self.assertEqual('1 2 -3', solver.get_model_str())
def test_dpll_solve_satlib_uf50_01(self): """ Tests the solver on a SATisfiable instance from satlib :return: """ # the following is a satisfiable assignment for 'uf50-01.cnf': # -1 2 -3 4 5 6 7 8 9 -10 -11 12 -13 14 15 -16 -17 -18 19 20 -21 -22 23 -24 -25 # -26 27 -28 -29 -30 -31 32 -33 -34 35 36 37 38 39 -40 -41 -42 -43 -44 -45 -46 # 47 48 49 -50 self.assertGreater( len(dpll.DPLL(cnf_file='instances/uf50-01.cnf').get_model_list()), 0)
def test_dpll_solve_unsat20vars(self): """ Tests the solver 100 UNSATisfiable random 3CNFinstances with 20 vars (they were generated with cnfgen rather than from SATLIB) :return: """ tmp_dir = '/tmp/unsat100' with tarfile.open('instances/3cnf_v20_unsat.tar.gz') as tf: tf.extractall(tmp_dir) for f in os.listdir(tmp_dir): print(f'Testing {f}') model = dpll.DPLL( cnf_file=os.path.join(tmp_dir, f)).get_model_list() self.assertEqual(len(model), 0) shutil.rmtree(tmp_dir)
def test_dpll_solve_with_pure_literals(self): f = pysat.formula.CNF(from_clauses=[[1, -2], [1, 3], [-3, -2]]) model = dpll.DPLL(formula=f).solve() self.assertIsNotNone(model) self.assertEqual({1: 1, 3: -3}, model)
def test_dpll_solve_tautology(self): f = pysat.formula.CNF(from_clauses=[[1, -1]]) model = dpll.DPLL(formula=f).solve() self.assertIsNotNone(model)
def test_dpll_solve_trivial_contradiction(self): f = pysat.formula.CNF(from_clauses=[[1], [-1]]) solver = dpll.DPLL(formula=f) model = solver.solve() self.assertIsNone(model)
def test_dpll_solve_empty(self): f = pysat.formula.CNF(from_clauses=[]) model = dpll.DPLL(formula=f).solve() self.assertIsNotNone(model) self.assertEqual({}, model)