-
Notifications
You must be signed in to change notification settings - Fork 274
Open
Description
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
getSolVal only requires Expr. So, for a MatrixExpr, it has to use a loop to query the result
But getVal could receive MatrixExpr.
PySCIPOpt/src/pyscipopt/scip.pxi
Line 10750 in 2f473ba
| def getSolVal(self, Solution sol, Expr expr): |
PySCIPOpt/src/pyscipopt/scip.pxi
Line 10779 in 2f473ba
| def getVal(self, expr: Union[Expr, MatrixExpr] ): |
Describe the solution you'd like
A clear and concise description of what you want to happen.
Change the getSolVal argument.
Additional context
Add any other context or screenshots about the feature request here.
A demo is as follows. And np.vectorize could also simplify getSol codes.
from functools import wraps
import numpy as np
from pyscipopt import Model
def to_ndarray(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs).view(np.ndarray)
return wrapper
@to_ndarray
@np.vectorize
def getSolVal(expr, model, sol):
return model.getSolVal(sol, expr)
if __name__ == "__main__":
model = Model()
model.hideOutput()
x = model.addMatrixVar((2, 3), ub=10)
model.setObjective(x.sum(), "maximize")
model.optimize()
res1 = model.getVal(x)
print(type(res1))
# <class 'numpy.ndarray'>
print(res1)
# [[10. 10. 10.]
# [10. 10. 10.]]
sol = model.getBestSol()
res2 = getSolVal(x, model, sol)
print(type(res2))
# <class 'numpy.ndarray'>
print(res2)
# [[10. 10. 10.]
# [10. 10. 10.]]
assert np.array_equal(res1, res2)Metadata
Metadata
Assignees
Labels
No labels