I'm trying to use if3 method in gekko but it's working different than in docs:
y = m.if3(condition,x1,x2)
y = x1 when condition<0
y = x2 when condition>=0
Please see the example below:
from gekko import GEKKO
m = GEKKO(remote=False)
p = m.Param()
y = m.if3(p,1,0)
for val, expected in [(-1, 1), (0, 0), (1, 0)]:
p.value = val
m.solve(disp=False)
print("P=", p.value, " y=",y.value, " expected=", expected)
print()
m2 = GEKKO(remote=False)
p2 = m2.Param()
y2 = m2.if3(-p2,0,1)
for val, expected in [(-1, 1), (0, 1), (1, 0)]:
p2.value = val
m2.solve(disp=False)
print("P=", p2.value, " y=",y2.value, " expected=", expected)
The output is: (I'm using python3.9, gekko=1.0.4)
P= [-1.0] y= [1.0] expected= 1
P= [0.0] y= [1.0] expected= 0
P= [1.0] y= [0.0] expected= 0
P= [-1.0] y= [1.0] expected= 1
P= [0.0] y= [1.0] expected= 1
P= [1.0] y= [0.0] expected= 0
For the first case, for p=0 the solver should return 0 or am I missing something? In general, how to write in gekko the following if statement:
if val == 0:
y=constant1
elif val > 0:
y=constant2
I've found similar question (Gekko Optimization Suite for Python - if3 always <0) but upgrading the gekko version didn't help. Thanks for any answer