[英]How can I choose a specific period over the time span to integrate my objective function in Gekko?
I want to choose a specific time period over the time span to define my objective function in Gekko.我想在时间跨度内选择一个特定的时间段来定义我在 Gekko 中的目标 function。
For example, I want to minimize the integral of u^2 from t0=5 to tf=8.例如,我想最小化 u^2 从 t0=5 到 tf=8 的积分。 However, there seems to be no such way in Gekko.
然而,在Gekko中似乎没有这样的方式。
#%%Import packages
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
#%% Build model
#initialize GEKKO model
m = GEKKO()
#time
m.time = np.linspace(0,10,101)
#Parameters
mass1 = m.Param(value=10)
mass2 = m.Param(value=1)
final = np.zeros(np.size(m.time))
for i in range(np.size(m.time)):
if m.time[i] >= 6.2:
final[i] = 1
else:
final[i] = 0
final = m.Param(value=final)
#Manipulated variable
u = m.Var(value=0)
#Variables
theta = m.Var(value=0)
q = m.Var(value=0)
#Controlled Variable
y = m.Var(value=-1)
v = m.Var(value=0)
#Equations
m.Equations([y.dt() == v,
v.dt() == mass2/(mass1+mass2) * theta + u,
theta.dt() == q,
q.dt() == -theta - u])
#Objective
m.Obj(final * (y**2 + v**2 + theta**2 + q**2))
m.Obj(0.001 * u**2)
#%% Tuning
#global
m.options.IMODE = 6 #control
#%% Solve
m.solve()
#%% Plot solution
plt.figure()
plt.subplot(4,1,1)
plt.plot(m.time,u.value,'r-',lw=2)
plt.ylabel('Force')
plt.legend(['u'],loc='best')
plt.subplot(4,1,2)
plt.plot(m.time,v.value,'b--',lw=2)
plt.legend(['v'],loc='best')
plt.ylabel('Velocity')
plt.subplot(4,1,3)
plt.plot(m.time,y.value,'g:',lw=2)
plt.legend(['y'],loc='best')
plt.ylabel('Position')
plt.subplot(4,1,4)
plt.plot(m.time,theta.value,'m-',lw=2)
plt.plot(m.time,q.value,'k.-',lw=2)
plt.legend([r'$\theta$','q'],loc='best')
plt.ylabel('Angle')
plt.xlabel('Time')
plt.show()
The best I can do now is我现在能做的就是
m.Obj(m.integral(u**2))
But this will generate the integral over the entire time span instead of the period I want.但这将生成整个时间跨度而不是我想要的时间段的积分。
I want to do something like,我想做类似的事情
m.Obj(m.integral(u**2, t0 = 5, tf = 8))
Is there any way to achieve my goal in Gekko?有什么办法可以在 Gekko 中实现我的目标吗?
Use a new parameter interval
that is 1
in the range t>=5
and t<=8
.使用范围
t>=5
和t<=8
的新参数interval
1
。
p = np.zeros(101)
p[np.where((m.time>=5)&(m.time<=8))]=1
interval = m.Param(p)
This can then be applied in the objective function.然后可以将其应用于目标 function。
m.Minimize(0.001 * interval* u**2)
Here are the results:以下是结果:
#%%Import packages
import numpy as np
from gekko import GEKKO
import matplotlib.pyplot as plt
#%% Build model
#initialize GEKKO model
m = GEKKO(remote=False)
#time
m.time = np.linspace(0,10,101)
p = np.zeros(101)
p[np.where((m.time>=5)&(m.time<=8))]=1
interval = m.Param(p)
#Parameters
mass1 = m.Param(value=10)
mass2 = m.Param(value=1)
final = np.zeros(np.size(m.time))
for i in range(np.size(m.time)):
if m.time[i] >= 6.2:
final[i] = 1
else:
final[i] = 0
final = m.Param(value=final)
#Manipulated variable
u = m.Var(value=0)
#Variables
theta = m.Var(value=0)
q = m.Var(value=0)
#Controlled Variable
y = m.Var(value=-1)
v = m.Var(value=0)
#Equations
m.Equations([y.dt() == v,
v.dt() == mass2/(mass1+mass2) * theta + u,
theta.dt() == q,
q.dt() == -theta - u])
#Objective
m.Minimize(final * (y**2 + v**2 + theta**2 + q**2))
m.Minimize(0.001 * interval* u**2)
#%% Tuning
#global
m.options.IMODE = 6 #control
#%% Solve
m.solve()
#%% Plot solution
plt.figure()
plt.subplot(4,1,1)
plt.plot(m.time,u.value,'r-',lw=2)
plt.ylabel('Force')
plt.legend(['u'],loc='best')
plt.subplot(4,1,2)
plt.plot(m.time,v.value,'b--',lw=2)
plt.legend(['v'],loc='best')
plt.ylabel('Velocity')
plt.subplot(4,1,3)
plt.plot(m.time,y.value,'g:',lw=2)
plt.legend(['y'],loc='best')
plt.ylabel('Position')
plt.subplot(4,1,4)
plt.plot(m.time,theta.value,'m-',lw=2)
plt.plot(m.time,q.value,'k.-',lw=2)
plt.legend([r'$\theta$','q'],loc='best')
plt.ylabel('Angle')
plt.xlabel('Time')
plt.savefig('results.png',dpi=300)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.