Gekko if3 为 0 和 -0 工作不同
Gekko if3 works differet for 0 and -0
我正在尝试在 gekko 中使用 if3 方法,但它的工作方式与在文档中不同:
y = m.if3(condition,x1,x2)
y = x1 when condition<0
y = x2 when condition>=0
请看下面的例子:
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)
输出是:(我正在使用 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
对于第一种情况,对于 p=0 求解器应该 return 0 还是我遗漏了什么?一般来说,如何在gekko中编写以下if语句:
if val == 0:
y=constant1
elif val > 0:
y=constant2
我发现了类似的问题 (Gekko Optimization Suite for Python - if3 always <0),但升级 gekko 版本没有帮助。
感谢您的回答
感谢您发现 documentation of if3(). The documentation has been updated to be consistent with the algorithm behavior. At the transition point, the solution may be x1
or x2
for the if3()
function. For the if2()
function (MPCC form), the solution at the transition point may also be a linear combination of the two values. Gekko computes a numerical solution with a solver tolerance that is 1e-6 by default 中的错误。将求解器容差设置得更紧将有助于提高过渡点的分辨率。
m.options.RTOL=1e-8
m.options.OTOL=1e-8
可以移动 m.if3()
过渡点以避免 0 值的问题。一种常见的策略是将转换向左 m.if3(x-0.1)
或向右 m.if3(x+0.1)
移动少量,特别是对于 x=0
具有所需输出的混合整数问题。例如,泵的能源使用情况可能类似于:
energy = 2e-5*voltage**2 + 0.5*voltage + 3.0
pump_energy = m.if3(voltage-0.1,0,energy)
这确保泵在电压低于 0.1 时报告没有能源使用。
我正在尝试在 gekko 中使用 if3 方法,但它的工作方式与在文档中不同:
y = m.if3(condition,x1,x2)
y = x1 when condition<0
y = x2 when condition>=0
请看下面的例子:
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)
输出是:(我正在使用 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
对于第一种情况,对于 p=0 求解器应该 return 0 还是我遗漏了什么?一般来说,如何在gekko中编写以下if语句:
if val == 0:
y=constant1
elif val > 0:
y=constant2
我发现了类似的问题 (Gekko Optimization Suite for Python - if3 always <0),但升级 gekko 版本没有帮助。 感谢您的回答
感谢您发现 documentation of if3(). The documentation has been updated to be consistent with the algorithm behavior. At the transition point, the solution may be x1
or x2
for the if3()
function. For the if2()
function (MPCC form), the solution at the transition point may also be a linear combination of the two values. Gekko computes a numerical solution with a solver tolerance that is 1e-6 by default 中的错误。将求解器容差设置得更紧将有助于提高过渡点的分辨率。
m.options.RTOL=1e-8
m.options.OTOL=1e-8
可以移动 m.if3()
过渡点以避免 0 值的问题。一种常见的策略是将转换向左 m.if3(x-0.1)
或向右 m.if3(x+0.1)
移动少量,特别是对于 x=0
具有所需输出的混合整数问题。例如,泵的能源使用情况可能类似于:
energy = 2e-5*voltage**2 + 0.5*voltage + 3.0
pump_energy = m.if3(voltage-0.1,0,energy)
这确保泵在电压低于 0.1 时报告没有能源使用。