diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b699ee56..8405580d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ### Added +- Wrapped SCIPcreateOrigSol and added tests - Added verbose option for writeProblem and writeParams - Expanded locale test - Added methods for creating expression constraints without adding to problem diff --git a/src/pyscipopt/scip.pxd b/src/pyscipopt/scip.pxd index 73c2227ff..dd1bb5bf4 100644 --- a/src/pyscipopt/scip.pxd +++ b/src/pyscipopt/scip.pxd @@ -841,6 +841,7 @@ cdef extern from "scip/scip.h": SCIP_Real SCIPgetSolTransObj(SCIP* scip, SCIP_SOL* sol) SCIP_RETCODE SCIPcreateSol(SCIP* scip, SCIP_SOL** sol, SCIP_HEUR* heur) SCIP_RETCODE SCIPcreatePartialSol(SCIP* scip, SCIP_SOL** sol,SCIP_HEUR* heur) + SCIP_RETCODE SCIPcreateOrigSol(SCIP* scip, SCIP_SOL** sol, SCIP_HEUR* heur) SCIP_RETCODE SCIPsetSolVal(SCIP* scip, SCIP_SOL* sol, SCIP_VAR* var, SCIP_Real val) SCIP_RETCODE SCIPtrySolFree(SCIP* scip, SCIP_SOL** sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool* stored) SCIP_RETCODE SCIPtrySol(SCIP* scip, SCIP_SOL* sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool* stored) diff --git a/src/pyscipopt/scip.pxi b/src/pyscipopt/scip.pxi index dfa5bd93b..95545b2da 100644 --- a/src/pyscipopt/scip.pxi +++ b/src/pyscipopt/scip.pxi @@ -4507,7 +4507,7 @@ cdef class Model: locale.setlocale(locale.LC_NUMERIC,user_locale) def createSol(self, Heur heur = None): - """Create a new primal solution. + """Create a new primal solution in the transformed space. :param Heur heur: heuristic that found the solution (Default value = None) @@ -4541,6 +4541,25 @@ cdef class Model: partialsolution = Solution.create(self._scip, _sol) return partialsolution + def createOrigSol(self, Heur heur = None): + """Create a new primal solution in the original space. + + :param Heur heur: heuristic that found the solution (Default value = None) + + """ + cdef SCIP_HEUR* _heur + cdef SCIP_SOL* _sol + + if isinstance(heur, Heur): + n = str_conversion(heur.name) + _heur = SCIPfindHeur(self._scip, n) + else: + _heur = NULL + + PY_SCIP_CALL(SCIPcreateOrigSol(self._scip, &_sol, _heur)) + solution = Solution.create(self._scip, _sol) + return solution + def printBestSol(self, write_zeros=False): """Prints the best feasible primal solution.""" user_locale = locale.getlocale(category=locale.LC_NUMERIC) diff --git a/tests/test_solution.py b/tests/test_solution.py index d1b7c9d33..61846b167 100644 --- a/tests/test_solution.py +++ b/tests/test_solution.py @@ -47,6 +47,34 @@ def test_solution_create(): assert m.getSolObjVal(s1) == -1 m.freeSol(s1) +def test_createOrigSol(): + m = Model() + + x = m.addVar("x", lb=0, ub=2, obj=-1) + y = m.addVar("y", lb=1, ub=4, obj=1) + z = m.addVar("z", lb=1, ub=5, obj=10) + m.addCons(x * x <= y*z) + m.presolve() + + s = m.createOrigSol() + s[x] = 2.0 + s[y] = 5.0 + s[z] = 10.0 + assert not m.checkSol(s) + assert m.addSol(s, free=True) + + s1 = m.createOrigSol() + m.setSolVal(s1, x, 1.0) + m.setSolVal(s1, y, 1.0) + m.setSolVal(s1, z, 1.0) + assert m.checkSol(s1) + assert m.addSol(s1, free=False) + + m.optimize() + + assert m.getSolObjVal(s1) == 10.0 + m.freeSol(s1) + def test_solution_evaluation(): m = Model()