clc;
clear;
close all;
%% Time structure
t0 = 0;
tf = 30;
dt = 1;
t = t0:dt:tf-dt;
%% 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 parameter
Q = 1;
R = 1;
N = 16;
M = blkdiag(Q,R);
w1 = randn(1,500);
w1 = w1-mean(w1);
w1 = w1/1;
muw1 = mean(w1);
sigmaw1 = var(w1);
w2 = randn(1,500);
w2 = w2-mean(w2);
w2 = w2/1;
muw2 = mean(w2);
sigmaw2 = var(w2);
w=[w1;w2];
sigmaw = [sigmaw1,0;0,sigmaw2];
v = randn(1,500);
v = v-mean(v);
v = v/1;
muv = mean(v);
sigmav = var(v);
x = randn(2,1);
y = C*x+v(:,1);
%mux{1} = [-5;2];
%muy{1} = C*mux{1};
h1y = [1;1];
h2u = 1;
h3u = -1;
epsilon = 0.01;
g1 = 0.1;
g2 = 1;
g3 = 1;
%% Pre-allocation
U1 = zeros(1,200);
mux1 = zeros(1,200);
mux2 = zeros(1,200);
f1 = cell(1,200);
Y1 = cell(1,200);
K1 = cell(1,200);
u2 = cell(1,200);
muy1 = zeros(1,200);
y1 = zeros(1,200);
ud1 = zeros(1,200);
x1 = zeros(1,200);
x2 = zeros(1,200);
%% Main loop
c=0;
for t=t0:dt:tf-dt
c=c+1;
F = 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);
muu = sdpvar(repmat(1,1,N),repmat(1,1,N)) ;
mux = sdpvar(repmat(2,1,N),repmat(1,1,N));
muy = sdpvar(repmat(1,1,N),repmat(1,1,N));
sigmay = sdpvar(repmat(1,1,N+1),repmat(1,1,N+1));
sigmau = sdpvar(repmat(1,1,N),repmat(1,1,N));
sigmax= sdpvar(repmat(2,1,N+1),repmat(2,1,N+1));
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}));
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;
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;
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,constraint1,constraint2,constraint3,constraint4,constraint5,constraint6,constraint7];
end
objective=objective+(1/2*trace((trace(Q*pN))));
constraint8 = h1y'*(mux{N})<=(1-(0.5*epsilon))*g1-((0.95/2*epsilon*g1*(0.1))*h1y'*(C*sigmax{N}*C'+sigmav)*h1y);
constraint9 = [pN-(sigmay{k}),(muy{N});(muy{N})',1] >=0;
constraint = [constraint,constraint8,constraint9];
ops = sdpsettings('solver','mosek');
%a = optimize(constraint,objective,ops);
controller = optimizer(constraint,objective,ops,mux{1},muu{1});
mux{1} = [-5;2];
muy{1} = C*mux{1};
%u = value(muu{1});
u = controller{mux};
mux3 = value(mux{1});
mux1(c) = mux3(1,1);
mux2(c) = mux3(2,1);
muy3 = muy{1};
F = value(F{1});
Y = value(sigmay{1});
x = value(x);
y = value(y);
U1(c) = u(1,1);
f1{c} = F;
Y1{c} = Y;
K = F/Y;
K1{c} = K;
K2 = value(K1{c});
u1 = u+K2*((y)-(muy3));
u1 = value(u1);
u2{c} = u1;
x = A*x+B*u1+G*w(:,c);
y = C*x+v(:,c);
mux{1} = value(mux{2});
sigmax{1} = value(sigmax{2});
muy{1} = C*mux{1};
sigmay{1} = C*sigmax{1}*C'+sigmav;
muy1(c) = muy3;
y1(c) = y;
ud = value(u2{c});
ud1(c) = ud(1,1);
x1(c) = x(1,1);
x2(c) = x(2,1);
end
%% Plot results
figure(1)
subplot(3,1,1)
plot(muy1,'linewidth',2)
xlabel('Time')
ylabel('muy1')
subplot(3,1,2)
plot(mux1,'linewidth',2)
xlabel('Time')
ylabel('mux1')
subplot(3,1,3);
plot(mux2,'linewidth',2)
xlabel('Time')
ylabel('mux2')
% At this point mux{1} is an sdpvar vector
controller = optimizer(constraint,objective,ops,mux{1},muu{1});
mux{1} = [-5;2];
muy{1} = C*mux{1};
%u = value(muu{1
});
% Now you are sending a cell
u = controller{mux};
clc;
clear;
close all;
%% Time structure
t0 = 0;
tf = 30;
dt = 1;
loop
F = 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);
muu = sdpvar(repmat(1,1,N),repmat(1,1,N)) ;
mux = sdpvar(repmat(2,1,N),repmat(1,1,N));
muy = sdpvar(repmat(1,1,N),repmat(1,1,N));
sigmay = sdpvar(repmat(1,1,N+1),repmat(1,1,N+1));
sigmau = sdpvar(repmat(1,1,N),repmat(1,1,N));
sigmax= sdpvar(repmat(2,1,N+1),repmat(2,1,N+1));
constraints = [];
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}));
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;
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;
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;
constraints = [constraints,constraint1,constraint2,constraint3,constraint4,constraint5,constraint6,constraint7];
end
objective=objective+(1/2*trace((trace(Q*pN))));
constraint8 = h1y'*(mux{N})<=(1-(0.5*epsilon))*g1-((0.95/2*epsilon*g1*(0.1))*h1y'*(C*sigmax{N}*C'+sigmav)*h1y);
constraint9 = [pN-(sigmay{k}),(muy{N});(muy{N})',1] >=0;
constraints = [constraints,constraint8,constraint9];
ops = sdpsettings('solver','mosek');
%a = optimize(constraint,objective,ops);
controller = optimizer(constraints,objective,ops,mux{1},muu{1});
%mux = {[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2],[-5;2]};
mux{1} = [-5;2];
muy{1} = C*mux{1};
%u = value(muu{1});
u = controller{mux{1}};
c=0;
for t=t0:dt:tf-dt
c=c+1;
mux3 = value(mux{1});
mux1(c) = mux3(1,1);
mux2(c) = mux3(2,1);
muy3 = muy{1};
F = value(F{1});
Y = value(sigmay{1});
x = value(x);
y = value(y);
U1(c) = u(1,1);
f1{c} = F;
Y1{c} = Y;
K = F/Y;
K1{c} = K;
K2 = value(K1{c});
u1 = u+K2*((y)-(muy3));
u1 = value(u1);
u2{c} = u1;
x = A*x+B*u1+G*w(:,c);
y = C*x+v(:,c);
mux{1} = value(mux{2});
sigmax{1} = value(sigmax{2});
muy{1} = C*mux{1};
sigmay{1} = C*sigmax{1}*C'+sigmav;
muy1(c) = muy3;
y1(c) = y;
ud = value(u2{c});
ud1(c) = ud(1,1);
x1(c) = x(1,1);
x2(c) = x(2,1);
end
%% Plot results
figure(1)
subplot(3,1,1)
plot(muy1,'linewidth',2)
xlabel('Time')
ylabel('muy1')
subplot(3,1,2)
plot(mux1,'linewidth',2)
xlabel('Time')
ylabel('mux1')
subplot(3,1,3);
plot(mux2,'linewidth',2)
xlabel('Time')
ylabel('mux2')
P = optimizer(constraints,objective,options,{parameter1,parameter2,...},{output1,output2,...})
...
parametervalue1 = someconstant
...
for ...
solution = P(parametervalue1,parametervalue2,...)
theoptimalvalueofoutput1 = solution{1};
theoptimalvalueofoutput2 = solution{2};
parametervalue1 = ...
end
...
P = optimizer(constraints,objective,options,{parameter1,parameter2,...},{output1,output2,...})
...
parametervalue1 = someconstant
...
what are these
parameter1,parameter2,...
and how should I choose them???
and
parametervalue1 is initial value for
parameter1?
for t=t0:dt:tf-dt
c=c+1;
sigmax= sdpvar(repmat(2,1,N+1),repmat(2,1,N+1));
sigmay = sdpvar(repmat(1,1,N+1),repmat(1,1,N+1));
sigmau = sdpvar(repmat(1,1,N),repmat(1,1,N));
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));
%mux = sdpvar(repmat(2,1,N),repmat(1,1,N));
muu = sdpvar(repmat(1,1,N),repmat(1,1,N)) ;
%muy = 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,constraint1,constraint2,constraint3,constraint4,constraint5,constraint6,constraint7];
end
objective=objective+(1/2*trace((trace(Q*pN))));
constraint8 = h1y'*(mux{N})<=(1-(0.5*epsilon))*g1-((0.95/2*epsilon*g1*(0.1))*h1y'*(C*sigmax{N}*C'+sigmav)*h1y);
constraint9 = [pN-(sigmay{k}),(muy{N});(muy{N})',1] >=0;
constraint = [constraint,constraint8,constraint9];
F = value(F{1});
Y = value(sigmay{1});
f1{c} = F;
Y1{c} = Y;
K = F/Y;
K1{c} = K;
K2 = value(K1{c});
P = optimizer(constraints,objective,options,{parameter1,parameter2,...},{output1,output2,...})
and another question is that initial constant values are arbitrary or not???
P= optimizer(constraint,objective,options,mux{1},{muu{1},F{1},sigmay{1}})...
P = optimizer(constraints,objective,options,{parameter1,parameter2,...},{output1,output2,...})
controller = optimizer(constraints, objective,[],x{1},[u{:}]);
parameters_in = {x{1},[r{:}],d,pastu,B};
solutions_out = {[u{:}], [x{:}]};
controller = optimizer(constraints, objective,sdpsettings('solver','gurobi'),parameters_in,solutions_out);
parameters_in = {F{1},p{1},pN,muu{1},mux{1},muy{1},sigmay{1},sigmau{1},sigmax{1}};
solutions_out = {[mux{1}],[muu{1}],[F{1}],[sigmay{1}],[muy{1}]};
controller = optimizer(constraints,objective,ops,parameters_in,solutions_out);
F{1} = 0;
muu{1} = 0;
mux{1} = [-5;2];
muy{1} = C*mux{1};
sigmax{1} = [1,0;0,1];
sigmay{1} = C*sigmax{1}*C
'+sigmav;
c=0;
for t=t0:dt:tf-dt
c=c+1;
sol = controller(F{1},p{1},pN,muu{1},mux{1},muy{1},sigmay{1},sigmau{1},sigmax{1});
mux3 = sol{1};
u = sol{2};
F = sol{3};
Y = sol{4};
muy3 = sol{5};
mux1(c) = mux3(1,1);
mux2(c) = mux3(2,1);
x = value(x);
y = value(y);
U1(c) = u(1,1);
f1{c} = F;
Y1{c} = Y;
K = F/Y;
K1{c} = K;
K2 = value(K1{c});
u1 = u+K2*((y)-(muy3));
u1 = value(u1);
u2{c} = u1;
x = A*x+B*u1+G*w(:,c);
y = C*x+v(:,c);
sol{1} = value(mux{2});
sigmax{1} = value(sigmax{2});
sol{5} = C*sol{1};
sigmay{1} = C*sigmax{1}*C'
+sigmav;
muy1(c) = muy3;
y1(c) = y;
ud = value(u2{c});
ud1(c) = ud(1,1);
x1(c) = x(1,1);
x2(c) = x(2,1);
end
%% Plot results
figure(1)
subplot(3,1,1)
plot(muy1,'linewidth',2)
xlabel('Time')
ylabel('muy1')
subplot(3,1,2)
plot(mux1,'linewidth',2)
xlabel('Time')
ylabel('mux1')
subplot(3,1,3);
plot(mux2,'linewidth',2)
xlabel('Time')
ylabel('mux2')
controller = optimizer(constraints,objective,ops,mux{1},muu{1}
);
x =
for t = ...
u =controller(x);
x =A*x+B*u+...
end
solutions_out = {[mux{1}],[muu{1}],[F{1}],[sigmay{1}],[muy{1}]};
mux3 = sol{1};
u = sol{2};
F = sol{3};
Y = sol{4};
muy3 = sol{5};
for t=t0:dt:tf-dt
c=c+1;
%sol = controller(F{1},p{1},pN,muu{1},mux{1},muy{1},sigmay{1},sigmau{1},sigmax{1});
sol=controller(mux{1});
mux3 = sol{1};
u = sol{2};
F = sol{3};
Y = sol{4};
muy3 = sol{5};
mux1(c) = mux3(1,1);
mux2(c) = mux3(2,1);
U1(c) = u(1,1);
f1{c} = F;
Y1{c} = Y;
K = F/Y;
K1{c} = K;
K2 = value(K1{c});
u1 = u+K2*((y)-(muy3));
u1 = value(u1);
u2{c} = u1;
x = A*x+B*u1+G*w(:,c);
y = C*x+v(:,c);
sol{1} = value(mux{2});
sigmax{1} = value(sigmax{2});
sol{5} = C*sol{1};
sigmay{1} = C*sigmax{1}*C
'+sigmav;
muy1(c) = muy3;
sol{4} = C*sigmax{1}*C'+sigmav;
y1(c) = y;
ud = value(u2{c});
ud1(c) = ud(1,1);
x1(c) = x(1,1);
x2(c) = x(2,1);
end
%parameters_in = {F{1},p{1},pN,muu{1},mux{1},muy{1},sigmay{1},sigmau{1},sigmax{1}};
solutions_out = {[mux{1}],[muu{1}],[F{1}],[sigmay{1}],[muy{1}]};
controller = optimizer(constraints,objective,ops,mux{1},solutions_out);
...
...
Controller = optimizer(con,obj,ops,mux{1},{mux{2},muu{1},F{1}, sigmay{1} and muy{1} )
x = randn(2,1)
mux1 = [-2;5]% or what ever you logic is for initial guess of estimate ...
for t = ...
sol = Controller(mux1),
construct u1 from solution
x = A*x + B*u1 + ...
% Take mux{2} solution as new state estimate
mux1 = sol{1};
end