-
Notifications
You must be signed in to change notification settings - Fork 271
BUG: MatrixExpr can't be compared with Expr #1069
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
Zeroto521
wants to merge
29
commits into
scipopt:master
Choose a base branch
from
Zeroto521:fix/1061
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
d33379c
Replace Variable with Expr in MatrixExpr
Zeroto521 6c48a73
add test case
Zeroto521 b578ea5
Replace Variable with Expr in MatrixExprCons
Zeroto521 e8db5a1
add test case
Zeroto521 aae9df9
Update CHANGELOG.md
Zeroto521 d8a9377
Add test for ranged matrix constraint
Zeroto521 99446bc
Refactor matrix comparison operators using helper
Zeroto521 a09be1a
Replace TypeError with NotImplementedError in __eq__
Zeroto521 771437b
Add tests for matrix constraint operators
Zeroto521 2b9a3c0
Update CHANGELOG.md
Zeroto521 b7b1321
BUG: fix circular imports
Zeroto521 987c219
Fix matrix comparison shape handling
Zeroto521 7a1275d
Fix redundant .all() calls in matrix variable tests
Zeroto521 f1dc2fa
Fix matrix variable test assertions to use getVal
Zeroto521 b6dcf42
let MatrixExprCons support <= and >= operator
Zeroto521 64ae70e
Refactor matrix comparison tests to optimize assertions and remove re…
Zeroto521 f69ce7e
let MatrixExprCons support <= and >= operator
Zeroto521 3700261
find what type it is
Zeroto521 c677b34
align with `__add__`
Zeroto521 bca7262
test "==" first
Zeroto521 cb600b2
Revert "let MatrixExprCons support <= and >= operator"
Zeroto521 a3a6239
Revert "let MatrixExprCons support <= and >= operator"
Zeroto521 06f8ebc
find what type it is
Zeroto521 ef5aecf
test expr
Zeroto521 ceaab05
Change the order
Zeroto521 3861420
Remove ExprCons
Zeroto521 a2ae9c9
Ranged ExprCons requires number
Zeroto521 6afa150
Update CHANGELOG.md
Zeroto521 88a935f
Lint codes with 4 spaces
Zeroto521 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import numpy as np | ||
from typing import Union | ||
|
||
|
||
def _is_number(e): | ||
try: | ||
f = float(e) | ||
|
@@ -15,6 +16,33 @@ def _is_number(e): | |
except TypeError: # for other types (Variable, Expr) | ||
return False | ||
|
||
|
||
def _matrixexpr_richcmp(self, other, op): | ||
def _richcmp(self, other, op): | ||
if op == 1: # <= | ||
return self.__le__(other) | ||
elif op == 5: # >= | ||
return self.__ge__(other) | ||
elif op == 2: # == | ||
return self.__eq__(other) | ||
else: | ||
raise NotImplementedError("Can only support constraints with '<=', '>=', or '=='.") | ||
Comment on lines
+21
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't use |
||
|
||
res = np.empty(self.shape, dtype=object) | ||
if _is_number(other) or isinstance(other, Expr): | ||
for idx in np.ndindex(self.shape): | ||
res[idx] = _richcmp(self[idx], other, op) | ||
|
||
elif isinstance(other, np.ndarray): | ||
for idx in np.ndindex(self.shape): | ||
res[idx] = _richcmp(self[idx], other[idx], op) | ||
|
||
else: | ||
raise TypeError(f"Unsupported type {type(other)}") | ||
|
||
return res.view(MatrixExprCons) | ||
|
||
|
||
class MatrixExpr(np.ndarray): | ||
def sum(self, **kwargs): | ||
""" | ||
|
@@ -24,51 +52,15 @@ class MatrixExpr(np.ndarray): | |
res = super().sum(**kwargs) | ||
return res if res.size > 1 else res.item() | ||
|
||
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)}") | ||
def __le__(self, other: Union[float, int, Expr, np.ndarray, 'MatrixExpr']) -> MatrixExprCons: | ||
return _matrixexpr_richcmp(self, other, 1) | ||
|
||
return expr_cons_matrix.view(MatrixExprCons) | ||
def __ge__(self, other: Union[float, int, Expr, np.ndarray, 'MatrixExpr']) -> MatrixExprCons: | ||
return _matrixexpr_richcmp(self, other, 5) | ||
|
||
def __ge__(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)}") | ||
def __eq__(self, other: Union[float, int, Expr, np.ndarray, 'MatrixExpr']) -> MatrixExprCons: | ||
return _matrixexpr_richcmp(self, other, 2) | ||
|
||
return expr_cons_matrix.view(MatrixExprCons) | ||
|
||
def __eq__(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) | ||
|
||
def __add__(self, other): | ||
return super().__add__(other).view(MatrixExpr) | ||
|
||
|
@@ -104,41 +96,11 @@ class MatrixGenExpr(MatrixExpr): | |
|
||
class MatrixExprCons(np.ndarray): | ||
|
||
def __le__(self, other: Union[float, int, Variable, MatrixExpr]) -> np.ndarray: | ||
|
||
if not _is_number(other) or not isinstance(other, MatrixExpr): | ||
raise TypeError('Ranged MatrixExprCons is not well defined!') | ||
|
||
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) | ||
|
||
def __ge__(self, other: Union[float, int, Variable, MatrixExpr]) -> np.ndarray: | ||
|
||
if not _is_number(other) or not isinstance(other, MatrixExpr): | ||
raise TypeError('Ranged MatrixExprCons is not well defined!') | ||
Comment on lines
-127
to
-128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This checking is duplicated to |
||
|
||
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)}") | ||
def __le__(self, other: Union[float, int, Expr, np.ndarray, MatrixExpr]) -> MatrixExprCons: | ||
return _matrixexpr_richcmp(self, other, 1) | ||
|
||
return expr_cons_matrix.view(MatrixExprCons) | ||
def __ge__(self, other: Union[float, int, Expr, np.ndarray, MatrixExpr]) -> MatrixExprCons: | ||
return _matrixexpr_richcmp(self, other, 5) | ||
|
||
def __eq__(self, other): | ||
raise TypeError | ||
raise NotImplementedError("Cannot compare MatrixExprCons with '=='.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove duplicated parts. It also appears in matrix.pxi