Skip to main content

Behavior of the lazy constraints added within a callback

Answered

Comments

8 comments

  • Maliheh Aramon
    • Gurobi Staff

    Hi Jose, 

    You can read more about two different approaches to implement lazy constraints in the article “How do I implement lazy Constraints in Gurobi?”. As soon as an already feasible solution is violated by a lazy constraint, the solution is discarded, and the lazy constraint is added to the LP relaxation.

    Gurobi does not allow you to access the lazy constraints that were added to the model via a callback. If you are using a callback function to generate lazy constraints, you can track and store them as you generate them. However, you cannot remove them from the model afterward. Therefore, if a later step in your procedure requires starting from a model without lazy constraints, you must either reload the original model from a file or keep a copy (using copy(targetenv=None)) of the original model before adding lazy constraints. 

    I saw your response to a similar post, and I understand that reading from a file or keeping a copy is not ideal. However, there is currently no way to remove/update lazy constraints already added to the model via a callback function. You can store them as explained above, but not access and change them via the model object.

    Best regards,

    Maliheh

     

    0
  • Jose Walteros
    • Conversationalist
    • Gurobi-versary
    • First Question

    Thank you, Maliheh. Could you please confirm whether the pool of lazy cuts added during the callback is reset every time model.optimize() is called. I would like to know whether these constraints persist in the lazy pool each time the model is re-optimized.    

    Best, 

    --Jose

     

    0
  • Maliheh Aramon
    • Gurobi Staff

    If the model object remains the same and is not disposed of, yes the lazy constraints persist if you recall model.optimize(cb). As an example, if you initially call model.optimize(cb) with TimeLimit=20. After the time limit is reached and the problem is not solved to optimality, you can change the TimeLimit parameter and call model.optimize(cb) again. You should then see the message Continuing optimization… in the log.

    Best regards,

    Maliheh

    0
  • Jose Walteros
    • Conversationalist
    • Gurobi-versary
    • First Question

    Hi Maliheh, 

    I am sorry, I need one more clarification. What do you mean by “If the model object remains the same”? Is there anything that one can do to the model to make the lazy constraints not persist?

    --Jose

    0
  • Maliheh Aramon
    • Gurobi Staff

    Sorry for the confusion. I was not sure why you needed to call the optimize() method again, and was just trying to make sure you meant calling it on the same model object again.

    0
  • Jose Walteros
    • Conversationalist
    • Gurobi-versary
    • First Question

    That is what I meant. But, based on your last comment, does it mean that if the object model changes, say by adding a new constraint, or something like that, does the pool get reset? What needs to change in the model for such a reset to occur?
     

    0
  • Jose Walteros
    • Conversationalist
    • Gurobi-versary
    • First Question

    Hi Maliheh, 

    Pardon the insistence on this matter. After some experimentation, it seems that calling model.update() resets the lazy pool. I am not 100% sure if this is true, though. Could you please confirm this?

    -Jose

    0
  • Maliheh Aramon
    • Gurobi Staff

    Hi Jose, 

    Yes, you are right. If you modify the model, for example, by adding a new constraint or variable, or by changing coefficients, the lazy constraints generated in the callback are discarded as they live in the internal solving data structures. So calling model.reset() or modifying the model should remove the lazy constraints.

    Best regards,

    Maliheh

    0

Please sign in to leave a comment.