Skip to content

API: addMatrixConsIndicator supports ExprCons #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
if rc == SCIP_OKAY:
pass
elif rc == SCIP_ERROR:
raise Exception('SCIP: unspecified error!')

Check failure on line 311 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / test-coverage (3.11)

SCIP: unspecified error!
elif rc == SCIP_NOMEMORY:
raise MemoryError('SCIP: insufficient memory error!')
elif rc == SCIP_READERROR:
Expand Down Expand Up @@ -6946,7 +6946,7 @@
return pyCons


def addMatrixConsIndicator(self, cons: MatrixExprCons, binvar: Union[Variable, MatrixVariable] = None,
def addMatrixConsIndicator(self, cons: Union[ExprCons, MatrixExprCons], binvar: Union[Variable, MatrixVariable] = None,
activeone: Union[bool, np.ndarray] = True, name: Union[str, np.ndarray] = "",
initial: Union[bool, np.ndarray] = True, separate: Union[bool, np.ndarray] = True,
enforce: Union[bool, np.ndarray] = True, check: Union[bool, np.ndarray] = True,
Expand All @@ -6960,7 +6960,7 @@

Parameters
----------
cons : MatrixExprCons
cons : ExprCons or MatrixExprCons
a linear inequality of the form "<=".
binvar : Variable or MatrixVariable, optional
binary indicator variable / matrix variable, or None if it should be created. (Default value = None)
Expand Down Expand Up @@ -6994,9 +6994,14 @@
The newly created Indicator MatrixConstraint object.
"""

assert isinstance(cons, MatrixExprCons), (
f"given constraint is not MatrixExprCons but {cons.__class__.__name__}"
)
if not isinstance(cons, (ExprCons, MatrixExprCons)):
raise TypeError("given constraint is not MatrixExprCons nor ExprCons but %s" % cons.__class__.__name__)

if isinstance(cons, ExprCons):
return self.addConsIndicator(cons, binvar=binvar, activeone=activeone, name=name,
initial=initial, separate=separate, enforce=enforce, check=check,
propagate=propagate, local=local, dynamic=dynamic, removable=removable,
stickingatnode=stickingatnode)

shape = cons.shape

Expand Down
14 changes: 13 additions & 1 deletion tests/test_matrix_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ def test_matrix_cons_indicator():
with pytest.raises(Exception):
m.addMatrixConsIndicator(x >= y, is_equal)

# require MatrixExprCons or ExprCons
with pytest.raises(TypeError):
m.addMatrixConsIndicator(x)

# test MatrixExprCons
for i in range(2):
m.addMatrixConsIndicator(x[i] >= y[i], is_equal[0, i])
m.addMatrixConsIndicator(x[i] <= y[i], is_equal[0, i])
Expand All @@ -386,9 +391,16 @@ def test_matrix_cons_indicator():
m.addMatrixConsIndicator(x[:, i] >= y[:, i], is_equal[0])
m.addMatrixConsIndicator(x[:, i] <= y[:, i], is_equal[0])

m.setObjective(is_equal.sum(), "maximize")
# test ExprCons
z = m.addVar(vtype="B")
binvar = m.addVar(vtype="B")
m.addMatrixConsIndicator(z >= 1, binvar, activeone=True)
m.addMatrixConsIndicator(z <= 0, binvar, activeone=False)

m.setObjective(is_equal.sum() + binvar, "maximize")
m.optimize()

assert m.getVal(is_equal).sum() == 2
assert (m.getVal(x) == m.getVal(y)).all().all()
assert (m.getVal(x) == np.array([[5, 5, 5], [5, 5, 5]])).all().all()
assert m.getVal(z) == 1