Skip to content

Commit 9a7f586

Browse files
committed
Add createOrigSol and tests
1 parent 92061ca commit 9a7f586

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/pyscipopt/scip.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ cdef extern from "scip/scip.h":
837837
SCIP_Real SCIPgetSolTransObj(SCIP* scip, SCIP_SOL* sol)
838838
SCIP_RETCODE SCIPcreateSol(SCIP* scip, SCIP_SOL** sol, SCIP_HEUR* heur)
839839
SCIP_RETCODE SCIPcreatePartialSol(SCIP* scip, SCIP_SOL** sol,SCIP_HEUR* heur)
840+
SCIP_RETCODE SCIPcreateOrigSol(SCIP* scip, SCIP_SOL** sol, SCIP_HEUR* heur)
840841
SCIP_RETCODE SCIPsetSolVal(SCIP* scip, SCIP_SOL* sol, SCIP_VAR* var, SCIP_Real val)
841842
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)
842843
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)

src/pyscipopt/scip.pxi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,6 +4429,25 @@ cdef class Model:
44294429
partialsolution = Solution.create(self._scip, _sol)
44304430
return partialsolution
44314431

4432+
def createOrigSol(self, Heur heur = None):
4433+
"""Create a new primal solution.
4434+
4435+
:param Heur heur: heuristic that found the solution (Default value = None)
4436+
4437+
"""
4438+
cdef SCIP_HEUR* _heur
4439+
cdef SCIP_SOL* _sol
4440+
4441+
if isinstance(heur, Heur):
4442+
n = str_conversion(heur.name)
4443+
_heur = SCIPfindHeur(self._scip, n)
4444+
else:
4445+
_heur = NULL
4446+
4447+
PY_SCIP_CALL(SCIPcreateOrigSol(self._scip, &_sol, _heur))
4448+
solution = Solution.create(self._scip, _sol)
4449+
return solution
4450+
44324451
def printBestSol(self, write_zeros=False):
44334452
"""Prints the best feasible primal solution."""
44344453
user_locale = locale.getlocale(category=locale.LC_NUMERIC)

tests/test_solution.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ def test_solution_create():
4747
assert m.getSolObjVal(s1) == -1
4848
m.freeSol(s1)
4949

50+
def test_createOrigSol():
51+
m = Model()
52+
53+
x = m.addVar("x", lb=0, ub=2, obj=-1)
54+
y = m.addVar("y", lb=1, ub=4, obj=1)
55+
z = m.addVar("z", lb=1, ub=5, obj=10)
56+
m.addCons(x * x <= y*z)
57+
m.presolve()
58+
59+
s = m.createOrigSol()
60+
s[x] = 2.0
61+
s[y] = 5.0
62+
s[z] = 10.0
63+
assert not m.checkSol(s)
64+
assert m.addSol(s, free=True)
65+
66+
s1 = m.createOrigSol()
67+
m.setSolVal(s1, x, 1.0)
68+
m.setSolVal(s1, y, 1.0)
69+
m.setSolVal(s1, z, 1.0)
70+
assert m.checkSol(s1)
71+
72+
m.optimize()
73+
74+
assert m.getSolObjVal(s1) == 10.0
75+
m.freeSol(s1)
76+
5077

5178
def test_solution_evaluation():
5279
m = Model()

0 commit comments

Comments
 (0)