From 34122289f6668cb753cf315b4d89e4b545d232da Mon Sep 17 00:00:00 2001 From: Joao-Dionisio Date: Thu, 7 Aug 2025 12:03:53 +0100 Subject: [PATCH 1/2] Add some stage checks --- src/pyscipopt/scip.pxi | 46 ++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index a380b1456..61bd4c1df 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -1804,37 +1804,6 @@ cdef class Variable(Expr): mayround = SCIPvarMayRoundUp(self.scip_var) return mayround - def getNBranchings(self, branchdir): - """ - returns the number of times a bound of the variable was changed in given direction due to branching - - Parameters - ---------- - branchdir : PY_SCIP_BRANCHDIR - branching direction (downwards, or upwards) - - Returns - ------- - int - """ - return SCIPvarGetNBranchings(self.scip_var, branchdir) - - def getNBranchingsCurrentRun(self, branchdir): - """ - returns the number of times a bound of the variable was changed in given direction due to branching in the - current run - - Parameters - ---------- - branchdir : PY_SCIP_BRANCHDIR - branching direction (downwards, or upwards) - - Returns - ------- - int - """ - return SCIPvarGetNBranchingsCurrentRun(self.scip_var, branchdir) - class MatrixVariable(MatrixExpr): def vtype(self): @@ -4737,6 +4706,11 @@ cdef class Model: list of Column """ + stage_check = SCIPgetStage(self._scip) in [SCIP_STAGE_SOLVING] + + if not stage_check: + raise Warning("Method cannot be called in stage ", self.getStage()) + cdef SCIP_COL** cols cdef int ncols cdef int i @@ -9738,6 +9712,11 @@ cdef class Model: def restartSolve(self): """Restarts the solving process as soon as possible.""" + stage_check = SCIPgetStage(self._scip) in [SCIP_STAGE_INITPRESOLVE, SCIP_STAGE_PRESOLVING, SCIP_STAGE_EXITPRESOLVE, SCIP_STAGE_SOLVING] + + if not stage_check: + raise Warning("Method cannot be called in stage ", self.getStage()) + PY_SCIP_CALL(SCIPrestartSolve(self._scip)) # Solution functions @@ -10109,6 +10088,11 @@ cdef class Model: whether given solution was feasible and good enough to keep """ + stage_check = SCIPgetStage(self._scip) in [SCIP_STAGE_TRANSFORMED, SCIP_STAGE_INITPRESOLVE, SCIP_STAGE_PRESOLVING, SCIP_STAGE_EXITPRESOLVE, SCIP_STAGE_PRESOLVED, SCIP_STAGE_SOLVING] + + if not stage_check: + raise Warning("Method cannot be called in stage ", self.getStage()) + cdef SCIP_Bool stored if free: PY_SCIP_CALL(SCIPtrySolFree(self._scip, &solution.sol, printreason, completely, checkbounds, checkintegrality, checklprows, &stored)) From c451d48e83f3815cb55c824bdd0457cca6ac81f6 Mon Sep 17 00:00:00 2001 From: Joao-Dionisio Date: Thu, 7 Aug 2025 12:13:15 +0100 Subject: [PATCH 2/2] fix forgotten stuff --- src/pyscipopt/scip.pxi | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index 61bd4c1df..c12d3ebdb 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -1804,6 +1804,37 @@ cdef class Variable(Expr): mayround = SCIPvarMayRoundUp(self.scip_var) return mayround + def getNBranchings(self, branchdir): + """ + returns the number of times a bound of the variable was changed in given direction due to branching + + Parameters + ---------- + branchdir : PY_SCIP_BRANCHDIR + branching direction (downwards, or upwards) + + Returns + ------- + int + """ + return SCIPvarGetNBranchings(self.scip_var, branchdir) + + def getNBranchingsCurrentRun(self, branchdir): + """ + returns the number of times a bound of the variable was changed in given direction due to branching in the + current run + + Parameters + ---------- + branchdir : PY_SCIP_BRANCHDIR + branching direction (downwards, or upwards) + + Returns + ------- + int + """ + return SCIPvarGetNBranchingsCurrentRun(self.scip_var, branchdir) + class MatrixVariable(MatrixExpr): def vtype(self):