簡體   English   中英

使用 GEKKO sysid 進行自適應建模

[英]Adaptive modelling using GEKKO sysid

我有 100 個數據點,我試圖在GEKKO中使用sysid來描述它們。 在某個時間點(在這種情況下為 t = 50),數據發生了顯着變化,預測不再准確。 如果預測是 model 的 x 倍,我正在嘗試包含一個 if 語句,該語句評估實際與預測並生成新的 model (新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()

您需要將yp更新為新的 yp 值yp = yp_new ,否則在進行下一次系統識別時只需返回yp 但是,您用於重做sysid計算的數據與您之前使用的數據相同,因此 model 預測沒有變化。 您是否嘗試僅使用最新數據更新時間序列 model,例如yp_new,p_new,K = m.sysid(t[i:],u[i:],y[i:],na,nb) ?

系統識別

model 當前更新與原始時間序列 model 預測不一致的周期。

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()

更新了 sysid

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM