卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

时间最优控制示例 GEKKO

时间最优控制示例 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 的已知限制,如此处所述。

卓越飞翔博客
上一篇: Go:如何在地图和切片中使用接口
下一篇: 如何在Golang中使用Mockery v0.0.0-dev?
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏