Hi Johan,
I am back with one more question. You solved my problem last time regarding the understanding of the closed-loop Robust
MPC.
The question now is that when using closed loop MPC, the input U is not linear
anymore, and now I would like to know, how to construct objective function of the form:
objective = sum(U(1:N).*var1(1:N)) - sum(R(1:N).*var2(1:N))
in which, var1 and var2 are time varying. As the expression is polynomial, robustify is not able to cope with it. But it's a pity since I already know the exact value of var1, and var2, and the objective funciton should remain in theory bilinear. But I am not able to find a way to call the model as in to roll var1, and var2 with time, such that:
optimizer(constraints, objective,ops,{blah blah, var1, var2},{U,R});
As a reference, following is the code in which var1 = usep, and var2 = res:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
function [Ureal,Rreal] = closedloopRobustMPC_optimizer( set_back, Q_real, Q_mean_mat, Q_mean_vec, Q_ub, Q_lb,N,sim_Time,m,Ad,Bd,usep,res)
usep_in = sdpvar(N,1);
res_in = sdpvar(N,1);
slack = sdpvar(N,1);
W = sdpvar(N,1);
x1 = sdpvar(5,1);
x2 = sdpvar(5,1);
Q_mean_mat_var = sdpvar(4,N);
sb = sdpvar(N,1);
C = [0 0 0 0 1];
Y1 = [];
Y2 = [];
xk1 = x1;
xk2 = x2;
Q_mean_mat2 = Q_mean_mat_var;
set_back2 = sb;
V = sdpvar(N,1);
Z = sdpvar(N,1);
memory = 48; % Between N and 0
L = sdpvar(N,N,'full').*triu(tril(ones(N),-1),-memory);
M= sdpvar(N,N,'full').*triu(tril(ones(N),-1),-memory);
slack = sdpvar(N,1);
U = L*W + V;
R = M*W + Z;
objective= 0;
for k = 1:N
xk2 = Ad*xk1 - Bd(:,1)*(U(k)-R(k)) + Bd(:,2:end-1)*Q_mean_mat2(:,k) + Bd(:,end)*W(k);
Y2 = [Y2;C*xk2];
xk1 = Ad*xk1 - Bd(:,1)*(U(k)) + Bd(:,2:end-1)*Q_mean_mat2(:,k) + Bd(:,end)*W(k);
Y1 = [Y1;C*xk1];
end
%The input is bounded, and the objective is to stay as close as possible to
%the level y=1 while guaranteeing that y<1 is satisfied despite the
%disturbances w.
% F = [70 - slack - set_back2 <= Y <= 74 + slack + set_back2, 0 <= U <= 100, slack >= 0];
F = [70 - slack - set_back2 <= Y1 <= 74 + slack + set_back2,70 - slack - set_back2 <= Y2 <= 74 + slack + set_back2, ...
0 <= U + R <= 100, slack >= 0, R >= 0, U - R >= 0];
% objective = sum(1e6.*slack) + sum(U.*usep(1:N)')*1800*0.00055;
objective = 1e6*sum(slack) + sum(U.*usep_in')*1800*0.00055 - sum(R.*res_in')*1800*0.00055;
ops = sdpsettings('verbose',2, 'solver', '+cplex');
xk1 = [72.896;75.9814;75.8665;72.4428;72.5114];
xk2 = [72.896;75.9814;75.8665;72.4428;72.5114];
set_back2 = set_back;
Q_mean_mat2 = Q_mean_mat;
Ureal = [];
Rreal = [];
G = [uncertain(W), min(Q_lb(5,1:N)) <= W <= max(Q_ub(5,1:N))];
controller = optimizer([F,G], objective,ops,{x1(:,1),x2(:,1), Q_mean_mat_var,sb,usep_in,res_in},{U,R});
for i = 1:48
[sol,diagnostics] = controller{{xk1(:,end), xk2(:,end), Q_mean_mat2(:,i:i+N-1),set_back2(i:i+N-1)',usep(1:N)',res(1:N)'}};
uk = sol{1}(1,1);
rk = sol{2}(1,1);
xk2 = [xk2 Ad*xk1(:,end) - Bd(:,1)*(uk-rk) + Bd(:,2:end-1)*Q_mean_mat2(:,i) + Bd(:,end)*Q_real(i)];
xk1 = [xk1 Ad*xk1(:,end) - Bd(:,1)*uk + Bd(:,2:end-1)*Q_mean_mat2(:,i) + Bd(:,end)*Q_real(i)];
Ureal = [Ureal;uk];
Rreal = [Rreal;rk];
end
figure();
plot(C*xk1(:,2:end),'r','LineWidth',3.5); hold on
plot(C*xk2(:,2:end),'k','LineWidth',3.5);
plot(1:sim_Time,74 + set_back2(1:sim_Time)','LineWidth',4);
plot(1:sim_Time,70 - set_back2(1:sim_Time)','LineWidth',4);
title('CL-RMPC');
figure();
stairs(Ureal,'r','LineWidth',3.5); hold on;
stairs(Rreal,'k','LineWidth',3.5);
title('CL-RMPC')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sdpvar x w c
P = optimizer([-2 <= x <= 2, uncertain(w), -1 <= w <= 1], x + x*w*c,sdpsettings('solver','+gurobi'),c,x)
Optimizer object with 1 inputs and 1 outputs. Solver: GUROBI-GUROBI
K>> P{0}
ans =
-2
K>> P{-10}
ans =
0
f+g'*w>= 0 for all |w|<=r
f-r*norm(g,1)>= 0
objective = norm(1e6*slack(k),inf) + norm(U(k)*usep_in(k)*1800*0.00055,inf) - norm(R(k)*res_in(k)*1800*0.00055,inf);
objective = objective +norm(1e6*slack(k),inf) + norm(U(k)*usep_in(k)*1800*0.00055,inf) - norm(R(k)*res_in(k)*1800*0.00055,inf);
>> {x1(:,1),x2(:,1), Q_mean_mat_var,sb,usep_in,res_in}
ans =
Columns 1 through 4
[5x1 sdpvar] [5x1 sdpvar] [4x48 sdpvar] [48x1 sdpvar]
Columns 5 through 6
[48x1 sdpvar] [48x1 sdpvar]
>> {xk1(:,end), xk2(:,end), Q_mean_mat2(:,i:i+N-1),set_back2(i:i+N-1)',usep(i:i+N-1)',res(i:i+N-1)'}
ans =
Columns 1 through 4
[5x1 double] [5x1 double] [4x48 double] [48x1 double]
Columns 5 through 6
[1x48 double] [1x48 double]
Sorry for the mistake, the transpose of these two terms should be removed. --> usep(i:i+N-1)',res(i:i+N-1)'
X = reshape(X,[],1);Z = reshape(Z,[],1);