本文整理汇总了Python中cnfformula.cnf.CNF类的典型用法代码示例。如果您正苦于以下问题:Python CNF类的具体用法?Python CNF怎么用?Python CNF使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CNF类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CountingPrinciple
def CountingPrinciple(M,p):
"""Generates the clauses for the counting matching principle.
The principle claims that there is a way to partition M in sets of
size p each.
Arguments:
- `M` : size of the domain
- `p` : size of each class
"""
cnf=CNF()
# Describe the formula
name="Counting Principle: {0} divided in parts of size {1}.".format(M,p)
cnf.header=name+"\n"+cnf.header
def var_name(tpl):
return "Y_{{"+",".join("{0}".format(v) for v in tpl)+"}}"
# Incidence lists
incidence=[[] for _ in range(M)]
for tpl in combinations(range(M),p):
for i in tpl:
incidence[i].append(tpl)
# Each element of the domain is in exactly one part.
for el in range(M):
edge_vars = [var_name(tpl) for tpl in incidence[el]]
cnf.add_equal_to(edge_vars,1)
return cnf
示例2: PerfectMatchingPrinciple
def PerfectMatchingPrinciple(graph):
"""Generates the clauses for the graph perfect matching principle.
The principle claims that there is a way to select edges to such
that all vertices have exactly one incident edge set to 1.
Arguments:
- `graph` : undirected graph
"""
cnf=CNF()
# Describe the formula
name="Perfect Matching Principle"
if hasattr(graph,'name'):
cnf.header=name+" of graph:\n"+graph.name+"\n"+cnf.header
else:
cnf.header=name+".\n"+cnf.header
def var_name(u,v):
if u<=v:
return 'x_{{{0},{1}}}'.format(u,v)
else:
return 'x_{{{0},{1}}}'.format(v,u)
# Each vertex has exactly one edge set to one.
for v in graph.nodes():
edge_vars = [var_name(u,v) for u in graph.adj[v]]
for cls in equal_to_constraint(edge_vars,1):
cnf.add_clause(cls)
return cnf
示例3: TseitinFormula
def TseitinFormula(graph,charges=None):
"""Build a Tseitin formula based on the input graph.
Odd charge is put on the first vertex by default, unless other
vertices are is specified in input.
Arguments:
- `graph`: input graph
- `charges': odd or even charge for each vertex
"""
V=enumerate_vertices(graph)
if charges==None:
charges=[1]+[0]*(len(V)-1) # odd charge on first vertex
else:
charges = [bool(c) for c in charges] # map to boolean
if len(charges)<len(V):
charges=charges+[0]*(len(V)-len(charges)) # pad with even charges
# init formula
tse=CNF()
for e in sorted(graph.edges(),key=sorted):
tse.add_variable(edgename(e))
# add constraints
for v,c in zip(V,charges):
# produce all clauses and save half of them
names = [ edgename((u,v)) for u in neighbors(graph,v) ]
for cls in parity_constraint(names,c):
tse.add_clause(list(cls),strict=True)
return tse
示例4: PerfectMatchingPrinciple
def PerfectMatchingPrinciple(G):
"""Generates the clauses for the graph perfect matching principle.
The principle claims that there is a way to select edges to such
that all vertices have exactly one incident edge set to 1.
Parameters
----------
G : undirected graph
"""
cnf=CNF()
# Describe the formula
name="Perfect Matching Principle"
if hasattr(G,'name'):
cnf.header=name+" of graph:\n"+G.name+"\n"+cnf.header
else:
cnf.header=name+".\n"+cnf.header
def var_name(u,v):
if u<=v:
return 'x_{{{0},{1}}}'.format(u,v)
else:
return 'x_{{{0},{1}}}'.format(v,u)
# Each vertex has exactly one edge set to one.
for v in enumerate_vertices(G):
edge_vars = [var_name(u,v) for u in neighbors(G,v)]
cnf.add_equal_to(edge_vars,1)
return cnf
示例5: test_one_eq
def test_one_eq(self) :
opb="""\
* #variable= 5 #constraint= 1
*
+1 x1 +1 x2 +1 x3 +1 x4 +1 x5 = 2;
"""
F=CNF()
F.add_equal_to(["a","b","c","d","e"],2)
self.assertCnfEqualsOPB(F,opb)
示例6: test_one_lt
def test_one_lt(self) :
opb="""\
* #variable= 5 #constraint= 1
*
-1 x1 -1 x2 -1 x3 -1 x4 -1 x5 >= -1;
"""
F=CNF()
F.add_strictly_less_than(["a","b","c","d","e"],2)
self.assertCnfEqualsOPB(F,opb)
示例7: test_one_leq
def test_one_leq(self) :
opb="""\
* #variable= 5 #constraint= 1
*
-1 x1 -1 x2 -1 x3 -1 x4 -1 x5 >= -2;
"""
F=CNF()
F.add_less_or_equal(["a","b","c","d","e"],2)
self.assertCnfEqualsOPB(F,opb)
示例8: test_one_gt
def test_one_gt(self) :
opb="""\
* #variable= 5 #constraint= 1
*
+1 x1 +1 x2 +1 x3 +1 x4 +1 x5 >= 3;
"""
F=CNF()
F.add_strictly_greater_than(["a","b","c","d","e"],2)
self.assertCnfEqualsOPB(F,opb)
示例9: test_one_clause
def test_one_clause(self) :
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")])
self.assertCnfEqualsOPB(F,opb)
示例10: test_weighted_leq
def test_weighted_leq(self) :
opb="""\
* #variable= 5 #constraint= 1
*
-1 x1 -2 x2 -3 x3 +1 x4 +2 x5 >= -2;
"""
F=CNF()
F.add_linear(1,"a",2,"b",3,"c",-1,"d",-2,"e","<=",2)
self.assertCnfEqualsOPB(F,opb)
示例11: test_weighted_gt
def test_weighted_gt(self) :
opb="""\
* #variable= 5 #constraint= 1
*
+1 x1 +2 x2 +3 x3 -1 x4 -2 x5 >= 3;
"""
F=CNF()
F.add_linear(1,"a",2,"b",3,"c",-1,"d",-2,"e",">",2)
self.assertCnfEqualsOPB(F,opb)
示例12: test_strict_clause_insertion
def test_strict_clause_insertion(self):
F=CNF()
F.mode_strict()
F.add_variable("S")
F.add_variable("T")
F.add_variable("U")
self.assertTrue(len(list(F.variables()))==3)
F.add_clause([(True,"S"),(False,"T")])
F.add_clause([(True,"T"),(False,"U")])
self.assertRaises(ValueError, F.add_clause,
[(True,"T"),(False,"V")])
示例13: build_cnf
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) ]
orcnf = CNF([clause])
orcnf.header = "Clause with {} positive and {} negative literals\n\n".format(args.P,args.N) + \
orcnf.header
return orcnf
示例14: test_parity
def test_parity(self) :
opb="""\
* #variable= 3 #constraint= 4
*
+1 x1 +1 x2 +1 x3 >= 1;
+1 x1 -1 x2 -1 x3 >= -1;
-1 x1 +1 x2 -1 x3 >= -1;
-1 x1 -1 x2 +1 x3 >= -1;
"""
F=CNF()
F.add_parity(["a","b","c"],1)
self.assertCnfEqualsOPB(F,opb)
示例15: EvenColoringFormula
def EvenColoringFormula(G):
"""Even coloring formula
The formula is defined on a graph :math:`G` and claims that it is
possible to split the edges of the graph in two parts, so that
each vertex has an equal number of incident edges in each part.
The formula is defined on graphs where all vertices have even
degree. The formula is satisfiable only on those graphs with an
even number of vertices in each connected component [1]_.
Arguments
---------
G : networkx.Graph
a simple undirected graph where all vertices have even degree
Raises
------
ValueError
if the graph in input has a vertex with odd degree
Returns
-------
CNF object
References
----------
.. [1] Locality and Hard SAT-instances, Klas Markstrom
Journal on Satisfiability, Boolean Modeling and Computation 2 (2006) 221-228
"""
F = CNF()
F.mode_strict()
F.header = "Even coloring formula on graph " + G.name + "\n" + F.header
def var_name(u,v):
if u<=v:
return 'x_{{{0},{1}}}'.format(u,v)
else:
return 'x_{{{0},{1}}}'.format(v,u)
for (u, v) in enumerate_edges(G):
F.add_variable(var_name(u, v))
# Defined on both side
for v in enumerate_vertices(G):
if G.degree(v) % 2 == 1:
raise ValueError("Markstrom formulas requires all vertices to have even degree.")
edge_vars = [ var_name(u, v) for u in neighbors(G, v) ]
F.add_exactly_half_ceil(edge_vars) # F.add_exactly_half_floor would work the same
return F