当前位置: 首页>>代码示例>>Python>>正文


Python CNF.add_less_or_equal方法代码示例

本文整理汇总了Python中cnfformula.cnf.CNF.add_less_or_equal方法的典型用法代码示例。如果您正苦于以下问题:Python CNF.add_less_or_equal方法的具体用法?Python CNF.add_less_or_equal怎么用?Python CNF.add_less_or_equal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cnfformula.cnf.CNF的用法示例。


在下文中一共展示了CNF.add_less_or_equal方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_one_leq

# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import add_less_or_equal [as 别名]
 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)
开发者ID:marcvinyals,项目名称:cnfgen,代码行数:11,代码来源:test_opb.py

示例2: DominatingSetOPB

# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import add_less_or_equal [as 别名]
def DominatingSetOPB(G,d,tiling,seed):
    F=CNF()

    def D(v):
        return "x_{{{0}}}".format(v)

    def N(v):
        return tuple(sorted([ v ] + [ u for u in G.neighbors(v) ]))

    # Fix the vertex order
    V=enumerate_vertices(G)

    #avgdegree=sum(len(set(N(v))) for v in V)/len(V)
    #d=len(V)/(avgdegree+1)
    
    # Create variables
    for v in V:
        F.add_variable(D(v))

    # Not too many true variables
    if not tiling:
        F.add_less_or_equal([D(v) for v in V],d)

    # Every neighborhood must have a true D variable
    neighborhoods = sorted( set(N(v) for v in V) )
    for N in neighborhoods:
        if tiling:
            F.add_equal_to([D(v) for v in N], 1)
        else:
            F.add_clause([(True,D(v)) for v in N])

    # Set some vertex to true
    if seed:
        F.add_clause([(True,D(V[0]))])
        
    return F
开发者ID:marcvinyals,项目名称:cnfgen,代码行数:38,代码来源:dominatingsetopb.py

示例3: VertexCover

# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import add_less_or_equal [as 别名]
def VertexCover(G,d):
    F=CNF()

    def D(v):
        return "x_{{{0}}}".format(v)

    def N(v):
        return tuple(sorted([ e for e in G.edges(v) ]))

    # Fix the vertex order
    V=enumerate_vertices(G)

    # Create variables
    for v in V:
        F.add_variable(D(v))

    # Not too many true variables
    F.add_less_or_equal([D(v) for v in V],d)

    # Every edge must have a true D variable
    for e in G.edges():
        F.add_clause([ (True,D(v)) for v in e])
        
    return F
开发者ID:marcvinyals,项目名称:cnfgen,代码行数:26,代码来源:vertexcover.py

示例4: DominatingSet

# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import add_less_or_equal [as 别名]
def DominatingSet(G,d, alternative = False):
    r"""Generates the clauses for a dominating set for G of size <= d

    The formula encodes the fact that the graph :math:`G` has
    a dominating set of size :math:`d`. This means that it is possible
    to pick at most :math:`d` vertices in :math:`V(G)` so that all remaining
    vertices have distance at most one from the selected ones.

    Parameters
    ----------
    G : networkx.Graph
        a simple undirected graph
    d : a positive int
        the size limit for the dominating set
    alternative : bool
        use an alternative construction that 
        is provably hard from resolution proofs.

    Returns
    -------
    CNF
       the CNF encoding for dominating of size :math:`\leq d` for graph :math:`G`

    """
    F=CNF()

    if not isinstance(d,int) or d<1:
        ValueError("Parameter \"d\" is expected to be a positive integer")
    
    # Describe the formula
    name="{}-dominating set".format(d)
    
    if hasattr(G,'name'):
        F.header=name+" of graph:\n"+G.name+".\n\n"+F.header
    else:
        F.header=name+".\n\n"+F.header

    # Fix the vertex order
    V=enumerate_vertices(G)
    

    def D(v):
        return "x_{{{0}}}".format(v)

    def M(v,i):
        return "g_{{{0},{1}}}".format(v,i)

    def N(v):
        return tuple(sorted([ v ] + [ u for u in G.neighbors(v) ]))
    
    # Create variables
    for v in V:
        F.add_variable(D(v))
    for i,v in product(range(1,d+1),V):
        F.add_variable(M(v,i))
    
    # No two (active) vertices map to the same index
    if alternative:
        for u,v in combinations(V,2):
            for i in range(1,d+1):
                F.add_clause( [ (False,D(u)),(False,D(v)), (False,M(u,i)), (False,M(v,i))    ])
    else:
        for i in range(1,d+1):
            F.add_less_or_equal([M(v,i) for v in V],1)
                
    # (Active) Vertices in the sequence are not repeated
    if alternative:
        for v in V:
            for i,j in combinations(range(1,d+1),2):
                F.add_clause([(False,D(v)),(False,M(v,i)),(False,M(v,j))])
    else:
        for i,j in combinations_with_replacement(range(1,d+1),2):
            i,j = min(i,j),max(i,j)
            for u,v in combinations(V,2):
                u,v = max(u,v),min(u,v)
                F.add_clause([(False,M(u,i)),(False,M(v,j))])

    # D(v) = M(v,1) or M(v,2) or ... or M(v,d)        
    if not alternative:
        for i,v in product(range(1,d+1),V):
            F.add_clause([(False,M(v,i)),(True,D(v))])
    for v in V:
        F.add_clause([(False,D(v))] + [(True,M(v,i)) for i in range(1,d+1)])
    
        
    # Every neighborhood must have a true D variable
    neighborhoods = sorted( set(N(v) for v in V) )
    for N in neighborhoods:
        F.add_clause([ (True,D(v)) for v in N])
        
    return F
开发者ID:marcvinyals,项目名称:cnfgen,代码行数:93,代码来源:dominatingset.py


注:本文中的cnfformula.cnf.CNF.add_less_or_equal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。