GEKKO: MHE load data of previous cycle

Viewed 49

i am developing a model predictive controller (MPC) with a moving horizon estimation (MHE) Plugin for a dynamic simulation program.

My Problem is, that the simulation program executes the Python script in each timestep. So each timestep a new model in GEKKO is produced. Is there a possibility reload the model and the data files? So for example give the path of the data to GEKKO?

Best Regards,

Moritz

1 Answers

Try using a Pickle file to store the Gekko model. If the Gekko model archive exists then it is read back into Python.

from os.path import exists
import pickle
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt

if exists('m.pkl'):
    # load model from subsequent call
    m = pickle.load(open('m.pkl','rb'))
    m.solve()
else:
    # define model the first time
    m = GEKKO()
    m.time = np.linspace(0,20,41)

    m.p = m.MV(value=0, lb=0, ub=1)
    m.v = m.CV(value=0)
    m.Equation(5*m.v.dt() == -m.v + 10*m.p)
    m.options.IMODE = 6
    m.p.STATUS = 1; m.p.DCOST = 1e-3
    m.v.STATUS = 1; m.v.SP = 40; m.v.TAU = 5
    m.options.CV_TYPE = 2
    m.solve()
pickle.dump(m,open('m.pkl','wb'))

plt.figure()
plt.subplot(2,1,1)
plt.plot(m.time,m.p.value,'b-',lw=2)
plt.ylabel('gas')
plt.subplot(2,1,2)
plt.plot(m.time,m.v.value,'r--',lw=2)
plt.ylabel('velocity')
plt.xlabel('time')
plt.show()

Each cycle of the controller, the plot updates with the automatic time-shift of the initial condition.

Plot

This is similar to what happens in a loop with a combined MHE and MPC. As long as you include everything in the Pickle file, it should reload on the next cycle.

MHE and MPC

Here is the example code for MHE and MPC.

Related