时间最优控制示例 GEKKO

2024年 2月 10日 29.7k 0

时间最优控制示例 gekko

问题内容

我正在尝试在 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
POSITION = m.Var(value=0, ub=330, lb=0)
VELOCITY = m.Var(value=0, ub=33, lb=0)
m.fix_final(VELOCITY, 0)
m.fix_final(POSITION, 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(POSITION.dt() / tf == VELOCITY)
m.Equation(VELOCITY.dt() / tf == u)

# set the objective
m.Obj(tf)

# set up the options
m.options.IMODE = 6 # optimal control
m.options.SOLVER = 3 # IPOPT

# 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, POSITION, label='Position')
plt.plot(np.linspace(0,1,100)*tf.NEWVAL, VELOCITY, label='Velocity')
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()

登录后复制

照原样,它工作得很好,但是当我想删除对 velocity 最终值的约束时。

如果我注释 m.fix_final(velocity, 0) 行,结果不会改变。无论如何,它似乎假设最终速度应该为零。此外,如果我将最终速度从零更改为任何其他数字,我会从 gekko 收到错误: exception: @error: solution not found

该解决方案应该很容易找到,特别是如果对最终速度没有施加任何约束,则最佳控制将是在整个时间内保持加速()。

任何帮助将不胜感激! 🙂

正确答案

将最终约束从 m.fix_final(velocity, 0)m.fix_final(position, 300) 更改为:

p = np.zeros(100); p[-1] = 1
last = m.Param(p)
m.Equation(last*(POSITION-300)>=0)

登录后复制

这在最后一个节点应用了不等式约束,以便 position>=300,但它也可以是等式约束。如果不可行的解决方案阻止求解器实现最终条件,我们有时也会使用软约束,例如 m.minimize(last*(position-300)**2) 。相反,它会尝试使解决方案尽可能接近最终约束。当使用 m.fix_final() 固定最终值时,导数也固定为零,因为不再计算该变量。这是 gekko 的已知限制,如此处所述。

以上就是时间最优控制示例 GEKKO的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

如何删除WordPress中的所有评论
检查WordPress服务器磁盘使用情况的7种简便方法(查找大文件和数据)
如何更改WordPress常量FS_METHOD
如何为区块编辑器、Elementor等构建WordPress文章模板
如何彻底地删除WordPress主题及相关内容
如何使用WordPress搭建一个内网

发布评论