Skip to main content

a Variable which is the upper bound of a sum

Answered

Comments

3 comments

  • Richard Oberdieck
    Gurobi Staff Gurobi Staff

    Hi,

    I would model this using auxiliary variables /(y_i/):

    M = m.addVar(vtype=GRB.INTEGER,lb=1,ub=4)
    y_i = m.addVars(4, vtype=GRB.BINARY, name='y')

    1-sum(P_i*y_i) # The constraint you want
    m.addConstrs(i*y_i <= M for i in range(4)) # This forces y_i to be 1 only if M reaches it

    Is this what you are looking for?

    0
  • ibtissam labriji
    Gurobi-versary
    Curious
    Conversationalist

    Hello Richard, 

    Thank you for your response. Yes it does the trick. Although, I think that : the range should go to 5. So that y_4 can be taken into consideration : 

    m.addConstrs(i*y_i <= M for i in range(5))

    Please, correct me if I am wrong.

    Regards, 

    Ibtissam

    0
  • Richard Oberdieck
    Gurobi Staff Gurobi Staff

    Yes, you are right, it actually should be

    m.addConstrs(i*y_i <= M for i in range(1,5))

    since y_0 does not exist.

    0

Please sign in to leave a comment.