关于GEKKO中的条件语句('m.if3')的问题 [英] Question about the conditional statement ('m.if3') in the GEKKO

查看:132
本文介绍了关于GEKKO中的条件语句('m.if3')的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在GEKKO代码中添加一些条件语句.

I would like to add some conditional statements in the GEKKO code.

我在下面的语句中添加了"m.if3".但是,它返回的语法错误是我没有条件语句所没有的.

I added the below statement with 'm.if3'. However, it has returned syntax error that I didn't have without the conditional statement.

'R1_1 = m.if3(R1 <0,0,R1)'

'R1_1 = m.if3(R1<0, 0, R1)'

#%% GEKKO
m = GEKKO(remote=False)

#print(m.version)
#m.time = np.linspace(0, 3600, 100)
m.time = np.array([0,tstep])

cH = m.CV(value=cs0[0])
cM = m.CV(value=cs0[1])
cW = m.CV(value=cs0[2])
cF = m.CV(value=cs0[3])

R1_1 = m.Var()

r3 = m.Intermediate(r0*(1-cF/cFeMax)**(1/3))
r2 = m.Intermediate(r0*((2*cH + 3*cM)/cFeMax)**(1/3))
r1 = m.Intermediate(r0*(2*cH/cFeMax)**(1/3))
x = m.Intermediate(r1/r0)
y = m.Intermediate(r2/r0)
z = m.Intermediate(r3/r0)
A1 = m.Intermediate(1/x**2/(kd[0]*(1+1/Keq[0])))
A2 = m.Intermediate(1/y**2/(kd[1]*(1+1/Keq[1])))
A3 = m.Intermediate(1/z**2/(kd[2]*(1+1/Keq[2])))
B1 = m.Intermediate((y-x)/x/y*r0/Dif[1])
B2 = m.Intermediate((z-y)/y/z*r0/Dif[2])
B3 = m.Intermediate((1-z)/z*r0/Dif[3])
F = 0
W = m.Intermediate((A1+B1)*(A3*(A2+B2+B3+F)+(A2+B2)*(B3+F))+A2*(A3*(B2+B3+F))+B2*(B3+F))
ceq1 = m.Intermediate((cg0[0]+cg0[1])/(1+Keq[0]))
ceq2 = m.Intermediate((cg0[0]+cg0[1])/(1+Keq[1]))
ceq3 = m.Intermediate((cg0[0]+cg0[1])/(1+Keq[2]))

R1 = m.Intermediate(3/r0/W*((A3*(A2+B2+B3+F)+(A2+B2)*(B3+F))*(cg0[0]-ceq1) \
                     -(A3*(B2+B3+F)+B2*(B3+F))*(cg0[0]-ceq2) \
                     -A2*(B3+F)*(cg0[0]-ceq3)))
R2 = m.Intermediate(3/r0/W*(-(B2*(A3+B3+F)+A3*(B3+F))*(cg0[0]-ceq1) \
                     +((A1+B1+B2)*(A3+B3+F)+A3*(B3+F))*(cg0[0]-ceq2) \
                     -(A1+B1)*(B3+F)*(cg0[0]-ceq3)))
R3 = m.Intermediate(3/r0/W*(-A2*(B3+F)*(cg0[0]-ceq1) \
                    -(A1+B1)*(B3+F)*(cg0[0]-ceq2) \
                    +((A1+B1)*(A2+B2+B3+F)+A2*(B2+B1+F))*(cg0[0]-ceq3)))

R1_1 = m.if3(R1<0, 0, R1)

m.Equation(cH.dt() == nus[0].dot([R1_1, R2, R3]))
m.Equation(cM.dt() == nus[1].dot([R1_1, R2, R3]))
m.Equation(cW.dt() == nus[2].dot([R1_1, R2, R3]))
m.Equation(cF.dt() == nus[3].dot([R1_1, R2, R3]))

m.options.IMODE = 4
m.options.SOLVER = 3
m.options.nodes = 2

异常:@error:模型表达式 ***函数字符串的语法错误:括号不匹配

Exception: @error: Model Expression *** Error in syntax of function string: Mismatched parenthesis

位置:4
(0)))-((((((1-int_v5))*(i35)-slk_1 ?

Position: 4
(0)))-((((1-int_v5))*(i35)-slk_1 ?

推荐答案

只需使用一项更正(删除<0)即可使用Gekko中的if3函数.

You need just one correction (remove the <0) to use the if3 function in Gekko.

R1_1 = m.if3(R1, 0, R1)

m.if3函数使用一个条件,该条件根据条件是小于零还是大于或等于零来切换使用的条件(参数2或3).这是帮助(m.if3)的结果,并带有一些附加说明:

The m.if3 function uses a condition that switches what is used (argument 2 or 3) based on whether the condition is less than zero or greater than or equal to zero. Here is the result of help (m.if3) with some additional explanation:

if3(condition, x1, x2) method of gekko.gekko.GEKKO instance

IF conditional with a binary switch variable.
The traditional method for IF statements is not continuously
differentiable and can cause a gradient-based optimizer to fail
to converge.

Usage: y = m.if3(condition,x1,x2)
Inputs:
   condition: GEKKO variable, parameter, or expression
   x1 and x2: GEKKO variable, parameter, or expression
Output: GEKKO variable y = x1 when condition<0
                       y = x2 when condition>=0

要考虑的另一件事是if3函数使用二进制变量,您将需要使用混合整数求解器来找到整数解.您可以删除将求解器切换到IPOPT的行(默认情况下if3切换到APOPT求解器),也可以手动将选项切换到APOPT.

One other thing to consider is that the if3 function uses binary variables and you'll need to use a Mixed Integer solver to find an integer solution. You can either remove the line that switches the solver to IPOPT (if3 switches to the APOPT solver by default) or you can switch the option to APOPT manually.

m.options.SOLVER = 1

脚本中缺少某些常量.我添加了一些虚拟变量只是为了使其运行.

Some of the constants were missing from your script. I added some dummy variables just to make it run.

from gekko import GEKKO
import numpy as np

m = GEKKO()

help(m.if3)

tstep = 1.0
cs0=[1,1,1,1]
r0 = 1.0
cFeMax = 1.0
kd = [1,1,1]
Keq = [1,1,1]
Dif = [1,1,1,1]
cg0 = [1,1,1]
nus = np.array([[1,1,1],[1,1,1],[1,1,1],[1,1,1]])

m.time = np.array([0,tstep])

cH = m.CV(value=cs0[0])
cM = m.CV(value=cs0[1])
cW = m.CV(value=cs0[2])
cF = m.CV(value=cs0[3])

R1_1 = m.Var()

r3 = m.Intermediate(r0*(1-cF/cFeMax)**(1/3))
r2 = m.Intermediate(r0*((2*cH + 3*cM)/cFeMax)**(1/3))
r1 = m.Intermediate(r0*(2*cH/cFeMax)**(1/3))
x = m.Intermediate(r1/r0)
y = m.Intermediate(r2/r0)
z = m.Intermediate(r3/r0)
A1 = m.Intermediate(1/x**2/(kd[0]*(1+1/Keq[0])))
A2 = m.Intermediate(1/y**2/(kd[1]*(1+1/Keq[1])))
A3 = m.Intermediate(1/z**2/(kd[2]*(1+1/Keq[2])))
B1 = m.Intermediate((y-x)/x/y*r0/Dif[1])
B2 = m.Intermediate((z-y)/y/z*r0/Dif[2])
B3 = m.Intermediate((1-z)/z*r0/Dif[3])
F = 0
W = m.Intermediate((A1+B1)*(A3*(A2+B2+B3+F)+(A2+B2)*(B3+F))+A2*(A3*(B2+B3+F))+B2*(B3+F))
ceq1 = m.Intermediate((cg0[0]+cg0[1])/(1+Keq[0]))
ceq2 = m.Intermediate((cg0[0]+cg0[1])/(1+Keq[1]))
ceq3 = m.Intermediate((cg0[0]+cg0[1])/(1+Keq[2]))

R1 = m.Intermediate(3/r0/W*((A3*(A2+B2+B3+F)+(A2+B2)*(B3+F))*(cg0[0]-ceq1) \
                     -(A3*(B2+B3+F)+B2*(B3+F))*(cg0[0]-ceq2) \
                     -A2*(B3+F)*(cg0[0]-ceq3)))
R2 = m.Intermediate(3/r0/W*(-(B2*(A3+B3+F)+A3*(B3+F))*(cg0[0]-ceq1) \
                     +((A1+B1+B2)*(A3+B3+F)+A3*(B3+F))*(cg0[0]-ceq2) \
                     -(A1+B1)*(B3+F)*(cg0[0]-ceq3)))
R3 = m.Intermediate(3/r0/W*(-A2*(B3+F)*(cg0[0]-ceq1) \
                    -(A1+B1)*(B3+F)*(cg0[0]-ceq2) \
                    +((A1+B1)*(A2+B2+B3+F)+A2*(B2+B1+F))*(cg0[0]-ceq3)))

R1_1 = m.if3(R1, 0, R1)

m.Equation(cH.dt() == nus[0].dot([R1_1, R2, R3]))
m.Equation(cM.dt() == nus[1].dot([R1_1, R2, R3]))
m.Equation(cW.dt() == nus[2].dot([R1_1, R2, R3]))
m.Equation(cF.dt() == nus[3].dot([R1_1, R2, R3]))

m.options.IMODE = 4
m.options.SOLVER = 1
m.options.nodes = 2

m.solve()

这篇关于关于GEKKO中的条件语句('m.if3')的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆