使用 m.CV 与 m.Var
Using m.CV vs m.Var
我正在使用 gekko python 优化管柱设计。我使用不同的变量类型 m.SV
和 m.CV
代替 m.Var
对代码进行了试验,对求解器或结果没有明显影响。这些不同的变量类型有什么作用?
我在下面包含了我的模型。
m = GEKKO()
#%% Constants
pi = m.Const(3.14159,'pi')
P = 2300 # compressive load (kg_f)
o_y = 450 # yield stress (kg_f/cm^2)
E = 0.65e6 # elasticity (kg_f/cm^2)
p = 0.0020 # weight density (kg_f/cm^3)
l = 300 # length of the column (cm)
#%% Variables
d = m.CV(value=8.0,lb=2.0,ub=14.0) # mean diameter (cm)
t = m.SV(value=0.3,lb=0.2,ub=0.8) # thickness (cm)
cost = m.Var()
#%% Intermediates
d_i = m.Intermediate(d - t)
d_o = m.Intermediate(d + t)
W = m.Intermediate(p*l*pi*(d_o**2 - d_i**2)/4) # weight (kgf)
o_i = m.Intermediate(P/(pi*d*t)) # induced stress
# second moment of area of the cross section of the column
I = m.Intermediate((pi/64)*(d_o**4 - d_i**4))
# buckling stress (Euler buckling load/cross-sectional area)
o_b = m.Intermediate((pi**2*E*I/l**2)*(1/(pi*d*t)))
#%% Equations
m.Equations([
o_i - o_y <= 0,
o_i - o_b <= 0,
cost == 5*W + 2*d
])
#%% Objective
m.Obj(cost)
#%% Solve and print solution
m.options.SOLVER = 1
m.solve()
print('Optimal cost: ' + str(cost[0]))
print('Optimal mean diameter: ' + str(d[0]))
print('Optimal thickness: ' + str(t[0]))
变量
变量是求解器为满足方程式或确定多个选项中的最佳结果而调整的值。每个方程通常至少有一个变量。为避免过度指定,模拟通常具有相等数量的方程和变量。对于优化问题,变量通常多于方程。更改额外变量以最小化或最大化 objective 函数。 Gekko documentation and APMonitor documentation 中有关于这些对象的更多信息。
x = m.Var(5) # declare a variable with initial condition
还有 "special" 种执行特定功能的变量。例如,额外的方程被添加到模型中,用于具有数据协调测量的变量。为避免为所有变量添加这些额外的方程式,只为指定为受控变量 (CV) 的变量添加测量方程式。状态变量 (SV) 也可以被测量,通常被指定为仅用于监视目的。
状态变量 (SV)
状态是可以测量或对观察特别感兴趣的模型变量。对于时变模拟,SV 在时间范围内发生变化以满足方程的可行性。
x = m.SV() # state variable
受控变量 (CV)
受控变量是包含在控制器或优化器 objective 中的模型变量。这些变量被控制在一个范围内,最大化或最小化。受控变量也可以是包含在数据核对中的测量值。对于时变模拟,CV 在时间范围内发生变化以满足方程式并最小化 objective 函数。
x = m.CV() # controlled variable
示例应用程序
有documentation for options for the different variable and parameter types (FV, MV, SV, CV)。下面是一个模型预测控制应用程序,显示了操纵变量和受控变量的使用。
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
m = GEKKO()
m.time = np.linspace(0,20,41)
# Parameters
mass = 500
b = m.Param(value=50)
K = m.Param(value=0.8)
# Manipulated variable
p = m.MV(value=0, lb=0, ub=100)
p.STATUS = 1 # allow optimizer to change
p.DCOST = 0.1 # smooth out gas pedal movement
p.DMAX = 20 # slow down change of gas pedal
# Controlled Variable
v = m.CV(value=0)
v.STATUS = 1 # add the SP to the objective
m.options.CV_TYPE = 2 # squared error
v.SP = 40 # set point
v.TR_INIT = 1 # set point trajectory
v.TAU = 5 # time constant of trajectory
# Process model
m.Equation(mass*v.dt() == -v*b + K*b*p)
m.options.IMODE = 6 # control
m.solve(disp=False)
# 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,p.value,'b-',label='MV Optimized')
plt.legend()
plt.ylabel('Input')
plt.subplot(2,1,2)
plt.plot(m.time,results['v1.tr'],'k-',label='Reference Trajectory')
plt.plot(m.time,v.value,'r--',label='CV Response')
plt.ylabel('Output')
plt.xlabel('Time')
plt.legend(loc='best')
plt.show()