Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to update MPC with MHE in the same gekko class?

Tags:

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
like image 783
Andrew T Avatar asked Oct 10 '19 14:10

Andrew T


1 Answers

Gekko facilitates transfer of information between MHE and MPC but combining them into a single application isn't a current feature. Warmstart files est.t0 (MHE) and ctl.t0 (MPC) store the prior solution and use it to initialize the next solve. A file est.xfer (MHE) is a transfer file to update the initial conditions and parameters from the MHE application. You can look at these files by opening the run folder if remote=False (local solve):

mhe.open_folder()
mpc.open_folder()

Why a Single Application is Challenging

Gekko also uses a CSV file to transfer values and update the application before the next m.solve() command. Each variable x in Gekko only has one x.value. If you have an MHE and MPC application, you would need to manage how the x.value and all the options are reloaded for all of the variables before each of the m.solve() commands. It would be very tedious to manage that in a script, even with a deepcopy() function.

Create MHE and MPC Models in a Loop

An easier way is to create two separate models that are used for MHE and MPC. To facilitate this, the model can be built in a loop (see complete example) so that variables and equations are only defined once.

MHE with 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)

After the equations are defined, the MHE and MPC applications can be configured with various options that are specific to that mode.

Application Specific Configuration

# ------------------------------
# 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

If you'd like to easily transfer values between the MHE and MPC applications at every cycle then one option is to copy the est.xfer file from the MHE folder mhe.path into the MPC folder mpc.path. This will use the updated states and parameters from the MHE application in the MPC application.

like image 153
John Hedengren Avatar answered Dec 25 '22 20:12

John Hedengren