-
Notifications
You must be signed in to change notification settings - Fork 5
/
SyntGlblBMIReg.m
316 lines (256 loc) · 8.26 KB
/
SyntGlblBMIReg.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
function [pwasys,pwactrl] = SyntGlblBMIReg(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;
E=pwasys.E;
e=pwasys.e;
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);
%% 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_excuded_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([]);
%% 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
Q=sdpvar(n);
for i=1:NR
K{i}=K_mtrx{i}(:,1:Num_S);
k{i}=K_mtrx{i}(:,Num_S+1);
end
dE=size(E{1}, 1);
for i=region_excuded_istar
Z{i}=sdpvar(dE);
end
%% Central Region equations: Y, Z and DV
%inequality for negative definitness vdot < alpha * v
for i=istar
for j=col_index
Abarij=A{i,j}+B{i,j}*K{i};
P_11 = Q*Abarij+Abarij'*Q+alpha*Q;
DV{i,j}=[P_11];
end
end
%% Regions exculded istar: DV
% Regions exculded istar: DV
for i=region_excuded_istar
for j=col_index
Abarij=A{i,j}+B{i,j}*K{i};
abarij=a{i,j}+B{i,j}*k{i};
P_11 = Q*Abarij+Abarij'*Q+alpha*Q+E{i}'*Z{i}*E{i};
P_12 = Q*abarij+E{i}'*Z{i}*e{i};
P_21 = P_12';
P_22 = e{i}'*Z{i}*e{i};
DV{i,j}=[P_11 P_12; P_21 P_22];
end
end
constraints=set([]);
constraints=constraints+set(Q>0,['Q' '>0']);
for i=1:NR
for j=col_index
constraints=constraints+set(DV{i,j}<0, ['DV' num2str(i) '-' num2str(j) '<0']);
end
end
for i=region_excuded_istar
constraints=constraints+set(Z{i}(:)>0,['Z' 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;
Q=double(Q);
pwactrl.Q = Q;
for i=1:NR
K{i}=double(K{i});
k{i}=double(k{i});
end
for i=region_excuded_istar
Z{i}=double(Z{i});
pwactrl.Z{i} = Z{i};
end
for i=1:NR,
pwactrl.Kbar{i} = [K{i} k{i}];
end
pwactrl.Ebar = pwasys.Ebar;