Extracting Runtime for Root Node
AnsweredDear Gurobi Team,
I am trying to retrieve and print the runtime for solving the root node. Unfortunately my model does not print any value in the end. Is there any mistake in my code? I already checked older posts, but cannot find my mistake.
Thank you very much!
Best,
Elina
def mycallback(model, where):
if where == GRB.Callback.MIPNODE:
depth = model.cbGet(GRB.Callback.MIPNODE_NODCNT)
if depth == 0:
if model.cbGet(GRB.Callback.MIPNODE_STATUS) == GRB.Status.OPTIMAL:
print("Root relaxation runtime is", model.Runtime)
m.optimize(mycallback)
Output:
And I would like to print out this value:
-
Hi Elina,
The correct syntax to obtain the runtime in a callback is:runtime = model.cbGet(GRB.Callback.RUNTIME)
As shown in the Callback Codes page, this attribute is available everywhere except when \(\texttt{where}=\texttt{GRB.Callback.POLLING}\).
Cheers,
David0 -
Hey David,
thank you very much! This worked perfectly. Is it now possible to print those numbers also "in the end" to extract those numbers and compare it with different optimizations?
Best,
Elina
0 -
Hi Elina,
Glad it worked.
Of course, from the Python Callbacks section:If you would like to pass data to your callback function, you can do so through the Model object. For example, if your program includes the statement
model._value = 1
before the optimization begins, then your callback function can query the value ofmodel._value
. Note that the name of the user data field must begin with an underscore.So, in your case, you can set \(\texttt{model._runtime}=0\) at the beginning, update that attribute in the callback, then finally query it at the end of the optimisation.
Cheers,
David0
Please sign in to leave a comment.
Comments
3 comments