Add constraint in GurobiPy using conditional decision variable
回答済みHello,
I am new to optimisation and have a fairly basic query.
I have a model with two decision variables
x and y that vary in time. I'd like to add a conditional constraint on y at time t depending upon x[t-1], such that I've implemented the following code:
for t in model.timesteps:
if t>1:
if model.x[t-1] <= 1:
model.addConstr(model.y[t] >= 100)
elif model.x[t-1] <= 0.5:
model.addConstr(model.y[t] >= 50)
elif model.x[t-1] <= 0.3:
model.addConstr(model.y[t] >= 20)
However, the above code produces the error:
File "tempconstr.pxi", line 44, in gurobipy.TempConstr.bool
gurobipy.GurobiError: Constraint has no bool value (are you trying "lb <= expr <= ub"?)
Having done a little reading on previous related queries on this page, I believe I might need to use a binary indicator variable in order to implement the above. However, I'm not certain as to whether this would solve the above issue.
Could anyone point me in the right direction here please?
Many thanks in advance!
0
-
正式なコメント
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 Aman,
The error occurs because \(\texttt{model.x[t-1]}\) is a variable object and thus, cannot be evaluated to a value in the \(\texttt{if}\)-statement.
You can find a detailed description of how to model a \(\texttt{if-elseif}\)-statement in this stackexchange post.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント