Skip to content

Commit d70fb78

Browse files
committed
proper fix for #493
1 parent e280010 commit d70fb78

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()
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()
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()
563564
for i in range(SCIPgetNVars(self.scip)):
564565
scip_var = SCIPgetVars(self.scip)[i]
565566

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

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

573579
cdef class BoundChange:
574580
"""Bound change."""
@@ -4151,6 +4157,8 @@ cdef class Model:
41514157
"""
41524158
if sol == None:
41534159
sol = Solution.create(self._scip, NULL)
4160+
if sol.sol == NULL and not self.getStage() == SCIP_STAGE_SOLVING:
4161+
raise Warning(f"method cannot only be called in stage SOLVING without a valid solution")
41544162
if original:
41554163
objval = SCIPgetSolOrigObj(self._scip, sol.sol)
41564164
else:

0 commit comments

Comments
 (0)