Skip to content

Commit 98b7e01

Browse files
Fixes potentially outdated value of getVal (#993)
* Fixes potentially outdated value of getVal * Fix funny error * Check if stored solution remains best * typo * Update changelog --------- Co-authored-by: Joao-Dionisio <up201606210@up.pt> Co-authored-by: João Dionísio <57299939+Joao-Dionisio@users.noreply.github.com> Co-authored-by: Joao-Dionisio <joao.goncalves.dionisio@gmail.com>
1 parent a3a9fdc commit 98b7e01

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Fixed the type of MatrixExpr.sum(axis=...) result from MatrixVariable to MatrixExpr.
1111
- Updated IIS result in PyiisfinderExec()
1212
- Fixed lotsizing_lazy example
13+
- Fixed incorrect getVal() result when _bestSol.sol was outdated
1314
### Changed
1415
- changed default value of enablepricing flag to True
1516
### Removed

src/pyscipopt/scip.pxi

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,8 @@ cdef class Solution:
10681068
"""Base class holding a pointer to corresponding SCIP_SOL."""
10691069

10701070
# We are raising an error here to avoid creating a solution without an associated model. See Issue #625
1071-
def __init__(self, raise_error = False):
1072-
if not raise_error:
1071+
def __init__(self, raise_error = True):
1072+
if raise_error:
10731073
raise ValueError("To create a solution you should use the createSol method of the Model class.")
10741074

10751075
@staticmethod
@@ -1093,7 +1093,7 @@ cdef class Solution:
10931093
"""
10941094
if scip == NULL:
10951095
raise Warning("cannot create Solution with SCIP* == NULL")
1096-
sol = Solution(True)
1096+
sol = Solution(raise_error=False)
10971097
sol.sol = scip_sol
10981098
sol.scip = scip
10991099
return sol
@@ -10795,11 +10795,20 @@ cdef class Model:
1079510795
A variable is also an expression.
1079610796
1079710797
"""
10798-
stage_check = SCIPgetStage(self._scip) not in [SCIP_STAGE_INIT, SCIP_STAGE_FREE]
10798+
cdef SCIP_SOL* current_best_sol
1079910799

10800-
if not stage_check or self._bestSol.sol == NULL and SCIPgetStage(self._scip) != SCIP_STAGE_SOLVING:
10800+
stage_check = SCIPgetStage(self._scip) not in [SCIP_STAGE_INIT, SCIP_STAGE_FREE]
10801+
if not stage_check:
1080110802
raise Warning("Method cannot be called in stage ", self.getStage())
1080210803

10804+
# Ensure _bestSol is up-to-date (cheap pointer comparison)
10805+
current_best_sol = SCIPgetBestSol(self._scip)
10806+
if self._bestSol is None or self._bestSol.sol != current_best_sol:
10807+
self._bestSol = Solution.create(self._scip, current_best_sol)
10808+
10809+
if self._bestSol.sol == NULL and SCIPgetStage(self._scip) != SCIP_STAGE_SOLVING:
10810+
raise Warning("No solution available")
10811+
1080310812
if isinstance(expr, MatrixExpr):
1080410813
result = np.empty(expr.shape, dtype=float)
1080510814
for idx in np.ndindex(result.shape):

0 commit comments

Comments
 (0)