本文整理汇总了Python中cnfformula.cnf.CNF.unary_mapping方法的典型用法代码示例。如果您正苦于以下问题:Python CNF.unary_mapping方法的具体用法?Python CNF.unary_mapping怎么用?Python CNF.unary_mapping使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cnfformula.cnf.CNF
的用法示例。
在下文中一共展示了CNF.unary_mapping方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SparseStoneFormula
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import unary_mapping [as 别名]
def SparseStoneFormula(D,B):
"""Sparse Stone formulas
This is a variant of the :py:func:`StoneFormula`. See that for
a description of the formula. This variant is such that each
vertex has only a small selection of which stone can go to that
vertex. In particular which stones are allowed on each vertex is
specified by a bipartite graph :math:`B` on which the left
vertices represent the vertices of DAG :math:`D` and the right
vertices are the stones.
If a vertex of :math:`D` correspond to the left vertex :math:`v`
in :math:`B`, then its neighbors describe which stones are allowed
for it.
The vertices in :math:`D` do not need to have the same name as the
one on the left side of :math:`B`. It is only important that the
number of vertices in :math:`D` is the same as the vertices in the
left side of :math:`B`.
In that case the element at position :math:`i` in the ordered
sequence ``enumerate_vertices(D)`` corresponds to the element of
rank :math:`i` in the sequence of left side vertices of
:math:`B` according to the output of ``Left, Right =
bipartite_sets(B)``.
Standard :py:func:`StoneFormula` is essentially equivalent to
a sparse stone formula where :math:`B` is the complete graph.
Parameters
----------
D : a directed acyclic graph
it should be a directed acyclic graph.
B : bipartite graph
Raises
------
ValueError
if :math:`D` is not a directed acyclic graph
ValueError
if :math:`B` is not a bipartite graph
ValueError
when size differs between :math:`D` and the left side of
:math:`B`
See Also
--------
StoneFormula
"""
if not is_dag(D):
raise ValueError("Stone formulas are defined only for directed acyclic graphs.")
if not has_bipartition(B):
raise ValueError("Vertices to stones mapping must be specified with a bipartite graph")
Left, Right = bipartite_sets(B)
nstones = len(Right)
if len(Left) != D.order():
raise ValueError("Formula requires the bipartite left side to match #vertices of the DAG.")
cnf = CNF()
if hasattr(D, 'name'):
cnf.header = "Sparse Stone formula of: " + D.name + "\nwith " + str(nstones) + " stones\n" + cnf.header
else:
cnf.header = "Sparse Stone formula with " + str(nstones) + " stones\n" + cnf.header
# add variables in the appropriate order
vertices=enumerate_vertices(D)
stones=range(1,nstones+1)
mapping = cnf.unary_mapping(vertices,stones,sparsity_pattern=B)
stone_formula_helper(cnf,D,mapping)
return cnf
示例2: SubgraphFormula
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import unary_mapping [as 别名]
def SubgraphFormula(graph,templates, symmetric=False):
"""Test whether a graph contains one of the templates.
Given a graph :math:`G` and a sequence of template graphs
:math:`H_1`, :math:`H_2`, ..., :math:`H_t`, the CNF formula claims
that :math:`G` contains an isomorphic copy of at least one of the
template graphs.
E.g. when :math:`H_1` is the complete graph of :math:`k` vertices
and it is the only template, the formula claims that :math:`G`
contains a :math:`k`-clique.
Parameters
----------
graph : networkx.Graph
a simple graph
templates : list-like object
a sequence of graphs.
symmetric:
all template graphs are symmetric wrt permutations of
vertices. This allows some optimization in the search space of
the assignments.
induce:
force the subgraph to be induced (i.e. no additional edges are allowed)
Returns
-------
a CNF object
"""
F=CNF()
# One of the templates is chosen to be the subgraph
if len(templates)==0:
return F
elif len(templates)==1:
selectors=[]
elif len(templates)==2:
selectors=['c']
else:
selectors=['c_{{{}}}'.format(i) for i in range(len(templates))]
for s in selectors:
F.add_variable(s)
if len(selectors)>1:
for cls in F.equal_to_constraint(selectors,1):
F.add_clause( cls , strict=True )
# comment the formula accordingly
if len(selectors)>1:
F.header=dedent("""\
CNF encoding of the claim that a graph contains one among
a family of {0} possible subgraphs.
""".format(len(templates))) + F.header
else:
F.header=dedent("""\
CNF encoding of the claim that a graph contains an induced
copy of a subgraph.
""".format(len(templates))) + F.header
# A subgraph is chosen
N=graph.order()
k=max([s.order() for s in templates])
var_name = lambda i,j: "S_{{{0},{1}}}".format(i,j)
for i,j in product(range(k),range(N)):
F.add_variable( var_name(i,j) )
if symmetric:
gencls = F.unary_mapping(range(k),range(N),var_name=var_name,
functional=True,injective=True,
nondecreasing=True)
else:
gencls = F.unary_mapping(range(k),range(N),var_name=var_name,
functional=True,injective=True)
for cls in gencls:
F.add_clause( cls, strict=True )
# The selectors choose a template subgraph. A mapping must map
# edges to edges and non-edges to non-edges for the active
# template.
if len(templates)==1:
activation_prefixes = [[]]
elif len(templates)==2:
activation_prefixes = [[(True,selectors[0])],[(False,selectors[0])]]
else:
#.........这里部分代码省略.........
示例3: PigeonholePrinciple
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import unary_mapping [as 别名]
def PigeonholePrinciple(pigeons,holes,functional=False,onto=False):
"""Pigeonhole Principle CNF formula
The pigeonhole principle claims that no M pigeons can sit in N
pigeonholes without collision if M>N. The counterpositive CNF
formulation requires such mapping to be satisfied. There are
different variants of this formula, depending on the values of
`functional` and `onto` argument.
- PHP: pigeon can sit in multiple holes
- FPHP: each pigeon sits in exactly one hole
- onto-PHP: pigeon can sit in multiple holes, every hole must be
covered.
- Matching: one-to-one bijection between pigeons and holes.
Arguments:
- `pigeon`: number of pigeons
- `hole`: number of holes
- `functional`: add clauses to enforce at most one hole per pigeon
- `onto`: add clauses to enforce that any hole must have a pigeon
>>> print(PigeonholePrinciple(4,3).dimacs(export_header=False))
p cnf 12 22
1 2 3 0
4 5 6 0
7 8 9 0
10 11 12 0
-1 -4 0
-1 -7 0
-1 -10 0
-4 -7 0
-4 -10 0
-7 -10 0
-2 -5 0
-2 -8 0
-2 -11 0
-5 -8 0
-5 -11 0
-8 -11 0
-3 -6 0
-3 -9 0
-3 -12 0
-6 -9 0
-6 -12 0
-9 -12 0
"""
def var_name(p,h):
return 'p_{{{0},{1}}}'.format(p,h)
if functional:
if onto:
formula_name="Matching"
else:
formula_name="Functional pigeonhole principle"
else:
if onto:
formula_name="Onto pigeonhole principle"
else:
formula_name="Pigeonhole principle"
php=CNF()
php.header="{0} formula for {1} pigeons and {2} holes\n".format(formula_name,pigeons,holes)\
+ php.header
for p in xrange(1,pigeons+1):
for h in xrange(1,holes+1):
php.add_variable(var_name(p,h))
clauses=php.unary_mapping(
xrange(1,pigeons+1),
xrange(1,holes+1),
var_name,
complete = True,
injective = True,
functional = functional,
surjective = onto)
for c in clauses:
php.add_clause(c,strict=True)
return php
示例4: GraphPigeonholePrinciple
# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import unary_mapping [as 别名]
def GraphPigeonholePrinciple(graph,functional=False,onto=False):
"""Graph Pigeonhole Principle CNF formula
The graph pigeonhole principle CNF formula, defined on a bipartite
graph G=(L,R,E), claims that there is a subset E' of the edges such that
every vertex on the left size L has at least one incident edge in E' and
every edge on the right side R has at most one incident edge in E'.
This is possible only if the graph has a matching of size |L|.
There are different variants of this formula, depending on the
values of `functional` and `onto` argument.
- PHP(G): each left vertex can be incident to multiple edges in E'
- FPHP(G): each left vertex must be incident to exaclty one edge in E'
- onto-PHP: all right vertices must be incident to some vertex
- matching: E' must be a perfect matching between L and R
Arguments:
- `graph` : bipartite graph
- `functional`: add clauses to enforce at most one edge per left vertex
- `onto`: add clauses to enforce that any right vertex has one incident edge
Remark: the graph vertices must have the 'bipartite' attribute
set. Left vertices must have it set to 0 and the right ones to
1. A KeyException is raised otherwise.
"""
def var_name(p,h):
return 'p_{{{0},{1}}}'.format(p,h)
if functional:
if onto:
formula_name="Graph matching"
else:
formula_name="Graph functional pigeonhole principle"
else:
if onto:
formula_name="Graph onto pigeonhole principle"
else:
formula_name="Graph pigeonhole principle"
gphp=CNF()
gphp.header="{0} formula for graph {1}\n".format(formula_name,graph.name)
Left, Right = bipartite_sets(graph)
mapping = gphp.unary_mapping(Left,Right,
sparsity_pattern=graph,
var_name=var_name,
injective = True,
functional = functional,
surjective = onto)
for v in mapping.variables():
gphp.add_variable(v)
for c in mapping.clauses():
gphp.add_clause_unsafe(c)
return gphp