@@ -15,10 +15,9 @@ from libc.stdlib cimport malloc, free
1515from libc.stdio cimport fdopen, fclose
1616from posix.stdio cimport fileno
1717
18- from collections.abc import Iterable, Sequence
18+ from collections.abc import Iterable
1919from itertools import repeat
2020from dataclasses import dataclass
21- from concurrent.futures import ThreadPoolExecutor, as_completed
2221
2322include " expr.pxi"
2423include " lp.pxi"
@@ -1034,6 +1033,27 @@ cdef class Solution:
10341033 """ retransforms solution to original problem space """
10351034 PY_SCIP_CALL(SCIPretransformSol(self .scip, self .sol))
10361035
1036+ def translate (self , Model target ):
1037+ """
1038+ translate solution to a target model solution
1039+
1040+ Parameters
1041+ ----------
1042+ target : Model
1043+
1044+ Returns
1045+ -------
1046+ targetSol: Solution
1047+ """
1048+ if self .getSolOrigin() != SCIP_SOLORIGIN_ORIGINAL:
1049+ PY_SCIP_CALL(SCIPretransformSol(self .scip, self .sol))
1050+ cdef Solution targetSol = Solution.create(target._scip, NULL )
1051+ cdef SCIP_VAR** source_vars = SCIPgetOrigVars(self .scip)
1052+
1053+ PY_SCIP_CALL(SCIPtranslateSubSol(target._scip, self .scip, self .sol, NULL , source_vars, & (targetSol.sol)))
1054+ return targetSol
1055+
1056+
10371057cdef class BoundChange:
10381058 """ Bound change."""
10391059
@@ -2074,38 +2094,28 @@ cdef class Model:
20742094 n = str_conversion(problemName)
20752095 PY_SCIP_CALL(SCIPcreateProbBasic(self ._scip, n))
20762096
2077- def copyModel (self , problemName = ' copy_model ' ):
2097+ def addOrigVarsConssObjectiveFrom (self , Model source ):
20782098 """
2079- Create a copy of the model/problem .
2099+ add original variables and constraints from source model.
20802100
20812101 Parameters
20822102 ----------
2083- problemName : str, optional
2084- name of model or problem (Default value = 'model')
2085-
2103+ source : Model
2104+ source model copy original variables and constraints to target(self) model
20862105 """
2087- cdef Model cpy
20882106 cdef SCIP_Bool valid
20892107 cdef SCIP_HASHMAP* localvarmap
20902108 cdef SCIP_HASHMAP* localconsmap
20912109
2092- cpy = Model(createscip = False )
2093- PY_SCIP_CALL(SCIPcreate(& cpy._scip))
2094- cpy._bestSol = None
2095- cpy.includeDefaultPlugins()
2096- # cpy._bestSol = <Solution> model._bestSol
2097- cname = str_conversion(problemName)
2110+ PY_SCIP_CALL( SCIPhashmapCreate(& localvarmap, SCIPblkmem(self ._scip), SCIPgetNVars(source._scip)) )
2111+ PY_SCIP_CALL( SCIPhashmapCreate(& localconsmap, SCIPblkmem(self ._scip), SCIPgetNConss(source._scip)) )
20982112
2099- PY_SCIP_CALL( SCIPhashmapCreate(& localvarmap, SCIPblkmem(cpy._scip), SCIPgetNVars(self ._scip)) )
2100- PY_SCIP_CALL( SCIPhashmapCreate(& localconsmap, SCIPblkmem(cpy._scip), SCIPgetNConss(self ._scip)) )
2101-
2102- PY_SCIP_CALL(SCIPcopyOrigProb(self ._scip, cpy._scip, localvarmap, localconsmap, cname))
2103- PY_SCIP_CALL(SCIPcopyOrigVars(self ._scip, cpy._scip, localvarmap, localconsmap, NULL , NULL , 0 ))
2104- PY_SCIP_CALL(SCIPcopyOrigConss(self ._scip, cpy._scip, localvarmap, localconsmap, False , & valid))
2113+ PY_SCIP_CALL(SCIPcopyOrigVars(source._scip, self ._scip, localvarmap, localconsmap, NULL , NULL , 0 ))
2114+ PY_SCIP_CALL(SCIPcopyOrigConss(source._scip, self ._scip, localvarmap, localconsmap, False , & valid))
2115+ PY_SCIP_CALL(SCIPsetObjsense(self ._scip, SCIPgetObjsense(source._scip)))
21052116
21062117 SCIPhashmapFree(& localvarmap)
21072118 SCIPhashmapFree(& localconsmap)
2108- return cpy
21092119
21102120 def freeProb (self ):
21112121 """ Frees problem and solution process data."""
@@ -6227,36 +6237,6 @@ cdef class Model:
62276237 PY_SCIP_CALL(rc)
62286238 self ._bestSol = Solution.create(self ._scip, SCIPgetBestSol(self ._scip))
62296239
6230- @staticmethod
6231- def solveFirstInterruptOthers (executor: ThreadPoolExecutor , models: Sequence[Model]) -> Tuple[int , Model]:
6232- """
6233- Solve models return the first solved model and interrupt other models.
6234-
6235- Parameters
6236- ----------
6237- executor : ThreadPoolExecutor
6238- models: Sequence[Model]
6239-
6240- Returns
6241- -------
6242- first_seq : int
6243- returns the index of the first resolved model from models
6244- models[first_idx] : Model
6245- returns the first resolved model
6246- """
6247- futures = [executor.submit(Model.optimizeNogil, model) for model in models]
6248- interrupt_callback = lambda model : model.interruptSolve()
6249- for future in as_completed(futures ):
6250- first_future = future
6251- break
6252- for idx, furture_to_cancel in enumerate (futures):
6253- if furture_to_cancel != first_future:
6254- interrupt_callback(models[idx])
6255- furture_to_cancel.cancel()
6256- else :
6257- first_idx = idx
6258- return first_idx, models[first_idx]
6259-
62606240 def solveConcurrent (self ):
62616241 """ Transforms, presolves, and solves problem using additional solvers which emphasize on
62626242 finding solutions.
@@ -8121,23 +8101,6 @@ cdef class Model:
81218101 PY_SCIP_CALL(SCIPaddSol(self ._scip, solution.sol, & stored))
81228102 return stored
81238103
8124- def addCopyModelSol (self , Solution solution ):
8125- if solution.getSolOrigin() != SCIP_SOLORIGIN_ORIGINAL:
8126- PY_SCIP_CALL(SCIPretransformSol(solution.scip, solution.sol))
8127- cdef Solution newsol = Solution.create(self ._scip, NULL )
8128- cdef SCIP_VAR** subvars = SCIPgetOrigVars(solution.scip)
8129- SCIPtranslateSubSol(self ._scip, solution.scip, solution.sol, NULL , subvars, & (newsol.sol))
8130- self .addSol(newsol, free = True )
8131-
8132- def addCopyModelBestSol (self , Model cpy_model ):
8133- solution = cpy_model.getBestSol()
8134- self .addCopyModelSol(solution)
8135-
8136- def addCopyModelSols (self , Model cpy_model ):
8137- solutions = cpy_model.getSols()
8138- for solution in solutions:
8139- self .addCopyModelSol(solution)
8140-
81418104 def freeSol (self , Solution solution ):
81428105 """
81438106 Free given solution
0 commit comments