Skip to main content

Python:How to create a constraint which includes a variable and a kind of index(two indices simultaneous)

Answered

Comments

4 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Jaromił Najman
    • Gurobi Staff

    Hi,

    you could use integers as indices, i.e.,

    #index

    J=[int(i)+1 for i in range(21)]

    #code

    b=len(J)-1
    c=0
    for j in J:
    c+=1
    if c<=b:
    m.addConstr((F[j+1]<=F[j]) ,'c'+str(j)

    Please note that you have to use addConstr instead of addConstrs.

    Best regards,
    Jaromił

    1
  • Ching Chang
    • Gurobi-versary
    • First Comment
    • First Question

    Dear Jaromił

    Thank you for solving my problem.

    B.R.
     
     
     
     
     
     
    1
  • Jaromił Najman
    • Gurobi Staff

    Hi,

    since you are using Python, you could make your code even shorter

    J = range(1,22)
    m.addConstrs( (F[j+1] <= F[j] for j in J[:-1]), name = 'c' )

    Best regards,
    Jaromił

    1

Post is closed for comments.