diff --git a/CHANGELOG.md b/CHANGELOG.md index 52df46228..69f3fe58e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - Fixed the type of MatrixExpr.sum(axis=...) result from MatrixVariable to MatrixExpr. - Updated IIS result in PyiisfinderExec() - Fixed lotsizing_lazy example +- Fixed incorrect getVal() result when _bestSol.sol was outdated ### Changed - changed default value of enablepricing flag to True ### Removed diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index da23028f9..c4e11ebcb 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -1068,8 +1068,8 @@ cdef class Solution: """Base class holding a pointer to corresponding SCIP_SOL.""" # We are raising an error here to avoid creating a solution without an associated model. See Issue #625 - def __init__(self, raise_error = False): - if not raise_error: + def __init__(self, raise_error = True): + if raise_error: raise ValueError("To create a solution you should use the createSol method of the Model class.") @staticmethod @@ -1093,7 +1093,7 @@ cdef class Solution: """ if scip == NULL: raise Warning("cannot create Solution with SCIP* == NULL") - sol = Solution(True) + sol = Solution(raise_error=False) sol.sol = scip_sol sol.scip = scip return sol @@ -10795,11 +10795,20 @@ cdef class Model: A variable is also an expression. """ - stage_check = SCIPgetStage(self._scip) not in [SCIP_STAGE_INIT, SCIP_STAGE_FREE] + cdef SCIP_SOL* current_best_sol - if not stage_check or self._bestSol.sol == NULL and SCIPgetStage(self._scip) != SCIP_STAGE_SOLVING: + stage_check = SCIPgetStage(self._scip) not in [SCIP_STAGE_INIT, SCIP_STAGE_FREE] + if not stage_check: raise Warning("Method cannot be called in stage ", self.getStage()) + # Ensure _bestSol is up-to-date (cheap pointer comparison) + current_best_sol = SCIPgetBestSol(self._scip) + if self._bestSol is None or self._bestSol.sol != current_best_sol: + self._bestSol = Solution.create(self._scip, current_best_sol) + + if self._bestSol.sol == NULL and SCIPgetStage(self._scip) != SCIP_STAGE_SOLVING: + raise Warning("No solution available") + if isinstance(expr, MatrixExpr): result = np.empty(expr.shape, dtype=float) for idx in np.ndindex(result.shape):