def resolve_conflict(self): calc_conflict = Clause() for l in self.imp_graph.lit_assign_ord: calc_conflict.append(-l) f_len = len(self.formula) f_doc = self.formula conf_doc = self.conflict if self.lvl == 0: self.set_unsat() return learnt_clause = self.imp_graph.explain(self.conflict, self.last_decision[0]) self.imp_graph.del_conflicts() lvls = set([self.var_assign[l.x]["lvl"] for l in learnt_clause]) if len(lvls) == 1: if lvls.pop() == 0: self.set_unsat() return else: backjump_lvl = 0 else: lvls.remove(max(lvls)) backjump_lvl = max(lvls) self.backjump(backjump_lvl) self.add_clause(learnt_clause) self.conflict = None if (f_len + 1) != len(self.formula): raise Exception("No clause learnt: " + str(learnt_clause) + "\n Old formula=" + str(f_doc) + "\nNew formula=" + str(self.formula) + "\nOriginal conflict=" + str(conf_doc) + "\nExplained=" + str(learnt_clause) + "\nCalculated=" + str(calc_conflict))
def resolve_clauses(c1, c2, lit): if (lit in c1) and (-lit in c2): pos = c1 neg = c2 elif (lit in c2) and (-lit in c1): pos = c2 neg = c1 else: raise Exception( "Attempt to resolve clauses around a wrong literal. c1={} and c2={} literal={}" .format(c1, c2, lit)) new_clause = Clause() for l in pos: if l != lit: new_clause.append(l) for l in neg: if l != -lit: new_clause.append(l) return new_clause
def iff_clause(self, tseitin_literal_x, tseitin_literal_y): """ get equation (x <-> y) and append to f (x <-> y) <-> g witch in CNF it: (x||-y||-g)&&(-x||y||-g)&&(g||y||x)&&(-x||-y||g) :param tseitin_literal_x :param tseitin_literal_y :return: the new name of (x <-> y) """ g = Literal(self.generate_name()) c = Clause() c.append(tseitin_literal_x) c.append(-tseitin_literal_y) c.append(-g) self.f.append(c) c = Clause() c.append(-tseitin_literal_x) c.append(tseitin_literal_y) c.append(-g) self.f.append(c) c = Clause() c.append(-tseitin_literal_x) c.append(-tseitin_literal_y) c.append(g) self.f.append(c) c = Clause() c.append(tseitin_literal_x) c.append(tseitin_literal_y) c.append(g) self.f.append(c) return g
def not_clause(self, tseitin_literal): g = Literal(self.generate_name()) c = Clause() c.append(-tseitin_literal) c.append(-g) self.f.append(c) c = Clause() c.append(tseitin_literal) c.append(g) self.f.append(c) return g
def run_TsetinTransformation(self, tree): self.parser(tree) c = Clause() c.append(self.var_name) self.f.append(c) return self.f
from SAT.Preprocess import remove_redundant_clauses from SAT.CNF_formula import CNF_formula, Clause, Literal from SAT.Assignment import Assignment f = CNF_formula() c1 = Clause() c2 = Clause() c3 = Clause() c4 = Clause() for i in {1, -4}: c1.append(Literal(i)) for i in {2}: c2.append(Literal(i)) for i in {-2, 3}: c3.append(Literal(i)) for i in {-2, -3, 4}: c4.append(Literal(i)) f.append(c1) f.append(c2) f.append(c3) f.append(c4) print("Original formula={}\n".format(f)) a = Assignment(f) print("Initial Assignment:{}".format(a)) a.plp_iteration() print("Assignment after PLP:{}".format(a))
my_sat_cnt += 1 if my_sat else 0 g_unsat_cnt += 1 if not g_sat else 0 my_unsat_cnt += 1 if not my_sat else 0 if g_sat != my_sat: fail = True fail_num += 1 out += ("Failing formula: " + str(my_formula) + "\n") out += ("Index is : " + str(ii) + "\n") if g_sat: out += ("Glucose thinks SAT : " + "\n") out += (str(g_model) + "\n") false_unsat += 1 fail_formula.append((False, my_formula)) else: out += ("My sat_solver thinks SAT : " + "\n") out += (str(Clause.create_clause(my_model)) + "\n") false_sat += 1 fail_formula.append((True, my_formula)) g.delete() if (ii % (ITERATION_NUM / toolbar_width) == 0): sys.stdout.write("-") sys.stdout.flush() #print("\n") out += ("\n") out += ("FAIL" if fail else "PASS" + "\n") out += ("Failure: " + str(fail_num) + " Passing: " + str(ITERATION_NUM - fail_num) + "\n") out += ("False SAT: " + str(false_sat) + " False UNSAT: " + str(false_unsat) + "\n")
formulas.append( CNF_formula.create_formula([{-1}, {-1}, {1}, {1, -1}, {1}, {-1}])) required_results.append(False) formulas.append( CNF_formula.create_formula([{1, 2}, {1, -2}, {-1, 2}, {-1, -2}])) required_results.append(False) formulas.append( CNF_formula.create_formula([{1, 4}, {1, 5}, {1, 3}, {-1, 2}, {-2, 3}, {-2, -3}])) required_results.append(True) for i, f in enumerate(formulas): print("Preprocessed version=" + str(remove_redundant_clauses(f))) print("result for formula :" + str(i) + " = " + str(f)) result = Assignment.cnf_sat_solve(f) if result[0]: print("SAT") print(str(Clause.create_clause(result[1]))) else: print("UNSAT") result_record.append(bool(result[0])) print("Required result is :" + ("SAT" if required_results[i] else "UNSAT")) print("\n\n") print("Required:" + str(required_results)) print("Output :" + str(result_record)) if any([req != rec for req, rec in zip(required_results, result_record)]): print("FAIL") else: print("PASS")
from SAT.Preprocess import remove_redundant_clauses from SAT.CNF_formula import CNF_formula, Clause, Literal f = CNF_formula() c1 = Clause() c2 = Clause() c3 = Clause() c4 = Clause() c5 = Clause() c6 = Clause() for i in {1, 2, 3}: c1.append(Literal(i)) c2.append(Literal(i)) for i in {1, 2, 2}: c3.append(Literal(i)) for i in {1, 2, -2}: c4.append(Literal(i)) f.append(c1) f.append(c2) f.append(c3) f.append(c4) f.append(c5) f.append(c6) f_pp = remove_redundant_clauses(f) f_target = CNF_formula() c1_target = Clause() c2_target = Clause()