GEKKO 中的 MPC:错误代码“位置 4”解决方案

问题描述

问题已更新

以前,我模型中的控制变量m.P范围很广,导致求解器失败。根据约翰博士的建议,我在目标函数中尝试了 logarithmic scaling of P。 虽然新错误出现错误代码“位置 4”:

*** Error in syntax of function string: Missing opening parenthesis
 
Position: 4                   
 log_p-(log10(p))
    ?

以下是模型和求解器设置的一部分。 模型中,我有控制变量m.log_P,两个多变量m.u_1、m.u_2,其他变量P、Q、D_1、D_2为状态变量,小写字母均为常量。)>

m.log_P = m.CV(value = np.log10(P_ini),name = 'log_P')  # log_P for scaling
m.log_P.STATUS = 1    # add log_P to objective function,instead of P
m.log_P.FSTATUS = 1
m.P= m.SV(value = P_ini,name = 'P')   # now original P is treated as a state variable

m.u_1 = m.MV(value=u_0,lb = 0,name = 'u_1')
m.u_2 = m.MV(value=u_0,name = 'u_2')
m.u_1.STATUS = 1
m.u_1.FSTATUS = 1
m.u_2.STATUS = 1
m.u_2.FSTATUS = 1

m.Equations([m.P.dt() == (a-mm-n) * m.P + b * m.Q - k_1 * m.D_1 * m.P - k_2 * m.D_2 * m.P,m.Q.dt() == mm*m.P - b*m.Q,m.D_1.dt() == m.u_1 - r_1 * m.D_1,m.D_2.dt() == m.u_2 - r_2 * m.D_2,m.log_P == m.log10(m.P)]) # first 4 equations defined by model. The last equation take logrithmic value of P.

m.options.IMODE = 6
m.options.SOLVER = 3
m.options.CV_TYPE = 2   # l2-norm
m.options.EV_TYPE = 2
m.solver_options = ['linear_solver mumps',\
                    'mu_strategy adaptive']

这里我用 '''m.options.CV_TYPE = 2 # l2-norm''' 设置了二次误差项。

for i in range(len(t)-1):

    ts = [t[i],t[i+1]]
    y = odeint(chemo_model,x0,ts,args = (u_infu1[i],u_infu2[i])) # 'chemo_model' is a model function
    P_cell[i+1] = y[-1][0]
    Q_cell[i+1] = y[-1][1]
    D1_drug[i+1] = y[-1][2]
    D2_drug[i+1] = y[-1][3]

    ''' update '''
    m.log_P.MEAS = np.log10(P_cell[i+1])  # insert measurement
    m.log_P.SP = np.log10(P_sp[i+1]) # P_sp is predefined trajectory points
    
 
    ''' solve MPC '''
    m.solve(disp=True)

一个带有 error code 'Position 5' 的在线案例是由于该等式中缺少“dt()”。看起来很相似,但不是我的情况。谁可以帮我这个事?提前致谢!!

解决方法

通过不使用保留关键字来命名变量来解决此错误。

name = 'log_P'

变量名如 lg_P 会起作用。以下是 reserved keywords in APMonitor 的列表:

abs()   Absolute value  abs(x*y)=0
exp()   Exponentiation  exp(x*y)=0
log10() Base-10 Log log10(x*y)=0
log()   Natural Log log(x*y)=0
sqrt()  Square Root sqrt(x*y)=0
sinh()  Hyperbolic Sine sinh(x*y)=0
cosh()  Hyperbolic Cosine   cosh(x*y)=0
tanh()  Hyperbolic Tangent  tanh(x*y)=0
sin()   Sine    sin(x*y)=0
cos()   Cosine  cos(x*y)=0
tan()   Tangent tan(x*y)=0
asin()  Arc-sine    asin(x*y)=0
acos()  Arc-cos acos(x*y)=0
atan()  Arc-tangent atan(x*y)=0
erf()   Error function  erf(x*y)=0
erfc()  Complementary error function    erfc(x*y)=0

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...