當前位置: 首頁>>代碼示例>>Python>>正文


Python MixedIntegerLinearProgram.number_of_variables方法代碼示例

本文整理匯總了Python中sage.numerical.mip.MixedIntegerLinearProgram.number_of_variables方法的典型用法代碼示例。如果您正苦於以下問題:Python MixedIntegerLinearProgram.number_of_variables方法的具體用法?Python MixedIntegerLinearProgram.number_of_variables怎麽用?Python MixedIntegerLinearProgram.number_of_variables使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sage.numerical.mip.MixedIntegerLinearProgram的用法示例。


在下文中一共展示了MixedIntegerLinearProgram.number_of_variables方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: SatLP

# 需要導入模塊: from sage.numerical.mip import MixedIntegerLinearProgram [as 別名]
# 或者: from sage.numerical.mip.MixedIntegerLinearProgram import number_of_variables [as 別名]
class SatLP(SatSolver):
    def __init__(self, solver=None):
        r"""
        Initializes the instance

        INPUT:

        - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
          solver to be used. If set to ``None``, the default one is used. For
          more information on LP solvers and which default solver is used, see
          the method
          :meth:`solve <sage.numerical.mip.MixedIntegerLinearProgram.solve>`
          of the class
          :class:`MixedIntegerLinearProgram <sage.numerical.mip.MixedIntegerLinearProgram>`.

        EXAMPLES::

            sage: S=SAT(solver="LP"); S
            an ILP-based SAT Solver
        """
        SatSolver.__init__(self)
        self._LP = MixedIntegerLinearProgram()
        self._vars = self._LP.new_variable(binary=True)

    def var(self):
        """
        Return a *new* variable.

        EXAMPLES::

            sage: S=SAT(solver="LP"); S
            an ILP-based SAT Solver
            sage: S.var()
            1
        """
        nvars = n = self._LP.number_of_variables()
        while nvars==self._LP.number_of_variables():
            n += 1
            self._vars[n] # creates the variable if needed
        return n

    def nvars(self):
        """
        Return the number of variables.

        EXAMPLES::

            sage: S=SAT(solver="LP"); S
            an ILP-based SAT Solver
            sage: S.var()
            1
            sage: S.var()
            2
            sage: S.nvars()
            2
        """
        return self._LP.number_of_variables()

    def add_clause(self, lits):
        """
        Add a new clause to set of clauses.

        INPUT:

        - ``lits`` - a tuple of integers != 0

        .. note::

            If any element ``e`` in ``lits`` has ``abs(e)`` greater
            than the number of variables generated so far, then new
            variables are created automatically.

        EXAMPLES::

            sage: S=SAT(solver="LP"); S
            an ILP-based SAT Solver
            sage: for u,v in graphs.CycleGraph(6).edges(labels=False):
            ....:     u,v = u+1,v+1
            ....:     S.add_clause((u,v))
            ....:     S.add_clause((-u,-v))
        """
        if 0 in lits:
            raise ValueError("0 should not appear in the clause: {}".format(lits))
        p = self._LP
        p.add_constraint(p.sum(self._vars[x] if x>0 else 1-self._vars[-x] for x in lits)
                         >=1)

    def __call__(self):
        """
        Solve this instance.

        OUTPUT:

        - If this instance is SAT: A tuple of length ``nvars()+1``
          where the ``i``-th entry holds an assignment for the
          ``i``-th variables (the ``0``-th entry is always ``None``).

        - If this instance is UNSAT: ``False``

        EXAMPLES::
#.........這裏部分代碼省略.........
開發者ID:mcognetta,項目名稱:sage,代碼行數:103,代碼來源:sat_lp.py


注:本文中的sage.numerical.mip.MixedIntegerLinearProgram.number_of_variables方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。