def cnf_from_variables_and_clauses(variables, clauses) : cnf = CNF() for variable in variables : cnf.add_variable(variable) for clause in clauses : cnf.add_clause(clause) return cnf
def test_polarity_shuffle_vs_flip(self) : cnf = CNF([[(True,'x'),(True,'y'),(False,'z')]]) variable_permutation = list(cnf.variables()) clause_permutation = range(len(cnf)) polarity_flip = [-1]*len(variable_permutation) shuffled = Shuffle(cnf, variable_permutation, clause_permutation, polarity_flip) flipped = FlipPolarity(cnf) self.assertCnfEqual(flipped,shuffled)
def test_single_vertex_graph(self): """Singleton graph has no nontrivial automorphism.""" G1=nx.Graph() G1.add_node(0) cnf1 = GraphAutomorphism(G1) v = list(cnf1.variables())[0] cnf2 = CNF() cnf2.add_clause([(True,v)]) cnf2.add_clause([(False,v)]) self.assertCnfEqual(cnf1,cnf2)
def test_polarity_shuffle_vs_flip(self): cnf = CNF([[(True, 'x'), (True, 'y'), (False, 'z')]]) variable_permutation = list(cnf.variables()) clause_permutation = list(range(len(cnf))) polarity_flip = [-1] * len(variable_permutation) shuffled = Shuffle(cnf, variable_permutation, clause_permutation, polarity_flip) flipped = FlipPolarity(cnf) self.assertCnfEqual(flipped, shuffled)
def test_one_clause(self): dimacs = """\ p cnf 4 1 1 2 -3 -4 0 """ opb = """\ * #variable= 4 #constraint= 1 * +1 x1 +1 x2 -1 x3 -1 x4 >= -1; """ F = CNF() F.add_clause([(True, "a"), (True, "b"), (False, "c"), (False, "d")]) F = Expand(F) self.assertCnfEqualsDimacs(F, dimacs) self.assertCnfEqualsOPB(F, opb)
def test_contradiction(self): dimacs = """\ p cnf 3 1 0 """ opb = """\ * #variable= 3 #constraint= 1 * >= 1; """ F = CNF() F.add_greater_or_equal(["a", "b", "c"], 4) F = Expand(F) self.assertCnfEqualsDimacs(F, dimacs) self.assertCnfEqualsOPB(F, opb)
def test_subset_sum(self): dimacs = """\ p cnf 3 7 -1 -2 -3 0 -1 2 -3 0 -1 2 3 0 1 -2 -3 0 1 -2 3 0 1 2 -3 0 1 2 3 0 """ F = CNF() F.add_linear(3, "a", 5, "b", 7, "c", "==", 8) F = Expand(F) self.assertCnfEqualsDimacs(F, dimacs)
def test_subset_sum(self) : dimacs="""\ p cnf 3 7 -1 -2 -3 0 -1 2 -3 0 -1 2 3 0 1 -2 -3 0 1 -2 3 0 1 2 -3 0 1 2 3 0 """ F=CNF() F.add_linear(3,"a",5,"b",7,"c","==",8) F = Expand(F) self.assertCnfEqualsDimacs(F,dimacs)
def test_contradiction(self) : dimacs="""\ p cnf 3 1 0 """ opb="""\ * #variable= 3 #constraint= 1 * >= 1; """ F=CNF() F.add_greater_or_equal(["a","b","c"],4) F = Expand(F) self.assertCnfEqualsDimacs(F,dimacs) self.assertCnfEqualsOPB(F,opb)
def test_one_clause(self) : dimacs="""\ p cnf 4 1 1 2 -3 -4 0 """ opb="""\ * #variable= 4 #constraint= 1 * +1 x1 +1 x2 -1 x3 -1 x4 >= -1; """ F=CNF() F.add_clause([(True,"a"),(True,"b"),(False,"c"),(False,"d")]) F = Expand(F) self.assertCnfEqualsDimacs(F,dimacs) self.assertCnfEqualsOPB(F,opb)
def test_empty_vs_empty(self): """Empty graphs are isomorphic.""" G1 = nx.Graph() G2 = nx.Graph() cnf1 = CNF() cnf2 = GraphIsomorphism(G1, G2) self.assertCnfEqual(cnf1, cnf2)
def test_empty_vs_non_empty(self): """Empty graph is not isomorphic to a non empty graph.""" G1 = nx.Graph() G2 = nx.complete_graph(3) cnf1 = CNF([[]]) # one empty clause cnf2 = GraphIsomorphism(G1, G2) self.assertCnfEqual(cnf1, cnf2)
def test_empty(self): G = CNF() graph=nx.Graph() for functional in (True,False): for onto in (True,False): F = GraphPigeonholePrinciple(graph,functional,onto) self.assertCnfEqual(F,G)
def build_cnf(args): """Build a conjunction Arguments: - `args`: command line options """ clauses = [ [(True,"x_{}".format(i))] for i in range(args.P) ] + \ [ [(False,"y_{}".format(i))] for i in range(args.N) ] return CNF(clauses, header="""Singleton clauses: {} positive and {} negative""".format(args.P,args.N))
def test_one_inequality(self) : dimacs="""\ p cnf 3 3 1 2 0 1 3 0 2 3 0 """ opb="""\ * #variable= 3 #constraint= 3 * +1 x1 +1 x2 >= 1; +1 x1 +1 x3 >= 1; +1 x2 +1 x3 >= 1; """ F=CNF() F.add_greater_or_equal(["a","b","c"],2) F = Expand(F) self.assertCnfEqualsDimacs(F,dimacs) self.assertCnfEqualsOPB(F,opb)
def test_one_inequality(self): dimacs = """\ p cnf 3 3 1 2 0 1 3 0 2 3 0 """ opb = """\ * #variable= 3 #constraint= 3 * +1 x1 +1 x2 >= 1; +1 x1 +1 x3 >= 1; +1 x2 +1 x3 >= 1; """ F = CNF() F.add_greater_or_equal(["a", "b", "c"], 2) F = Expand(F) self.assertCnfEqualsDimacs(F, dimacs) self.assertCnfEqualsOPB(F, opb)
def build_cnf(args): """Build an disjunction Arguments: - `args`: command line options """ clause = [ (True,"x_{}".format(i)) for i in range(args.P) ] + \ [ (False,"y_{}".format(i)) for i in range(args.N) ] return CNF([clause], header="""Single clause with {} positive and {} negative literals""".format(args.P,args.N))
def cnf_from_variables_and_clauses(variables, clauses): cnf = CNF() for variable in variables: cnf.add_variable(variable) for clause in clauses: cnf.add_clause(clause) return cnf
def test_single_vertex_graph(self): """Singleton graph has no nontrivial automorphism.""" G1 = nx.Graph() G1.add_node(0) cnf1 = GraphAutomorphism(G1) v = list(cnf1.variables())[0] cnf2 = CNF() cnf2.add_clause([(True, v)]) cnf2.add_clause([(False, v)]) self.assertCnfEqual(cnf1, cnf2)
def test_simple_flip(self): flipped = FlipPolarity(CNF([[(False, 'x'), (False, 'y'), (True, 'z')]])) expected = CNF([[(True, 'x'), (True, 'y'), (False, 'z')]]) self.assertCnfEqual(expected, flipped)
def test_empty(self): G = CNF() graph = nx.Graph() F = EvenColoringFormula(graph) self.assertCnfEqual(F, G)
def test_empty_graph(self): """Empty graph has no nontrivial automorphism.""" G1 = nx.Graph() cnf1 = CNF([[]]) # one empty clause cnf2 = GraphAutomorphism(G1) self.assertCnfEqual(cnf1, cnf2)
def test_empty(self): cnf = CNF() lift = Expand(cnf) self.assertCnfEqual(cnf, lift)
def reshuffle(cnf, variable_permutation=None, clause_permutation=None, polarity_flip=None ): """ Reshuffle the given cnf. Returns a formula logically equivalent to the input with the following transformations applied in order: 1. Polarity flips. polarity_flip is a {-1,1}^n vector. If the i-th entry is -1, all the literals with the i-th variable change its sign. 2. Variable permutations. variable_permutation is a permutation of [vars(cnf)]. All the literals with the old i-th variable are replaced with the new i-th variable. 3. Clause permutations. clause_permutation is a permutation of [0..m-1]. The resulting clauses are reordered according to the permutation. """ # empty cnf out=CNF(header='') out.header="Reshuffling of:\n\n"+cnf.header variables=list(cnf.variables()) N=len(variables) M=len(cnf) # variable permutation if variable_permutation==None: variable_permutation=variables random.shuffle(variable_permutation) else: assert len(variable_permutation)==N # polarity flip if polarity_flip==None: polarity_flip=[random.choice([-1,1]) for x in xrange(N)] else: assert len(polarity_flip)==N # # substitution of variables # for v in variable_permutation: out.add_variable(v) substitution=[None]*(2*N+1) reverse_idx=dict([(v,i) for (i,v) in enumerate(out.variables(),1)]) polarity_flip = [None]+polarity_flip for i,v in enumerate(cnf.variables(),1): substitution[i]= polarity_flip[i]*reverse_idx[v] substitution[-i]= -substitution[i] # # permutation of clauses # if clause_permutation==None: clause_permutation=range(M) random.shuffle(clause_permutation) # load clauses out._clauses = [None]*M for (old,new) in enumerate(clause_permutation): out._clauses[new]=tuple( substitution[l] for l in cnf._clauses[old]) # return the formula assert out._check_coherence(force=True) return out
def reshuffle(cnf, variable_permutation=None, clause_permutation=None, polarity_flip=None): """ Reshuffle the given cnf. Returns a formula logically equivalent to the input with the following transformations applied in order: 1. Polarity flips. polarity_flip is a {-1,1}^n vector. If the i-th entry is -1, all the literals with the i-th variable change its sign. 2. Variable permutations. variable_permutation is a permutation of [vars(cnf)]. All the literals with the old i-th variable are replaced with the new i-th variable. 3. Clause permutations. clause_permutation is a permutation of [0..m-1]. The resulting clauses are reordered according to the permutation. """ # empty cnf out = CNF(header='') out.header = "Reshuffling of:\n\n" + cnf.header variables = list(cnf.variables()) N = len(variables) M = len(cnf) # variable permutation if variable_permutation == None: variable_permutation = variables random.shuffle(variable_permutation) else: assert len(variable_permutation) == N # polarity flip if polarity_flip == None: polarity_flip = [random.choice([-1, 1]) for x in xrange(N)] else: assert len(polarity_flip) == N # # substitution of variables # for v in variable_permutation: out.add_variable(v) substitution = [None] * (2 * N + 1) reverse_idx = dict([(v, i) for (i, v) in enumerate(out.variables(), 1)]) polarity_flip = [None] + polarity_flip for i, v in enumerate(cnf.variables(), 1): substitution[i] = polarity_flip[i] * reverse_idx[v] substitution[-i] = -substitution[i] # # permutation of clauses # if clause_permutation == None: clause_permutation = range(M) random.shuffle(clause_permutation) # load clauses out._clauses = [None] * M for (old, new) in enumerate(clause_permutation): out._clauses[new] = tuple(substitution[l] for l in cnf._clauses[old]) # return the formula assert out._check_coherence(force=True) return out
def test_empty(self): G = CNF() graph = nx.Graph() F = SubsetCardinalityFormula(graph) self.assertCnfEqual(F, G)
def test_empty(self): G = CNF() graph = nx.Graph() F = GraphOrderingPrinciple(graph) self.assertCnfEqual(F, G)
def test_empty(self): G = CNF() graph = nx.Graph() F = PerfectMatchingPrinciple(graph) self.assertCnfEqual(F, G)
def test_null_graph(self): G = nx.DiGraph() peb = PebblingFormula(G) self.assertTrue(peb._check_coherence()) self.assertCnfEqual(peb, CNF())