Custom termination criteria can be set within callbacks in order to avoid calling optimize()
several times. The following example uses the Python API to implement a "soft" time limit that is only valid under certain conditions, such as a found solution that is "good enough". The TimeLimit parameter forces the optimization to terminate after 100 seconds. In addition, when the MIP gap is smaller than 50% and at least 5 seconds have passed, the optimization will terminate.
import gurobipy as gp
from gurobipy import GRB
softlimit = 5
hardlimit = 100
def softtime(model, where):
if where == GRB.Callback.MIP:
runtime = model.cbGet(GRB.Callback.RUNTIME)
objbst = model.cbGet(GRB.Callback.MIP_OBJBST)
objbnd = model.cbGet(GRB.Callback.MIP_OBJBND)
gap = abs(objbst - objbnd) / abs(objbst)
if runtime > softlimit and gap < 0.5:
model.terminate()
model.setParam('TimeLimit', hardlimit)
model.optimize(softtime)
Comments
0 comments
Article is closed for comments.