@@ -1098,7 +1098,16 @@ cdef class Solution:
10981098 sol.scip = scip
10991099 return sol
11001100
1101- def __getitem__ (self , expr: Union[Expr , MatrixExpr]):
1101+ def __getitem__ (
1102+ self ,
1103+ expr: Union[Expr , GenExpr , MatrixExpr],
1104+ ) -> Union[float , np.ndarray]:
1105+ if not isinstance(expr , (Expr , GenExpr , MatrixExpr )):
1106+ raise TypeError (
1107+ " Argument 'expr' has incorrect type, expected 'Expr', 'GenExpr', or "
1108+ f" 'MatrixExpr', got {type(expr).__name__!r}"
1109+ )
1110+
11021111 self ._checkStage(" SCIPgetSolVal" )
11031112 return expr._evaluate(self )
11041113
@@ -10968,75 +10977,63 @@ cdef class Model:
1096810977 def getSolVal (
1096910978 self ,
1097010979 Solution sol ,
10971- expr: Union[Expr , GenExpr],
10980+ expr: Union[Expr , GenExpr , MatrixExpr ],
1097210981 ) -> Union[float , np.ndarray]:
1097310982 """
10974- Retrieve value of given variable or expression in the given solution or in
10975- the LP/pseudo solution if sol == None
10983+ Retrieve value of given variable or expression in the given solution.
1097610984
1097710985 Parameters
1097810986 ----------
1097910987 sol : Solution
10980- expr : Expr
10981- polynomial expression to query the value of
10988+ Solution to query the value from. If None , the current LP/pseudo solution is
10989+ used.
10990+
10991+ expr : Expr , GenExpr , MatrixExpr
10992+ Expression to query the value of.
1098210993
1098310994 Returns
1098410995 -------
10985- float
10996+ float or np. ndarray
1098610997
1098710998 Notes
1098810999 -----
1098911000 A variable is also an expression.
1099011001
1099111002 """
10992- if not isinstance(expr , (Expr , GenExpr )):
10993- raise TypeError (
10994- " Argument 'expr' has incorrect type (expected 'Expr' or 'GenExpr', "
10995- f" got {type(expr)})"
10996- )
1099711003 # no need to create a NULL solution wrapper in case we have a variable
1099811004 return (sol or Solution.create(self._scip , NULL ))[expr]
1099911005
11000- def getVal (self , expr: Union[Expr , GenExpr , MatrixExpr] ) :
11006+ def getVal(self , expr: Union[Expr , GenExpr , MatrixExpr]) -> Union[ float , np.ndarray] :
1100111007 """
1100211008 Retrieve the value of the given variable or expression in the best known solution.
1100311009 Can only be called after solving is completed.
1100411010
1100511011 Parameters
1100611012 ----------
1100711013 expr : Expr , GenExpr or MatrixExpr
11014+ Expression to query the value of.
1100811015
1100911016 Returns
1101011017 -------
11011- float
11018+ float or np. ndarray
1101211019
1101311020 Notes
1101411021 -----
1101511022 A variable is also an expression.
1101611023
1101711024 """
11018- cdef SCIP_SOL* current_best_sol
11019-
11020- stage_check = SCIPgetStage(self ._scip) not in [SCIP_STAGE_INIT, SCIP_STAGE_FREE]
11021- if not stage_check:
11025+ if SCIPgetStage(self._scip ) in {SCIP_STAGE_INIT , SCIP_STAGE_FREE}:
1102211026 raise Warning("Method cannot be called in stage ", self.getStage())
1102311027
1102411028 # Ensure _bestSol is up-to-date (cheap pointer comparison )
11025- current_best_sol = SCIPgetBestSol(self ._scip)
11029+ cdef SCIP_SOL* current_best_sol = SCIPgetBestSol(self ._scip)
1102611030 if self._bestSol is None or self._bestSol.sol != current_best_sol:
1102711031 self._bestSol = Solution.create(self ._scip, current_best_sol)
1102811032
1102911033 if self._bestSol.sol == NULL and SCIPgetStage(self._scip ) != SCIP_STAGE_SOLVING:
1103011034 raise Warning("No solution available")
1103111035
11032- if isinstance (expr, MatrixExpr):
11033- result = np.empty(expr.shape, dtype = float )
11034- for idx in np.ndindex(result.shape):
11035- result[idx] = self .getSolVal(self ._bestSol, expr[idx])
11036- else :
11037- result = self .getSolVal(self ._bestSol, expr)
11038-
11039- return result
11036+ return self._bestSol[expr]
1104011037
1104111038 def hasPrimalRay(self ):
1104211039 """
0 commit comments