"If" in object function
Awaiting user inputHi,
I have a semi-complicated objective function that has an "if" in it. The picture below shows a simplified version of it. In this objective function, Pgrid could be positive or negative (depending on optimization variables)- That is why I have mix and min.

I tried to solve it by defining a binary variable and adding a constrain (code below). But no success. Any guidance to solve this problem is highly appreciated.
b1 = m.addVars(n-1,vtype=GRB.BINARY, name="b1")
b2 = m.addVars(n-1,vtype=GRB.BINARY, name="b2")
for i in range (n-1):
if (Pgrid[i]) >= 0:
m.addConstr (b1[i] == 1)
for i in range (n-1):
if (Pgrid[i]) <= 0:
m.addConstr (b2[i] == 1)
obj = sum( ((Pimport[i]*10*b1[i])+(Pimport[i]*5*b2[i])) for i in range(n-1))
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 why not try our AI Gurobot?. -
Hi Saeed,
I am not entirely sure that this is what you are trying to model but to me, this appears to be a case for splitting the \(P_\text{grid}\) variables into a positive and a negative part:
Pgrid_pos = m.addVars(n-1, name="P_grid_pos", obj=10)
Pgrid_neg = m.addVars(n-1, name="P_grid_neg", obj=5)
m.addConstrs(Pgrid[i] == Pgrid_pos[i] - Pgrid_neg[i] for i in range(n-1))Cheers,
Matthias0
Post is closed for comments.
Comments
2 comments