forked from aminiam90/consensus.attack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example1.m
387 lines (281 loc) · 9.86 KB
/
Example1.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
%% TO RUN This m file, YALMIP and SDPT3 Should be Installed beforehand:
%%%
%%% 1-Simply set matlab current folder as the folder which contains Yalmip
%%% and SDPT3
%%% 2- right click on both Yalmip and SDPT3
%%% 3- Add to path --> Selected folders and subfolders. Done!
close all
clear all
global A B K Laplacian j_sim na e x_broad ma u_s gamma_bar
N=5; %number of agents
%% agent model
A=[0 1; 0 -0.4 ];
B=[0.8 0.5]';
% A=rand(2,2);
na=size(A,1);
% B=rand(2,1);
ma=size(B,2);
%% initial parameters
tau=0.01; % buffer time
h=0.05; % sampling period
mu=tau+h;
zeta1=0.3; % exponential rate
Theta_bar=0.3;
gamma_bar=0.1;
u_s=0.05;
psi_FDI=0.5;
thresh=0.0001;
t_sim=0:0.01:15.01;
alpha0=0.01;
alpha1=1;
alpha2=0.001;
sample_period=0.01;
%%% robot parameters
%% Laplacian
Laplacian = [ 2 -1 0 0 -1;
-1 2 -1 0 0;
0 -1 2 -1 0 ;
0 0 -1 2 -1 ;
-1 0 0 -1 2 ];
%%%%%%%%%%%%%%%%%%%%%%%%%%
L=Laplacian;
[aa,bb]=eig(L);
V_inverse=inv(aa);
J=bb(2:end,2:end); V=V_inverse(2:end,:);
%% decision variables
Q=sdpvar(na,na);
Y111=sdpvar(na,na); Y112=sdpvar(na,na,'full'); Y122=sdpvar(na,na);
Y211=sdpvar(na,na); Y212=sdpvar(na,na,'full'); Y222=sdpvar(na,na);
Y311=sdpvar(na,na); Y312=sdpvar(na,na,'full'); Y322=sdpvar(na,na);
Y1=[Y111 Y112; Y112' Y122];
Y2=[Y211 Y212; Y212' Y222];
Y3=[Y311 Y312; Y312' Y322];
P1=sdpvar(na,na); P2=sdpvar(na,na); P3=sdpvar(na,na); P4=sdpvar(na,na); P5=sdpvar(na,na);
H=sdpvar(na,na,'full');
F1=sdpvar(na,na,'full'); F2=sdpvar(na,na,'full');
F=[F1' F2']';
E1=sdpvar(na,na,'full'); E2=sdpvar(na,na,'full');
E=[E1' E2']';
G1=sdpvar(na,na,'full'); G2=sdpvar(na,na,'full');
G=[G1' G2']';
S1=sdpvar(na,na,'full'); S2=sdpvar(na,na,'full');
S=[S1' S2']';
U1=sdpvar(na,na,'full'); U2=sdpvar(na,na,'full');
U=[U1' U2']';
D1=sdpvar(na,na,'full'); D2=sdpvar(na,na,'full');
D=[D1' D2']';
M=sdpvar(ma,na);
%%%%%%%%% BLOCKS %%%%%%%%%%%%
W11=kron(eye(N-1),P2+P3+E1+E1'+S1'+S1+A*H'+H*A'+tau*Y111+mu*Y211+(mu-tau)*Y311+2*zeta1*P1);
W12=kron(eye(N-1),G1-E1+E2'+F1-S1+S2'+U1-D1+H*A'+tau*Y112+mu*Y212+(mu-tau)*Y312)+kron(J,B*M);
W13= kron(eye(N-1),D1-F1);
W14=kron(eye(N-1),-G1-U1);
W15=kron(eye(N-1),P1-H'+H*A');
W16=kron(J*V,B*M);
T11=Theta_bar*kron(J*V,B*M);
T12=kron(V,B);
T13=-kron(V,B);
T14=-kron(V,B);
T15= zeros((N-1)*na,(N)*ma);
T16= zeros((N-1)*na,N*ma);
W21=W12';
W22=kron(eye(N-1),F2+F2'+G2+G2'+U2+U2'-S2-S2'-E2-E2'-D2-D2'+tau*Y122+mu*Y222+(mu-tau)*Y322)+kron(J,B*M)+kron(J,B*M)';
W23=kron(eye(N-1),D2-F2);
W24=kron(eye(N-1),-G2-U2);
W25=-kron(eye(N-1),H')+kron(J,M'*B');
W26=kron(J*V,B*M);
T21=Theta_bar*kron(J*V,B*M);
T22=kron(V,B);
T23=-kron(V,B);
T24=-kron(V,B);
T25=gamma_bar*kron(J*V,M');
T26= zeros((N-1)*na,N*ma);
W31=W13'; W32=W23';
W33=kron(eye(N-1),-exp(-2*tau*zeta1)*P2);
W34=zeros((N-1)*na,(N-1)*na); W35=zeros((N-1)*na,(N-1)*na);
W36=zeros((N-1)*na,(N)*na);
T31=zeros((N-1)*na,(N)*na);
T32=zeros((N-1)*na,(N)*ma);
T33=zeros((N-1)*na,(N)*ma);
T34=zeros((N-1)*na,(N)*ma);
T35=zeros((N-1)*na,(N)*ma);
T36=zeros((N-1)*na,(N)*ma);
W41=W14'; W42=W24'; W43=W34';
W44=kron(eye(N-1),-exp(-2*mu*zeta1)*P3);
W45=zeros((N-1)*na,(N-1)*na); W46=zeros((N-1)*na,(N)*na);
T41=zeros((N-1)*na,(N)*na);
T42=zeros((N-1)*na,(N)*ma);
T43=zeros((N-1)*na,(N)*ma);
T44=zeros((N-1)*na,(N)*ma);
T45=zeros((N-1)*na,(N)*ma);
T46=zeros((N-1)*na,(N)*ma);
W51=W15'; W52=W25'; W53=W35'; W54=W45';
W55=kron(eye(N-1),tau*P4+mu*P4+(mu-tau)*P5-H'-H);
W56=kron(J*V,B*M);
T51=Theta_bar*kron(J*V,B*M);
T52=kron(V,B);
T53=-kron(V,B);
T54=-kron(V,B);
T55=zeros((N-1)*na,(N)*ma);
T56= zeros((N-1)*na,(N)*ma);
W61=W16'; W62=W26'; W63=W36'; W64=W46'; W65=W56'; W66=-kron(eye(N),Q);
T61=zeros((N)*na,(N)*na);
T62=zeros((N)*na,(N)*ma);
T63=zeros((N)*na,(N)*ma);
T64=zeros((N)*na,(N)*ma);
T65=zeros((N)*na,(N)*ma);
T66=gamma_bar*kron(V'*J*V,M');
W=[W11 W12 W13 W14 W15 W16;
W21 W22 W23 W24 W25 W26;
W31 W32 W33 W34 W35 W36;
W41 W42 W43 W44 W45 W46;
W51 W52 W53 W54 W55 W56;
W61 W62 W63 W64 W65 W66];
T=[T11 T12 T13 T14 T15 T16;
T21 T22 T23 T24 T25 T26;
T31 T32 T33 T34 T35 T36;
T41 T42 T43 T44 T45 T46;
T51 T52 T53 T54 T55 T56;
T61 T62 T63 T64 T65 T66];
R=blkdiag(-kron(eye(N),H+H')+eye(N*na),-eye(N*ma),-eye(N*ma),-eye(N*ma),-eye((N)*ma),-eye(N*ma));
C1=[ W T; T' R];
C2=[Y1 F;F' exp(-2*tau*zeta1)*P4]; C3=[Y1 E;E' exp(-2*tau*zeta1)*P4]; C4=[Y2 G;G' exp(-2*mu*zeta1)*P4];
C5=[Y2 S;S' exp(-2*mu*zeta1)*P4]; C6=[Y3 U;U' exp(-2*mu*zeta1)*P5]; C7=[Y3 D;D' exp(-2*mu*zeta1)*P5];
constraints =[ C1<=0, C2>=0, C3>=0, C4>=0, C5>=0, C6>=0, C7>=0, P1>=0, P2>=0, P3>=0, P4>=0, P5>=0, Q>=0 ];
info=solvesdp(constraints,[], sdpsettings('solver','sdpt3'))
%% Design parameters for leaders
K=double(M)*inv(double(H)')
Phi=inv(double(H))*double(Q)*inv(double(H)')
%
eig(kron(eye(N-1),A)+kron(J,B*K))
bar_P1=inv(double(H))*double(P1)*inv(double(H)');
bar_P2=inv(double(H))*double(P2)*inv(double(H)');
bar_P3=inv(double(H))*double(P3)*inv(double(H)');
b1=max(eig(bar_P1))+tau*max(eig(bar_P2))+mu*max(eig(bar_P3));
b2=min(eig(bar_P1));
b_ratio=sqrt(b1/b2);
r=sqrt(N*(u_s^2+psi_FDI^2+alpha0+alpha2)/(2*zeta1));
%%%%%%%%%%%
%% real-time consensus %%%%%%%%%%%%%%%%%%%%%%
for i=1:N
x_init=[6*i i]'; %%% initial values
X_init{i}= x_init;
X_init_exp{i}=X_init{i}' ;
x_stack{i}=[];
u_stack{i}=[];
event_time{i}=0;
end
%%% Initial values for X
X_init_save=X_init;
for i=1:N
x_broad{i}=X_init{i}; x_broad_for_e{i}=X_init{i};
x_old{i}=X_init{i}; xi5_old{i}=0;
end
counter=zeros(1,N);
Tk=zeros(1,N);
z_store=[];
rate_upper_bound_store=[];
z_zero=norm(kron(V,eye(na))*reshape(cell2mat(X_init),[1 N*na])');
for iterations=1:length(t_sim)-2
x_store=[];
for j_sim=1:N
if rand <=gamma_bar
flag=1;
else
flag=0;
end
[t,xx] =ode45(@IS_dif,[t_sim(iterations) t_sim(iterations+1) t_sim(iterations+2) ],x_old{j_sim});
x_stack{j_sim} = [ x_stack{j_sim} xx(2,:)'];
current_x{j_sim}=xx(2,:)';
x_store=[x_store; current_x{j_sim} ];
e{j_sim}=(x_broad_for_e{j_sim})-current_x{j_sim};
x_old{j_sim}=xx(2,:);
end
if mod(iterations,h*100)==0
for j_sim=1:N
% z{j_sim}=norm((kron(Laplacian(j_sim,:),eye(na)))*cell2mat((x_broad)'));
if e{j_sim}'*Phi*e{j_sim}>alpha0*exp(-alpha1*t_sim(iterations))+alpha2
x_broad_for_e{j_sim}=current_x{j_sim};
if rand <=Theta_bar
x_broad{j_sim}=current_x{j_sim}+psi_FDI*tanh(0.1*j_sim*t_sim(iterations));
else
x_broad{j_sim}=current_x{j_sim};
end
counter(j_sim)=counter(j_sim)+1; %%%%%%% event isnats
event_time{j_sim}=[event_time{j_sim} t_sim(iterations)];
Tk(j_sim)=t_sim(iterations);
end
end
end
for j_sim=1:N
u_stack{j_sim} = [ u_stack{j_sim} K*kron(Laplacian(j_sim,:),eye(na))*cell2mat((x_broad)')];
end
z=norm(kron(V,eye(na))*reshape(cell2mat(current_x),[1 N*na])');
z_store=[z_store z];
rate_upper_bound=b_ratio*z_zero*exp(-zeta1*t_sim(iterations))+r;
rate_upper_bound_store=[rate_upper_bound_store rate_upper_bound];
% if <=thresh*norm(kron(V,eye(na))*reshape(cell2mat(X_init),[1 N*na])')
% break
% end
end
%%
C = {'k','b','r','g','m',[.5 .6 .7],[.8 .2 .6]};
figure(2)
plot(t_sim(1:iterations),x_stack{1}(1,1:end),'color','b','linewidth',2)
hold on
grid on
plot(t_sim(1:iterations),x_stack{1}(2,1:end),'color','r','linewidth',2)
AX=legend('$r_i(t)$','$v_i(t)$','Interpreter','latex','FontSize',20,'fontweight','bold');
for j_sim=1:N
fig_h= plot(t_sim(1:iterations),x_stack{j_sim}(1,1:end),'color','b','linewidth',2);
set(fig_h.Annotation.LegendInformation, 'IconDisplayStyle', 'off')
hold on
fig_h= plot(t_sim(1:iterations),x_stack{j_sim}(2,1:end),'color','r','linewidth',2);
set(fig_h.Annotation.LegendInformation, 'IconDisplayStyle', 'off')
end
xlabel('Time (sec)', 'FontSize', 20)
ylabel('States ', 'FontSize', 20)
title('(a)', 'FontSize', 20)
set(gca,'FontSize',20,'fontweight','bold')
xlim([0, t_sim(iterations)])
figure(3)
%
for j_sim=1:N
for i=1:length(event_time{j_sim})
plot([event_time{j_sim}(i) event_time{j_sim}(i) ],[ j_sim j_sim+0.2], 'color',C{j_sim},'linewidth',0.5)
hold on
end
plot([0 iterations/100],[j_sim j_sim], 'color', 'k')
end
%
xlabel('Events (sec)', 'FontSize', 20)
ylabel('Agent', 'FontSize', 20)
set(gca,'FontSize',20,'fontweight','bold')
xlim([0, t_sim(iterations)])
set(gca,'FontSize',20,'fontweight','bold')
% end
figure(4)
for j_sim=1:N
fig_h= plot(t_sim(1:iterations),u_stack{j_sim}(1,1:end),'color',C{j_sim},'linewidth',2);
% set(fig_h.Annotation.LegendInformation, 'IconDisplayStyle', 'off')
hold on
end
grid on
AX=legend('$u_1^F(t)$','$u_2^F(t)$','$u_3^F(t)$','$u_4^F(t)$','$u_5^F(t)$','Interpreter','latex','FontSize',20,'fontweight','bold');
xlabel('Time (sec)', 'FontSize', 20)
set(gca,'FontSize',20,'fontweight','bold')
xlim([0, t_sim(iterations)])
figure(5)
plot(t_sim(1:iterations),z_store(1:end),'color','b','linewidth',2)
hold on
grid on
plot(t_sim(1:iterations),rate_upper_bound_store(1:end),'color','r','linewidth',2)
AX=legend('$\| z(t) \|$','$\sqrt{b_1/b_2}e^{-\zeta_1 t}\|z(0) \|+c$','Interpreter','latex','FontSize',20,'fontweight','bold');
xlabel('Time (sec)', 'FontSize', 20)
set(gca,'FontSize',20,'fontweight','bold')
xlim([0, t_sim(iterations)])
tf=iterations/100
AE=mean(counter(1:N))
AIET=tf/AE
control_max=max(max(abs(cell2mat(u_stack'))))
% [norm(K) norm(Phi) iterations/100 AT (iterations/100)/AT ]