Can I add conditional statements in Gurobi - Python?
AnsweredI have a constrain that changes whether i = 1 or i != 1
if t==1:
m.addConstr(i_pit[p,i,t] == Ipi0[p][i] + other terms)
else:
m.addConstr(i_pit[p,i,t] == i_pit[p,i,t-1] + same other terms)
Is there a way to condense everything into same line?
m.addConstr(i_pit[p,i,t] == (Ipi0[p][i] if t=1) + (i_pit[p,i,t-1] if t!=1)+ same other terms)
Thanks in advance,
Eduardo.
0
-
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Eduardo,
I don't know whether there is a way to condense everything into one line and still keep it readable.
I would propose a 2 liner
m.addConstr(i_pit[p,i,1] == Ipi0[p][i] + other terms)
m.addConstrs(i_pit[p,i,t] == i_pit[p,i,t-1] + same other terms for t in range(T) if t != 1)Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments