本文介绍了在Gekko中进行不频繁的测量并带有额外的仿真点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个更复杂的动态数据协调问题,该问题与

 将numpy导入为np将matplotlib.pyplot导入为plt#plot解决方案plt.plot(m.time,x.value,'bo',\label ='Predicted(k ='+ str(np.round(k,2))+')')plt.plot(m.time,x_data,'rx',label ='Measured')#绘制精确解t = np.linspace(0,1);xe = 2 * np.exp(-k * t)plt.plot(t,xe,'k:',label ='Exact Solution')plt.legend()plt.xlabel('时间'),plt.ylabel('值')plt.show() 

如何在Gekko中使用具有更频繁的仿真点和不频繁测量的低阶配置模式?

解决方案

您可以使用 join()函数在Pandas中将不频繁的测量与更频繁的模拟步骤结合起来:

从gekko导入

 导入GEKKO将numpy导入为np将熊猫作为pd导入# 测量t_data = [0,0.1,0.2,0.4,0.8,1]x_data = [2.0,1.6,1.2,0.7,0.3,0.15]df1 = pd.DataFrame({'time':t_data,'x':x_data})df1.set_index('time',inplace = True)#模拟时间点df2 = pd.DataFrame({'time':np.linspace(0,1,51)})df2.set_index('time',inplace = True)#合并数据框df = df2.join(df1,how ='outer')#获得True(1)或False(0)进行测量df ['meas'] =(df ['x'].values == df ['x'].values).astype(int)#用零替换NaNdf0 = df.fillna(值= 0)打印(df.head(15)) 

这为缺少的数据点提供了带有 NaN 的组合集.

  x测量时间0.00 2.0 10.02 NaN 00.04 NaN 00.06 NaN 00.08 NaN 00.10 1.6 10.12 NaN 00.14 NaN 00.16 NaN 00.18钠00.20 1.2 10.22 NaN 00.24 NaN 00.26 NaN 00.28 NaN 0 

然后您可以创建一个修改后的目标函数,该函数使用 meas 来控制何时仅在定义的点上最小化仿真和测量.

  m = GEKKO(remote = False)m.time = df.index.valuesx = m.Var(value = x_data [0])#适合测量xd = m.Param(df0 ['x'].values)meas = m.Param(df0 ['meas'].values)k = m.FV();k.STATUS = 1#可调参数m.Equation(x.dt()== -k * x)#微分方程m.最小化(meas *(x-xd)** 2)m.options.SOLVER = 1#APOPT求解器m.options.IMODE = 5#动态估算m.options.NODES = 2#个并置节点m.solve(disp = True)#显示求解器输出k = k.value [0] 

图显示结果.我包含了更多的数据点,因为 NODES = 2 不太准确.切换到 NODES = 3 可以比添加更多点更有助于提高准确性.

 将numpy导入为np将matplotlib.pyplot导入为plt#plot解决方案plt.plot(m.time,x.value,'bo',\label ='Predicted(k ='+ str(np.round(k,2))+')')plt.plot(m.time,df ['x'].values,'rs',label ='Measured')#绘制精确解t = np.linspace(0,1);xe = 2 * np.exp(-k * t)plt.plot(t,xe,'k:',label ='Exact Solution')plt.legend()plt.xlabel('时间'),plt.ylabel('值')plt.ylim([-0.2,2.2])plt.show() 

I'm solving a more complex dynamic data reconciliation problem that is similar to the example problem posted in the APMonitor dynamic optimization course. Similar to this problem, I have infrequent measurements at irregular intervals. Instead of using the more accurate NODES=5 (for increased accuracy), I would like to have more frequent simulation points with NODES=2 at [0,0.1,0.2,0.3,0.4,0.5,0.6.0.7,0.8,0.9,1.0] that also includes the infrequent measurements for the objective at [0, 0.1, 0.2, 0.4, 0.8, 1].

from gekko import GEKKO
t_data = [0, 0.1, 0.2, 0.4, 0.8, 1]
x_data = [2.0,  1.6,  1.2, 0.7,  0.3,  0.15]
m = GEKKO(remote=False)
m.time = t_data
x = m.Var(value=x_data)     # fit to measurement
xd = m.Param(x_data)
k = m.FV(); k.STATUS = 1    # adjustable parameter
m.Equation(x.dt()== -k * x) # differential equation
m.Minimize((x-xd)**2)
m.options.IMODE = 5   # dynamic estimation
m.options.NODES = 2   # collocation nodes
m.solve(disp=False)   # display solver output
k = k.value[0]

One of the reasons for this is that the simulation model needs to solve more frequently than the measurements to maintain integration accuracy with NODES=2. The exact solution deviates from the numerical solution if I only solve at the measurement points.

import numpy as np
import matplotlib.pyplot as plt  # plot solution
plt.plot(m.time,x.value,'bo',\
         label='Predicted (k='+str(np.round(k,2))+')')
plt.plot(m.time,x_data,'rx',label='Measured')
# plot exact solution
t = np.linspace(0,1); xe = 2*np.exp(-k*t)
plt.plot(t,xe,'k:',label='Exact Solution')
plt.legend()
plt.xlabel('Time'), plt.ylabel('Value')
plt.show()

How can I use a lower order collocation mode with more frequent simulation points and infrequent measurements in Gekko?

解决方案

You can combine infrequent measurements with more frequent simulation steps in Pandas with the join() function:

from gekko import GEKKO
import numpy as np
import pandas as pd
# measurements
t_data = [0,0.1,0.2,0.4,0.8,1]
x_data = [2.0,1.6,1.2,0.7,0.3,0.15]
df1 = pd.DataFrame({'time':t_data,'x':x_data})
df1.set_index('time', inplace=True)
# simulation time points
df2 = pd.DataFrame({'time':np.linspace(0,1,51)})
df2.set_index('time', inplace=True)
# merge dataframes
df = df2.join(df1,how='outer')
# get True (1) or False (0) for measurement
df['meas'] = (df['x'].values==df['x'].values).astype(int)
# replace NaN with zeros
df0 = df.fillna(value=0)
print(df.head(15))

This gives the combined set with NaN for the missing data points.

        x  meas
time
0.00  2.0     1
0.02  NaN     0
0.04  NaN     0
0.06  NaN     0
0.08  NaN     0
0.10  1.6     1
0.12  NaN     0
0.14  NaN     0
0.16  NaN     0
0.18  NaN     0
0.20  1.2     1
0.22  NaN     0
0.24  NaN     0
0.26  NaN     0
0.28  NaN     0

You can then create a modified objective function that uses meas to control when to minimize the simulation and measurement only at the defined points.

m = GEKKO(remote=False)
m.time = df.index.values
x = m.Var(value=x_data[0])     # fit to measurement
xd = m.Param(df0['x'].values)
meas = m.Param(df0['meas'].values)
k = m.FV(); k.STATUS = 1    # adjustable parameter
m.Equation(x.dt()== -k * x) # differential equation
m.Minimize(meas*(x-xd)**2)
m.options.SOLVER = 1  # APOPT solver
m.options.IMODE = 5   # dynamic estimation
m.options.NODES = 2   # collocation nodes
m.solve(disp=True)   # display solver output
k = k.value[0]

A plot shows the results. I included more data points because NODES=2 isn't very accurate. A switch to NODES=3 helps to improve the accuracy much more than adding more points.

import numpy as np
import matplotlib.pyplot as plt  # plot solution
plt.plot(m.time,x.value,'bo',\
         label='Predicted (k='+str(np.round(k,2))+')')
plt.plot(m.time,df['x'].values,'rs',label='Measured')
# plot exact solution
t = np.linspace(0,1); xe = 2*np.exp(-k*t)
plt.plot(t,xe,'k:',label='Exact Solution')
plt.legend()
plt.xlabel('Time'), plt.ylabel('Value')
plt.ylim([-0.2,2.2])
plt.show()

这篇关于在Gekko中进行不频繁的测量并带有额外的仿真点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:25