本文整理汇总了Python中cnfformula.cnf.CNF.binary_mapping方法的典型用法代码示例。如果您正苦于以下问题:Python CNF.binary_mapping方法的具体用法?Python CNF.binary_mapping怎么用?Python CNF.binary_mapping使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cnfformula.cnf.CNF
的用法示例。
在下文中一共展示了CNF.binary_mapping方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BinaryPigeonholePrinciple
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import binary_mapping [as 别名]
def BinaryPigeonholePrinciple(pigeons,holes):
"""Binary Pigeonhole Principle CNF formula
The pigeonhole principle claims that no M pigeons can sit in
N pigeonholes without collision if M>N. This formula encodes the
principle using binary strings to identify the holes.
Parameters
----------
pigeon : int
number of pigeons
holes : int
number of holes
"""
bphp=CNF()
bphp.header="Binary Pigeonhole Principle for {0} pigeons and {1} holes\n".format(pigeons,holes)\
+ bphp.header
bphpgen=bphp.binary_mapping(xrange(1,pigeons+1), xrange(1,holes+1), injective = True)
for v in bphpgen.variables():
bphp.add_variable(v)
for c in bphpgen.clauses():
bphp.add_clause(c,strict=True)
return bphp
示例2: BinaryCliqueFormula
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import binary_mapping [as 别名]
def BinaryCliqueFormula(G,k):
"""Test whether a graph has a k-clique.
Given a graph :math:`G` and a non negative value :math:`k`, the
CNF formula claims that :math:`G` contains a :math:`k`-clique.
This formula uses the binary encoding, in the sense that the
clique elements are indexed by strings of bits.
Parameters
----------
G : networkx.Graph
a simple graph
k : a non negative integer
clique size
Returns
-------
a CNF object
"""
F=CNF()
F.header="Binary {0}-clique formula\n".format(k) + F.header
clauses_gen=F.binary_mapping(xrange(1,k+1), G.nodes(),
injective = True,
nondecreasing = True)
for v in clauses_gen.variables():
F.add_variable(v)
for c in clauses_gen.clauses():
F.add_clause(c,strict=True)
for (i1,i2),(v1,v2) in product(combinations(xrange(1,k+1),2),
combinations(G.nodes(),2)):
if not G.has_edge(v1,v2):
F.add_clause( clauses_gen.forbid_image(i1,v1) + clauses_gen.forbid_image(i2,v2),strict=True)
return F