Error when using mycallback function with a new model
AnsweredHello community,
I want to find a feasible solution to my model when one of the two conditions of the "mycallback" function is satisfied. callback.py - Gurobi Optimization
1) The optimality gap is less than 10%
2) At least 10000 nodes have been explored, and an integer feasible solution has been found.
Unlike the code in the online example, I am not reading my model from a file, but instead I am implementing my model by declaring the variables, constraints, the objective function.
When I run the code I get an error here:
IndexError Traceback (most recent call last)
File src\gurobipy\callback.pxi:209, in gurobipy.CallbackClass.callback()
Cell In [1], line 63, in mycallback(model, where)
61 if model.cbGet(GRB.Callback.MIPNODE_STATUS) == GRB.OPTIMAL:
62 x = model.cbGetNodeRel(model._vars)
---> 63 model.cbSetSolution(model.getVars(), x)
64 elif where == GRB.Callback.BARRIER:
65 # Barrier callback
66 itcnt = model.cbGet(GRB.Callback.BARRIER_ITRCNT)
File src\gurobipy\model.pxi:6566, in gurobipy.Model.cbSetSolution() IndexError: list index out of range
Please could someone explain me how to fix this error.
-
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. -
Hi Libia,
Could you provide a minimal reproducible example?
Depending on what you are trying to achieve, it is possible that the number of variables changed in between calls such that the \(\texttt{model._vars}\) object does not longer fit the \(\texttt{model.getVars}\) call. You might want to try
x = model.cbGetNodeRel(model._vars)
model.cbSetSolution(model._vars, x)However, without an example of your executed code, it is hard to help here.
Please note that only specific callback functions are meant to be safely callable from within a callback. Thus, using \(\texttt{model.getVars()}\) in this case is not good. We are aware that this is part of our callback.py example and will be adjusted in an upcoming release.
Best regards,
Jaromił0 -
Thanks Jaromil, I will try your advice.
0
Post is closed for comments.
Comments
3 comments