How to write if condition in python gurobi 9.5?
Answeredits my first time using gurobi. I am trying to implement the following code via if statement however i keep getting errors .
m.addConstrs(p2g[t]==0 for t in range(A) if pv_generation[t]<=load[t] pfor t in range(A) )
m.addConstrs(p2g[t]==p2g[t] for t in range(A) if pv_generation[t]>load[t] for t in range(A))
I tried indicator constraint and got this error (unsupported operand type(s) for -=: 'NoneType' and 'float') . I guess the indicator constraint doesn't work with multi-constraint (m.addConstrs), but only works with (m.addConstr)
Please can you provide me with and examples of using if statement in gurobi 9.5
-
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. -
\(\texttt{if}\)-statements cannot be directly implemented into Gurobi when using optimization variables. Thus, one has to model an \(\texttt{if}\)-condition with the help of binary variables and additional constraints. I think that the KB article How do I model conditional statements in Gurobi? is what you are looking for.
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments