本文整理汇总了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::
#.........这里部分代码省略.........