Skip to content

Commit 87e8117

Browse files
authored
Merge pull request #497 from scipopt/493-allow-NULL-solution
proper fix for #493
2 parents e280010 + 53264f5 commit 87e8117

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44
### Added
55
### Fixed
6+
- allow NULL solutions to query LP solutions in the correct solving stage
67
### Changed
78
### Removed
89

src/pyscipopt/scip.pyx

+10-2
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,6 @@ cdef class Solution:
533533
cdef create(SCIP* scip, SCIP_SOL* scip_sol):
534534
if scip == NULL:
535535
raise Warning("cannot create Solution with SCIP* == NULL")
536-
if scip_sol == NULL:
537-
return None
538536
sol = Solution()
539537
sol.sol = scip_sol
540538
sol.scip = scip
@@ -543,11 +541,13 @@ cdef class Solution:
543541
def __getitem__(self, Expr expr):
544542
# fast track for Variable
545543
if isinstance(expr, Variable):
544+
self._checkStage("SCIPgetSolVal")
546545
var = <Variable> expr
547546
return SCIPgetSolVal(self.scip, self.sol, var.scip_var)
548547
return sum(self._evaluate(term)*coeff for term, coeff in expr.terms.items() if coeff != 0)
549548

550549
def _evaluate(self, term):
550+
self._checkStage("SCIPgetSolVal")
551551
result = 1
552552
for var in term.vartuple:
553553
result *= SCIPgetSolVal(self.scip, self.sol, (<Variable> var).scip_var)
@@ -560,6 +560,7 @@ cdef class Solution:
560560
cdef SCIP_VAR* scip_var
561561

562562
vals = {}
563+
self._checkStage("SCIPgetSolVal")
563564
for i in range(SCIPgetNVars(self.scip)):
564565
scip_var = SCIPgetVars(self.scip)[i]
565566

@@ -569,6 +570,12 @@ cdef class Solution:
569570

570571
vals[name] = SCIPgetSolVal(self.scip, self.sol, scip_var)
571572
return str(vals)
573+
574+
def _checkStage(self, method):
575+
if method in ["SCIPgetSolVal", "getSolObjVal"]:
576+
if self.sol == NULL and not SCIPgetStage(self.scip) == SCIP_STAGE_SOLVING:
577+
raise Warning(f"{method} cannot only be called in stage SOLVING without a valid solution (current stage: {SCIPgetStage(self.scip)})")
578+
572579

573580
cdef class BoundChange:
574581
"""Bound change."""
@@ -4151,6 +4158,7 @@ cdef class Model:
41514158
"""
41524159
if sol == None:
41534160
sol = Solution.create(self._scip, NULL)
4161+
sol._checkStage("getSolObjVal")
41544162
if original:
41554163
objval = SCIPgetSolOrigObj(self._scip, sol.sol)
41564164
else:

0 commit comments

Comments
 (0)