Skip to content

Commit 53289a0

Browse files
API: addMatrixConsIndicator supports ExprCons (#1047)
* to support ExprCons * Add tests for MatrixConsIndicator input types Expanded test_matrix_cons_indicator to check TypeError for invalid input types and added tests for MatrixExprCons and ExprCons cases. Also updated objective and assertions to include new binary variable. * Fix typo in type annotation for addMatrixConsIndicator * Expand addMatrixConsIndicator to support ExprCons --------- Co-authored-by: João Dionísio <[email protected]>
1 parent ec16f77 commit 53289a0

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/pyscipopt/scip.pxi

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6979,7 +6979,7 @@ cdef class Model:
69796979
return pyCons
69806980

69816981

6982-
def addMatrixConsIndicator(self, cons: MatrixExprCons, binvar: Union[Variable, MatrixVariable] = None,
6982+
def addMatrixConsIndicator(self, cons: Union[ExprCons, MatrixExprCons], binvar: Union[Variable, MatrixVariable] = None,
69836983
activeone: Union[bool, np.ndarray] = True, name: Union[str, np.ndarray] = "",
69846984
initial: Union[bool, np.ndarray] = True, separate: Union[bool, np.ndarray] = True,
69856985
enforce: Union[bool, np.ndarray] = True, check: Union[bool, np.ndarray] = True,
@@ -6993,7 +6993,7 @@ cdef class Model:
69936993

69946994
Parameters
69956995
----------
6996-
cons : MatrixExprCons
6996+
cons : ExprCons or MatrixExprCons
69976997
a linear inequality of the form "<=".
69986998
binvar : Variable or MatrixVariable, optional
69996999
binary indicator variable / matrix variable, or None if it should be created. (Default value = None)
@@ -7027,9 +7027,14 @@ cdef class Model:
70277027
The newly created Indicator MatrixConstraint object.
70287028
"""
70297029

7030-
assert isinstance(cons, MatrixExprCons), (
7031-
f"given constraint is not MatrixExprCons but {cons.__class__.__name__}"
7032-
)
7030+
if not isinstance(cons, (ExprCons, MatrixExprCons)):
7031+
raise TypeError("given constraint is not MatrixExprCons nor ExprCons but %s" % cons.__class__.__name__)
7032+
7033+
if isinstance(cons, ExprCons):
7034+
return self.addConsIndicator(cons, binvar=binvar, activeone=activeone, name=name,
7035+
initial=initial, separate=separate, enforce=enforce, check=check,
7036+
propagate=propagate, local=local, dynamic=dynamic, removable=removable,
7037+
stickingatnode=stickingatnode)
70337038

70347039
shape = cons.shape
70357040

tests/test_matrix_variable.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ def test_matrix_cons_indicator():
375375
with pytest.raises(Exception):
376376
m.addMatrixConsIndicator(x >= y, is_equal)
377377

378+
# require MatrixExprCons or ExprCons
379+
with pytest.raises(TypeError):
380+
m.addMatrixConsIndicator(x)
381+
382+
# test MatrixExprCons
378383
for i in range(2):
379384
m.addMatrixConsIndicator(x[i] >= y[i], is_equal[0, i])
380385
m.addMatrixConsIndicator(x[i] <= y[i], is_equal[0, i])
@@ -386,12 +391,19 @@ def test_matrix_cons_indicator():
386391
m.addMatrixConsIndicator(x[:, i] >= y[:, i], is_equal[0])
387392
m.addMatrixConsIndicator(x[:, i] <= y[:, i], is_equal[0])
388393

389-
m.setObjective(is_equal.sum(), "maximize")
394+
# test ExprCons
395+
z = m.addVar(vtype="B")
396+
binvar = m.addVar(vtype="B")
397+
m.addMatrixConsIndicator(z >= 1, binvar, activeone=True)
398+
m.addMatrixConsIndicator(z <= 0, binvar, activeone=False)
399+
400+
m.setObjective(is_equal.sum() + binvar, "maximize")
390401
m.optimize()
391402

392403
assert m.getVal(is_equal).sum() == 2
393404
assert (m.getVal(x) == m.getVal(y)).all().all()
394405
assert (m.getVal(x) == np.array([[5, 5, 5], [5, 5, 5]])).all().all()
406+
assert m.getVal(z) == 1
395407

396408

397409
def test_matrix_matmul_return_type():

0 commit comments

Comments
 (0)