I'm implementing an optimization problem in pyomo. However, I need to define a constraint based on temperatures variables by using if statement. The constraint is like: if self.b.temperatures['supply',t]=self.b.temperatures['return',t] then: self.b.temperatures['supply',t]=self.b.temperatures['supply',t-1] else:
self.b.heat_flow[t]=(self.b.temperatures['supply',t]-self.b.temperatures['return',t])*self.cp * b.mass_flow[t]
I know that I cannot use a variable in if statement. So, now I'm looking for some tricks to still implement this constraint. A part of the code is as below:
self.block.heat_flow = Var(self.TIME, within=NonNegativeReals) lines = self.params['lines'].v()
def _mass_flow(b, t):
return self.params['mass_flow'].v(t)
self.block.mass_flow = Param(self.TIME, rule=_mass_flow)
def _decl_init_heat_flow(b):
return b.heat_flow[0] == (
self.params['temperature_supply'].v() -
self.params['temperature_return'].v()) * \
self.cp * b.mass_flow[0]
self.block.decl_init_heat_flow = Constraint(
rule=_decl_init_heat_flow)
self.block.temperatures = Var(lines, self.TIME)
self.block.del_T_on = Var(self.TIME, within=Binary)
# linearization of the product term b.del_T_on[t]*b.temperatures['supply', t-1] by defining inequality constraints:
self.block.temp_del_on = Var(self.TIME)
def _temp_on_1_u(b, t):
return b.temp_del_on[t] <= self.params['temperature_max'].v() * b.del_T_on[t]
def _temp_on_1_l(b, t):
return b.temp_del_on[t] >= self.params['temperature_min'].v() * b.del_T_on[t]
def _temp_on_2_u(b, t):
return b.temp_del_on[t] <= self.params['temperature_max'].v()
def _temp_on_2_l(b, t):
return b.temp_del_on[t] >= min(self.params['temperature_min'].v(), 0)
def _temp_on_3_l(b, t):
if t==0:
return Constraint.Skip
else:
return b.temp_del_on[t] >= b.temperatures['supply', t-1] - (1 - b.del_T_on[t]) * self.params['temperature_max'].v()
def _temp_on_3_u(b, t):
if t==0:
return Constraint.Skip
else:
return b.temp_del_on[t] <= b.temperatures['supply', t-1] - (1 - b.del_T_on[t]) * self.params['temperature_min'].v()
def _temp_on_4(b, t):
if t==0:
return Constraint.Skip
else:
return b.temp_del_on[t] <= b.temperatures['supply', t-1] + (1 - b.del_T_on[t]) * self.params['temperature_max'].v()
self.block.temp_on_1_u = Constraint(self.TIME, rule=_temp_on_1_u)
self.block.temp_on_1_l = Constraint(self.TIME, rule=_temp_on_1_l)
self.block.temp_on_2_u = Constraint(self.TIME, rule=_temp_on_2_u)
self.block.temp_on_2_l = Constraint(self.TIME, rule=_temp_on_2_l)
self.block.temp_on_3_u = Constraint(self.TIME, rule=_temp_on_3_u)
self.block.temp_on_3_l = Constraint(self.TIME, rule=_temp_on_3_l)
self.block.temp_on_4 = Constraint(self.TIME, rule=_temp_on_4)
# linearization of the product term b.del_T_on[t]*b.temperatures['return', t] by defining inequality constraints:
self.block.temp_del_on_r = Var(self.TIME)
def _temp_on_r_1_u(b, t):
return b.temp_del_on_r[t] <= self.params['temperature_return_max'].v() * b.del_T_on[t]
def _temp_on_r_1_l(b, t):
return b.temp_del_on_r[t] >= self.params['temperature_return_min'].v() * b.del_T_on[t]
def _temp_on_r_2_u(b, t):
return b.temp_del_on_r[t] <= self.params['temperature_return_max'].v()
def _temp_on_r_2_l(b, t):
return b.temp_del_on_r[t] >= min(self.params['temperature_return_min'].v(), 0)
def _temp_on_r_3_l(b, t):
return b.temp_del_on_r[t] >= b.temperatures['return', t] - (1 - b.del_T_on[t]) * \
self.params['temperature_return_max'].v()
def _temp_on_r_3_u(b, t):
return b.temp_del_on_r[t] <= b.temperatures['return', t] - (1 - b.del_T_on[t]) * \
self.params['temperature_return_min'].v()
def _temp_on_r_4(b, t):
return b.temp_del_on[t] <= b.temperatures['return', t] + (1 - b.del_T_on[t]) * self.params['temperature_return_max'].v()
self.block.temp_on_r_1_u = Constraint(self.TIME, rule=_temp_on_r_1_u)
self.block.temp_on_r_1_l = Constraint(self.TIME, rule=_temp_on_r_1_l)
self.block.temp_on_r_2_u = Constraint(self.TIME, rule=_temp_on_r_2_u)
self.block.temp_on_r_2_l = Constraint(self.TIME, rule=_temp_on_r_2_l)
self.block.temp_on_r_3_u = Constraint(self.TIME, rule=_temp_on_r_3_u)
self.block.temp_on_r_3_l = Constraint(self.TIME, rule=_temp_on_r_3_l)
self.block.temp_on_r_4 = Constraint(self.TIME, rule=_temp_on_r_4)
# linearization of the product term b.del_T_on[t]*b.heat_flow[t] by defining inequality constraints:
self.block.heat_del_on = Var(self.TIME)
def _heat_on_1(b, t):
return b.heat_del_on[t] <= b.Qmax * b.del_T_on[t]
def _heat_on_2(b, t):
return b.heat_del_on[t] <= b.heat_flow[t]
def _heat_on_3(b, t):
return b.heat_del_on[t] >= b.heat_flow[t] - (1 - b.del_T_on[t]) * b.Qmax
def _heat_4(b, t):
return 0 <= b.heat_flow[t]
self.block.heat_on_1 = Constraint(self.TIME, rule=_heat_on_1)
self.block.heat_on_2 = Constraint(self.TIME, rule=_heat_on_2)
self.block.heat_on_3 = Constraint(self.TIME, rule=_heat_on_3)
self.block.heat_4 = Constraint(self.TIME, rule=_heat_4)
###################################################################
def _decl_temperatures(b, t):
if t == 0:
return Constraint.Skip
elif b.mass_flow[t] == 0:
return Constraint.Skip
else:
return b.temperatures['supply', t] - (b.temperatures['supply', t - 1] - b.temp_del_on[
t]) == b.heat_del_on[t] / b.mass_flow[t] / self.cp + b.temp_del_on_r[t]
self.block.decl_temperatures = Constraint(self.TIME, rule=_decl_temperatures)
--
You received this message because you are subscribed to a topic in the Google Groups "Pyomo Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyomo-forum/ZujyGkozTQk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/14a21062-b086-4607-96d8-8d8cbb757b97o%40googlegroups.com.
To unsubscribe from this group and all its topics, send an email to pyomo...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/932e8e08-0833-4390-81b8-b695201e1aeao%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/932e8e08-0833-4390-81b8-b695201e1aeao%40googlegroups.com.
To unsubscribe from this group and all its topics, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/cf52e8cb-e873-48e4-9707-f359f7f2d522o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/cf52e8cb-e873-48e4-9707-f359f7f2d522o%40googlegroups.com.
model.W1 = Var(model.V1, within = PositiveReals) ### Arrival Time at 1st-Level Nudes Variabel
model.W2 = Var(model.V2, within = PositiveReals) ### Arrival Time at 2ed-Level Nudes Variabel
model.e = Param(model.Vc) ### Time Window Lower Bond model.l = Param(model.Vc) ### Time Window Upper Bondmodel.alpha = Param(model.Vc) ### Prefer Timemodel.P = Var(model.VcT, within = Binary)
def Fx (model, j):
W = model.W1[j] if j in model.Vc3 else model.W2[j] if model.e[j]!= model.alpha[j]: return (W - model.e[j])/(model.alpha[j] - model.e[j]) else: return 0 def Gx (model, j): W = model.W1[j] if j in model.Vc3 else model.W2[j] if model.l[j]!= model.alpha[j]: return (model.l[j] - W)/(model.l[j] - model.alpha[j]) else: return 0 def __Expr1(model, j): W = model.W1[j] if j in model.Vc3 else model.W2[j] return W - model.alpha[j] <= M * (1-model.P[j]) model.Expr1 = Constraint(model.VcT , rule = __Expr1) def __Expr2(model, j): W = model.W1[j] if j in model.Vc3 else model.W2[j] return model.alpha[j] - W <= M * model.P[j] model.Expr2 = Constraint(model.VcT , rule = __Expr2) def __Expr3(model, j):
return Fx(model, j) * model.P[j] + Gx(model, j) * (1-model.P[j]) model.Mu = Expression(model.VcT , rule = __Expr3)
I hope this will help.
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/3c5641a7-1588-4b29-a372-ad1745a8c3a5o%40googlegroups.com.