Skip to main content

insert heuristic into CallBack

Answered

Comments

4 comments

  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    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,
    Maliheh

    1
  • Murilo Machado
    First Question
    Conversationalist

    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
  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    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
  • Murilo Machado
    First Question
    Conversationalist

    Hi Maliheh,

     

    Thanks for the answers,

     

    was very clear.

    0

Please sign in to leave a comment.

Powered by Zendesk