-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunADMM_fd.m
156 lines (151 loc) · 4.07 KB
/
runADMM_fd.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
function [sigma_ADMM_fd,l_ADMM_fd,sigma_n_ADMM_fd,Steps,IterCounts] = runADMM_fd(Agents,M,stepSize,epsilon,maxOutIter,maxInIter)
%RUNADMM Summary of this function goes here
% Detailed explanation goes here
if usejava('desktop')
wbVisibility=true;
else
wbVisibility=false;
end
D=length(Agents(1).l);
outADMMflag=1;
outIterCount=0;
% the outer iteration begains here
updated_z=[Agents(1).sigma_f;Agents(1).l;Agents(1).sigma_n];
Zs=cell(M,1);
for m=1:M
Zs{m}=[];
end
IterCounts=cell(M,1);
for m=1:M
IterCounts{m}=1;
end
Steps=cell(M,1);
for m=1:M
Steps{m}=[];
end
outSteps=[];
if wbVisibility
wb=waitbar(0,'Preparing','Name','ADMM_{fd}');
set(wb,'color','w');
end
sub_old_z=zeros(D+2,M);
for m=1:M
sub_old_z(:,m)=[Agents(m).sigma_f;Agents(m).l;Agents(m).sigma_n];
end
while outADMMflag
outIterCount=outIterCount+1;
if ~mod(outIterCount,500)
disp(outIterCount);
disp(step);
end
Agents=Agents.obtain_data_from_Neighbor_ADMM_fd;
global_step=0;
updated_z=0;
parfor m=1:M
% [Agents(m),~,~,~]=Agents(m).runLocalInnerADMM_fd(maxInIter,epsilon);
[Agents(m),Zs_m,Steps_m,inIterCount_m]=Agents(m).runLocalInnerADMM_fd(maxInIter,epsilon);
Zs{m}=[Zs{m},Zs_m];
Steps{m}=[Steps{m},Steps_m];
IterCounts{m}=[IterCounts{m},IterCounts{m}(outIterCount)+inIterCount_m];
step_m=norm([Agents(m).sigma_f;Agents(m).l;Agents(m).sigma_n]-sub_old_z(:,m));
global_step=max(global_step,step_m);
sub_old_z(:,m)=[Agents(m).sigma_f;Agents(m).l;Agents(m).sigma_n];
updated_z=updated_z+[Agents(m).sigma_f;Agents(m).l;Agents(m).sigma_n];
end
updated_z=updated_z/M;
step = global_step;
outSteps=[outSteps,step];
if wbVisibility
waitbar(outIterCount/maxOutIter,wb,sprintf('%s %.2f %s %f','ADMM_{fd}: ',outIterCount/maxOutIter*100,'% , step:', step))
end
if step < epsilon
outADMMflag=0;
end
if outIterCount>=maxOutIter
outADMMflag=0;
end
end
sigma_ADMM_fd=updated_z(1);
l_ADMM_fd=updated_z(2:(2+D-1));
sigma_n_ADMM_fd=updated_z(end);
% Plot
gcf=figure;
tiledlayout(D+2,1,'TileSpacing','Compact','Padding','Compact');
realDataSet=Agents(1).realdataset;
for z_i=1:(D+2)
nexttile(z_i);
hold on
y_c=yline(Zs{1}(z_i,end),'b-.');
if realDataSet==0
y_r=yline(Agents(1).realz(z_i),'r-.');
end
for m=1:M
plot(Zs{m}(z_i,:));
end
xlabel('steps')
if z_i==1
ylabel('\sigma_f');
elseif z_i==D+2
ylabel('\sigma_n');
else
ylabel(strcat('l_',num2str(z_i-1)));
end
if z_i==1
if realDataSet==0
legendTxt=cell(2,1);
legendTxt{1}='converged value';
legendTxt{2}='real hyperparameter value';
lgd=legend([y_c;y_r],legendTxt,'Location','northoutside','Orientation', 'Horizontal');
else
lgd=legend(y_c,'converged value','Location','northoutside','Orientation', 'Horizontal');
end
% lgd.Layout.Tile = 'north';
end
sgtitle('ADMM_{fd} - hyperparameters')
hold off
end
s=hgexport('factorystyle');
s.LineWidthMin=1.5;
s.Resolution=600;
s.Format='png';
s.FontSizeMin=14;
s.Format='png';
top_dir_path=strcat('results/HO');
folder_name=strcat(num2str(M),'_a_',num2str(Agents(1).TotalNumLevel),'_pl');
full_path=strcat(top_dir_path,'/',folder_name,'/');
fname=strcat(full_path,'ADMM_fd_vars');
% fname=strcat(fname,'_',num2str(M),'_agents');
hgexport(gcf,fname,s);
for z_i=1:(D+2)
nexttile(z_i);
set(gca,'XScale','log')
end
fname=strcat(fname,'_logx');
hgexport(gcf,fname,s);
close gcf
%
gcf=figure;
%% below there is bug
semilogy(IterCounts{1}(2:end),outSteps);
xlabel('steps')
ylabel('step size')
title('ADMM_{fd} - step size')
s=hgexport('factorystyle');
s.LineWidthMin=1.5;
s.Resolution=600;
s.Format='png';
s.FontSizeMin=14;
s.Format='png';
s.Format='png';
fname=strcat(full_path,'ADMM_fd_Steps');
% fname=strcat(fname,'_',num2str(M),'_agents');
hgexport(gcf,fname,s);
set(gca,'XScale','log')
fname=strcat(fname,'_logx');
hgexport(gcf,fname,s);
close gcf
Steps=outSteps;
if wbVisibility
delete(wb);
end
end