insert heuristic into CallBack
AnsweredGood morning,
I would like to insert heuristics in Gurobi to solve a certain problem.
I'm using the callback function. However I have a doubt at the moment that I'm giving a solution.
I use the condition,
if (where == GRB_CB_MIPNODE) {}
to pass a heuristic solution to the gurobi.
My question is:
Should I pass the value of all variables using setSolution()?
or can i just pass the values of some variables?
Because in some cases it's working when I pass only part of the variables of a solution. But in some cases the gurobi returns an infeasible solution when I parse the getSolution() function inside the callBack with the condition
if (where == GRB_CB_MIPSOL) {}
A second question is whether within this condition
if (where == GRB_CB_MIPSOL) {} does the getSolution() function always return a viable solution ?
Thanks for listening

Hi Murilo,
My question is:
Should I pass the value of all variables using setSolution()?
or can i just pass the values of some variables?You have probably already checked the documentation of Model.cbSetSolution(). You do not need to pass the values for all variables. Gurobi will try to find a feasible solution from the partial values identified. You can also use the method Model.cbUseSolution() such that Gurobi tries to immediately find a heuristic solution.
A second question is whether within this condition if (where == GRB_CB_MIPSOL) {} does the getSolution() function always return a viable solution?
The MIPSOL callback is only invoked if a new incumbent solution is found, so Model.cbGetSolution() always returns a viable solution.
You mentioned that:But in some cases the gurobi returns an infeasible solution when I parse the getSolution() function inside the callBack
The solution is infeasible with respect to what? How are you checking the infeasibility?
Best regards,
Maliheh1 
Hi Maliheh Aramon,
When I say an unfeasible solution, I mean that the solution does not respect all restrictions.
Example: a TSP solution repeating cities.
Another question arose,
model.cbGetSolution() in python is equivalent to setSolution() in C++ ?
Thank you very much for the answers.
0 
Hi Murilo,
When I say an unfeasible solution, I mean that the solution does not respect all restrictions.
Sure, I get this. My point is MIPSOL callback is invoked only if a new incumbent solution is found. An incumbent solution is always feasible with respect to all constraints in the model. If you see otherwise, the issue is likely somewhere else. Using the TSP example that you just referred to, check that the constraint for visiting each city exactly once is modelled correctly. If things are not still clear, it would be great if you could post a minimal reproducible example.
Another question arose,
model.cbGetSolution() in python is equivalent to setSolution() in C++ ?No, the methods for getting and setting solutions in the C++ API are GRBCallback::getSolution() and GRBCallback::setSolution(). The corresponding methods in the Python API are Model.cbGetSolution() and Model.cbSetSolution().
Best regards,
Maliheh
0 
Hi Maliheh,
Thanks for the answers,
was very clear.
0
Please sign in to leave a comment.
Comments
4 comments