-
Notifications
You must be signed in to change notification settings - Fork 3
/
static_group_ADMM_closedForm.m
167 lines (128 loc) · 3.82 KB
/
static_group_ADMM_closedForm.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
function [obj_GADMM, loss_GADMM, Iter, gadmm_time, com_cost]=static_group_ADMM_closedForm(XX,YY, rho, no_workers, num_feature, noSamples, num_iter, obj0...
, acc, coherence_Time, pathCost_static_matrix)
Iter= num_iter;
s1=num_feature;
s2=noSamples;
lambda = zeros(s1,no_workers);
out=zeros(s1,no_workers);
out_prev=zeros(s1,no_workers);
max_iter = num_iter;
gadmm_time(1)=0;
%path0=path;
%pathCost0=pathCost
kk=1;
pathCost=pathCost_static_matrix(kk,:);
kk=kk+1;
for i = 1:max_iter
% i
if (i > 1 && mod(i,coherence_Time)== 0)
%path0=path_matrix(:,kk);
pathCost=pathCost_static_matrix(kk,:);
%[path0, pathCost]=findPath(no_workers);
kk=kk+1;
end
for ii =1:2:no_workers
if(ii~=1)
l=ii-1;
end
if(ii~=no_workers)
r=ii+1;
end
first = (ii-1)*s2+1;
last = first+s2-1;
H=XX(first:last,1:s1);
Y=YY(first:last);
if(i==1)
com_cost(i)=sum(pathCost);
else
com_cost(i)= com_cost(i-1)+sum(pathCost);
end
C1=lambda(:,ii);
if ii==1
%C1 = zeros(s1,1);
term_1 =zeros(s1,1);
else
%C1= lambda(:,ii-1);
term_1=rho*out(:,l);
end
if ii == no_workers
%C2 = zeros(s1,1);
term_2 = zeros(s1,1);
else
%C2= lambda(:,ii);
term_2 = rho*out(:,r);
end
%q=H'*Y+C1-C2+term_1+term_2;
%[L, U] = factor(H, rho);
%x=(H'*H+2*rho*eye(s1,s1))\(H'*Y+C1-C2+term_1+term_2);
%x = U \ (L \ q);
if(ii==1||ii==no_workers)
if(ii==1 && i > 1)
tic
end
x=(H'*H+rho*eye(s1,s1))\(H'*Y-C1+term_1+term_2);
else
x=(H'*H+2*rho*eye(s1,s1))\(H'*Y-C1+term_1+term_2);
end
out_prev(:,ii)=out(:,ii);
out(:,ii) =x;
if(ii==1 && i > 1)
gadmm_time(i)=gadmm_time(i-1)+2*toc;
end
end
for ii =2:2:no_workers
if(ii~=1)
l=ii-1;
end
if(ii~=no_workers)
r=ii+1;
end
first = (ii-1)*s2+1;
last = first+s2-1;
H=XX(first:last,1:s1);
Y=YY(first:last);
C1= lambda(:,ii);
if ii == no_workers
%C2 = zeros(s1,1);
term_2 = zeros(s1,1);
else
%C2= lambda(:,ii);
term_2 = rho*out(:,r);
end
term_1=rho*out(:,l);
if(ii==no_workers)
x=(H'*H+rho*eye(s1,s1))\(H'*Y-C1+term_1+term_2);
else
x=(H'*H+2*rho*eye(s1,s1))\(H'*Y-C1+term_1+term_2);
end
out_prev(:,ii)=out(:,ii);
out(:,ii) =x;
end
for ii=1:no_workers
if(ii~=1)
l=ii-1;
end
if(ii~=no_workers)
r=ii+1;
end
if(ii==1)
lambda(:,ii) = lambda(:,ii) + rho*(out(:,ii)-out(:,r));
elseif(ii==no_workers)
lambda(:,ii) = lambda(:,ii) - rho*(out(:,l)-out(:,ii));
else
lambda(:,ii) = lambda(:,ii) - rho*(out(:,l)-out(:,ii)) + rho*(out(:,ii)-out(:,r));
end
end
final_obj = 0;
for ii =1:no_workers
first = (ii-1)*s2+1;
last = first+s2-1;
final_obj = final_obj + 0.5*norm(XX(first:last,1:s1)*out(:,ii) - YY(first:last))^2;
end
obj_GADMM(i)=final_obj;
loss_GADMM(i)=abs(final_obj-obj0);
if(loss_GADMM(i) < acc)
Iter = i;
break;
end
end