我开始学习壁虎,我正在研究时间最优控制问题.我正在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)

推荐答案

随着Gekko之前的发布,参数的顺序更改为m.fix().正确的顺序是m.fix(variable, value, time point),如documentation所示.

m.fix(Z2, 0, len(m.time)-1)
m.fix(Z1, 300, len(m.time)-1)

optimal solution

较新的功能m.fix_final()还消除了指定结束时间索引的需要.

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_final(Z2, 0)
m.fix_final(Z1, 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()

Python-3.x相关问答推荐

将字符串转换为python日期时间时出错

在循环中使用Print&S结束参数时出现奇怪的问题

Pandas 插入的速度太慢了.对于跟踪代码,什么是更快的替代方案?

错误:无法为 pyconcorde 构建轮子,这是安装基于 pyproject.toml 的项目所必需的

将自定义函数应用于 pandas 数据框的每一列

如何获取实例化 `types.GenericAlias` 的下标类?

为什么不能用格式字符串 '-' 绘制点?

从列表的元素和python中的多个多索引数据帧执行方程

在 groupby 之后,Pandas 在特定类别中获得最常见和最后的值

无法使用 curve_fit() 在 python 中复制高斯函数的曲线拟合

Python 3 - 给定未知数量的类别动态地将字典嵌套到列表中

过滤阈值大小数据以使用 Pyspark 或 Python 读取

Django 2 个字段之一不能为空

多个返回函数的列表理解?

Generic[T] 基类 - 如何从实例中获取 T 的类型?

multiprocessing.Queue 中的 ctx 参数

pandas 中 df.reindex() 和 df.set_index() 方法的区别

为什么 2to3 将 mydict.keys() 更改为 list(mydict.keys())?

如何遍历某些扩展名的文件?

如何使用 Python 订阅 Websocket API 通道?