Skip to content

Commit e7df521

Browse files
Matthias Koeppemantepse
authored andcommitted
MixedIntegerLinearProgram.add_constraint: Do not refuse to add empty constraints; they could be infeasible or we may need their row index for later
1 parent 5bd8f39 commit e7df521

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/sage/numerical/mip.pyx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,10 +2014,22 @@ cdef class MixedIntegerLinearProgram(SageObject):
20142014
x_0 is a continuous variable (min=0.0, max=+oo)
20152015
x_1 is a continuous variable (min=0.0, max=+oo)
20162016
2017-
Empty constraint::
2017+
Trivially true empty constraint:
20182018
20192019
sage: p = MixedIntegerLinearProgram(solver='GLPK')
2020-
sage: p.add_constraint(sum([]),min=2)
2020+
sage: p.add_constraint(sum([]), max=2)
2021+
0
2022+
sage: p.solve()
2023+
0.0
2024+
2025+
Infeasible empty constraint::
2026+
2027+
sage: p = MixedIntegerLinearProgram(solver='GLPK')
2028+
sage: p.add_constraint(sum([]), min=2)
2029+
sage: p.solve()
2030+
Traceback (most recent call last):
2031+
...
2032+
MIPSolverException: GLPK: Problem has no feasible solution
20212033
20222034
Min/Max are numerical ::
20232035
@@ -2078,9 +2090,6 @@ cdef class MixedIntegerLinearProgram(SageObject):
20782090
...
20792091
ValueError: argument must be a linear function or constraint, got True
20802092
"""
2081-
if linear_function is 0:
2082-
return
2083-
20842093
from sage.numerical.linear_functions import is_LinearFunction, is_LinearConstraint
20852094
from sage.numerical.linear_tensor import is_LinearTensor
20862095
from sage.numerical.linear_tensor_constraints import is_LinearTensorConstraint
@@ -2149,8 +2158,14 @@ cdef class MixedIntegerLinearProgram(SageObject):
21492158
return self.add_constraint(relation.lhs() - relation.rhs(),
21502159
min=M(0) if relation.is_equation() else None,
21512160
max=M(0), name=name)
2161+
elif isinstance(linear_function, bool):
2162+
raise ValueError('argument must be a linear function or constraint, got ' + str(linear_function))
21522163
else:
2153-
raise ValueError('argument must be a linear function or constraint, got '+str(linear_function))
2164+
try:
2165+
linear_function = self.linear_functions_parent()(linear_function)
2166+
except (TypeError, ValueError):
2167+
raise ValueError('argument must be a linear function or constraint, got ' + str(linear_function))
2168+
return self.add_constraint(linear_function, max=max, min=min, name=name)
21542169

21552170
def _is_redundant_constraint(self, constraint, min_bound, max_bound):
21562171
"""

0 commit comments

Comments
 (0)