gpt4 book ai didi

python - 为什么 GEKKO 没有进行初始测量?

转载 作者:行者123 更新时间:2023-12-03 02:19:36 25 4
gpt4 key购买 nike

在使用 GEKKO 通过初始测量对动态系统进行建模时,即使 FSTATUS 打开,GEKKO 似乎也完全忽略了测量。造成这种情况的原因是什么以及如何让 GEKKO 识别初始测量值?

我希望求解器能够考虑初始测量并相应地调整解决方案。

adjust

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

# measurement
tm = 0
xm = 25

m = GEKKO()
m.time = np.linspace(0,20,41)
tau = 10
b = m.Param(value=50)
K = m.Param(value=0.8)

# Manipulated Variable
u = m.MV(value=0, lb=0, ub=100)
u.STATUS = 1 # allow optimizer to change
u.DCOST = 0.1
u.DMAX = 30

# Controlled Variable
x = m.CV(value=0,name='x')
x.STATUS = 1 # add the SP to the objective
m.options.CV_TYPE = 2 # squared error
x.SP = 40 # set point
x.TR_INIT = 1 # set point trajectory
x.TAU = 5 # time constant of trajectory
x.FSTATUS = 1
x.MEAS = xm

# Process model
m.Equation(tau*x.dt() == -x + K*u)
m.options.IMODE = 6 # control
m.solve()

# get additional solution information
import json
with open(m.path+'//results.json') as f:
results = json.load(f)
plt.figure()
plt.subplot(2,1,1)
plt.plot(m.time,u.value,'b-',label='MV Optimized')
plt.legend()
plt.ylabel('Input')
plt.subplot(2,1,2)
plt.plot(tm,xm,'ro', label='Measurement')
plt.plot(m.time,results['x.tr'],'k-',label='Reference Trajectory')
plt.plot(m.time,results['x.bcv'],'r--',label='CV Response')
plt.ylabel('Output')
plt.xlabel('Time')
plt.legend()
plt.show()

最佳答案

Gekko 忽略 MPC 初始化第一个周期的测量。如果您进行另一个求解,那么它会使用测量结果。

m.solve() # for MPC initialization

x.MEAS = xm
m.solve() # update initial condition with measurement

反馈状态 (FSTATUS) 是测量值的一阶滤波器,范围在 0(无更新)和 1(完全测量更新)之间。

MEAS = LSTVAL * (1-FSTATUS) + MEAS * FSTATUS

然后在偏差计算中使用新的测量值 (MEAS)。存在无偏(不受测量影响的原始预测)模型预测和有偏模型预测。偏差计算为无偏差模型预测与测量之间的差异。

BIAS = MEAS - UNBIASED_MODEL

Biased and Unbiased Model

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

# measurement
tm = 0
xm = 25

m = GEKKO()
m.time = np.linspace(0,20,41)
tau = 10
b = m.Param(value=50)
K = m.Param(value=0.8)

# Manipulated Variable
u = m.MV(value=0, lb=0, ub=100)
u.STATUS = 1 # allow optimizer to change
u.DCOST = 0.1
u.DMAX = 30

# Controlled Variable
x = m.CV(value=0,name='x')
x.STATUS = 1 # add the SP to the objective
m.options.CV_TYPE = 2 # squared error
x.SP = 40 # set point
x.TR_INIT = 1 # set point trajectory
x.TAU = 5 # time constant of trajectory
x.FSTATUS = 1

# Process model
m.Equation(tau*x.dt() == -x + K*u)
m.options.IMODE = 6 # control
m.solve(disp=False)

m.options.TIME_SHIFT = 0
x.MEAS = xm
m.solve(disp=False)
# turn off time shift, only for initialization
m.options.TIME_SHIFT = 1

# get additional solution information
import json
with open(m.path+'//results.json') as f:
results = json.load(f)
plt.figure()
plt.subplot(2,1,1)
plt.plot(m.time,u.value,'b-',label='MV Optimized')
plt.legend()
plt.ylabel('Input')
plt.ylim([-5,105])
plt.subplot(2,1,2)
plt.plot(tm,xm,'ro', label='Measurement')
plt.plot(m.time,results['x.tr'],'k-',label='Reference Trajectory')
plt.plot(m.time,results['x.bcv'],'r--',label='CV Response Biased')
plt.plot(m.time,x.value,'g:',label='CV Response Unbiased')
plt.ylim([-1,41])
plt.ylabel('Output')
plt.xlabel('Time')
plt.legend()
plt.show()

这就是它目前的工作方式,因为上述计算没有 LSTVAL 或无偏模型预测。第一个周期计算这些值并允许在后续周期中进行更新。如果您确实需要第一个周期的更新值,那么您可以在第二个求解时使用选项 m.option.TIME_SHIFT=0 进行求解,以不更新模型的初始条件。您需要更改后续周期的TIME_SHIFT=1,以获得动态模型的预期时间进展。

关于python - 为什么 GEKKO 没有进行初始测量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58981955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com