As of Gurobi version 12.0.0, a number of parameters that control the termination criterion of different algorithms in Gurobi Optimizer can be modified within the callback. The list of parameters includes TimeLimit, WorkLimit, NodeLimit, BarIterLimit, and PumpPasse. Modifying these parameters within the callback allows for custom termination criteria implementation (see Callback settable parameters for more information).
Modifying parameters not included in the above list is not possible within the callback. Instead, you can follow the procedure below:
- terminating the optimization process in the callback,
- changing the parameter value, and then
- continuing the optimization from where it was interrupted.
The procedure above should be followed for changing any parameters within the callback for Gurobi versions < 12.0.0.
An example that changes the MIPGap after 4 seconds:
import gurobipy as gp
from gurobipy import GRB
def my_callback(model, where):
if where == GRB.Callback.MIP:
run_time = model.cbGet(GRB.Callback.RUNTIME)
mip_gap = model.params.MIPGap
if run_time > time_of_change and mip_gap != new_gap:
model._changeParam = True
model.terminate()
time_of_change = 4
new_gap = 0.2
m = gp.read('glass4.mps')
m._changeParam = False
m.optimize(my_callback)
if m._changeParam:
m.params.MIPGap = new_gap
m.optimize()
Note that some parameters influence the initialization of some data structures and cannot be changed when optimization is already in progress. For example changing the parameters PoolSearchMode or PoolSolutions during optimization (i.e., when only terminating the optimization process) has no effect. To effectively alter these parameters model.reset() is necessary. A reset of the model is automatically triggered if the model was changed, e.g. by changing coefficients or adding new constraints.
An update of the Threads parameter during optimization is possible with Gurobi Version 11 but has no effect with an earlier version.
Comments
0 comments
Article is closed for comments.