Matlab解析LQR与MPC的关系

mathworks社区中的这个资料还是值得一说的。

openExample('mpc/mpccustomqp')

我们从几个角度来解析两者关系,简单的说就是MPC是带了约束的LQR.

下面我们从代码的角度解析这个问题:
1, 定义被控系统:

A = [1.1 2; 0 0.95];
B = [0; 0.0787];
C = [-1 1];
D = 0;
Ts = 1;
sys = ss(A,B,C,D,Ts);
x0 = [0.5;-0.5]; % initial states at [0.5 -0.5]

2,设计无约束LQR:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Qy = 1;
R = 0.01;
K_lqr = lqry(sys,Qy,R);

3, 运行仿真闭环结果:

t_unconstrained = 0:1:10;
u_unconstrained = zeros(size(t_unconstrained));
Unconstrained_LQR = tf([-1 1])*feedback(ss(A,B,eye(2),0,Ts),K_lqr);
lsim(Unconstrained_LQR,'-',u_unconstrained,t_unconstrained,x0);
hold on;

在这里插入图片描述

4,设计MPC控制器:

%%
% The MPC objective function is |J(k) = sum(x(k)'*Q*x(k) + u(k)'*R*u(k) +
% x(k+N)'*Q_bar*x(k+N))|. To ensure that the MPC objective function has the
% same quadratic cost as the infinite horizon quadratic cost used by LQR,
% terminal weight |Q_bar| is obtained by solving the following Lyapunov
% equation:
Q = C'*C;
Q_bar = dlyap((A-B*K_lqr)', Q+K_lqr'*R*K_lqr); 

%%
% Convert the MPC problem into a standard QP problem, which has the
% objective function |J(k) = U(k)'*H*U(k) + 2*x(k)'*F'*U(k)|.
Q_hat = blkdiag(Q,Q,Q,Q_bar);
R_hat = blkdiag(R,R,R,R);
H = CONV'*Q_hat*CONV + R_hat;
F = CONV'*Q_hat*M;

%%
% When there are no constraints, the optimal predicted input sequence U(k)
% generated by MPC controller is |-K*x|, where |K = inv(H)*F|.
K = H\F;

%% 
% In practice, only the first control move |u(k) = -K_mpc*x(k)| is applied
% to the plant (receding horizon control).
K_mpc = K(1,:);

%% 
% Run a simulation with initial states at [0.5 -0.5]. The closed-loop
% response is stable.  
Unconstrained_MPC = tf([-1 1])*feedback(ss(A,B,eye(2),0,Ts),K_mpc);
lsim(Unconstrained_MPC,'*',u_unconstrained,t_unconstrained,x0)
legend show

在这里插入图片描述
到这里,完全可以说明,在无约束前提下,两种方法是一致的:

K_lqr =

    4.3608   18.7401


K_mpc =

    4.3608   18.7401

5,对LQR施加约束:
x = x0;
t_constrained = 0:40;
for ct = t_constrained
uLQR(ct+1) = -K_lqrx;
uLQR(ct+1) = max(-1,min(1,uLQR(ct+1)));
x = A
x+BuLQR(ct+1);
yLQR(ct+1) = C
x;
end
figure
subplot(2,1,1)
plot(t_constrained,uLQR)
xlabel(‘time’)
ylabel(‘u’)
subplot(2,1,2)
plot(t_constrained,yLQR)
xlabel(‘time’)
ylabel(‘y’)
legend(‘Constrained LQR’)
在这里插入图片描述
6,对MPC施加约束:

%% MPC Controller Solves QP Problem Online When Applying Constraints 
% One of the major benefits of using MPC controller is that it handles
% input and output constraints explicitly by solving an optimization
% problem at each control interval.
%
% Use the built-in KWIK QP solver, |mpcqpsolver|, to implement the custom
% MPC controller designed above. The constraint matrices are defined as
% Ac*x>=b0.
Ac = -[1 0 0 0;...
      -1 0 0 0;...
       0 1 0 0;...
       0 -1 0 0;...
       0 0 1 0;...
       0 0 -1 0;...
       0 0 0 1;...
       0 0 0 -1];
b0 = -[1;1;1;1;1;1;1;1];

%% 
% The |mpcqpsolver| function requires the first input to be the inverse of
% the lower-triangular Cholesky decomposition of the Hessian matrix H.
L = chol(H,'lower');
Linv = L\eye(size(H,1));

%%
% Run a simulation by calling |mpcqpsolver| at each simulation step.
% Initially all the inequalities are inactive (cold start). 
x = x0;
iA = false(size(b0));
opt = mpcqpsolverOptions;
opt.IntegrityChecks = false;
for ct = t_constrained
    [u, status, iA] = mpcqpsolver(Linv,F*x,Ac,b0,[],zeros(0,1),iA,opt);
    uMPC(ct+1) = u(1);
    x = A*x+B*uMPC(ct+1);
    yMPC(ct+1) = C*x;
end
figure
subplot(2,1,1)
plot(t_constrained,uMPC)
xlabel('time')
ylabel('u')
subplot(2,1,2)
plot(t_constrained,yMPC)
xlabel('time')
ylabel('y')
legend('Constrained MPC')

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章