Skip to content

Commit 18d6f39

Browse files
Merge branch 'master' into fix-binops
2 parents 80cb82f + 53289a0 commit 18d6f39

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
## Unreleased
44
### Added
5+
- Added basic type stubs to help with IDE autocompletion and type checking.
56
### Fixed
67
- Implemented all binary operations between MatrixExpr and GenExpr
8+
- Fixed the type of @ matrix operation result from MatrixVariable to MatrixExpr.
79
### Changed
810
### Removed
911

@@ -25,7 +27,6 @@
2527
### Fixed
2628
- Raised an error when an expression is used when a variable is required
2729
- Fixed some compile warnings
28-
- Fixed the type of @ matrix operation result from MatrixVariable to MatrixExpr.
2930
### Changed
3031
- MatrixExpr.sum() now supports axis arguments and can return either a scalar or MatrixExpr, depending on the result dimensions.
3132
- AddMatrixCons() also accepts ExprCons.

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
@@ -377,6 +377,11 @@ def test_matrix_cons_indicator():
377377
with pytest.raises(Exception):
378378
m.addMatrixConsIndicator(x >= y, is_equal)
379379

380+
# require MatrixExprCons or ExprCons
381+
with pytest.raises(TypeError):
382+
m.addMatrixConsIndicator(x)
383+
384+
# test MatrixExprCons
380385
for i in range(2):
381386
m.addMatrixConsIndicator(x[i] >= y[i], is_equal[0, i])
382387
m.addMatrixConsIndicator(x[i] <= y[i], is_equal[0, i])
@@ -388,12 +393,19 @@ def test_matrix_cons_indicator():
388393
m.addMatrixConsIndicator(x[:, i] >= y[:, i], is_equal[0])
389394
m.addMatrixConsIndicator(x[:, i] <= y[:, i], is_equal[0])
390395

391-
m.setObjective(is_equal.sum(), "maximize")
396+
# test ExprCons
397+
z = m.addVar(vtype="B")
398+
binvar = m.addVar(vtype="B")
399+
m.addMatrixConsIndicator(z >= 1, binvar, activeone=True)
400+
m.addMatrixConsIndicator(z <= 0, binvar, activeone=False)
401+
402+
m.setObjective(is_equal.sum() + binvar, "maximize")
392403
m.optimize()
393404

394405
assert m.getVal(is_equal).sum() == 2
395406
assert (m.getVal(x) == m.getVal(y)).all().all()
396407
assert (m.getVal(x) == np.array([[5, 5, 5], [5, 5, 5]])).all().all()
408+
assert m.getVal(z) == 1
397409

398410

399411
_binop_model = Model()

0 commit comments

Comments
 (0)