While working on my project, I need to change my MIP-model with a callback function. I followed the documentation closely, but I get always an error message.
I believe, the error comes with the cbGet-function, without this, I get an output, although I don't know whether it's correct.
Without the callback function, my model works just fine.
Here's my code:
import gurobipy as gb
...
some code with the model
...
def cb(mod, where):
if where == gb.GRB.Callback.MIP:
print(mod.cbGet(gb.GRB.Callback.MIP_NODCNT))
def cb2(mod, where):
if where == gb.GRB.Callback.MIP:
print(gb.GRB.Callback.MIP_NODCNT)
m.optimize(cb(m, gb.GRB.Callback.MIP))
Please note that if I use cb2
as callback function, I get the exit code 0 and an output. I'm not sure whether this output is right.
But when I'm using cb
as callback function, I get the error message
Traceback (most recent call last):
File "C:/Users/dbigb/PycharmProjects/MA/MIP_model.py", line 39, in <module> m.optimize(cb(m, gb.GRB.Callback.MIP))
File "C:/Users/dbigb/PycharmProjects/MA/MIP_model.py", line 32, in cb print(mod.cbGet(gb.GRB.Callback.MIP_NODCNT))
File "model.pxi", line 4136, in gurobipy.Model.cbGet
AttributeError: 'NoneType' object has no attribute '_cbdata'
I don't understand which attribute is none, neither what this _cbdata
is or where I could find and check this. Any help would be appreciated.
I didn't include the whole model, since it's working fine. If it's important for the solution I will edit the question and add the code.
You should call optimize
as follows:
m.optimize(cb)
The optimize
method expects an argument of type function.
In your code, cb(m, gb.GRB.Callback.MIP)
would be evaluated (and become a constant) before starting the optimization.
In the cb2
case, this simply prints 3002
, i.e., the integer representation of gb.GRB.Callback.MIP_NODCNT
. (See here.) The argument of optimize
evaluates to None
.
In the cb
case, the call to cbGet
tries to access the model's callback, which is not defined. This is the NoneType
in the error message.