微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Gekko Example 1b 得到不同形式的相同终端约束的不同结果

如何解决Gekko Example 1b 得到不同形式的相同终端约束的不同结果

我尝试了https://apmonitor.com/do/index.php/Main/DynamicOptimizationBenchmarks示例 1b

我构造了两种不同形式的相同终止条件如下:

import numpy as np
import matplotlib.pyplot as plt
from gekko import GEKKO

m = GEKKO()
nt = 101
m.time = np.linspace(0,2,nt)

# Variables
x1 = m.Var(value=1)
x2 = m.Var(value=0)
u = m.Var(value=-0.48)
p = np.zeros(nt)
p[-1] = 1.0
final = m.Param(value=p)

# Equations
m.Equation(x1.dt()==u)
m.Equation(x2.dt()==x1**2 + u**2)

# terminal conditions
m.Equation(final*(x1-1)==0) # 1st form
# m.fix_final(x1,val=1) # 2nd form

m.Obj(x2*final)
m.options.IMODE = 6
m.solve()

plt.figure(1)
plt.plot(m.time,x1.value,'k:',linewidth=2,label=r'$x_1$')
plt.plot(m.time,x2.value,'b-',label=r'$x_2$')
plt.plot(m.time,u.value,'r--',label=r'$u$')
plt.legend(loc='best')plt.xlabel('Time')plt.ylabel('Value')plt.show()

他们的行为不同,并实现了如下不同的解决方案。

第一种形式m.Equation(final*(x1-1)==0)

enter image description here

第二种形式m.fix_final(x1,val=1)

enter image description here

我更喜欢使用 fix_final 来定义像 x(tf)=0 这样的终端约束。但这似乎使问题无法解决。就像https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization中的第十三个问题

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt  

m = GEKKO() # initialize GEKKO
nt = 501
m.time = np.linspace(0,1,nt)

x1 = m.Var(value=np.pi/2.0)
x2 = m.Var(value=4.0)
x3 = m.Var(value=0.0)
p = np.zeros(nt) # final time = 1
p[-1] = 1.0
final = m.Param(value=p)
tf = m.FV(value=1.0,lb=0.1,ub=100.0)
tf.STATUS = 1

u = m.MV(value=0,lb=-2,ub=2)
u.STATUS = 1
m.Equation(x1.dt()==u*tf)
m.Equation(x2.dt()==m.cos(x1)*tf)
m.Equation(x3.dt()==m.sin(x1)*tf)

# terminal constraints
m.Equation(x2*final<=0) # get solution
m.Equation(x3*final<=0)
# or
# m.fix(x2,pos=len(m.time)-1,val=0) # solution not found
# m.fix(x3,val=0)
# or
# m.fix_final(x2,val=0) # solution not found
# m.fix_final(x3,val=0)

m.Obj(tf)
m.options.IMODE = 6
m.solve()

plt.figure(1)
plt.plot(tm,'k-',label=r'$x_1$')
plt.plot(tm,label=r'$x_2$')
plt.plot(tm,x3.value,'g--',label=r'$x_3$')
plt.plot(tm,label=r'$u$')plt.legend(loc='best')plt.xlabel('Time')plt.show()

解决方法

当您确定变量的最终条件时,它还会将导数设置为零。这也作为 issue on GitHub 讨论。此问题有一些解决方法,例如软终端约束(请参阅 Trajectory Planner with GEKKO is not able to handle given goal velocities)或您展示的方式的硬终端约束。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。