Rongting Yue
Rongting Yue

Reputation: 21

MPC in GEKKO: error code 'Position 4' solution

Question updated

Previously, the control variable m.P in my model has a wide range that leads to the failure of the solver. With Dr.John's advice, I tried logarithmic scaling of P in the objective function. While new error rises with error code 'Position 4':

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

The following is part of the model and solver setting. In the model, I have the control variable m.log_P, and two mulnipulate variables m.u_1, m.u_2, other variables P,Q,D_1 and D_2 are state variable, and the lower case letters are all constants.)

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, lb = 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']

Here I have set the quadratic error term with '''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)

One online case with error code 'Position 5' is due to the missing "dt()" in that equation. It seems similar but not my case. Can anyone help me on this? Thanks in advance!!

Upvotes: 2

Views: 140

Answers (1)

John Hedengren
John Hedengren

Reputation: 14346

This error is resolved by not using the reserved keyword to name the variable.

name = 'log_P'

A variable name such as lg_P would work. Here is a list of 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

Upvotes: 1

Related Questions