【问题标题】:Equation Definition Error (Equation without an equality or inequality) while using GEKKO MHE使用 GEKKO MHE 时出现方程定义错误(没有等式或不等式的方程)
【发布时间】:2020-12-15 15:06:22
【问题描述】:

我目前正在尝试 GEKKO MHE 模式。我在模型中有两个指定的操纵变量和控制变量,还有一个我希望通过 MHE 估计的参数。当我当前运行模型时,我得到一个方程定义错误,说

不带等式 (=) 或不等式 (>,

模型被初始化为:

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

n = 17

m = GEKKO(remote=False)

m.time = np.linspace(0,8,n)

c1_in_arr = np.load('c1_in_arr.npy')
c2_in_arr = np.load('c2_in_arr.npy')


V1_measured = np.load('V1_measured.npy')
V2_measured = np.load('V2_measured.npy')


#mmanipulated variables, feeding in the arrays for them
m.C1_in = m.MV(value=c1_in_arr)
m.C2_in = m.MV(value=c2_in_arr)


#estimated variables, feeding in the upper and lower bounds for them
m.C1_eff = m.FV(value = 0.98, lb = 0.95, ub = 1.0)

#controlled variables, feeding in the measurements for them
m.V1 = m.CV(value=V1_measured)
m.V2 = m.CV(value=V2_measured)


mdot_1 = m.Var()
mdot_2 = m.Var()


m.Equation(mdot_1== 1.52*m.C1_eff*m.C1_in)
m.Equation(mdot_2==-0.668*mdot_1 + 1.33*m.C1_eff*m.C2_in)

df_c = pd.read_csv('Values_C.csv',index_col=0)

Hhat_C1 = m.Var()
Hhat_C1 = m.Var()
M_m = 125
mdot_m = 75
mdot_s = 46

m.Equations([Hhat_C1 == -0.606 + 0.0057 * mdot_1,
             Hhat_C2 == -3.933 + 0.00096 * mdot_1])

C1_m = m.Var(value = 200)
C2_m = m.Var(value = 150)

m.Equations([C1_m.dt() == mdot_1 - C1_m/M_m*mdot_m,
             C2_m.dt() == mdot_2 - C2_m/M_s*mdot_s)

m.Equation(m.V1==0.8*C1_m/M_m)


m.Equation(m.V2 == 0.78*C1_m/C2_m)


m.options.IMODE = 5
#setting the solver settings to MHE

m.options.EV_TYPE = 1
#setting the solver for the MHE to calculate the parameters based on the sum of absolute errors

m.C1_in.STATUS = 0
m.C2_in.STATUS = 0
m.SiO2_in.STATUS = 0

m.C1_eff.STATUS = 1
m.V1.STATUS = 1
m.V2.STATUS = 1

m.C1_in.FSTATUS = 1
m.C2_in.FSTATUS = 1


m.C1_eff.FSTATUS = 0
m.V1.FSTATUS = 1
m.V2.FSTATUS = 1

m.C1_eff.DMAX = 1.0

m.V1.MEAS_GAP = 0.001
m.V2.MEAS_GAP = 0.001


m.open_folder() 
m.solve(disp = False)
   

当我在求解之前打开 GEKKO 文件夹时,文件夹中也不存在不可行性文件。

当 MV 和 CV 被初始化为“测量”数组的第一个变量时,模型能够正常运行

例如。 m.C1_in = m.MV(值=c1_in_arr[0])

然而,提供的参数估计是不正确的。

我认为这个错误可能是由于模型中处理我的 MV 和 CV 的方式。有没有办法查明是哪个方程导致了这个错误,或者是由于 MV/CV 初始化引起的?

谢谢!

【问题讨论】:

    标签: python gekko


    【解决方案1】:

    问题可能在于在 Gekko 方程中使用 Numpy 数组或 Pandas 数据框,例如:

    # incorrect
    df_c = pd.read_csv('Values_C.csv',index_col=0)
    m.Equation(m.C1_in==df_c)
    

    您可以通过创建输入参数来解决此错误,例如:

    # correct
    df_c = pd.read_csv('Values_C.csv',index_col=0)
    df_c = m.Param(df_c)
    m.Equation(m.C1_in==df_c)
    

    我没有您的 .npy 文件,因此无法重现您的错误。但是,我确实用长度为n 的随机数组输入替换了那些,以获得成功的解决方案。还有一些参数,例如 M_s 是未定义的,所以我包含了一些示例值。您对MVsCVs 的定义很好。该错误可能是由于其他输入参数需要转换为 Gekko 类型参数,然后才能在方程式中使用。

    from gekko import GEKKO
    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    
    n = 17
    
    m = GEKKO(remote=False)
    
    m.time = np.linspace(0,8,n)
    
    c1_in_arr = np.random.rand(n)
    c2_in_arr = np.random.rand(n)
    
    V1_measured = np.random.rand(n)
    V2_measured = np.random.rand(n)
    
    #mmanipulated variables, feeding in the arrays for them
    m.C1_in = m.MV(value=c1_in_arr)
    m.C2_in = m.MV(value=c2_in_arr)
    
    
    #estimated variables, feeding in the upper and lower bounds for them
    m.C1_eff = m.FV(value = 0.98, lb = 0.95, ub = 1.0)
    
    #controlled variables, feeding in the measurements for them
    m.V1 = m.CV(value=V1_measured)
    m.V2 = m.CV(value=V2_measured)
    
    mdot_1 = m.Var()
    mdot_2 = m.Var()
    
    m.Equation(mdot_1== 1.52*m.C1_eff*m.C1_in)
    m.Equation(mdot_2==-0.668*mdot_1 + 1.33*m.C1_eff*m.C2_in)
    
    Hhat_C1 = m.Var()
    Hhat_C2 = m.Var()
    M_m = 125
    M_s = 125
    mdot_m = 75
    mdot_s = 46
    
    m.Equations([Hhat_C1 == -0.606 + 0.0057 * mdot_1,
                 Hhat_C2 == -3.933 + 0.00096 * mdot_1])
    
    C1_m = m.Var(value = 200)
    C2_m = m.Var(value = 150)
    
    m.Equations([C1_m.dt() == mdot_1 - C1_m/M_m*mdot_m,
                 C2_m.dt() == mdot_2 - C2_m/M_s*mdot_s])
    
    m.Equation(m.V1==0.8*C1_m/M_m)
    
    
    m.Equation(m.V2 == 0.78*C1_m/C2_m)
    
    
    m.options.IMODE = 5
    #setting the solver settings to MHE
    
    m.options.EV_TYPE = 1
    #setting the solver for the MHE to calculate the
                #parameters based on the sum of absolute errors
    
    m.C1_in.STATUS = 0
    m.C2_in.STATUS = 0
    
    m.C1_eff.STATUS = 1
    m.V1.STATUS = 1
    m.V2.STATUS = 1
    
    m.C1_in.FSTATUS = 1
    m.C2_in.FSTATUS = 1
    
    m.C1_eff.FSTATUS = 0
    m.V1.FSTATUS = 1
    m.V2.FSTATUS = 1
    
    m.C1_eff.DMAX = 1.0
    
    m.V1.MEAS_GAP = 0.001
    m.V2.MEAS_GAP = 0.001
    
    m.open_folder() 
    m.solve(disp = True)
    

    如果存在阻止求解器运行的模型错误或存在成功的求解,则不会创建文件 infeasibilities.txt。有了随机输入值,就有了成功的解决方案。

    ----------------------------------------------------------------
     APMonitor, Version 0.9.2
     APMonitor Optimization Suite
     ----------------------------------------------------------------
     
     
     --------- APM Model Size ------------
     Each time step contains
       Objects      :  0
       Constants    :  0
       Variables    :  11
       Intermediates:  0
       Connections  :  0
       Equations    :  8
       Residuals    :  8
     
     Warning: CV( 1 ) on at cycle  1 with no MVs on
     Warning: CV( 2 ) on at cycle  1 with no MVs on
     Number of state variables:    417
     Number of total equations: -  416
     Number of slack variables: -  0
     ---------------------------------------
     Degrees of freedom       :    1
     
     **********************************************
     Dynamic Estimation with Interior Point Solver
     **********************************************
      
      
     Info: Exact Hessian
    
    ******************************************************************************
    This program contains Ipopt, a library for large-scale nonlinear optimization.
     Ipopt is released as open source code under the Eclipse Public License (EPL).
             For more information visit http://projects.coin-or.org/Ipopt
    ******************************************************************************
    
    This is Ipopt version 3.10.2, running with linear solver mumps.
    
    Number of nonzeros in equality constraint Jacobian...:      510
    Number of nonzeros in inequality constraint Jacobian.:      384
    Number of nonzeros in Lagrangian Hessian.............:       32
    
    Total number of variables............................:      417
                         variables with only lower bounds:      192
                    variables with lower and upper bounds:       33
                         variables with only upper bounds:        0
    Total number of equality constraints.................:      224
    Total number of inequality constraints...............:      192
            inequality constraints with only lower bounds:      192
       inequality constraints with lower and upper bounds:        0
            inequality constraints with only upper bounds:        0
    
    iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
       0 1.4079997e+001 1.20e+002 9.00e+000   0.0 0.00e+000    -  0.00e+000 0.00e+000   0
       1 2.2358394e+002 1.20e+002 9.95e+000  11.0 1.03e+011    -  1.05e-010 1.45e-012f  1
       2 3.0439837e+004 1.20e+002 2.27e+005  12.1 1.14e+012    -  2.78e-013 1.89e-011f  1
       3 3.0621616e+006 1.20e+002 2.44e+006  11.4 4.33e+010    -  1.00e+000 4.98e-008f  1
       4 3.0621722e+006 6.66e-001 2.49e+004   4.6 3.93e+000    -  9.90e-001 1.00e+000f  1
       5 3.0609833e+006 1.78e-014 2.55e+002   2.6 1.28e+000    -  9.90e-001 1.00e+000f  1
       6 2.9460876e+006 1.42e-014 2.55e+000   0.6 8.89e+001    -  9.90e-001 1.00e+000f  1
       7 7.0161757e+005 2.84e-014 2.55e-002  -1.3 1.69e+003    -  9.90e-001 1.00e+000f  1
       8 1.1625511e+004 2.84e-014 2.57e-004  -2.7 1.48e+003    -  9.90e-001 9.91e-001f  1
       9 1.0176844e+003 6.17e-009 5.69e-002  -0.1 3.45e+003    -  1.00e+000 9.32e-001f  1
    iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
      10 4.7702371e+002 1.77e-008 3.10e+000  -0.8 8.48e+003    -  1.00e+000 6.38e-001f  1
      11 2.8271410e+002 2.82e-007 1.44e+000  -1.0 5.55e+003    -  1.00e+000 7.81e-001f  1
      12 2.3676481e+002 8.50e-008 1.67e+000  -1.9 1.06e+004    -  1.00e+000 7.37e-001f  1
      13 2.2794093e+002 1.98e-007 1.82e+000  -3.0 6.91e+003    -  9.98e-001 7.00e-001f  1
      14 2.2582143e+002 7.63e-008 9.56e-001  -3.2 2.35e+003    -  1.00e+000 7.47e-001f  1
      15 2.2529511e+002 2.39e-008 2.72e-001  -4.0 7.00e+002    -  1.00e+000 7.20e-001f  1
      16 2.2508076e+002 5.09e-010 2.51e-004  -4.6 2.10e+002    -  1.00e+000 1.00e+000f  1
      17 2.2507517e+002 7.65e-011 2.25e-004  -6.7 5.16e+000    -  1.00e+000 8.51e-001f  1
      18 2.2507454e+002 1.43e-011 3.79e-005  -6.2 6.63e-001    -  1.00e+000 8.13e-001f  1
      19 2.2507438e+002 3.69e-012 1.75e-005  -7.0 1.68e-001    -  1.00e+000 7.43e-001f  1
    iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
      20 2.2507434e+002 9.33e-013 5.66e-006  -8.2 4.29e-002    -  1.00e+000 7.47e-001f  1
      21 2.2507433e+002 2.36e-013 1.64e-006  -8.6 1.09e-002    -  1.00e+000 7.47e-001f  1
      22 2.2507432e+002 6.01e-014 1.33e-006  -9.3 2.23e-003    -  1.00e+000 7.45e-001f  1
      23 2.2507432e+002 1.42e-014 9.06e-014 -11.0 2.74e-004    -  1.00e+000 1.00e+000h  1
    
    Number of Iterations....: 23
    
                                       (scaled)                 (unscaled)
    Objective...............:  2.2507432359796402e+002   2.2507432359796402e+002
    Dual infeasibility......:  9.0594198809412774e-014   9.0594198809412774e-014
    Constraint violation....:  9.4739031434680035e-015   1.4210854715202004e-014
    Complementarity.........:  1.2089838737827345e-011   1.2089838737827345e-011
    Overall NLP error.......:  1.2089838737827345e-011   1.2089838737827345e-011
    
    
    Number of objective function evaluations             = 24
    Number of objective gradient evaluations             = 24
    Number of equality constraint evaluations            = 24
    Number of inequality constraint evaluations          = 24
    Number of equality constraint Jacobian evaluations   = 24
    Number of inequality constraint Jacobian evaluations = 24
    Number of Lagrangian Hessian evaluations             = 23
    Total CPU secs in IPOPT (w/o function evaluations)   =      0.163
    Total CPU secs in NLP function evaluations           =      0.083
    
    EXIT: Optimal Solution Found.
    
     The solution was found.
    
     The final value of the objective function is  225.07432359796402
     
     ---------------------------------------------------
     Solver         :  IPOPT (v3.12)
     Solution time  :  0.2523 sec
     Objective      :  225.07433063732404
     Successful solution
     ---------------------------------------------------
    

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 2020-09-26
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多