@@ -533,8 +533,6 @@ cdef class Solution:
533
533
cdef create(SCIP* scip, SCIP_SOL* scip_sol):
534
534
if scip == NULL :
535
535
raise Warning (" cannot create Solution with SCIP* == NULL" )
536
- if scip_sol == NULL :
537
- return None
538
536
sol = Solution()
539
537
sol.sol = scip_sol
540
538
sol.scip = scip
@@ -543,11 +541,13 @@ cdef class Solution:
543
541
def __getitem__ (self , Expr expr ):
544
542
# fast track for Variable
545
543
if isinstance (expr, Variable):
544
+ self ._checkStage(" SCIPgetSolVal" )
546
545
var = < Variable> expr
547
546
return SCIPgetSolVal(self .scip, self .sol, var.scip_var)
548
547
return sum (self ._evaluate(term)* coeff for term, coeff in expr.terms.items() if coeff != 0 )
549
548
550
549
def _evaluate (self , term ):
550
+ self ._checkStage(" SCIPgetSolVal" )
551
551
result = 1
552
552
for var in term.vartuple:
553
553
result *= SCIPgetSolVal(self .scip, self .sol, (< Variable> var).scip_var)
@@ -560,6 +560,7 @@ cdef class Solution:
560
560
cdef SCIP_VAR* scip_var
561
561
562
562
vals = {}
563
+ self ._checkStage(" SCIPgetSolVal" )
563
564
for i in range (SCIPgetNVars(self .scip)):
564
565
scip_var = SCIPgetVars(self .scip)[i]
565
566
@@ -569,6 +570,12 @@ cdef class Solution:
569
570
570
571
vals[name] = SCIPgetSolVal(self .scip, self .sol, scip_var)
571
572
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
+
572
579
573
580
cdef class BoundChange:
574
581
""" Bound change."""
@@ -4151,6 +4158,7 @@ cdef class Model:
4151
4158
"""
4152
4159
if sol == None :
4153
4160
sol = Solution.create(self ._scip, NULL )
4161
+ sol._checkStage(" getSolObjVal" )
4154
4162
if original:
4155
4163
objval = SCIPgetSolOrigObj(self ._scip, sol.sol)
4156
4164
else :
0 commit comments