在同一个gekko类中,是否可以用MHE更新MPC?

And*_*w T 6 gekko

I am currently using an MPC to have the TCLab heater reach a certain set point temperature. I am trying to have an MHE update certain parameter values every 50 seconds. I have a previous MPC model that worked amazing and I tried to add a part in my main loop that has it switch to improve certain values and then switch back into MPC mode. I have seen that other people doing this same problem have made a gekko class for the MPC and also for the MHE and then had them work together, but is there a way that I can add a part in my current MPC loop that will allow the MHE to update certain values and then switch back into MPC?

Here is the Code that I added into my loop to have it update the variables but it isn't updating my values

 if i%50 == 0 or i == 0:

            m.options.IMODE = 5
            Q1.STATUS = 0
            Q1.FSTATUS = 1
            Q2.STATUS = 0
            Q2.FSTATUS = 1

            U.FSTATUS = 1
            ?1.FSTATUS = 1
            ?2.FSTATUS = 1
            ?.FSTATUS = 1

            m.solve(disp = False)

            Q1.STATUS = 1
            Q1.FSTATUS = 1
            Q2.STATUS = 1
            Q2.FSTATUS = 1


            m.options.IMODE = 6
            U.FSTATUS = 0
            ?1.FSTATUS = 0
            ?2.FSTATUS = 0
            ?.FSTATUS = 0
Run Code Online (Sandbox Code Playgroud)

Joh*_*ren 2

Gekko 促进了 MHE 和 MPC 之间的信息传输,但将它们组合到单个应用程序中并不是当前的功能。热启动文件est.t0(MHE) 和ctl.t0(MPC) 存储先前的解决方案并使用它来初始化下一个解决方案。文件est.xfer(MHE) 是用于更新 MHE 应用程序的初始条件和参数的传输文件。你可以通过打开run文件夹来查看这些文件if remote=False(本地解决):

mhe.open_folder()
mpc.open_folder()
Run Code Online (Sandbox Code Playgroud)

为什么单个应用程序具有挑战性

Gekko 还使用 CSV 文件传输值并在下一个m.solve()命令之前更新应用程序。Gekko 中的每个变量x只有一个x.value. 如果您有 MHE 和 MPC 应用程序,则需要管理如何x.value在每个命令之前为所有变量重新加载所有选项m.solve()。即使使用函数,在脚本中管理它也会非常乏味deepcopy()

在循环中创建 MHE 和 MPC 模型

一种更简单的方法是创建两个用于 MHE 和 MPC 的独立模型。为了促进这一点,可以在循环中构建模型(参见完整示例),以便变量和方程仅定义一次。

MHE 与 MPC

# initialize MHE and MPC
mhe = GEKKO(name='tclab-mhe')
mpc = GEKKO(name='tclab-mpc')

# create 2 models (MHE and MPC) in loop
for m in [mhe,mpc]:
    # Adjustable Parameters
    # heat transfer (W/m2-K)
    m.U = m.FV(value=2.76,lb=1.0,ub=5.0)
    # Semi-fundamental correlations (energy balances)
    m.Equation(mass*Cp*m.TH1.dt() == m.U*A*(m.TaK-m.T1i) \
                  + eps * sigma * A * (m.TaK**4 - m.T1i**4) \
                  + m.Q_C12 + m.Q_R12 \
                  + m.alpha1 * m.Q1)
    # Empirical correlations (lag equations to emulate conduction)
    m.Equation(m.tau * m.TC1.dt() == -m.TC1 + m.TH1)
Run Code Online (Sandbox Code Playgroud)

定义方程后,可以使用特定于该模式的各种选项来配置 MHE 和 MPC 应用程序。

应用特定配置

# ------------------------------
# Configure MHE
mhe.time = np.linspace(0,120,31)
mhe.options.IMODE   = 5 # MHE
# FV tuning
mhe.U.STATUS = 1
mhe.Ta.STATUS = 0
# ------------------------------
# Configure MPC
mpc.time = [0,4,8,12,15,20,25,30,35,40,50,60,70,80,90]
# FV tuning
mpc.U.STATUS = 0
mpc.Ta.STATUS = 0
mpc.U.FSTATUS = 1
mpc.Ta.FSTATUS = 1
# Global Options
mpc.options.IMODE   = 6 # MPC
Run Code Online (Sandbox Code Playgroud)

如果您希望在每个周期在 MHE 和 MPC 应用程序之间轻松传输值,那么一种选择是将文件est.xfer从 MHE 文件夹复制mhe.path到 MPC 文件夹中mpc.path。这将在 MPC 应用程序中使用来自 MHE 应用程序的更新状态和参数。