gpt4 book ai didi

python - 如何通过使用 Gekko 调整参数来解决超调?

转载 作者:行者123 更新时间:2023-12-03 17:09:51 26 4
gpt4 key购买 nike

GEKKO是混合整数和微分代数方程的优化软件。加上large-scale solverslinear, quadratic, nonlinear , 和混合整数规划 ( LP, QP, NLP, MILP, MINLP )。
我用 gekko控制我的 TCLab Arduino ,但是当我给一个扰动时,无论我如何调整参数,都会有一个过冲温度。我怎么解决这个问题?
这是我的代码:

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

# Connect to Arduino
a = tclab.TCLab()

# Get Version
print(a.version)

# Turn LED on
print('LED On')
a.LED(100)

# Run time in minutes
run_time = 60.0

# Number of cycles
loops = int(60.0*run_time)
tm = np.zeros(loops)

# Temperature (K)
T1 = np.ones(loops) * a.T1 # temperature (degC)
Tsp1 = np.ones(loops) * 35.0 # set point (degC)

# heater values
Q1s = np.ones(loops) * 0.0

#########################################################
# Initialize Model
#########################################################
# use remote=True for MacOS
m = GEKKO(name='tclab-mpc',remote=False)

# 100 second time horizon
m.time = np.linspace(0,100,101)

# Parameters
Q1_ss = m.Param(value=0)
TC1_ss = m.Param(value=a.T1)
Kp = m.Param(value=0.8)
tau = m.Param(value=160.0)

# Manipulated variable
Q1 = m.MV(value=0)
Q1.STATUS = 1 # use to control temperature
Q1.FSTATUS = 0 # no feedback measurement
Q1.LOWER = 0.0
Q1.UPPER = 100.0
Q1.DMAX = 50.0
# Q1.COST = 0.0
Q1.DCOST = 0.2

# Controlled variable
TC1 = m.CV(value=TC1_ss.value)
TC1.STATUS = 1 # minimize error with setpoint range
TC1.FSTATUS = 1 # receive measurement
TC1.TR_INIT = 2 # reference trajectory
TC1.TR_OPEN = 2 # reference trajectory
TC1.TAU = 35 # time constant for response

m.Equation(tau * TC1.dt() + (TC1-TC1_ss) == Kp * (Q1-Q1_ss))

# Global Options
m.options.IMODE = 6 # MPC
m.options.CV_TYPE = 1 # Objective type
m.options.NODES = 2 # Collocation nodes
m.options.SOLVER = 1 # 1=APOPT, 3=IPOPT
##################################################################

# Create plot
plt.figure()
plt.ion()
plt.show()

filter_tc1 = []
def movefilter(predata, new, n):
if len(predata) < n:
predata.append(new)
else:
predata.pop(0)
predata.append(new)
return np.average(predata)

# Main Loop
start_time = time.time()
prev_time = start_time
try:
for i in range(1,loops):
# Sleep time
sleep_max = 1.0
sleep = sleep_max - (time.time() - prev_time)
if sleep>=0.01:
time.sleep(sleep)
else:
time.sleep(0.01)

# Record time and change in time
t = time.time()
dt = t - prev_time
prev_time = t
tm[i] = t - start_time

# Read temperatures in Kelvin
curr_T1 = a.T1
last_T1 = curr_T1
avg_T1 = movefilter(filter_tc1, last_T1, 3)
T1[i] = curr_T1

###############################
### MPC CONTROLLER ###
###############################
TC1.MEAS = avg_T1
# input setpoint with deadband +/- DT
DT = 0.1
TC1.SPHI = Tsp1[i] + DT
TC1.SPLO = Tsp1[i] - DT
# solve MPC
m.solve(disp=False)
# test for successful solution
if (m.options.APPSTATUS==1):
# retrieve the first Q value
Q1s[i] = Q1.NEWVAL
else:
# not successful, set heater to zero
Q1s[i] = 0

# Write output (0-100)
a.Q1(Q1s[i])

# Plot
plt.clf()
ax=plt.subplot(2,1,1)
ax.grid()
plt.plot(tm[0:i],T1[0:i],'ro',MarkerSize=3,label=r'$T_1$')
plt.plot(tm[0:i],Tsp1[0:i],'b-',MarkerSize=3,label=r'$T_1 Setpoint$')
plt.ylabel('Temperature (degC)')
plt.legend(loc='best')
ax=plt.subplot(2,1,2)
ax.grid()
plt.plot(tm[0:i],Q1s[0:i],'r-',LineWidth=3,label=r'$Q_1$')
plt.ylabel('Heaters')
plt.xlabel('Time (sec)')
plt.legend(loc='best')
plt.draw()
plt.pause(0.05)

# Turn off heaters
a.Q1(0)
a.Q2(0)
print('Shutting down')
a.close()

# Allow user to end loop with Ctrl-C
except KeyboardInterrupt:
# Disconnect from Arduino
a.Q1(0)
a.Q2(0)
print('Shutting down')
a.close()

# Make sure serial connection still closes when there's an error
except:
# Disconnect from Arduino
a.Q1(0)
a.Q2(0)
print('Error: Shutting down')
a.close()
raise
有测试结果图片。
Image

最佳答案

当您添加干扰(例如打开另一个加热器)时,系统增益会增加,因为温度上升得比 Controller 预期的要高。这意味着您开始在失配图上向左移动(导致最差的控制性能)。
MPC Objective
这是 Hedengren, J. D., Eaton, A. N., Overview of Estimation Methods for Industrial Dynamic Systems 中的图 14 , 优化与工程, Springer, Vol 18 (1), 2017, pp. 155-178, DOI: 10.1007/s11081-015-9295-9。
超调的原因之一是因为模型不匹配。这里有几种方法可以解决这个问题:

  • 增加模型增益 K (可能为 1)或减少您的模型 tau (也许到 120)以便 Controller 变得不那么激进。您可能还想重新识别您的模型,以便更好地反射(reflect)您的 TCLab 系统动态。这是获取 first order 的教程或 second order模型。更高阶的 ARX 模型也适用于 TCLab。
  • 使用 TC.TAU=50 将引用轨迹更改为不那么激进和 include the reference trajectory on the plot以便您可以观察 Controller 的计划。我还喜欢在图中包含无偏模型以显示模型的表现。
  • 看看这个 Control Tuning有关其他 MV 和 CV 调整选项的帮助页面。 Jupyter 笔记本小部件可以帮助您直观地了解这些选项。

  • MPC Tuning

    关于python - 如何通过使用 Gekko 调整参数来解决超调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65368005/

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