使用 python GEKKO 库 运行 状态 space 方程时出错

Error whilst running a state space equation using python GEKKO library

尝试对建筑物的热损失进行建模,我使用状态-space 方程将以下代码实施到 运行 模型。它似乎按照预期从 .csv 文件中获取变量,并将它们排列成正确形成的矩阵。但是,它正在返回 "Exception: Data arrays must have the same length, and match time discretization in dynamic problems"。 在尝试调试时,我打印了方程式中的所有矩阵以查看它们是否匹配(见下文)。我觉得这个应该很容易看出来,但是我不知道哪里做错了。

[[-9.15750916  9.15750916]
 [-0.12465719  0.13056645]]
[[295]
 [295]]
[[0.00000000e+00 7.38461538e+00 3.84615385e+00]
 [5.90925744e-03 0.00000000e+00 0.00000000e+00]]
[[[21.  14.  23.  28.  19. ]]

 [[ 1.5  1.2  1.4  1.7  2. ]]

 [[ 0.   0.   0.   0.   0. ]]]
[[1 0]]
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# SYSTEM BOUNDARIES
comfort = {'Occupied': np.array([20, 24])+273.15, \
           'Unoccupied': np.array([18, 26])+273.15}  
build_type = {'detached':np.array([0.42,0.26,8.86,19.1,1.92]),\
             'semi-detached':np.array([]),\
             'end-terrace':np.array([]),\
             'mid-terrace':np.array([]),\
             'bungalow':np.array([]),\
             'flat':np.array([])}
# get csv data
# datafile = ""
df = pd.read_csv(r"COP_test.csv")
# sort data by column names
x0 = np.array(df["Ta"]) # Ambient temp
x1 = np.array(df["Ih"]) # Solar radiation
x2 = np.array(df["Qo"]) # Additional heating influences
#GEKKO model and initialise model simulator
m = GEKKO(remote=False); m.options.IMODE=4
# Building temperature variables
T_int = m.FV(295, lb=18+273.15, ub=24+273.15)
T_wal = m.FV(295, lb=15+273.15, ub=30+273.15)
# independent variables
Q_ext = m.Array(m.Param,3); Q_ext[0].value=x0; Q_ext[1].value=x1; Q_ext[2].value=x2, 
# building coefficients
R_int, C_int, R_wal, C_wal, gA = build_type[btype]
# Matrix equations
A = np.array([[-1/(R_int*C_int), 1/(R_int*C_int)],
              [-1/(R_int*C_wal), -(-1/(R_wal*C_wal)-1/(R_int*C_wal))]])
Bc = 1/C_int
Bx = np.array([[0, gA/C_int, 1/C_int],
              [1/(R_wal*C_wal), 0, 0]])
C = np.array([[1, 0]])
# [T_int.dt(), T_wal.dt()] = m.equations()
x, y, u = m.state_space(A,Bx,C,D=None,discrete=True)
# right now does not include Bc, could append into matrix Bx
u[0].value = np.array([[x0],[x1],[x2]])
x[0].value = np.array([[T_int],[T_wal]])
print(A, x[0].value, Bx, u[0].value, C, sep='\n')
m.time = np.linspace(0,24,len(x0)) # time points, ideally to have a reading every half hour
m.options.nodes = 4
m.solve() # (GUI=True)

代码需要一些修改才能导入数据,但模型很好。该脚本还缺少数据文件和 btype 变量。这是一些用于 data.csv.

测试的示例数据
time,Ta,Ih,Qo
0,30,0,0
4,31,0,0
8,35,10,20
12,38,20,30
16,40,50,20
20,37,30,10
24,30,0,0

关于以后的问题,请附上一个完整的例子。这是修改后的代码。我已经注释掉了状态限制,因为这些可能导致不可行的解决方案。您还需要对离散状态 space 模型使用 m.options.NODES=2

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# SYSTEM BOUNDARIES
comfort = {'Occupied': np.array([20, 24])+273.15, \
           'Unoccupied': np.array([18, 26])+273.15}  
build_type = {'detached':np.array([0.42,0.26,8.86,19.1,1.92]),\
             'semi-detached':np.array([]),\
             'end-terrace':np.array([]),\
             'mid-terrace':np.array([]),\
             'bungalow':np.array([]),\
             'flat':np.array([])}
# get csv data
df = pd.read_csv(r"test.csv")
btype = 'detached'
#GEKKO model and initialise model simulator
m = GEKKO(remote=False); m.options.IMODE=4

T_int = m.FV(295, lb=18+273.15, ub=24+273.15)
T_wal = m.FV(295, lb=15+273.15, ub=30+273.15)

# building coefficients
R_int, C_int, R_wal, C_wal, gA = build_type[btype]
# Matrix equations
A = np.array([[-1/(R_int*C_int), 1/(R_int*C_int)],
              [-1/(R_int*C_wal), -(-1/(R_wal*C_wal)-1/(R_int*C_wal))]])
Bc = 1/C_int
Bx = np.array([[0, gA/C_int, 1/C_int],
              [1/(R_wal*C_wal), 0, 0]])
C = np.array([[1, 0]])
# State Space
x, y, u = m.state_space(A,Bx,C,D=None,discrete=True)
# right now does not include Bc, could append into matrix Bx
Ta,Ih,Qo = u
# Building temperature variables
T_int,T_wal = x
T_int.value = 295
T_wal.value = 295

# Bounds on states can make the problem infeasible
#T_int.lower = 18+273.15
#T_int.upper = 24+273.15
#T_wal.lower = 15+273.15
#T_wal.upper = 30+273.15

# steady-state initialization
#m.options.IMODE = 1 
#m.solve() # (GUI=True)

# dynamic simulation
m.time = df['time'] # time points
m.options.nodes = 2 # must be 2 for discrete models
Ta.value = df["Ta"] # Ambient temp
Ih.value = df["Ih"] # Solar radiation
Qo.value = df["Qo"] # Additional heating influences
m.solve()

additional state space examples in the documentation, including a complete MPC application with an Arduino microcontroller.