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 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 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))
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()