2

我对使用 Gekko 和一般情况下的预测控制建模非常陌生。

我在 Gekko 中创建了一个 ARX MPC,效果很好。然而,我注意到在前 50-80 次迭代中,结果很好……令人失望。然而,在第一次迭代之后,我得到了很好的结果(我猜 ARX 算法在这里起作用或者可能是 BIAS?)。现在我的问题是模型可能会在一段时间后崩溃,我必须重做 50-80 次迭代才能再次获得良好的结果,有没有办法“保存”最后一个计算的模型并在重新启动计算时使用它?

4

1 回答 1

0

您可能遇到的问题是“先前”值尚未初始化。尝试使用稳态初始化求解一次,如示例 MPC 应用程序所示,其中 TCLabTCLab F的最终源模块。

m.options.IMODE=1
m.solve()

然后您可以切换到控制或模拟模式:

# set up MPC
m.options.IMODE   = 6 # MPC
m.time=np.linspace(0,120,61)

关于使用 ARX 模型的背景信息

ARX 模型的识别和 ARX 模型的预测或控制是两个独立的应用。

识别 ARX 模型

ARX 型号识别

识别 ARX 模型的m.sysid()函数不会保存存档,但会返回模型作为输出参数:

yp,p,K = m.sysid(t,u,y,na,nb,pred='meas')

模型返回为p

# see https://apmonitor.com/wiki/index.php/Apps/ARXTimeSeries
from gekko import GEKKO
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# load data and parse into columns
url = 'http://apmonitor.com/do/uploads/Main/tclab_dyn_data2.txt'
data = pd.read_csv(url)
t = data['Time']
u = data['H1']
y = data['T1']

m = GEKKO(remote=False)

# system identification
na = 2 # output coefficients
nb = 2 # input coefficients
yp,p,K = m.sysid(t,u,y,na,nb,pred='meas')

plt.figure()
plt.subplot(2,1,1)
plt.plot(t,u,label=r'$Heater_1$')
plt.legend()
plt.ylabel('Heater')
plt.subplot(2,1,2)
plt.plot(t,y)
plt.plot(t,yp)
plt.legend([r'$T_{meas}$',r'$T_{pred}$'])
plt.ylabel('Temperature (°C)')
plt.xlabel('Time (sec)')
plt.show()

使用 ARX 模型进行预测

使用 ARX 模型进行预测

下面是使用 ARX 模型进行预测的示例。

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

na = 2 # Number of A coefficients
nb = 1 # Number of B coefficients
ny = 2 # Number of outputs
nu = 2 # Number of inputs

# A (na x ny)
A = np.array([[0.36788,0.36788],\
              [0.223,-0.136]]) 
# B (ny x (nb x nu))
B1 = np.array([0.63212,0.18964]).T
B2 = np.array([0.31606,1.26420]).T
B = np.array([[B1],[B2]])

C = np.array([0,0])

# create parameter dictionary
# parameter dictionary p['a'], p['b'], p['c']
# a (coefficients for a polynomial, na x ny)
# b (coefficients for b polynomial, ny x (nb x nu))
# c (coefficients for output bias, ny)
p = {'a':A,'b':B,'c':C}

# Create GEKKO model
m = GEKKO(remote=False)

# Build GEKKO ARX model
y,u = m.arx(p)

# load inputs
tf = 20 # final time
u1 = np.zeros(tf+1)
u2 = u1.copy()
u1[5:] = 3.0
u2[10:] = 5.0
u[0].value = u1
u[1].value = u2

# customize names
mv1 = u[0]
mv2 = u[1]
cv1 = y[0]
cv2 = y[1]

# options
m.time = np.linspace(0,tf,tf+1)
m.options.imode = 4
m.options.nodes = 2

# simulate
m.solve()

m.open_folder()

plt.figure(1)
plt.subplot(2,1,1)
plt.plot(m.time,mv1.value,'r-',label=r'$MV_1$')
plt.plot(m.time,mv2.value,'b--',label=r'$MV_2$')
plt.ylabel('MV')
plt.legend(loc='best')
plt.subplot(2,1,2)
plt.plot(m.time,cv1.value,'r:',label=r'$CV_1$')
plt.plot(m.time,cv2.value,'b.-',label=r'$CV_2$')
plt.ylabel('CV')
plt.xlabel('Time (sec)')
plt.legend(loc='best')
plt.show()

模型保存在m.path可以使用 查看的文件夹中m.open_folder()。设置m = GEKKO(remote=False)为本地计算并观察用于生成模型和解决方案的所有文件。

于 2021-08-15T02:56:19.170 回答