Skip to content

Commit cfe24fe

Browse files
committed
add a doctest for adding and removing constraints
1 parent c57da31 commit cfe24fe

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/sage/numerical/mip.pyx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,8 +1862,7 @@ cdef class MixedIntegerLinearProgram(SageObject):
18621862

18631863
def add_constraint(self, linear_function, max=None, min=None, name=None,
18641864
return_indices=False):
1865-
r"""
1866-
Adds a constraint to the ``MixedIntegerLinearProgram``.
1865+
r"""Adds a constraint to the ``MixedIntegerLinearProgram``.
18671866
18681867
INPUT:
18691868
@@ -2098,6 +2097,43 @@ cdef class MixedIntegerLinearProgram(SageObject):
20982097
...
20992098
ValueError: argument must be a linear function or constraint, got True
21002099
2100+
Check that adding and removing constraints works::
2101+
2102+
sage: p = MixedIntegerLinearProgram(check_redundant=True)
2103+
sage: x = p.new_variable(nonnegative=True)
2104+
sage: p.add_constraint(x[0] + x[1] <= 3)
2105+
sage: p.set_objective(x[0] + 2*x[1])
2106+
sage: p.solve()
2107+
6.0
2108+
2109+
We add (non-trivially) redundant constraints::
2110+
2111+
sage: c1 = p.add_constraint(0 <= x[0] <= 3, return_indices=True); c1
2112+
[1, 2]
2113+
sage: p.solve()
2114+
6.0
2115+
2116+
We add a non-redundant constraint::
2117+
2118+
sage: c2 = p.add_constraint(1 <= x[1] <= 2, return_indices=True); c2
2119+
[3, 4]
2120+
sage: p.solve()
2121+
5.0
2122+
2123+
We remove the redundant constraints `1` and `2`, keep in mind
2124+
that indices change when removing constraints::
2125+
2126+
sage: p.remove_constraint(1)
2127+
sage: p.remove_constraint(1)
2128+
sage: p.solve()
2129+
5.0
2130+
2131+
We remove another constraint::
2132+
2133+
sage: p.remove_constraint(2)
2134+
sage: p.solve()
2135+
6.0
2136+
21012137
"""
21022138
from sage.numerical.linear_functions import is_LinearFunction, is_LinearConstraint
21032139
from sage.numerical.linear_tensor import is_LinearTensor

0 commit comments

Comments
 (0)