我开始学习壁虎,我正在研究时间最优控制问题.我正在try 用Gekko解决以下问题:Time optimal car problem
在研究如何使用Gekko实现此问题时,我发现了以下代码:
from gekko import GEKKO
import matplotlib.pyplot as plt
import numpy as np
# set up the gekko model
m = GEKKO()
# set up the time (minimize the time with time scaling)
m.time = np.linspace(0, 1, 100)
# set up the variables
Z1 = m.Var(value=0, ub=330, lb=0)
Z2 = m.Var(value=0, ub=33, lb=0)
m.fix(Z2, len(m.time)-1, 0)
m.fix(Z1, len(m.time)-1, 300)
# set up the value we modify over the horizon
tf = m.FV(value=500, lb=0.1)
tf.STATUS = 1
# set up the MV
u = m.MV(integer=True, lb=-2, ub=1)
u.STATUS = 1
# set up the equations
m.Equation(Z1.dt() / tf == Z2)
m.Equation(Z2.dt() / tf == u)
# set the objective
m.Obj(tf)
# set up the options
m.options.IMODE = 6
m.options.SOLVER = 1
# solve
m.solve(disp=False)
# print the time
print("Total time taken: " + str(tf.NEWVAL))
# plot the results
plt.figure()
plt.subplot(211)
plt.plot(np.linspace(0,1,100)*tf.NEWVAL, Z1, label=r'$Z_1$')
plt.plot(np.linspace(0,1,100)*tf.NEWVAL, Z2, label=r'$Z_2$')
plt.ylabel('Z')
plt.legend()
plt.subplot(212)
plt.plot(np.linspace(0,1,100)*tf.NEWVAL, u, label=r'$u$')
plt.ylabel('u')
plt.xlabel('Time')
plt.legend()
plt.show()
但在执行此代码时,它抛出以下错误:
Traceback (most recent call last):
File ~\OneDrive\Sin título2.py:43 in <module>
m.solve(disp=False)
File ~\anaconda3\lib\site-packages\gekko\gekko.py:2044 in solve
self._write_csv()
File ~\anaconda3\lib\site-packages\gekko\gk_write_files.py:224 in _write_csv
t[i[0]+1] = i[1] #index is +1 because of prepended header
IndexError: index 301 is out of bounds for axis 0 with size 101
我意识到错误来自于m.time
的大小应该是300而不是m.time
.即使我进行了更改,我仍然收到相同的错误,即使我更改为更高的值,程序仍保持运行.此外,配置变量Z1, Z2
应该是:
Z1 = m.Var(value=0, ub=33, lb=0)
Z2 = m.Var(value=0, ub=330, lb=0)