Skip to content

Commit 8267aa4

Browse files
API: addMatrixCons supports ExprCons (#1036)
* API: `addMatrixCons` supports `ExprCons` * test error * test accepting `ExprCons` * Update CHANGELOG.md * Update CHANGELOG.md * Update scip.pxi * Add comments to clarify error tests in matrix variable tests Added comments to the test_catching_errors function in test_matrix_variable.py to clarify the purpose of each exception test case. * Move addCons testcase to top * Add test for shape mismatch in addMatrixCons Added a test case to ensure addMatrixCons raises a ValueError when provided with an initial array of incorrect shape. * Apply suggestions from code review * Update test_matrix_variable.py * Update scip.pxi * tabs spaces * tabs and spaces finally? --------- Co-authored-by: João Dionísio <57299939+Joao-Dionisio@users.noreply.github.com>
1 parent 3ab3a1a commit 8267aa4

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
- Raised an error when an expression is used when a variable is required
1919
- Fixed some compile warnings
2020
### Changed
21-
- MatrixExpr.sum() now supports axis arguments and can return either a scalar or MatrixExpr depending on the result dimensions
21+
- MatrixExpr.sum() now supports axis arguments and can return either a scalar or MatrixExpr, depending on the result dimensions.
22+
- AddMatrixCons() also accepts ExprCons.
2223
### Removed
2324

2425
## 5.5.0 - 2025.05.06

src/pyscipopt/scip.pxi

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5775,7 +5775,7 @@ cdef class Model:
57755775
return constraints
57765776

57775777
def addMatrixCons(self,
5778-
cons: MatrixExprCons,
5778+
cons: Union[ExprCons, MatrixExprCons],
57795779
name: Union[str, np.ndarray] ='',
57805780
initial: Union[bool, np.ndarray] = True,
57815781
separate: Union[bool, np.ndarray] = True,
@@ -5792,8 +5792,8 @@ cdef class Model:
57925792
57935793
Parameters
57945794
----------
5795-
cons : MatrixExprCons
5796-
The matrix expression constraint that is not yet an actual constraint
5795+
cons : ExprCons or MatrixExprCons
5796+
The matrix expression constraint or expression constraint, that are not yet an actual constraint
57975797
name : str or np.ndarray, optional
57985798
the name of the matrix constraint, generic name if empty (Default value = "")
57995799
initial : bool or np.ndarray, optional
@@ -5820,12 +5820,17 @@ cdef class Model:
58205820
58215821
Returns
58225822
-------
5823-
MatrixConstraint
5824-
The created and added MatrixConstraint object.
5825-
5823+
Constraint or MatrixConstraint
5824+
The created and added Constraint or MatrixConstraint.
58265825
"""
5827-
assert isinstance(cons, MatrixExprCons), (
5828-
"given constraint is not MatrixExprCons but %s" % cons.__class__.__name__)
5826+
if not isinstance(cons, (ExprCons, MatrixExprCons)):
5827+
raise TypeError("given constraint is not MatrixExprCons nor ExprCons but %s" % cons.__class__.__name__)
5828+
5829+
if isinstance(cons, ExprCons):
5830+
return self.addCons(cons, name=name, initial=initial, separate=separate,
5831+
enforce=enforce, check=check, propagate=propagate,
5832+
local=local, modifiable=modifiable, dynamic=dynamic,
5833+
removable=removable, stickingatnode=stickingatnode)
58295834

58305835
shape = cons.shape
58315836

tests/test_matrix_variable.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ def test_catching_errors():
1515
y = m.addMatrixVar(shape=(3, 3))
1616
rhs = np.ones((2, 1))
1717

18+
# require ExprCons
1819
with pytest.raises(Exception):
19-
m.addMatrixCons(x <= 1)
20+
m.addCons(y <= 3)
2021

22+
# require MatrixExprCons or ExprCons
2123
with pytest.raises(Exception):
22-
m.addCons(y <= 3)
24+
m.addMatrixCons(x)
2325

26+
# test shape mismatch
2427
with pytest.raises(Exception):
2528
m.addMatrixCons(y <= rhs)
2629

@@ -169,7 +172,7 @@ def test_matrix_sum_argument():
169172

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

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

0 commit comments

Comments
 (0)