Skip to main content

Lazy Constraints / TSP

Answered

Comments

6 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Roberval,

    I think that the tsp.py example implements exactly what you are looking for.

    Detail, I know there is a very good model with lazy constraints, but I need a slower model to do some analysis in my PHD.

    You could try increasing the size of the model to "slow it down".

    Best regards, 
    Jaromił

    1
  • Roberval Gonçalves Moreira Filho
    First Comment
    First Question

    Thanks, I went on to add other constraints that aren't part of the classic model to get what I need.

    I don't understand why I can't add this C3 in a callback like this:

    def subTour(model, where):
        if where == gp.GRB.Callback.MIPSOL:
          model.cbLazy(u[i] - u[j] + tam * x[i, j] <= tam-1 
                              for i in m 
                              for j in n 
                            if (i != j))

    This error appears:

    TypeError                                 Traceback (most recent call last)
    
    src/gurobipy/callback.pxi in gurobipy.CallbackClass.callback()
    
    
    <ipython-input-6-2e1575e521d5> in subtourelim(model, where)
          5     if where == gp.GRB.Callback.MIPSOL:
          6         model.cbLazy(u[i] - u[j] + tam * x[i, j] <= tam-1 
    ----> 7                           for i in m
          8                           for j in n
          9                           if (i != j))
    
    
    src/gurobipy/model.pxi in gurobipy.Model.cbLazy()
    
    
    TypeError: unsupported operand type(s) for -: 'generator' and 'NoneType'
    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    What is the error you get?

    Note that you can't just access the model's variables and other parameters from a callback. You have to save the variables in a private object just as is done in the tsp.py example with \(\texttt{model._vars}\).

    1
  • Where can I read about private object in gurobipy?

    i read this:

    https://www.gurobi.com/documentation/9.5/refman/lazyconstraints.html

    https://www.gurobi.com/documentation/9.5/refman/py_model_cblazy.html

    and this:

    https://support.gurobi.com/hc/en-us/articles/360013197972-How-do-I-implement-lazy-constraints-in-Gurobi-

    And I didn't know that, can you give me an example of how to do it or the documentation about the private objects?

     

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    And I didn't know that, can you give me an example of how to do it or the documentation about the private objects?

    There is no Gurobi documentation about this, because it is a feature of the Python language. Having a closer look at the tsp.py example should be helpful.

    The error

    TypeError: unsupported operand type(s) for -: 'generator' and 'NoneType'

    indicates that one of the used object is uninitialized, i.e., \(\texttt{None}\). If after re-writing of your model with private objects as presented in the tsp.py example you still run into errors, please provide a minimal reproducible example describing the issue.

    1
  • Hi,
    Thanks for everything.
    I was successful in my project and I believe I understood enough for future implementations ^^

    0

Please sign in to leave a comment.