Skip to content

API: addMatrixCons supports ExprCons #1036

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

Merged
merged 20 commits into from
Aug 2, 2025
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
- Raised an error when an expression is used when a variable is required
- Fixed some compile warnings
### Changed
- MatrixExpr.sum() now supports axis arguments and can return either a scalar or MatrixExpr depending on the result dimensions
- MatrixExpr.sum() now supports axis arguments and can return either a scalar or MatrixExpr, depending on the result dimensions.
- AddMatrixCons() also accepts ExprCons.
### Removed

## 5.5.0 - 2025.05.06
Expand Down
21 changes: 13 additions & 8 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 @@ -5775,7 +5775,7 @@
return constraints

def addMatrixCons(self,
cons: MatrixExprCons,
cons: Union[ExprCons, MatrixExprCons],
name: Union[str, np.ndarray] ='',
initial: Union[bool, np.ndarray] = True,
separate: Union[bool, np.ndarray] = True,
Expand All @@ -5792,8 +5792,8 @@

Parameters
----------
cons : MatrixExprCons
The matrix expression constraint that is not yet an actual constraint
cons : ExprCons or MatrixExprCons
The matrix expression constraint or expression constraint, that are not yet an actual constraint
name : str or np.ndarray, optional
the name of the matrix constraint, generic name if empty (Default value = "")
initial : bool or np.ndarray, optional
Expand All @@ -5820,12 +5820,17 @@

Returns
-------
MatrixConstraint
The created and added MatrixConstraint object.

Constraint or MatrixConstraint
The created and added Constraint or MatrixConstraint.
"""
assert isinstance(cons, MatrixExprCons), (
"given constraint is not MatrixExprCons but %s" % 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.addCons(cons, name=name, initial=initial, separate=separate,
enforce=enforce, check=check, propagate=propagate,
local=local, modifiable=modifiable, dynamic=dynamic,
removable=removable, stickingatnode=stickingatnode)

shape = cons.shape

Expand Down
9 changes: 6 additions & 3 deletions tests/test_matrix_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ def test_catching_errors():
y = m.addMatrixVar(shape=(3, 3))
rhs = np.ones((2, 1))

# require ExprCons
with pytest.raises(Exception):
m.addMatrixCons(x <= 1)
m.addCons(y <= 3)

# require MatrixExprCons or ExprCons
with pytest.raises(Exception):
m.addCons(y <= 3)
m.addMatrixCons(x)

# test shape mismatch
with pytest.raises(Exception):
m.addMatrixCons(y <= rhs)

Expand Down Expand Up @@ -169,7 +172,7 @@ def test_matrix_sum_argument():

# compare the result of summing 2d array to a scalar with a scalar
x = m.addMatrixVar((2, 3), "x", "I", ub=4)
m.addCons(x.sum() == 24)
m.addMatrixCons(x.sum() == 24)

# compare the result of summing 2d array to 1d array
y = m.addMatrixVar((2, 4), "y", "I", ub=4)
Expand Down