本文整理汇总了Python中cnfformula.cnf.CNF.add_parity方法的典型用法代码示例。如果您正苦于以下问题:Python CNF.add_parity方法的具体用法?Python CNF.add_parity怎么用?Python CNF.add_parity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cnfformula.cnf.CNF
的用法示例。
在下文中一共展示了CNF.add_parity方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parity
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import add_parity [as 别名]
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)
示例2: TseitinFormula
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import add_parity [as 别名]
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()
edgename = { }
for (u,v) in sorted(graph.edges(),key=sorted):
edgename[(u,v)] = "E_{{{0},{1}}}".format(u,v)
edgename[(v,u)] = "E_{{{0},{1}}}".format(u,v)
tse.add_variable(edgename[(u,v)])
tse.mode_strict()
# add constraints
for v,charge in zip(V,charges):
# produce all clauses and save half of them
names = [ edgename[(u,v)] for u in neighbors(graph,v) ]
tse.add_parity(names,charge)
return tse