With Gurobi 9 it is not possible to change parameters in a callback. One way around this is to terminate the optimization process in the callback, change the parameter outside and then continue the optimization where it was interrupted.
This is an example that changes the MIPGap after 30 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 = 30 new_gap = 0.4 m = gp.read('C:\gurobi900\win64\examples\data\glass4.mps') m._changeParam = False m.optimize(my_callback) if m._changeParam: m.params.MIPGap = new_gap m.optimize()
Gurobi 8 does not throw an error when you try to set a parameter in a callback, but it is strongly discouraged to do this, because it may lead to undefined behavior.