Python:How to create a constraint which includes a variable and a kind of index(two indices simultaneous)
AnsweredI would like to write the constraint code as the content of picture. 
#index
J=['j1','j2','j3','j4','j5','j6','j7','j8','j9','j10','j11','j12','j13','j14','j15','j16','j17','j18','j19','j20', 'j21']
#variable of decision
F= m.addVars(J , vtype=GRB.BINARY,name="Fj")
#code
b=len(J)-1
c=0
for j in J:
c+=1
if c<=b:
m.addConstrs((F[j+1]<=F[j]) ,'c13')
#error report

I also have tried to create a letter to instead "j+1" but obviously the code shouldn't have two kinds of index.
HELP ME PLEASE
-
Official comment
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?. -
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 -
Dear Jaromił
Thank you for solving my problem.
B.R.1 -
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.
Comments
4 comments