Can I print values of integer variable during optimization?
AnsweredCurrently in my model, I have an integer variable, k. I can obviously print the value of k once the optimization is "complete". However, my model is fairly complex and never seems to converge. I've noticed that stopping the optimization early gives decent results for my use-case. It would be nice to query the variable k during the optimization to know when stopping the optimization early makes sense and when I should keep it running. Is there anyway to achieve this? Thanks!
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
You can use callbacks.
In particular, you can use the MIPSOL callback to get the latest found incumbent via the cbGetSolution method.
Let's assume that you are interested in the value of a particular variable \(x_{\text{important}}\).
def mycallback(model, where):
if where == GRB.Callback.MIPSOL:
print(model.cbGetSolution(model._importantvars))
# [...]
model = gp.Model()
x_important = model.addVar(...)
# [...]
model._importantvars = [x_important] # you can also add more variables of interest to the list
model.optimize(mycallback)Best regards,
Jaromił0 -
Thank you! This was really helpful!
Rahul
0
Post is closed for comments.
Comments
3 comments