Skip to main content

conditionally apply constraints in optimization problems

Answered

Comments

5 comments

  • Gwyneth Butera
    • Gurobi Staff

    Have you seen our Knowledge Base article How do I model conditional statements in Gurobi? This may be useful to you.

    0
  • Alvaro Torres
    • Gurobi-versary
    • First Comment
    • First Question

    Thanks for the suggestion! I did read that one previously, and all the documentation i could find in gurobi, but it showed a result which differs with the one i need in the sense that:

    1- it is continuous rather than discrete values

    2- the condition compares the value of two variables rather than comparing a single variable to a threshold value.

    Tryed to apply it to my code but i couldnt make it work, im sure the result is close to what that one article promotes, but im having a hard time adapting it to my case.

     

     

    0
  • Alvaro Torres
    • Gurobi-versary
    • First Comment
    • First Question

    this is how i applyed it:

    eps = 0.0001
    M = 5 + eps

    xb = m.addVar(vtype=gp.GRB.BINARY)
    m.addConstr(x[2] >= 3 + eps - M * (1 - xb))
    m.addConstr(x[2] <= 3 + M * xb)

    m.addConstr((xb == 1) >> (x[1] >= 4))
    0
  • Alvaro Torres
    • Gurobi-versary
    • First Comment
    • First Question

    ok im so sorry for the dumb question, found out all i had to do is take out the 3 in the second constraint and substitute by a 2 since both statements were triggered with a x[2] == 3, thank you so much for the help, have a great day.

    0
  • Mario Ruthmair
    • Gurobi Staff

    Since your x[2] variable is integer, you do not need the eps value in the first constraint, it might even lead to numerical issues (or forbid x[2] == 3 in some cases).

    0

Please sign in to leave a comment.