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')

在這裏插入圖片描述

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