callbackpyomogurobi

How to terminate gurobi callback using pyomo?


I am using pyomo and its 'gurobi_persistent'-interface to solve a MIP. I was trying to implement a callback, that checks for a sufficiently small optimality-gap and will terminate, if it is reached. Of course, this must happen in a way, where the current solution will be read back to the pyomo model. I am not sure how to do that. When the stop condition is fulfilled, I get

AttributeError: 'GurobiPersistent' object has no attribute 'terminate'

Typically, in gurobipy, the statement model.terminate() should work. Also see my callback definition below.

solver = pe.SolverFactory('gurobi_persistent')
solver.set_instance(m)

def my_callback(cb_m, cb_opt, cb_where):
    if cb_where == GRB.Callback.MIP:
        # General MIP callback
        objbst = cb_opt.cbGet(GRB.Callback.MIP_OBJBST)
        objbnd = cb_opt.cbGet(GRB.Callback.MIP_OBJBND)
        if abs(objbst - objbnd) < percentGap * (1.0 + abs(objbst)):
            print('Stop early - {} % gap achieved'.format(percentGap*100))
            # statement that does not exist
            cb_opt.terminate()
solver.set_callback(my_callback)
solver.solve(tee=True)

I would be grateful for some hints on how to achieve the desired behavior. Have a good day!


Solution

  • From checking the Pyomo docs on gurobi_direct and gurobi_persistent it seems like this is simply not possible. You would need to implement your callback using the Gurobi Python API.

    Another way of implementing a certain MIPGap is to simply set this gap as a termination value.