【问题标题】:Adaptive modelling using GEKKO sysid使用 GEKKO sysid 进行自适应建模
【发布时间】:2020-03-17 19:29:23
【问题描述】:

我有 100 个数据点,我试图在 GEKKO 中使用 sysid 进行描述。在某个点(在这种情况下为 t = 50),数据发生了显着变化,预测不再准确。我正在尝试包含一个 if 语句,该语句评估实际与预测并在预测比模型大 x 倍时生成新模型(新 yp)。这是我的示例代码。循环在每个时间点继续评估yp,但现在应该评估yp_new

from gekko import GEKKO
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# load data and parse into columns
t = np.linspace(0,1,101)
u = np.linspace(0,1,101)
y = np.zeros(len(t))
y[:50] = np.sin(u[:50])
y[50:] = np.exp(u[50:]/500)
# generate time-series model
m = GEKKO(remote=False)
# system identification
na = 2 # output coefficients
nb = 2 # input coefficients
yp,p,K = m.sysid(t,u,y,na,nb,diaglevel=1)
print(yp)
for i in range(len(t)):
    difference = np.abs((yp[i]-y[i])/max(0.01,y[i]))
    if difference>=0.2:   #If the difference is >20%
        yp_new,p_new,K = m.sysid(t,u,y,na,nb,diaglevel=0)
        print('Recalculating at i  = ' + str(i))
print(yp_new)
plt.figure()
plt.subplot(2,1,1)
plt.plot(t,u)
plt.legend([r'$u_0$',r'$u_1$'])
plt.ylabel('MVs')
plt.subplot(2,1,2)
plt.plot(t,y)
plt.plot(t,yp)
plt.plot(t,y)
plt.plot(t,yp_new)
plt.show()

【问题讨论】:

  • 我使用 ARX 模型 m.arx(p) 在完成 sysid 后提供更新的预测。系统标识通常不会在每个周期使用,仅在需要更新模型时使用。

标签: python loops regression autoregressive-models gekko


【解决方案1】:

您需要将yp 更新为新的yp 值yp = yp_new,否则在进行下一次系统识别时只需返回yp。但是,您用于重做 sysid 计算的数据与您之前使用的数据相同,因此模型预测没有变化。您是否尝试仅使用最新数据(例如 yp_new,p_new,K = m.sysid(t[i:],u[i:],y[i:],na,nb))更新时间序列模型?

模型当前更新的周期与原始时间序列模型预测不一致。

Recalculating at i  = 10
Recalculating at i  = 11
Recalculating at i  = 12
Recalculating at i  = 13
Recalculating at i  = 14
Recalculating at i  = 15
Recalculating at i  = 16
Recalculating at i  = 17
Recalculating at i  = 18
Recalculating at i  = 19
Recalculating at i  = 20
Recalculating at i  = 21
Recalculating at i  = 22
Recalculating at i  = 23
Recalculating at i  = 24
Recalculating at i  = 25
Recalculating at i  = 40
Recalculating at i  = 41
Recalculating at i  = 42
Recalculating at i  = 43
Recalculating at i  = 44
Recalculating at i  = 45
Recalculating at i  = 46
Recalculating at i  = 47
Recalculating at i  = 48
Recalculating at i  = 49
Recalculating at i  = 50
Recalculating at i  = 51
Recalculating at i  = 52

如果您只包含最新数据,那么它只会在第 10、42、46 和 48 周期重新计算。

from gekko import GEKKO
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# load data and parse into columns
t = np.linspace(0,1,101)
u = np.linspace(0,1,101)
y = np.zeros(len(t))
y[:50] = np.sin(u[:50])
y[50:] = np.exp(u[50:]/500)
# generate time-series model
m = GEKKO(remote=False)
# system identification
na = 2 # output coefficients
nb = 2 # input coefficients
yp,p,K = m.sysid(t,u,y,na,nb)
yp_init = yp.copy()
print(yp)
j = 0
for i in range(len(t)):
    difference = np.abs((yp[i-j]-y[i])/max(0.01,y[i]))
    if difference>=0.2:   #If the difference is >20%
        j = i # get cycle where the last update occurred
        yp,p,K = m.sysid(t[i:],u[i:],y[i:],na,nb)
        print('Recalculating at i  = ' + str(i))
plt.figure()
plt.subplot(2,1,1)
plt.plot(t,u)
plt.legend([r'$u_0$',r'$u_1$'])
plt.ylabel('MVs')
plt.subplot(2,1,2)
plt.plot(t,y)
plt.plot(t,yp_init)
plt.plot(t,y)
plt.plot(t[j:],yp)
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2020-11-17
    • 1970-01-01
    • 2021-07-11
    相关资源
    最近更新 更多