TOMLAB/PROPT 学习笔记
(---最优控制问题的求解方法 2)
1. PROPT 语法
简介:
包含状态方程、初始或终端条件、代价函数,其他方程和变量
1.1 标准函数、运算符语法:
1. collocate — Expand a propt tomSym to all collocation points on a phase.扩展到全部配置点上
格式:y = collocate(phase, x)
2. dot 微分
格式:dot(p,x)
如果已经调用 setPhase(p),可简化为 dot(x)。适用于其他?y = collocate(phase, x)?
3. final — Evaluate a propt tomSym at the final point of a phase.估算最后点的值
y = final(phase,
4. icollocate — Expand a propt tomSym to all interpolation points on a phase 扩展到全部插值点上
与 collocate 类似,除了插值点是用来代替搭配点。 这在构造初始猜测时通常很有用。
5. initial — Evaluate a propt tomSym at the initial point of a phase.计算初值
y = initial(phase,
6. integrate — Evaluate the integral of an expression in a phase. 计算积分值
y = integrate(phase,
7. mcollocate — Expand to all collocation points, endpoints and midpoints on a phase. 扩展到全部配置点、终点、
x)
x)
x)
中间点上,用于设置涉及状态变量的不等式。应注意确保不与任何初始或最终条件冲突。
x)
y = mcollocate(phase,
8. setPhase — Set the active phase when modeling PROPT problem.设置 phase 阶段
格式:setPhase(p)
可以省略 tomState,tomControl,initial,final,integrate 等命令的阶段参数。
9. tomControl — Generate a PROPT symbolic state. 定义控制变量
scalar PROPT control with
a
x = tomControl ,creates
x = tomControl(phase,label) , creates
a
x = tomControl(phase,label,m,n) ,creates
x = tomControl(phase,[],m,n) , creates
x = tomControl(phase,label,m,n,’int’) , creates
x = tomControl(phase,label,m,n,’symmetric’), creates
10.
tomControls
a matrix
an
x
y
z
an
scalar
control with
a m-by-n matrix
automatic
the
of
control with
integer matrix
controls.
an
symbol.
symmetric matrix
a
tomControls — Create tomControl objects 定义控制变量
name.
provided
name.
name.
automatic
symbol.
等效于:
x = tomControl(’x’);
y = tomControl(’y’);
z = tomControl(’z’);
11. tomPhase — Create a phase struct 产生配置点
n)
phase = tomPhase(label,
phase = tomPhase(label,
ipoints,
如:p = tomPhase('p', t, 0, tf, 30);0-tf时间内,划分30个点
If n > 128 then Chebyshev points are used instead.)
phase = tomPhase(label, t, tstart, tdelta, n, [], 'cheb')
tdelta,
tdelta,
tstart,
tstart,
t,
t,
cpoints)
an
name.
creates
automatic
a
creates
scalar PROPT state with
creates
12. tomState — Generate a PROPT symbolic state ,定义状态变量
x = tomState
a
x = tomState(phase,label)
x = tomState(phase,label,m,n)
a matrix
x = tomState(phase,[],m,n)
creates
creates
an
x = tomState(phase,label,m,n,’int’)
x = tomState(phase,label,m,n,’symmetric’)
symbol.
creates
13 tomStates — Create tomState objects as toms create tomSym objects ,定义状态变量
如:tomStates
the
scalar
state with
state.
a m-by-n matrix
state with
an
integer matrix
automatic
symbol.
symmetric matrix
provided
name.
name.
x
y
z
a
补充:
14. toms 定义时间变量
toms t t_f
15. ezsolve 求解
Solution = ezsolve (objective, constraints)
[solution, result] = ezsolve(objective, {cbox, cbnd, ceq}, x0, options);
t,
x)
Ezsolve 调用 tomDiagnose 来确定问题类型,使用 getSolver 找到合适的求解器,然后调用 sym2prob、tomRun、
getSoluton 依次获得解决方案。
1.2 高级函数(运算符)语法
1. atPoints — Expand a propt tomSym to a set of points on a phase.拓展/插值
y = atPoints(phase,
y =atPoints(phase, t, x, solution)
2. interp1p — Polynomial interpolation.多项式插值
yi = interp1p(x,y,xi),根据(x,y)得到多项式,对 xi 处估算其 yi 值
3. proptGausspoints — Generate a list of gauss points. 生成高斯点列表。
[r, w] = gausspoints(nquad) = proptGausspoints(n)
Input: n - The number of points requested
Output: r - n 阶勒让德多项式的根;w -高斯求积的权重。
4. proptDiffMatrix — Differentiation matrix for interpPoly.
M = proptDiffMatrix(X,XI,N)
1.3 Screen output 屏幕输出
options . PriLevOpt
1.4 结果输出 ,结构体 solution
= 1; % or
for more
higher
output
2 案例
2.1 A Linear Problem with Bang Bang Control
Find u over t in [0; tF] to minimize:
J = tF
subject to:
Problem setup:
Solve the problem:
编程方法总结,步骤:
1.时间变量定义,toms t t_f ,由于t_f自由,须定义它
2.配置点定义,
p = tomPhase('p', t, 0, t_f, 30);固定格式
setPhase(p);
3.控制变量和状态变量定义,
tomStates x1 x2
tomControls u
4.初值定义
x0 = {
t_f == 20
icollocate({x1 == 300*t/t_f; x2 == 0}) %在插值点上。 x1怎么不是0?
collocate(u==1-2*t/t_f) %在配置点上对u设定初值
};
5. Box(?)取值范围约束
cbox = {
10 <= t_f <= 40
mcollocate(-2 <= u <= 1) %u适用于全部点
};
%tf范围?
6.边界约束
cbnd = {
initial({x1 == 0; x2 == 0}) %初值
final({x1 == 300; x2 == 0}) %终值
};
7.ODE 方程组
ceq = collocate({dot(x1) == x2; dot(x2) == u});%用collocate,适用于配置点上?
8.目标函数
objective = t_f;
9.求解
options = struct;
options.name = 'Bang-Bang Free Time';
[solution, result] = ezsolve(objective, {cbox, cbnd, ceq}, x0, options);
10.结果输出
t_plot = linspace(0,subs(t_f,solution),100);subs替换作用
x1_plot = atPoints(t_plot,x1,solution);根据t_plot位置拓展/插值x1
x2_plot = atPoints(t_plot,x2,solution);
u_plot = atPoints(t_plot,u,solution);
2.2 Batch Fermentor
Problem description:
Find u(t) and t f over t in [t0; t f ] to maximize
subject to:
J = x2(tf )∗ x4(tf )
where x1, x2, and x3 are the biomass, penicillin and substrate concentrations (g=L), and x4 is the volume (L).
The initial conditions are:
path constraints:
x(t0) = [1.5 0 0 7]’
0 <= x1 <= 40
0 <= x3 <= 25
0 <= x4 <= 10
The upper and lower bounds on the only control variable (feed rate of substrate) are:
0 <= u <= 50
Solving the problem on multiple grids:
网格点从小到大,连续计算,具体步骤:
1.时间定义
toms t t_f
2. 配置点定义
采用不同网格数方法,nvec = [35 70 100];
for i=1:length(nvec)
.......一次计算
end
其中:
n = nvec(i);
p = tomPhase('p', t, 0, t_f, n, [], 'spline3');
配点定义与前面类似,多了i的循环
setPhase(p);
3.变量定义
tomStates x1 x2 x3 x4 %状态变量
tomControls u
4.初值定义
if i==1
%控制变量
x0 = {t_f == 126 猜测
icollocate(x1 == 1.5)%与给定的初值一致
icollocate(x2 == 0)
icollocate(x3 == 0)
icollocate(x4 == 7)
collocate(u==11.25)};猜测
else
end
% Copy the solution into the starting guess 以前次计算结果为初值
x0 = {t_f == tf_init
icollocate(x1 == x1_init)
icollocate(x2 == x2_init)
icollocate(x3 == x3_init)
icollocate(x4 == x4_init)
collocate(u == u_init)};
5.Box 约束 (变量取值范围约束)
cbox = {1 <= t_f <= 256
mcollocate({
1e-8 <= x1 <= 40 %避免分母出现0
0
0
1
0
};
<= x2 <= 50
<= x3 <= 25
<= x4 <= 10 %避免分母出现0
<= u <= 50})
6.边界约束(定义两点初始点/终点的约束)
cinit = initial({x1 == 1.5; x2 == 0
x3 == 0; x4 == 7});
cfinal = final(h2.*x1-0.01*x2) == 0;
7. 中间量/表达式描述
h1 = 0.11*(x3./(0.006*x1+x3));
h2 = 0.0055*(x3./(0.0001+x3.*(1+10*x3)));
8.微分方程组
ceq = collocate({
dot(x1) == h1.*x1-u.*(x1./500./x4)
dot(x2) == h2.*x1-0.01*x2-u.*(x2./500./x4)
dot(x3) == -h1.*x1/0.47-h2.*x1/1.2-x1.*...
(0.029*x3./(0.0001+x3))+u./x4.*(1-x3/500)
dot(x4) == u/500});
9.目标函数
objective = -final(x2)*final(x4);
10.求解
options = struct;
options.name = 'Batch Fermentor';
options.Prob.SOL.optPar(30) = 20000;
%options.scale = 'auto';
%if i==1
%
%
%end
solution = ezsolve(objective, {cbox, cinit, cfinal, ceq}, x0, options);
options.solver = 'multiMin';
options.xInit = 20;
11.返回计算结果给初值
x1_init = subs(x1,solution);
x2_init = subs(x2,solution);
x3_init = subs(x3,solution);
x4_init = subs(x4,solution);
u_init
= subs(u,solution);
tf_init = subs(t_f,solution);
2.3 Continuous State Constraint Problem
Problem description:
Problem setup:
1.时间定义
toms t
2. 配置点定义
p = tomPhase (’p’, t, 0, 1, 50);