简体   繁体   English

如何以图形方式验证我的动态优化结果,与 gekko 中的初始条件进行比较

[英]How to verify my dynamic optimization results graphically, comparing with initial conditions in gekko

Good morning friends and Professor Hedengren, I am new to Python and even more to Gekko, first of all, I would like to know if my code in Gekko is correct, that is, according to what I physically want, considering that my equations are correct.早上好朋友和Hedengren教授,我是Python的新手,更是Gekko的新手,首先,我想知道我在Gekko中的代码是否正确,也就是说,根据我的物理需要,考虑到我的方程是正确的。

My model tries to optimize the variables M2 and l_M2 (or the combination of these 2 variables), in order to minimize in module the amplitude of movement (positive or negative) of my variable q1 , my model receives inputs from the placed text file here , the model solution must respect the following:我的 model 尝试优化变量M2l_M2 (或这两个变量的组合),以便在模块中最小化我的变量q1的运动幅度(正或负),我的 model 从此处放置的文本文件接收输入, model 解决方案必须遵守以下规定:

  • With the initial values of M2 and l_M2 , the model is solved and the maximum amplitude (positive or negative) of q1 is obtained;M2l_M2 ,求解model,得到q1的最大幅度(正或负);
  • The input values do not vary over the horizon;输入值不会随时间变化;
  • In each iteration the value of the variable c_m2 must be updated according to the value of M2 and l_M2 , and it must remain constant throughout the horizon.在每次迭代中,变量c_m2的值必须根据M2l_M2的值进行更新,并且必须在整个范围内保持不变。

In order to minimize the variable q1 I proposed two types of objectives, which I do not use simultaneously:为了最小化变量q1 ,我提出了两种我不会同时使用的目标:

  • Minimize 1000*q1**2 ;最小化1000*q1**2
  • Minimize the integral of x1 = integral (0.5 q1 ** 2) dt evaluated from 0 to t, for which create an auxiliary variable x1 .最小化从 0 到 t 计算的x1 = integral (0.5 q1 ** 2) dt 的积分,为此创建一个辅助变量x1

Doubts to solve解疑解惑

  • When solving the model, I realized that the value of c_m2 (at the initial point) is 0. Which is not correct, as it should be the same value as the following, so there is an error in my code, which I don't know.在求解model时,我发现c_m2的值(在初始点)是0。这是不正确的,因为它应该和下面的值相同,所以我的代码有错误,我没有不知道。 How to solve;怎么解决;
  • On the other hand, I would like to be able to compare the responses of the model with the initial values of the variables versus the response with the optimized values (as shown in the figure), but I cannot understand how to save my response with the initial values.另一方面,我希望能够将 model 的响应与变量的初始值与优化值的响应进行比较(如图所示),但我无法理解如何保存我的响应初始值。 Optimization check figure优化校验图
  • Is it correct to use m.options.IMODE = 6 in this case?在这种情况下使用m.options.IMODE = 6是否正确?

this is my code:这是我的代码:

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

###################### CREATION OF LOAD RECORD
filename= 'Inputs 0.02sec.txt'
input_l=(np.loadtxt(filename, skiprows=1, dtype=float).flatten()).tolist()
dt=0.02

len_inputs=len(input_l)

m=GEKKO()

# time vector
t_final=dt*(len_inputs-1)
m.time=np.linspace(0, t_final, len_inputs)

# parameters
M1=m.Param(value=21956548.3771968)
Ri=m.Param(value=10609404.1758615)
taxa1=m.Param(value=0.02)
taxa2=m.Param(value=0.005)
grv=m.Param(value=9.80665)
in_loads=m.Param(value=input_l)

m.options.NODES = 4
m.options.IMODE = 6 #MPC

#Intermedias
Om1=m.Intermediate(m.sqrt(Ri/M1))
C_M1=m.Intermediate(2*M1*Om1*taxa1)

# variables
M2=m.FV(value=0.10*21956548.3771968,lb=0.01*M1 , ub=0.20*M1)
M2.STATUS = 1
l_M2=m.FV(value=7, lb=1, ub=20)
l_M2.STATUS = 1
c_m2=m.Var(value=2*taxa2*M2*m.sqrt(grv/l_M2))
x1=m.Var(value=0)           # auxiliar variable for integral of   x1=0.5*integral(q1**2)dt
q1=m.Var(value=0)
q1_p=m.Var(value=0)
q2=m.Var(value=0)
q2_p=m.Var(value=0)

# auxiliar equation for minimization of integral of x1=0.5*integral(q1**2)dt
m.Equation(x1.dt()==0.5*(q1**2))

# equations for actualization of c_m2
m.Equation(c_m2==2*taxa2*m.sqrt(grv/l_M2))

# equations of state
m.Equation(q1.dt()==q1_p)
m.Equation(q1_p.dt()==((-Ri*q1-C_M1*q1_p+M2*grv*q2+(c_m2*q2_p)/l_M2) \
                       /M1-in_loads))
m.Equation(q2.dt()==q2_p)
m.Equation(q2_p.dt()==(Ri*q1+C_M1*q1_p-(M1+M2)*grv*q2)/(l_M2*M1) \
                        -c_m2*(M1+M2)*q2_p/(M1*M2*l_M2**2))


m.Obj(1000*q1**2)       # for minimization of q1  (1000*q1**2)
# m.Obj(x1)             # for minimization of integral 0.5*q1**2


m.solve()

######################################### Plotting the results
fig=plt.figure(1)
ax4 = fig.add_subplot(1,1,1)
ax4.plot(m.time, q1.value, ls='-', label=f'q1 Opt')
ax4.set_ylabel('Amplitude of q1 [m]')
ax4.set_xlabel('Time [sec]')
ax4.set_title('Time - Amplitude \n')
ax4.legend(loc='best')
plt.grid()

minimo,maximo=min(q1.value),max(q1.value)
Max_q1=max(abs(minimo),abs(maximo))

# print results
print ('')
print ('--- Results of the Optimization Problem ---')
print ('M2= ' + str(M2.value))
print ('l_M2 = ' + str(l_M2.value))
print ('c_m2 = ' + str(c_m2.value))
print ('Absolute Max Amplitude q1= ', Max_q1)
print ('Percentage of massa m2= ' + str(M2.value[-1]/M1.value[-1]))

plt.show()

Nice work on this application.这个应用程序做得很好。 Everything that you described looks correct.您描述的一切看起来都是正确的。 The c_m2 value is zero at the beginning because initial conditions are fixed at the specified values (or default 0) and equations are not solved at t=0 . c_m2值一开始为零,因为初始条件固定为指定值(或默认值 0),并且在t=0时不求解方程。

Try using two m.solve() commands with the first one with m.options.COLDSTART=1 (temporarily sets STATUS=0 ) to see the initial and optimized solution.尝试使用两个m.solve()命令和第一个带有m.options.COLDSTART=1 (暂时设置STATUS=0 )的命令来查看初始和优化的解决方案。

m.options.COLDSTART = 1
m.solve()

fig=plt.figure(1)
ax4 = fig.add_subplot(1,1,1)
ax4.plot(m.time, q1.value, ls='-', label=f'q1 Initial')

m.options.COLDSTART = 0
m.options.TIME_SHIFT = 0
m.solve()

ax4.plot(m.time, q1.value, ls='-', label=f'q1 Opt')
ax4.set_ylabel('Amplitude of q1 [m]')
ax4.set_xlabel('Time [sec]')
ax4.set_title('Time - Amplitude \n')
ax4.legend(loc='best')

初始和最优

Here is the full script:这是完整的脚本:

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

###################### CREATION OF LOAD RECORD
filename= 'Inputs 0.02sec.txt'
input_l=(np.loadtxt(filename, skiprows=1, dtype=float).flatten()).tolist()
dt=0.02

len_inputs=len(input_l)

m=GEKKO()

# time vector
t_final=dt*(len_inputs-1)
m.time=np.linspace(0, t_final, len_inputs)

# parameters
M1=m.Param(value=21956548.3771968)
Ri=m.Param(value=10609404.1758615)
taxa1=m.Param(value=0.02)
taxa2=m.Param(value=0.005)
grv=m.Param(value=9.80665)
in_loads=m.Param(value=input_l)

m.options.NODES = 4
m.options.IMODE = 6 #MPC

#Intermedias
Om1=m.Intermediate(m.sqrt(Ri/M1))
C_M1=m.Intermediate(2*M1*Om1*taxa1)

# variables
M2=m.FV(value=0.10*21956548.3771968,lb=0.01*M1 , ub=0.20*M1)
M2.STATUS = 1
l_M2=m.FV(value=7, lb=1, ub=20)
l_M2.STATUS = 1
c_m2=m.Var(value=2*taxa2*M2*m.sqrt(grv/l_M2))
x1=m.Var(value=0)           # auxiliar variable for integral of   x1=0.5*integral(q1**2)dt
q1=m.Var(value=0)
q1_p=m.Var(value=0)
q2=m.Var(value=0)
q2_p=m.Var(value=0)

# auxiliar equation for minimization of integral of x1=0.5*integral(q1**2)dt
m.Equation(x1.dt()==0.5*(q1**2))

# equations for actualization of c_m2
m.Equation(c_m2==2*taxa2*m.sqrt(grv/l_M2))

# equations of state
m.Equation(q1.dt()==q1_p)
m.Equation(q1_p.dt()==((-Ri*q1-C_M1*q1_p+M2*grv*q2+(c_m2*q2_p)/l_M2) \
                       /M1-in_loads))
m.Equation(q2.dt()==q2_p)
m.Equation(q2_p.dt()==(Ri*q1+C_M1*q1_p-(M1+M2)*grv*q2)/(l_M2*M1) \
                        -c_m2*(M1+M2)*q2_p/(M1*M2*l_M2**2))

m.Minimize(1000*q1**2)       # for minimization of q1  (1000*q1**2)

m.options.COLDSTART = 1
m.solve()

fig=plt.figure(1)
ax4 = fig.add_subplot(1,1,1)
ax4.plot(m.time, q1.value, ls='-', label=f'q1 Initial')

m.options.COLDSTART = 0
m.options.TIME_SHIFT = 0
m.solve()

ax4.plot(m.time, q1.value, ls='-', label=f'q1 Opt')
ax4.set_ylabel('Amplitude of q1 [m]')
ax4.set_xlabel('Time [sec]')
ax4.set_title('Time - Amplitude \n')
ax4.legend(loc='best')

minimo,maximo=min(q1.value),max(q1.value)
Max_q1=max(abs(minimo),abs(maximo))

# print results
print ('')
print ('--- Results of the Optimization Problem ---')
print ('M2= ' + str(M2.value))
print ('l_M2 = ' + str(l_M2.value))
print ('c_m2 = ' + str(c_m2.value))
print ('Absolute Max Amplitude q1= ', Max_q1)
print ('Percentage of massa m2= ' + str(M2.value[-1]/M1.value[-1]))

plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM