Skip to content

BUG: matrix variable can't be compared with expr directly #1061

@Zeroto521

Description

@Zeroto521

Describe the bug

A matrix could be compared with a MatrixExpr, but can't be compared with an Expr.

To Reproduce

from pyscipopt import Model

model = Model()
matrix = model.addMatrixVar(3)
var = model.addVar()

model.addMatrixCons(matrix <= var + 1)  # Error!!!
# Traceback (most recent call last):
#   line 7, in <module>
#     model.addCons(matrix <= var + 1)  # Error!!!
#                   ^^^^^^^^^^^^^^^^^
#   File "src/pyscipopt/matrix.pxi", line 38, in pyscipopt.scip.MatrixExpr.__le__
# TypeError: Unsupported type <class 'pyscipopt.scip.Expr'>

matrix <= var  # OK

matrix <= matrix  # OK

Expected behavior

Should support expr type.

def __le__(self, other: Union[float, int, Variable, np.ndarray, 'MatrixExpr']) -> np.ndarray:
expr_cons_matrix = np.empty(self.shape, dtype=object)
if _is_number(other) or isinstance(other, Variable):
for idx in np.ndindex(self.shape):
expr_cons_matrix[idx] = self[idx] <= other
elif isinstance(other, np.ndarray):
for idx in np.ndindex(self.shape):
expr_cons_matrix[idx] = self[idx] <= other[idx]
else:
raise TypeError(f"Unsupported type {type(other)}")
return expr_cons_matrix.view(MatrixExprCons)

Screenshots

Image

System

  • OS: Windows 11
  • Version: 24H2 (26100.4061)
  • SCIP version: 9.2.3
  • How did you install pyscipopt?: build from c681f94, base on 5.5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions