Yalmip MPC Input

59 views
Skip to first unread message

Ali Esmaeilpour

unread,
Nov 10, 2019, 2:18:08 PM11/10/19
to YALMIP
excuse me professor the following code finds input and output of current time sample (only one sample). I wanted to give this input to the same code and get input and output of one step ahead. how can I do that???
clc;
clear
;
close all
;
%% System structure ===> x(k+1)=A*x(k)+B*u(k)
A
= [1.02 -0.1
   
0.1  0.98];
B
= [3.5; 2.6];
G
=[0.3 0;0 0.3];
C
=[1 0];
%% MPC parameters
Q  
=1;
R  
= 100;
Qf=1;
N
= 9;
M
= blkdiag(Q,R);
w1
= randn(1,1);
w1
= w1-mean(w1);
w1
= w1/1;
sigmaw1
= var(w1);
w2
= randn(1,1);
w2
= w2-mean(w2);
w2
= w2/1;
sigmaw2
= var(w2);
w
=[w1;w2];
sigmaw
= [sigmaw1 0 ;0 sigmaw2];
v
= randn(1,1);
v
= v-mean(v);
v
= v/1;
sigmav
= var(v);
x
= randn(2,1);
y
=C*x+v;
sigmax
{1} = [1 0;0 1];
sigmay
{1}=C*sigmax{1}*C'+sigmav;
h1y = [1 ; 1];
h2u = 1;
h3u = -1;
epsilon =0.01;
g1 = 0.1;
g2 = 1;
g3 = 1;
%% Optimizer
mux = sdpvar(repmat(2,1,N),repmat(1,1,N)) ;
muy= sdpvar(repmat(1,1,N),repmat(1,1,N));
sigmax= sdpvar(repmat (2,1,N+1),repmat(2,1,N+1));
sigmay = sdpvar(repmat (1,1,N+1),repmat(1,1,N+1));
p = sdpvar(repmat(2,1,N-1),repmat(2,1,N-1) );
pN = sdpvar(1,1);
F = sdpvar(repmat(1,1,N+1),repmat(1,1,N+1) );
muu = sdpvar(repmat(1,1,N),repmat(1,1,N)) ;
sigmau = sdpvar(repmat(1,1,N),repmat(1,1,N) );
constraint = [];
objective = 0;
for k=1:N-1
    mux{k+1}=A*mux{k}+B*muu{k};
    muy{k+1}=C*mux{k+1};
    objective = objective  +1/2*(trace(M*p{k})) ;
    constraint2 = [sigmay{k+1}       C*A*sigmax{k}   C*B*F{k}   C*G*sigmaw   sigmav
        ( C*A*sigmax{k})'
  sigmax{k}      zeros(2,1)  zeros(2,2)  zeros(2,1)
       
(C*B*F{k})'        zeros(1,2)     sigmay{k}     zeros(1,2)  zeros(1)
        (C*G*sigmaw)'
      zeros(2,2)     zeros(2,1)     sigmaw      zeros(2,1)
        sigmav              zeros
(1,2)     zeros(1)       zeros(1,2)   sigmav]>=0;
    constraint1
= [sigmax{k+1}    A*sigmax{k}    B*F{k}   G*sigmaw
       
(A*sigmax{k})'   sigmax{k}      zeros(2,1)      zeros(2)
        (B*F{k})'
       zeros(1,2)      sigmay{k}       zeros(1,2)
        G
*sigmaw          zeros(2,2)      zeros(2,1)      sigmaw]>=0;
    constraint3
= [sigmau{k} (F{k});(F{k})'  sigmay{k}]>=0;
    constraint4 = h1y'
*mux{k}<=(1-(0.5*epsilon))*g1-((0.95/2*epsilon*g1*(1-0.95))*h1y'*(sigmay{k})*h1y);
    constraint5 = h2u'
*muu{k}<=(1-(0.5*epsilon))*g2-((0.95/2*epsilon*g2*(1-0.95))*h2u'*sigmau{k}*h2u);
    constraint6 = h3u'
*muu{k}<=(1-(0.5*epsilon))*g3-((0.95/2*epsilon*g3*(1-0.95))*h3u'*sigmau{k}*h3u);
    constraint7 = [p{k}              [sigmay{k};F{k}]  [muy{k};muu{k}]
        ([sigmay{k} ;F{k}])'
  sigmay{k}        zeros(1)
       
([muy{k};muu{k}])'     zeros(1)          1]>=0;
    constraint = [constraint,constraint2,constraint1,constraint3,constraint4,constraint5,constraint6,constraint7];
end
objective=objective+(1/2*trace((trace(Qf*pN))));
constraint10 = h1y'
*(mux{N})<=(1-(0.5*epsilon))*g1-((0.95/2*epsilon*g1*(0.1))*h1y'*(C*sigmax{N}*C'+sigmav)*h1y);
constraint11
= [pN-(sigmay{k})  (muy{N});(muy{N})'    1] >=0;
constraint = [constraint,constraint10,constraint11];
option = sdpsettings('
solver','sedumi');
sol_out={mux{2},muu{1},F{1}, sigmay{1} , muy{1},mux{1},sigmax{2}};
controller = optimizer (constraint,objective,option,mux{1},sol_out);
mux{1} = [-5;2];
%% Results
sol=controller(mux{1});
muu = sol{2};
mux = sol{6};
muy =sol{5};
F = sol{3};
Y = sol{4};
K=F/Y;
u = muu+K*((y)-(muy));
x =A*x+B*u +G*w;
y = C*x+v;
sigmax{1} = sol{7};


Ali Esmaeilpour

unread,
Nov 10, 2019, 4:22:24 PM11/10/19
to YALMIP
Input is "muu" and State is "mux". I run my mpc and I get "muu". Now I want to give this "muu" as input to mpc and get the next "muu"...

Johan Löfberg

unread,
Nov 11, 2019, 2:05:22 AM11/11/19
to yal...@googlegroups.com
and what stops you from doing that. You have computed muu so you are done

Ali Esmaeilpour

unread,
Nov 11, 2019, 2:30:46 AM11/11/19
to YALMIP
Well I find muu of first step without any problem, but muu is defined as sdpvar and I want to give muu of Previous step to mpc (instead of that sdpvar) to get New muu of next step
How can I do this?

Johan Löfberg

unread,
Nov 11, 2019, 2:39:39 AM11/11/19
to YALMIP
muu is not an sdpvar

muu = sol{2};.

that's the whole idea (of course), you set up an optimization problem, and compute the optimal mu

Ali Esmaeilpour

unread,
Nov 11, 2019, 3:21:10 AM11/11/19
to YALMIP
So if I construct a close loop simulation that runs for 200 Time samples (receding horizon applied) muu{1} is Current input known as u(k-1) and other values of input for the rest of the samples are for Future and could be considered as prediction. Is this right?

Johan Löfberg

unread,
Nov 11, 2019, 6:53:18 AM11/11/19
to YALMIP
u is your current input, u(k), and the u you used in the previous iteration would thus be u(k-1), and if you still need it you better have saved it in a new variable in the previous iteration before you update u
Reply all
Reply to author
Forward
0 new messages