如何将 PySCIPOpt 模型的解决方案复制到另一个模型?
How to copy a solution of a PySCIPOpt model to another model?
给定 pyscipopt.Model
及其 Solution
,如何将其作为原始启发式传递给另一个模型?
我目前正在通过 writeSol()
将解决方案写入文件,然后调用 readSolFile()
和 addSol()
。应该有更清洁的方法。
这在一定程度上取决于您的两个模型的结构。如果它们具有相同顺序的相同变量(这很可能来自您所写的内容),那么您只需在模型中创建一个新解决方案并复制所有值,即:
variables = othermodel.getVars()
newvariables = model.getVars()
nvars = othermodel.getNVars()
newsol = self.model.createSol(self)
for i in range(nvars):
newsol[newvariables[i]] = othermodel.getSolVal(oldsol, variables[i])
self.model.trySol(newsol)
让我知道这项工作是否有效
给定 pyscipopt.Model
及其 Solution
,如何将其作为原始启发式传递给另一个模型?
我目前正在通过 writeSol()
将解决方案写入文件,然后调用 readSolFile()
和 addSol()
。应该有更清洁的方法。
这在一定程度上取决于您的两个模型的结构。如果它们具有相同顺序的相同变量(这很可能来自您所写的内容),那么您只需在模型中创建一个新解决方案并复制所有值,即:
variables = othermodel.getVars()
newvariables = model.getVars()
nvars = othermodel.getNVars()
newsol = self.model.createSol(self)
for i in range(nvars):
newsol[newvariables[i]] = othermodel.getSolVal(oldsol, variables[i])
self.model.trySol(newsol)
让我知道这项工作是否有效