Changing parameters in a callback is not possible in Gurobi.
Instead, parameters can be updated by:
- terminating the optimization process in the callback,
- changing the parameter value, and then
- continuing the optimization from where it was interrupted.
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: Gurobi v8 does not produce an error when you try to set a parameter in a callback. However, doing so may lead to undefined behavior and is strongly discouraged.
Comments
0 comments
Please sign in to leave a comment.