有没有办法对一个变量建模,该变量是前一个时间步(t-1)的值和当前时间步(t)的值(例如,eq1) x(t) = (some equation), eq2) a(t) = (x(t-1) + x(t))/2
)的平均值IMODE=6
在壁虎?
下面的简化代码显示了我在上面提出的问题(代码本身(在物理上)没有意义,但代码从原始代码简化为以更简洁的方式提出问题)。
import numpy as np
from gekko import GEKKO
# Create GEKKO model
m = GEKKO(remote=False)
print(m.path)
m.time = np.linspace(0,5,6)
# Variables
T_air_set_prev = m.Var(value=10, name="T_air_set_prev") # previous -> previous time step (t-1)
T_m0 = m.Var(value=15, name="T_m0")
T_m_ac_t = m.Var(value=25, name="T_m_ac_t") # t -> current time step (t)
T_m_ac = m.Var(value=25, name="T_m_ac")
Q_heating = m.Var(value=0, name="Q_heating")
actuator = m.MV(value=6, lb=3, ub=9, name="actuator")
actuator.STATUS = 1
actuator.DMAX = 1.5
# Equations
m.Equations([T_m0 == 0.5*T_air_set_prev + 1,\
T_m_ac_t == 0.7* T_m0 + 10,\
T_m_ac == (T_air_set_prev + T_m_ac_t)/2,\
Q_heating == 5*T_m_ac+20])
# This line is meant for saving the previous time step's value, but I don't know the correct way to implement this.
T_air_set_prev = m.Intermediate(T_m_ac_t)
m.Obj(0.5*Q_heating + actuator)
# Solve
m.options.IMODE = 6
m.options.DIAGLEVEL = 4
m.options.MAX_ITER = 1000
m.options.SOLVER = 3
m.solve(disp=True)
答案 0 :(得分:2)
尝试将 Gekko 中的 delay
函数用作 shown in the documentation。数字 1
是多少时间步长(不是时间),所以这应该是您所需要的。
m.delay(T_m_ac_t,T_air_set_prev,1)
这是完整的脚本。
import numpy as np
from gekko import GEKKO
# Create GEKKO model
m = GEKKO(remote=False)
#print(m.path)
m.time = np.linspace(0,5,6)
# Variables
# previous -> previous time step (t-1)
T_air_set_prev = m.Var(value=10, name="T_air_set_prev")
T_m0 = m.Var(value=15, name="T_m0")
# t -> current time step (t)
T_m_ac_t = m.Var(value=25, name="T_m_ac_t")
T_m_ac = m.Var(value=25, name="T_m_ac")
Q_heating = m.Var(value=0, name="Q_heating")
actuator = m.MV(value=6, lb=3, ub=9, name="actuator")
actuator.STATUS = 1
actuator.DMAX = 1.5
# Equations
m.Equations([T_m0 == 0.5*T_air_set_prev + 1,\
T_m_ac_t == 0.7* T_m0 + 10,\
T_m_ac == (T_air_set_prev + T_m_ac_t)/2,\
Q_heating == 5*T_m_ac+20])
# Time delay
m.delay(T_m_ac_t,T_air_set_prev,1)
m.Obj(0.5*Q_heating + actuator)
# Solve
m.options.IMODE = 6
m.options.DIAGLEVEL = 0
m.options.MAX_ITER = 1000
m.options.SOLVER = 3
m.solve(disp=True)
答案 1 :(得分:2)
您可能想在 GEKKO 中使用 time delay
函数,m.delay
。
基本上,添加一个新的 gekko 变量,其中 m.delay() 移动一个时间步长,然后您可以像往常一样在等式中使用它。
T_m_ac_t = m.Var(value=25, name="T_m_ac_t") # t -> current time step (t)
T_air_set_prev = m.Var(value=10, name="T_air_set_prev") # previous -> previous time step (t-1)
m.delay(T_m_ac_t, T_air_set_prev, 1)
查看 apmonitor 页面中的示例:Tiem Delay (Dead-Time)