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!
-
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
Please sign in to leave a comment.
Comments
2 comments