-
Notifications
You must be signed in to change notification settings - Fork 275
BUG: can't call getVal from GenExpr
#1148
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
20
commits into
scipopt:master
Choose a base branch
from
Zeroto521:issue/1062
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.
+131
−40
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f87432c
Add tests for expression evaluation in Model
Zeroto521 8c600a2
Add unified expression evaluation with _evaluate methods
Zeroto521 85ee97e
Optimize matrix expression evaluation in Solution
Zeroto521 135b954
Add test for matrix variable evaluation
Zeroto521 ad7d4c6
Remove matrix expression handling in Solution __getitem__
Zeroto521 5ff4144
Refactor Term class variable naming and usage
Zeroto521 95b3a80
Use double precision for expression evaluation
Zeroto521 7770a06
Add type annotations and improve Term class attributes
Zeroto521 50e9c6c
Make _evaluate methods public and refactor Expr/Variable
Zeroto521 239c3a3
Update CHANGELOG.md
Zeroto521 d2ab8e2
Remove hash method from Variable
Zeroto521 2b145dd
back to old behavior
Zeroto521 9afb07f
Fix MatrixExpr _evaluate to return ndarray type
Zeroto521 66c2f6b
Remove unused _evaluate method from Solution stub
Zeroto521 4ff2682
Add @disjoint_base decorator to Term and UnaryExpr
Zeroto521 073ac1c
Add noqa to suppress unused import warning
Zeroto521 22b9f4f
cache `_evaluate` function for matrix
Zeroto521 449e2fd
Refactor _evaluate to use np.frompyfunc
Zeroto521 7cd196a
Simplify _evaluate return in MatrixExpr
Zeroto521 3ff4658
Merge branch 'master' into issue/1062
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,8 +42,16 @@ | |
| # which should, in princple, modify the expr. However, since we do not implement __isub__, __sub__ | ||
| # gets called (I guess) and so a copy is returned. | ||
| # Modifying the expression directly would be a bug, given that the expression might be re-used by the user. </pre> | ||
| import math | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from pyscipopt.scip cimport Variable, Solution | ||
|
|
||
| include "matrix.pxi" | ||
|
|
||
| if TYPE_CHECKING: | ||
| double = float | ||
|
Contributor
Author
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. For IDE |
||
|
|
||
|
|
||
| def _is_number(e): | ||
| try: | ||
|
|
@@ -87,23 +95,25 @@ def _expr_richcmp(self, other, op): | |
| raise NotImplementedError("Can only support constraints with '<=', '>=', or '=='.") | ||
|
|
||
|
|
||
| class Term: | ||
| cdef class Term: | ||
Joao-Dionisio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| '''This is a monomial term''' | ||
|
|
||
| __slots__ = ('vartuple', 'ptrtuple', 'hashval') | ||
| cdef readonly tuple vartuple | ||
| cdef readonly tuple ptrtuple | ||
| cdef int hashval | ||
|
|
||
| def __init__(self, *vartuple): | ||
| def __init__(self, *vartuple: Variable): | ||
| self.vartuple = tuple(sorted(vartuple, key=lambda v: v.ptr())) | ||
| self.ptrtuple = tuple(v.ptr() for v in self.vartuple) | ||
| self.hashval = sum(self.ptrtuple) | ||
| self.hashval = hash(self.ptrtuple) | ||
|
|
||
| def __getitem__(self, idx): | ||
| return self.vartuple[idx] | ||
|
|
||
| def __hash__(self): | ||
| return self.hashval | ||
|
|
||
| def __eq__(self, other): | ||
| def __eq__(self, other: Term): | ||
| return self.ptrtuple == other.ptrtuple | ||
|
|
||
| def __len__(self): | ||
|
|
@@ -116,6 +126,13 @@ class Term: | |
| def __repr__(self): | ||
| return 'Term(%s)' % ', '.join([str(v) for v in self.vartuple]) | ||
|
|
||
| cpdef double _evaluate(self, Solution sol): | ||
| cdef double res = 1 | ||
| cdef Variable i | ||
| for i in self.vartuple: | ||
| res *= SCIPgetSolVal(sol.scip, sol.sol, i.scip_var) | ||
| return res | ||
|
|
||
|
|
||
| CONST = Term() | ||
|
|
||
|
|
@@ -157,7 +174,7 @@ def buildGenExprObj(expr): | |
| ##@details Polynomial expressions of variables with operator overloading. \n | ||
| #See also the @ref ExprDetails "description" in the expr.pxi. | ||
| cdef class Expr: | ||
|
|
||
| def __init__(self, terms=None): | ||
| '''terms is a dict of variables to coefficients. | ||
|
|
||
|
|
@@ -318,6 +335,14 @@ cdef class Expr: | |
| else: | ||
| return max(len(v) for v in self.terms) | ||
|
|
||
| cpdef double _evaluate(self, Solution sol): | ||
| cdef double res = 0 | ||
| cdef double coef | ||
| cdef Term term | ||
| for term, coef in self.terms.items(): | ||
| res += coef * term._evaluate(sol) | ||
| return res | ||
|
|
||
|
|
||
| cdef class ExprCons: | ||
| '''Constraints with a polynomial expressions and lower/upper bounds.''' | ||
|
|
@@ -427,10 +452,10 @@ Operator = Op() | |
| # | ||
| #See also the @ref ExprDetails "description" in the expr.pxi. | ||
| cdef class GenExpr: | ||
|
|
||
| cdef public _op | ||
| cdef public children | ||
|
|
||
|
|
||
| def __init__(self): # do we need it | ||
| ''' ''' | ||
|
|
||
|
|
@@ -625,44 +650,83 @@ cdef class SumExpr(GenExpr): | |
| def __repr__(self): | ||
| return self._op + "(" + str(self.constant) + "," + ",".join(map(lambda child : child.__repr__(), self.children)) + ")" | ||
|
|
||
| cpdef double _evaluate(self, Solution sol): | ||
| cdef double res = self.constant | ||
| cdef GenExpr child | ||
| cdef double coef | ||
| for child, coef in zip(self.children, self.coefs): | ||
| res += coef * child._evaluate(sol) | ||
| return res | ||
|
|
||
|
|
||
| # Prod Expressions | ||
| cdef class ProdExpr(GenExpr): | ||
|
|
||
| cdef public constant | ||
|
|
||
| def __init__(self): | ||
| self.constant = 1.0 | ||
| self.children = [] | ||
| self._op = Operator.prod | ||
|
|
||
| def __repr__(self): | ||
| return self._op + "(" + str(self.constant) + "," + ",".join(map(lambda child : child.__repr__(), self.children)) + ")" | ||
|
|
||
| cpdef double _evaluate(self, Solution sol): | ||
| cdef double res = self.constant | ||
| cdef GenExpr child | ||
| for child in self.children: | ||
| res *= child._evaluate(sol) | ||
| return res | ||
|
|
||
|
|
||
| # Var Expressions | ||
| cdef class VarExpr(GenExpr): | ||
|
|
||
| cdef public var | ||
|
|
||
| def __init__(self, var): | ||
| self.children = [var] | ||
| self._op = Operator.varidx | ||
|
|
||
| def __repr__(self): | ||
| return self.children[0].__repr__() | ||
|
|
||
| cpdef double _evaluate(self, Solution sol): | ||
| return self.children[0]._evaluate(sol) | ||
|
|
||
|
|
||
| # Pow Expressions | ||
| cdef class PowExpr(GenExpr): | ||
|
|
||
| cdef public expo | ||
|
|
||
| def __init__(self): | ||
| self.expo = 1.0 | ||
| self.children = [] | ||
| self._op = Operator.power | ||
|
|
||
| def __repr__(self): | ||
| return self._op + "(" + self.children[0].__repr__() + "," + str(self.expo) + ")" | ||
|
|
||
| cpdef double _evaluate(self, Solution sol): | ||
| return self.children[0]._evaluate(sol) ** self.expo | ||
|
|
||
|
|
||
| # Exp, Log, Sqrt, Sin, Cos Expressions | ||
| cdef class UnaryExpr(GenExpr): | ||
| def __init__(self, op, expr): | ||
| self.children = [] | ||
| self.children.append(expr) | ||
| self._op = op | ||
|
|
||
| def __repr__(self): | ||
| return self._op + "(" + self.children[0].__repr__() + ")" | ||
|
|
||
| cpdef double _evaluate(self, Solution sol): | ||
| return getattr(math, self._op)(self.children[0]._evaluate(sol)) | ||
|
|
||
|
|
||
| # class for constant expressions | ||
| cdef class Constant(GenExpr): | ||
| cdef public number | ||
|
|
@@ -673,6 +737,10 @@ cdef class Constant(GenExpr): | |
| def __repr__(self): | ||
| return str(self.number) | ||
|
|
||
| cpdef double _evaluate(self, Solution sol): | ||
| return self.number | ||
|
|
||
|
|
||
| def exp(expr): | ||
| """returns expression with exp-function""" | ||
| if isinstance(expr, MatrixExpr): | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -5,13 +5,16 @@ | |
|
|
||
| from typing import Optional, Tuple, Union | ||
| import numpy as np | ||
| from numpy.typing import NDArray | ||
| try: | ||
| # NumPy 2.x location | ||
| from numpy.lib.array_utils import normalize_axis_tuple | ||
| except ImportError: | ||
| # Fallback for NumPy 1.x | ||
| from numpy.core.numeric import normalize_axis_tuple | ||
|
|
||
| from pyscipopt.scip cimport Expr, Solution | ||
|
|
||
|
|
||
| def _is_number(e): | ||
| try: | ||
|
|
@@ -49,6 +52,9 @@ def _matrixexpr_richcmp(self, other, op): | |
| return res.view(MatrixExprCons) | ||
|
|
||
|
|
||
| _evaluate = np.frompyfunc(lambda expr, sol: expr._evaluate(sol), 2, 1) | ||
|
|
||
|
|
||
| class MatrixExpr(np.ndarray): | ||
|
|
||
| def sum( | ||
|
|
@@ -148,9 +154,14 @@ class MatrixExpr(np.ndarray): | |
| def __matmul__(self, other): | ||
| return super().__matmul__(other).view(MatrixExpr) | ||
|
|
||
| def _evaluate(self, Solution sol) -> NDArray[np.float64]: | ||
|
Contributor
Author
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.
|
||
| return _evaluate(self, sol).view(np.ndarray) | ||
|
|
||
|
|
||
| class MatrixGenExpr(MatrixExpr): | ||
| pass | ||
|
|
||
|
|
||
| class MatrixExprCons(np.ndarray): | ||
|
|
||
| def __le__(self, other: Union[float, int, np.ndarray]) -> MatrixExprCons: | ||
|
|
||
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
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
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.
This could let IDE know what
VariableandSolutionare.