本文整理汇总了Python中cnfformula.cnf.CNF.parity_constraint方法的典型用法代码示例。如果您正苦于以下问题:Python CNF.parity_constraint方法的具体用法?Python CNF.parity_constraint怎么用?Python CNF.parity_constraint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cnfformula.cnf.CNF
的用法示例。
在下文中一共展示了CNF.parity_constraint方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TseitinFormula
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import parity_constraint [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()
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 CNF.parity_constraint(names,c):
tse.add_clause(list(cls),strict=True)
return tse