How do I model if elif else conditional statements in Gurobi?
AnsweredHi Gurobi Team,
I have an existing energy grid which I want to optimize. My decision variable is binary delta_k[i][j][k]. delta_k is 1 if I adjust a power line from vertex i to vertex j with the capacity k in the grid and 0 if there is no change. I want to minimize the cost. My objective function is delta_k[i][j][k]*t[ij]*(c_fix+c_mat*capacity[k])
t=length from vertex i to vertex j
c_fix and c_mat are fixed parameters and capacity[k] is a capacity value
Now I need to connect the decision variable with my constraints.
y[i][j][k] (binary) is 1 if there is a power line from i to j with a capacity k in the adjusted grid. y[i][j][k] is 0 if there is no power line.
y[i][j][k] is in all my constraints so far but not in the objective function and delta_k is in my objective function but not in my constraints. So I need to connect them.
y_old[i][j][k] is 1 if there is a power line from i to j with a capacity k in the original energy grid, and y_old[i][j][k] is 0 if there is no power line in the original energy grid.
The if else statement is:
if delta_k[i][j][k] >0:
y[i][j][k]=1
elif delta_k[i][j][k] =0 and y_old[i][j][k]=1:
y[i][j][k]=1
else:
y[i][j][k]=0
I have tried to solve it with the big- approach and indicator constraints. However, I failed because I have an elif statement and thus do not have binary auxiliary variables which I can use.
How can I convert these conditions into Gurobi constraints form in the python platform?
Kind regards and thank you very much for your support,
Lenard
-
Hi Lenard,
Note that your \(\texttt{if-else}\)-clause states that
if delta_k == 1 or y_old == 1 then y = 1 else y = 0. I omitted the indices for better readability.
Since all of your variables are binary, you could use Gurobi's or constraint to model your \(\texttt{if-else}\)-clause.
model.addConstr(y == or_(delta_k, y_old))
Again, I omitted the indices for readability.
Best regards,
Jaromił0 -
Hi Jaromil,
thank you very much!
Best regards,
Lenard
0
Please sign in to leave a comment.
Comments
2 comments