Help with python callback -- adding callback on different line than optimize()
AnsweredI am using a cut callback in some python code. Normally, I would follow the example set in the tsp.py code, which does the following:
m.optimize(subtourelim)
You can see that it passes the callback "subtourelim" to optimize(). For code organization reasons, I would like to separate these two things, similar to the tsp.cpp example:
// Set callback function
subtourelim cb = subtourelim(vars, n);
model.setCallback(&cb);
// Optimize model
model.optimize();
Can this be done in python?
0
-
Official comment
This post is more than three 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 why not try our AI Gurobot?. -
The only way to set a callback function in the Python API is by passing the function object as an argument to Model.optimize().
The default argument of Model.optimize() is \( \texttt{None} \), which tells Gurobi there is no callback function. So you could do something like this:
m._cb = subtourelim # or None
m.optimize(m._cb)1
Post is closed for comments.
Comments
2 comments