简体   繁体   English

我如何选择时间跨度内的特定时间段将我的目标 function 整合到 Gekko 中?

[英]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>=5t<=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.

相关问题 如何在一段时间内 map 一只股票的特定行业? - How can I map a stock's specific sector over a period of time? 如何在没有已知目标函数(比如一些随机函数)和已知变量和约束的情况下使用 gekko 优化器? - how can I use gekko optimizer without a known objective function(let say some random function) and with known variables and constraints? 如何在 Python Gekko 中最大化目标 function? - How to maximize an objective function in Python Gekko? 如何在Gekko获得目标函数的价值 - How to get value for objective function in Gekko 重新采样/时间分组到特定的时间跨度/周期 - Resampling/time groupby to a specific time span/period 如果我的目标函数是非线性(也是指数解释)函数,我应该使用什么求解器? Python GEKKO - What solver should I use if my objective function is an nonlinear (also exponential explanation) function? Python GEKKO 特定时间段内的平均值 - Average over a specific time period 如何在特定时间段内平均数据记录结束时间 - How to average of data over specific time period recoding the ending time Python Gekko 中目标函数的约束 - Constraint on objective function in Python Gekko 我如何以数字方式(有效地)集成和 plot 我的 function? - How can I numerically (and efficiently) integrate and plot my function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM