-
Notifications
You must be signed in to change notification settings - Fork 5
/
SyntPWQBMIElp.m
411 lines (335 loc) · 11.2 KB
/
SyntPWQBMIElp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
function [pwasys,pwactrl] = SyntPWQBMIElp(pwasys, option)
% Copyright: Mohsen Zamani Fekri, Concordia University April 2011
%
%
[NR NS] = size(pwasys.Abar); % Number of Systems, Number of Regions
n = size(pwasys.Abar{1},1)-1; % Number of state variables
m = size(pwasys.Bbar{1},2); % Number of inputs
alpha=option.alpha;
istar = [];
A = pwasys.A;
a = pwasys.a;
B = pwasys.B;
F = pwasys.F;
f = pwasys.f;
xcl=pwasys.xcl;
pwatype=pwasys.type;
if strcmp(pwatype, 'lower-envelope')
col_index=[1];
elseif strcmp(pwatype, 'pwadi')
col_index=[1 2];
elseif strcmp(pwatype, 'null')
col_index=[];
end
L=length(col_index);
%% Regions equations with ellipsiodal approximations
% R=norm(E*z +e) < 1
if ~isfield(pwasys, 'EpA') | ~isfield(pwasys, 'Epb')
pwasys=MinElp(pwasys);
end
for i=1:NR
E{i}=pwasys.EpA{i};
e{i}=pwasys.Epb{i};
end
%% extracting system data in seperated form from pwasys cell
for i=1:NR,
xcl_is_inside_Ri = all(pwasys.E{i}*xcl+pwasys.e{i}>=0-1e-7);
if xcl_is_inside_Ri,
istar = union(istar,i); % Center region(s)
end
end
region_excluded_istar=setdiff([1:NR], istar);
%% shifting the equations with respect to the equilibrium point
% shifting a{i,j}
for i=1:NR,
for j=col_index,
a{i,j} = a{i,j}+ A{i,j}*xcl;
end
end
% shifting the intersection equations R_i \cap R_j= F_{ij}*s+f_{ij}
% F{i,j} remains the same. Only f{i,j} changes
for i=1:NR
for j=1:NR
if ~isempty(f{i,j})
f{i,j} = f{i,j}-xcl;
end
end
end
% shifting the ellipsidal regions equations. only e changes, E remains the
% same.
for i=1:NR
e{i}=e{i}+ E{i}*xcl;
sys_E{i}=pwasys.E{i};
sys_e{i}=pwasys.E{i}*xcl+pwasys.e{i};
end
%% building a shifted "pwasys" model.
for i=1:NR
shifted_model.Ebar{i}=pwasys.Ebar{i};
shifted_model.Ebar{i}(1:end-1, 1:end)=[sys_E{i} sys_e{i}];
for j=col_index
shifted_model.Abar{i,j}=pwasys.Abar{i,j};
shifted_model.Abar{i,j}(1:n, n+1)=a{i,j};
end
end
for i=1:NR
for j=1:NR
shifted_model.Fbar{i,j}=[];
if ~isempty(f{i,j})
shifted_model.Fbar{i,j}=pwasys.Fbar{i,j};
shifted_model.Fbar{i,j}(1:n, n)=f{i,j};
end
end
end
shifted_model.Bbar=pwasys.Bbar;
shifted_model.type=pwatype;
%% YALMIP initialzation
yalmip('clear');
constraints=set([]);
disp('Defining Matrix P_i for Lyapunov function')
%% defining varibales P_i for a piecewise-quadratic lyapunov
%% function
NR=size(pwasys.Abar,1);
Num_S=size(pwasys.Abar{1,1},1);
% converting MXN=0 equations to a unique AY=0 equation
[reshaping,lyap_info]=LyapContGeneral(shifted_model);
istar=[];
for i_P=1:NR,
xcl_is_inside_Ri = all(pwasys.Ebar{i_P}*[xcl;1]>=0-1e-7);
if xcl_is_inside_Ri,
istar = union(istar,i_P); % Center region(s)
end
end
clear p_var P_vec P_mtrx
unrptd_var_length=0;
for x=1:Num_S
unrptd_var_length=unrptd_var_length+x;
end
p_temp={};
p_var=sdpvar(1, NR*unrptd_var_length);
tot_index_P=1:size(lyap_info.S, 2); % indices of total variables
indp_index_P=lyap_info.d; % if B is fixed lower and upper boundry have the same result. we have chosen lower boundry for our alculation
depn_index_P=setdiff(tot_index_P, indp_index_P); % dependent variable
for m_P=indp_index_P
p_var(m_P)=0;
end
for m_P=depn_index_P
p_var(m_P)=sdpvar(1);
p_temp{size(p_temp,2)+1}=p_var(m_P);
if norm(lyap_info.S(:,m_P))~=0
[base_index_P, non_impt, base_coef]=find(lyap_info.w(:,m_P));
for k_P=1:length(base_index_P)
b_indx_indp=base_index_P(k_P); % index_P of the independent vector
b_indx_P=lyap_info.d(b_indx_indp);
val_indx_P=base_coef(k_P); % coefficient by which the independent
p_var(b_indx_P)=p_var(b_indx_P)-val_indx_P*(p_var(m_P)); % linear
end
end
end
for i_P=1:NR
P_vec{i_P}=p_var((i_P-1)*unrptd_var_length+1:i_P*unrptd_var_length);
end
for i_P=1:NR
P_mtrx{i_P}=sdpvar(Num_S);
for j_P=1:unrptd_var_length
r_P=reshaping(j_P ,2);
q_P=reshaping(j_P ,3);
P_mtrx{i_P}(r_P,q_P)=P_vec{i_P}(reshaping(j_P,1));
P_mtrx{i_P}(q_P,r_P)=P_vec{i_P}(reshaping(j_P,1));
end
end
%% DEFINIG VARIABLES IN YALMIP DEFINING K
clc
disp('Defining gain matrix K_i and k_i')
% defining varibales [K_i k_i] for a piecewise-quadratic lyapunov
% function
NR=size(pwasys.Abar,1);
Num_S=size(pwasys.Abar{1,1},1)-1; % number of states
Num_I=size(pwasys.Bbar{1,1},2); % number of inputs
xy_list=[];
for x=1:NR
for y=x+1:NR
if ~isempty(pwasys.Fbar{x,y})
xy_list=[xy_list; x y];
end
end
end
Num_rltn=size(xy_list,1); % number of unique F_ij relation
rgn_lngth=(Num_S+1)*Num_I;
istar=[];
for i_I=1:NR,
xcl_is_inside_Ri = all(pwasys.Ebar{i_I}*[xcl;1]>=0-1e-7);
if xcl_is_inside_Ri,
istar = union(istar,i_I); % Center region(s)
end
end
% converting MXN=0 equations to a unique AY=0 equation
%%-------------------------------------------------------------------------
%%all directons (more conservative)
if option.NormalDirectionOnly
% to force the normal direction and bypass the whole direction approach
row_check=0;
else
[reshaping, input_info]=CtrlContAllDir(istar, shifted_model);
affne_indx=NR*rgn_lngth+1: NR*rgn_lngth+NR;
indp_affne_indx=intersect(affne_indx, input_info.d);
row_check=1; % to make it a DEFINED variable
for i_I=indp_affne_indx
[r_I,c_I]=find(input_info.d==i_I);
row_check=input_info.w(c_I, :);
row_check(i_I)=[];
end
manual_istar_indx=(NR-1)*rgn_lngth+1:(NR-1)*rgn_lngth+Num_S*Num_I; %indices of varibles
% we intend to set manually as the equilibrium region
indp_manual_indx=intersect(manual_istar_indx, input_info.d);
end
if norm(row_check)==0 || ~isempty(indp_manual_indx)
%normal direction (less conservative)
[reshaping, input_info]=CtrlContNormalDir(istar, shifted_model);
affne_indx=NR*rgn_lngth+1: NR*rgn_lngth+NR;
indp_affne_indx=intersect(affne_indx, input_info.d);
for i_I=indp_affne_indx
[r_I,c_I]=find(input_info.d==i_I);
row_check=input_info.w(c_I, :);
row_check(i_I)=[];
if norm(row_check)==0 %second time we check
disp('error! equilibrium constraints cannot be satisfied')
return
end
end
manual_istar_indx=(NR-1)*rgn_lngth+1:(NR-1)*rgn_lngth+Num_S*Num_I; %indices of varibles
% we intend to set manually as the equilibrium region
indp_manual_indx=intersect(manual_istar_indx, input_info.d);
if ~isempty(indp_manual_indx) %second time we check
disp('linear relation exists between istar region gains')
disp('warning! manual gain selecting for istar region is not allowed')
return
end
end
clear K_mtrx K_vec k_var k_temp
k_var=sdpvar(1, NR*rgn_lngth);
K_robust=robust_LQR_piecewise_quad(istar(1),pwasys,option);
k_var(manual_istar_indx)=reshape(K_robust, 1, Num_S*Num_I);
k_temp={};
tot_index_I=1:size(input_info.S, 2); % indices of total variables
indp_index_I=input_info.d;
depn_index_I=setdiff(tot_index_I, indp_index_I); % dependent variables
for m_I=indp_index_I
k_var(m_I)=0;
end
for m_I=depn_index_I
% dependent vector corresponds to an independent
% variable. it is true even if the vector is completely zero.
if ismember(m_I, affne_indx)
k_var(m_I)=1;
elseif (~ismember(m_I, manual_istar_indx) & ~ismember(m_I, affne_indx)) % manual istar gains should not be set again
%elseif (~ismember(m_I, affne_indx))
k_var(m_I)=sdpvar(1);
k_temp{size(k_temp,2)+1}=k_var(m_I);
end
if norm(input_info.S(:,m_I))~=0 % if the vector is not completey zero,
% then the vsribles corresponding to
% its bases will depend on the varibale
% corresponding to lyap_info.S(:,m_I)
[base_index_I, non_impt, base_coef]=find(input_info.w(:,m_I));
for k_I=1:length(base_index_I)
b_indx_indp=base_index_I(k_I); % index_P of the independent vector
b_indx_I=input_info.d(b_indx_indp);
val_indx_I=base_coef(k_I); % coefficient by which the independent
k_var(b_indx_I)=k_var(b_indx_I)-val_indx_I*(k_var(m_I)); % li
end
end
end
for i_I=1:NR
m_I=reshaping(i_I);
K_vec{m_I}=k_var((i_I-1)*rgn_lngth+1:i_I*rgn_lngth);
end
for i_I=1:NR
K_mtrx{i_I}=sdpvar(Num_I, Num_S+1);
K_mtrx{i_I}=reshape(K_vec{i_I}, Num_I, Num_S+1);
end
%% end of defining variables
for i=1:NR
K{i}=K_mtrx{i}(:,1:Num_S);
k{i}=K_mtrx{i}(:,Num_S+1);
P{i}=P_mtrx{i}(1:end-1, 1:end-1);
q{i}=P_mtrx{i}(1:end-1, end);
r{i}=P_mtrx{i}(end,end);
end
for i=region_excluded_istar
miu{i}=sdpvar(1);
bita{i}=sdpvar(1);
end
%% Central Region equations: Y, Z and DV
%inequality for negative definitness vdot < alpha * v
for i=istar
for j=col_index
P_11 = P{i}*(A{i,j}+B{i,j}*K{i})+(A{i,j}+B{i,j}*K{i})'*P{i}+alpha*P{i};
DV{i, j}=[P_11];
end
end
for i=istar
PV{i}=[P{i} zeros(Num_S,1); zeros(1, Num_S) r{i}];
end
%% Regions exculded istar: DV
for i=region_excluded_istar
for j=col_index
P_11 = P{i}*(A{i,j}+B{i,j}*K{i})+(A{i,j}+B{i,j}*K{i})'*P{i}+alpha*P{i}...
+miu{i}*E{i}'*E{i};
P_12 = P{i}*(a{i,j}+B{i,j}*k{i})+miu{i}*E{i}'*e{i};
P_21 = P_12';
P_22 = -miu{i}*(1-e{i}'*e{i});
q_11=zeros(Num_S);
q_12=(A{i,j}+B{i,j}*K{i})'*q{i};
q_21=q_12';
q_22=2*q{i}'*(a{i,j}+B{i,j}*k{i});
DV{i, j}=[P_11 P_12; P_21 P_22]+[q_11 q_12; q_21 q_22];
end
end
for i=region_excluded_istar
PV_11 = P{i}-bita{i}*E{i}'*E{i};
PV_12 = -bita{i}*E{i}'*e{i};
PV_21 = PV_12';
PV_22 = bita{i}*(1-e{i}'*e{i});
qv_11=zeros(Num_S);
qv_12=q{i};
qv_21=qv_12';
qv_22=r{i};
PV{i}=[PV_11 PV_12; PV_21 PV_22]+[qv_11 qv_12; qv_21 qv_22];
end
constraints=set([]);
for i=1:NR
constraints=constraints+set(PV{i}>0,['PV' num2str(i) '>0']);
for j=col_index
constraints=constraints+set(DV{i,j}<0,['DV' num2str(i) '-' num2str(j) '<0']);
end
end
for i=region_excluded_istar
constraints=constraints+set(miu{i}<0,['miu' num2str(i) '<0']);
constraints=constraints+set(bita{i}<0,['bita' num2str(i) '<0']);
end
%
%% solution
u=solvesdp(constraints);
[u1 u2]=checkset(constraints);
pwactrl.problem=u.problem;
pwactrl.u1=u1;
pwactrl.u2=u2;
pwactrl.constraints=constraints;
pwactrl.xcl = xcl;
% pwactrl.istar = istar;
for i=1:NR
P{i}=double(P{i});
q{i}=double(q{i});
r{i}=double(r{i});
K{i}=double(K{i});
k{i}=double(k{i});
pwactrl.Kbar{i} = [K{i} k{i}];
pwactrl.Pbar{i} = [P{i} q{i}; q{i}' r{i}];
end
for i=region_excluded_istar
miu{i}=double(miu{i});
bita{i}=double(bita{i});
pwactrl.miu{i} = miu{i};
pwactrl.bita{i} = bita{i};
end
pwactrl.Ebar = pwasys.Ebar;