我试图用gekko来模拟一个带有输入(uu_DF)和初始条件(init_cond_DF)的状态空间方程。 该代码适用于小型阵列(例如,3*3)。然而,在高等级A、B、C矩阵(等级(A)=4553)中,代码出现以下错误 请注意,由于我的笔记本电脑(i5cpu@2.67ghz+4gbram)无法运行,我正在使用googlecolab来运行此代码
apm 35.196.244.129_gk_model5 <br><pre> ----------------------------------------------------------------
APMonitor, Version 0.9.2
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 1
Constants : 0
Variables : 27318
Intermediates: 0
Connections : 27318
Equations : 0
Residuals : 0
@error: Model Undefined
Error, did not find at least one of A, B, or C matrices:
statespace1.a.txt
statespace1.b.txt
statespace1.c.txt
Stopping...
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-13-bb51a821614c> in <module>()
14 #m.options.nodes = 2
15 m.open_folder
---> 16 m.solve() # (GUI=True)
17
18
/usr/local/lib/python3.6/dist-packages/gekko/gekko.py in solve(self, disp, debug, GUI, **kwargs)
2172 #print APM error message and die
2173 if (debug >= 1) and ('@error' in response):
-> 2174 raise Exception(response)
2175
2176 #load results
Exception: @error: Model Undefined
Error, did not find at least one of A, B, or C matrices:
statespace1.a.txt
statespace1.b.txt
statespace1.c.txt
Stopping...
我的代码如下所示
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
import pandas as pd
# Importing matrices via .csv file
M = pd.read_csv(r'M.csv',header=None)
C = pd.read_csv (r'C.csv',header=None)
K = pd.read_csv (r'K.csv',header=None)
X0 = pd.read_csv (r'X0.csv',header=None)
F = pd.read_csv (r'F.csv',header=None)
# Some prerequisite calculations
dim = len(X0)
DF_eye = pd.DataFrame(np.eye(dim,dtype='uint8'))
DF_zeros = pd.DataFrame(np.zeros((dim, dim),dtype='uint8'))
BB = DF_eye # coefficient inside B matrix
M_inv_DF = pd.DataFrame(np.linalg.inv(M))
CC = pd.DataFrame(np.zeros((2*dim, 2*dim),dtype='uint8')) # controller matrix
# System dynamics matrix
A_Row1_DF = pd.concat((DF_zeros, DF_eye), axis=1)
A_Row2_DF = pd.concat((M_inv_DF * K, M_inv_DF * C), axis=1)
A_DF = pd.concat((A_Row1_DF, A_Row2_DF), axis=0)
# System input matrix
B_Row1_DF = pd.concat((DF_zeros, DF_zeros), axis=1)
B_Row2_DF = pd.concat((DF_zeros, M_inv_DF * BB), axis=1)
B_DF = pd.concat((B_Row1_DF, B_Row2_DF), axis=0)
# Input matrix
uu_DF = pd.concat((pd.DataFrame(np.zeros((dim, 1))), pd.DataFrame(F)), axis=0)
# Initial condition matrix
init_cond_DF = pd.concat((X0, X0), axis=0)
m = GEKKO() # create GEKKO model
x,y,u = m.state_space(A_DF,B_DF,CC,D=None)
m.time = np.linspace(0,120,2*dim)
u[0].value = np.array(uu_DF)
x[0].value = np.array(init_cond_DF)
m.options.imode = 4
m.solve(GUI=True) # (GUI=True)
plt.subplot(2,2,1)
plt.plot(m.time,u[0].value,'r-',label=r'$Input$')
plt.legend()
plt.subplot(2,2,2)
plt.plot(m.time,x[0].value,'b--',label=r'$Initial condition$')
plt.legend()
plt.subplot(2,2,3)
plt.plot(m.time,x[4],'.-',label=r'$x_4$')
plt.legend()
plt.show()
我测试了我的A级矩阵代码:82126170214258298342386430474第一批四个系统用最后一次的120秒进行求解,求解时间最多4.36秒。接下来的五个系统的最终时间为60秒,且解决时间呈指数增长至100.13秒。矩阵秩为474的系统是gekko提供的最后一个模拟系统,其最终时间为10秒,求解时间为113.5秒。执行时间为8700.554s! 在我下一次尝试A秩902的矩阵时,gekko在最后时间为10秒时产生以下错误:
Error: 'results.json' not found. Check above for additional error details
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-14-897bb2f533e0> in <module>()
10 #m.options.NODES = 4
11 #m.open_folder()
---> 12 m.solve() # (GUI=True)
________________________________________
1 frames
________________________________________
/usr/local/lib/python3.6/dist-packages/gekko/gk_post_solve.py in load_JSON(self)
11 ## options.JSON has all APM options
12 def load_JSON(self):
---> 13 f = open(os.path.join(self._path,'options.json'))
14 data = json.load(f)
15 f.close()
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpajn2dnt_gk_model1/options.json'
如果您只需要模拟一个状态空间模型,那么
scipy.signal
可能是一个不错的选择。还有Scipy.signalhere的其他示例如果需要使用Gekko进行模型预测控制,那么可以使用^{} matrix option 将矩阵转换为稀疏形式。这有助于大型系统。还有Time Series (ARX)和Discrete State Space表格
相关问题 更多 >
编程相关推荐