[英]GEKKO variable update in real time
如果我想在每秒更新的在線模擬中使用 GEKKO,我需要如何設置m.time
並更新初始條件? 我努力了:
m.time = np.linspace(0,1,2)
while simulation_on:
m.solve()
x1.value = x1.value.value
x2.value = x2.value.value
x3.value = x3.value.value
但它似乎沒有更新值。 我正在使用IMODE = 4
這只是一個動態模擬應用程序。 暫時無法控制。
當m.options.TIME_SHIFT=1
(默認)時,Gekko 會自動管理初始條件。 下面是一個簡單的示例,其中包含一個仿真循環和一個單輸入和單 output。
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
m = GEKKO() # create GEKKO model
n = 10
m.time = np.linspace(0,n,n+1) # time points
# input step 0 to 0.5 at t=3
us = np.zeros_like(m.time); us[3:] = 0.5
u = m.Param(0)
x = m.Var(0.0)
m.Equation(2*x.dt()==-x+4*u)
m.options.IMODE = 4
xs=[0]
for i in range(n):
u.value=us[i] # new input
m.solve(disp=False)
xs.append(x.value[1])
# plot results
plt.plot(m.time,us,'ro',label='u(t)')
plt.plot(m.time,xs,'bx-',label='x(t)')
plt.ylabel('values')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()
如果需要逐個周期地更改初始條件,則可以調整初始條件,例如:
if i==5:
xs[i]=5
x.value = xs[i]
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
m = GEKKO() # create GEKKO model
n = 10
m.time = np.linspace(0,n,n+1) # time points
# input step 0 to 0.5 at t=3
us = np.zeros_like(m.time); us[3:] = 0.5
u = m.Param(0)
x = m.Var(0.0)
m.Equation(2*x.dt()==-x+4*u)
m.options.IMODE = 4
xs=[0]
for i in range(n):
u.value=us[i] # new input
if i==5:
xs[i]=5
x.value = xs[i]
m.solve(disp=False)
xs.append(x.value[1])
# plot results
plt.plot(m.time,us,'ro',label='u(t)')
plt.plot(m.time,xs,'bx-',label='x(t)')
plt.ylabel('values')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()
Gekko v1.0.1
中存在一個可能影響這些結果的錯誤。 我建議使用pip install gekko --upgrade
升級到最新版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.