How to copy a model when designing primal heuristics with pyscipopt?

49 Views Asked by At

I'm trying to design my own heuristics with pyscipopt. In my heuristics, a sub-mip is needed, so I try to copy the current model in this way:

from pyscipopt import Model, Heur, SCIP_RESULT, SCIP_PARAMSETTING, SCIP_HEURTIMING

class MyHeur(Heur):
    
    def __init__(self):
        super().__init__()
        
    def heurexec(self, heurtiming, nodeinfeasible):
        submip = Model('submip', sourceModel = self.model)
        submip.optimize()

def test_heur():
    s = Model()
    heuristic = MyHeur()
    s.includeHeur(heuristic, "PyHeur", "custom heuristic implemented in python", "Y", timingmask=SCIP_HEURTIMING.BEFORENODE)
    s.setPresolve(SCIP_PARAMSETTING.OFF)
    x = s.addVar("x", obj=1.0)
    y = s.addVar("y", obj=2.0)
    s.addCons(x + 2*y >= 5)

    # solve problem
    s.optimize()
    

if __name__ == "__main__":
    test_heur()

But then comes the error :

TypeError                                 Traceback (most recent call last)
Cell In[5], line 9, in MyHeur.heurexec(self, heurtiming, nodeinfeasible)
      8 def heurexec(self, heurtiming, nodeinfeasible):
----> 9     submip = Model('submip', sourceModel = self.model)

TypeError: Argument 'sourceModel' has incorrect type (expected pyscipopt.scip.Model, got weakproxy)

What should I do? Is there any method to input real model into the member function?

0

There are 0 best solutions below