Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pyscipopt/scip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,7 @@ cdef extern from "scip/scip.h":
SCIP_RETCODE SCIPfreeReoptSolve(SCIP* scip)
SCIP_RETCODE SCIPchgReoptObjective(SCIP* scip, SCIP_OBJSENSE objsense, SCIP_VAR** vars, SCIP_Real* coefs, int nvars)
SCIP_RETCODE SCIPenableReoptimization(SCIP* scip, SCIP_Bool enable)
SCIP_Bool SCIPisReoptEnabled(SCIP* scip)

BMS_BLKMEM* SCIPblkmem(SCIP* scip)

Expand Down
15 changes: 15 additions & 0 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -3973,6 +3973,17 @@ cdef class Model:
"""
PY_SCIP_CALL(SCIPenableReoptimization(self._scip, enable))

def isReoptEnabled(self):
"""
Returns whether reoptimization is enabled.

Returns
-------
bool

"""
return SCIPisReoptEnabled(self._scip)

def lpiGetIterations(self):
"""
Get the iteration count of the last solved LP.
Expand Down Expand Up @@ -11950,6 +11961,10 @@ cdef class Model:
def freeReoptSolve(self):
"""Frees all solution process data and prepares for reoptimization."""

if not SCIPisReoptEnabled(self._scip):
raise ValueError("freeReoptSolve requires reoptimization to be enabled. "
"Call enableReoptimization() before solving.")

if self.getStage() not in [SCIP_STAGE_INIT,
SCIP_STAGE_PROBLEM,
SCIP_STAGE_TRANSFORMED,
Expand Down
1 change: 1 addition & 0 deletions src/pyscipopt/scip.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,7 @@ class Model:
def isObjChangedProbing(self) -> Incomplete: ...
def isObjIntegral(self) -> Incomplete: ...
def isPositive(self, val: Incomplete) -> Incomplete: ...
def isReoptEnabled(self) -> bool: ...
def isZero(self, value: Incomplete) -> Incomplete: ...
def lpiGetIterations(self) -> Incomplete: ...
def markDoNotAggrVar(self, var: Incomplete) -> Incomplete: ...
Expand Down
25 changes: 25 additions & 0 deletions tests/test_reopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ def test_reopt_many_variables_sparse_objective(self):
self.assertAlmostEqual(m.getVal(vars[50]), 0.0)
self.assertAlmostEqual(m.getVal(vars[99]), 0.0)

def test_freeReoptSolve_requires_reoptimization_enabled(self):
"""freeReoptSolve must raise ValueError instead of crashing when
reoptimization was not enabled (see issue #624)."""
m = Model()
m.hideOutput()
self.assertFalse(m.isReoptEnabled())

x = m.addVar(name="x", vtype="I", ub=10)
m.addCons(x >= 3)
m.setObjective(x)
m.optimize()

with self.assertRaises(ValueError):
m.freeReoptSolve()

m2 = Model()
m2.enableReoptimization()
self.assertTrue(m2.isReoptEnabled())
m2.hideOutput()
y = m2.addVar(name="y", vtype="I", ub=10)
m2.addCons(y >= 3)
m2.setObjective(y)
m2.optimize()
m2.freeReoptSolve()

def test_reopt_zero_objective(self):
"""Test reoptimization with zero objective (no variables, all coefficients zero)."""
m = Model()
Expand Down
Loading