I am trying to implement a model, and I have this conditional Piecewise function, as part of the model.
where T_zone(=293) and T_in(=348) are previously defined constants, x2 and x5 are variables dependent on other equations that depend on T_in and T_out.
All of them have defined initial values.
temperature_zone=293.0
T_in = m.Var(value=temperature_zone)
T_zone = m.Var(value=temperature_zone)
x2 = m.Var(value=temperature_zone)
x5 = m.Var(value=temperature_zone)
Tout = m.Var(value=temperature_zone)
Tp1out=m.Var(value=temperature_zone)
m1out=m.if3(((2.0*(x2))-T_in-T_zone), 0, ((2.0*(x2))-T_in-T_zone))
m.Equation(T_p1out== T_zone+m1out)
m2out=m.if3(((2.0*(x5))-T_p1out-T_zone), 0, ((2.0*(x5))-T_p1out-T_zone))
m.Equation(T_out == T_zone+m2out)
In the code above, I tried separating the equation into 2 parts, and make the result of the if condition to be added as an additional variable to the base/minimum value, and it resulted in no solutions.
Here are some things to consider:
T_in
and T_zone
are constants then use m.Const
or m.Param
instead of m.Var
. Otherwise, they can be adjusted by the solver.m.if3
function is sufficient for this problem. You can define two m.if3
statements and then add the output but then you are creating additional binary variables that will make your problem more difficult to solve.condition
determines whether T_out=T_zone (condition<0)
or whether T_out=2*x5-2*x2+T_in (condition>=0)
.T_in
is 348.0, not temperature_zone (293.0)
.Here is a complete script for the problem you posed.
from gekko import GEKKO
m = GEKKO()
temperature_zone=293.0
T_in = m.Const(value=348.0)
T_zone = m.Const(value=temperature_zone)
x2 = m.Var(value=temperature_zone)
x5 = m.Var(value=temperature_zone)
condition = 2*x5-2*x2+T_in-T_zone
Tout=m.if3(condition, T_zone, 2*x5-2*x2+T_in)
m.solve()
The APOPT solver finds the solution in two iterations. Note that x2
and x5
are additional degrees of freedom in this example and that equations are still needed to specify their values.
Number of state variables: 6
Number of total equations: - 3
Number of slack variables: - 2
---------------------------------------
Degrees of freedom : 1
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 0.00 NLPi: 2 Dpth: 0 Lvs: 2 Obj: 0.00E+00 Gap: NaN
--Integer Solution: 0.00E+00 Lowest Leaf: 0.00E+00 Gap: 0.00E+00
Iter: 2 I: 0 Tm: 0.00 NLPi: 1 Dpth: 1 Lvs: 2 Obj: 0.00E+00 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 1.390000000537839E-002 sec
Objective : 0.000000000000000E+000
Successful solution
---------------------------------------------------