Skip to main content

how to remove and add back a constraint?

Answered

Comments

7 comments

  • Greg Glockner
    Gurobi Staff Gurobi Staff

    Gurobi does lazy updates to the model. You won't see that this constraint was deleted until the update is processed. To force the update to process immediately, call model.update().

    0
  • Mandar U
    Gurobi-versary
    First Comment
    First Question

    I understand. My question is, how can I add this constraint back to the model? 

    0
  • Greg Glockner
    Gurobi Staff Gurobi Staff

    If you want to remove a constraint and add it later, you need to get the elements of a constraint. For a linear constraint, it looks like this:

    c = m.getConstrs()[0] # for example
    lhs, sense, rhs, name = m.getRow(c), c.Sense, c.RHS, c.ConstrName
    m.remove(c)

    # Optional, to show the current state
    m.update()
    m.printStats()

    # Add the constraint
    c = m.addConstr(lhs, sense, rhs, name)

    # Also optional, to show the current state
    m.update()
    m.printStats()
    2
  • Jia-Kai Kuo
    Gurobi-versary
    First Comment
    First Question

    That's helpful, thanks.

    But if I want to add constraint back in a .NET environment, how can I do to get the lhs of the constraint c?
    I found that there's no attribute 'LHS' in a constraint of .NET environment so I don't know how to do that....

    Thanks!

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi,

    You can get the LHS by using the getRow() function.

    Best regards,
    Jaromił

    1
  • Fei Wang
    Gurobi-versary
    Conversationalist
    First Question

    Hello,

    It seems

    c = m.addConstr(lhs, sense, rhs, name) 

    is no longer supported in the newest version of gurobi. What should I do to add back a constraint in this case?

     

    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Fei,

    Model.addLConstr should solve your problem.

    - Riley

    0

Please sign in to leave a comment.