-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_exp.m
359 lines (358 loc) · 14 KB
/
main_exp.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
function EXP = main_exp(eidx)
% the main function for the experiments and save the results
%% clear and then load the experimental parameters
close all;
clear EXP;
EXP = exp_detail(eidx);
% load the parameters from EXP
d = EXP.d;
iters = EXP.max_iter;
l=EXP.lower;
u=EXP.upper;
x0=EXP.init;
alpha =EXP.alpha;
pre = EXP.precision;
% if EXP.A exists, which means A is not random
% do permutation on A, we get B, reordering on B we get C
if isfield(EXP,'A')
perm = randperm(EXP.d);
B = EXP.A(perm,perm);
% reordering and estimate its run time
tstart = tic;
reod = symrcm(B);
C = B(reod,reod);
tRCM = toc(tstart);
runMyMatA=0;
else
tRCM=zeros(EXP.n_loop,1);
runMyMatA=1;
end
% create variables for saving the results
% save runtime
T_c = zeros(9,EXP.n_loop);
T_r = zeros(9,EXP.n_loop);
% to save the KKT condition, related to normal cone
% 1,2,3 for matrix A, B, C
% first n_loop rows store size1
% second n_loop rows store size2
% third n_loop rows store size3
KKT_A = cell(3*EXP.n_loop,1);
KKT_B = cell(3*EXP.n_loop,1);
KKT_C = cell(3*EXP.n_loop,1);
% to save KKT condition of RBCD
KKTr_A = cell(3*EXP.n_loop,1);
KKTr_B = cell(3*EXP.n_loop,1);
KKTr_C = cell(3*EXP.n_loop,1);
% to save objective, which is function value
OBJ_A = cell(3*EXP.n_loop,1);
OBJ_B = cell(3*EXP.n_loop,1);
OBJ_C = cell(3*EXP.n_loop,1);
% to save the number of epochs
epoch1 = zeros(3*EXP.n_loop,1);
epoch2 = zeros(3*EXP.n_loop,1);
epoch3 = zeros(3*EXP.n_loop,1);
% to save the number of epochs of RBCD
epoch1r = zeros(3*EXP.n_loop,1);
epoch2r = zeros(3*EXP.n_loop,1);
epoch3r = zeros(3*EXP.n_loop,1);
%% loop to average the cenvergence
for loop=1:EXP.n_loop
if runMyMatA==1
EXP.A = MyMatA(eidx,EXP.d);
perm = randperm(EXP.d);
B = EXP.A(perm,perm);
% reordering and estimate its run time
tstart = tic;
reod = symrcm(B);
C = B(reod,reod);
tRCM(loop) = toc(tstart);
end
b = randn(EXP.d,1);
Bb= b(perm);
Cb = Bb(reod);
%%
% runtime of matrix A
%
profile on;
[~, kkt1a] = CBCD1(EXP.A, b, d, iters,pre,l,u,x0);
[~, kkt2a] = CBCD2(EXP.A, b, d, iters,pre,l,u,x0);
[~, kkt3a] = CBCD3(EXP.A, b, d, iters,pre,l,u,x0);
[~, kktr1a] = RBCD1(EXP.A, b, d, iters,pre,l,u,x0);
[~, kktr2a] = RBCD2(EXP.A, b, d, iters,pre,l,u,x0);
[~, kktr3a] = RBCD3(EXP.A, b, d, iters,pre,l,u,x0);
p=profile('info');
profile off;
if length(kkt1a)>iters-2
A=EXP.A;
save([EXP.output_dir 'A.mat'],'A','b');
end
% save time for matrix A
for i=1:size(p.FunctionTable,1)
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size1_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(1,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(1,loop));
fprintf('#epochs : %d \n',length(kkt1a)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size2_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(2,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(2,loop));
fprintf('#epochs : %d \n',length(kkt2a)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size3_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(3,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(3,loop));
fprintf('#epochs : %d \n',length(kkt3a)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD_size1_gc_u')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(1,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(1,loop));
fprintf('#epochs : %d \n',length(kktr1a)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD2')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(2,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(2,loop));
fprintf('#epochs : %d \n',length(kktr2a)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD3')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(3,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(3,loop));
fprintf('#epochs : %d \n',length(kktr3a)-1);
end
end
% save KKT condition, related to normal cone
KKT_A{loop} = kkt1a;
epoch1(loop) = length(kkt1a);
KKT_A{loop+EXP.n_loop} = kkt2a;
epoch1(loop+EXP.n_loop) = length(kkt2a);
KKT_A{loop+EXP.n_loop*2} = kkt3a;
epoch1(loop+EXP.n_loop*2) = length(kkt3a);
% save KKT condition of RBCD
KKTr_A{loop} = kktr1a;
epoch1r(loop) = length(kktr1a);
KKTr_A{loop+EXP.n_loop} = kktr2a;
epoch1r(loop+EXP.n_loop) = length(kktr2a);
KKTr_A{loop+EXP.n_loop*2} = kktr3a;
epoch1r(loop+EXP.n_loop*2) = length(kktr3a);
% run again to get objective(function value)
[~, obj1a] = CBCD_size1_fx(EXP.A, b, d, iters,pre,l,u,x0);
[~, obj2a] = CBCD_size2_fx(EXP.A, b, d, iters,pre,l,u,x0);
[~, obj3a] = CBCD_size3_fx(EXP.A, b, d, iters,pre,l,u,x0);
% save objective, which is function value
OBJ_A{loop} = obj1a;
OBJ_A{loop+EXP.n_loop} = obj2a;
OBJ_A{loop+EXP.n_loop*2} = obj3a;
%%
% runtime of matrix B
%
profile on;
[~, kkt1b] = CBCD1(B, Bb, d, iters,pre,l,u,x0);
[~, kkt2b] = CBCD2(B, Bb, d, iters,pre,l,u,x0);
[~, kkt3b] = CBCD3(B, Bb, d, iters,pre,l,u,x0);
[~, kktr1b] = RBCD1(B, Bb, d, iters,pre,l,u,x0);
[~, kktr2b] = RBCD2(B, Bb, d, iters,pre,l,u,x0);
[~, kktr3b] = RBCD3(B, Bb, d, iters,pre,l,u,x0);
p=profile('info');
profile off;
% save time for matrix B
for i=1:size(p.FunctionTable,1)
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size1_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(4,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(4,loop));
fprintf('#epochs : %d \n',length(kkt1b)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size2_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(5,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(5,loop));
fprintf('#epochs : %d \n',length(kkt2b)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size3_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(6,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(6,loop));
fprintf('#epochs : %d \n',length(kkt3b)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD_size1_gc_u')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(4,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(4,loop));
fprintf('#epochs : %d \n',length(kktr1b)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD2')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(5,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(5,loop));
fprintf('#epochs : %d \n',length(kktr2b)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD3')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(6,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(6,loop));
fprintf('#epochs : %d \n',length(kktr3b)-1);
end
end
% save objective
KKT_B{loop} = kkt1b;
epoch2(loop) = length(kkt1b);
KKT_B{loop+EXP.n_loop} = kkt2b;
epoch2(loop+EXP.n_loop) = length(kkt2b);
KKT_B{loop+EXP.n_loop*2} = kkt3b;
epoch2(loop+EXP.n_loop*2) = length(kkt3b);
% save objective of RBCD
KKTr_B{loop} = kktr1b;
epoch2r(loop) = length(kktr1b);
KKTr_B{loop+EXP.n_loop} = kktr2b;
epoch2r(loop+EXP.n_loop) = length(kktr2b);
KKTr_B{loop+EXP.n_loop*2} = kktr3b;
epoch2r(loop+EXP.n_loop*2) = length(kktr3b);
% run again to get objective(function value)
[~, obj1b] = CBCD_size1_fx(B, Bb, d, iters,pre,l,u,x0);
[~, obj2b] = CBCD_size2_fx(B, Bb, d, iters,pre,l,u,x0);
[~, obj3b] = CBCD_size3_fx(B, Bb, d, iters,pre,l,u,x0);
% save objective, which is function value
OBJ_B{loop} = obj1b;
OBJ_B{loop+EXP.n_loop} = obj2b;
OBJ_B{loop+EXP.n_loop*2} = obj3b;
%%
% runtime of matrix C
%
profile on;
[~, kkt1c] = CBCD1(C, Cb, d, iters,pre,l,u,x0);
[~, kkt2c] = CBCD2(C, Cb, d, iters,pre,l,u,x0);
[~, kkt3c] = CBCD3(C, Cb, d, iters,pre,l,u,x0);
[~, kktr1c] = RBCD1(C, Cb, d, iters,pre,l,u,x0);
[~, kktr2c] = RBCD2(C, Cb, d, iters,pre,l,u,x0);
[~, kktr3c] = RBCD3(C, Cb, d, iters,pre,l,u,x0);
p=profile('info');
profile off;
% save time for matrix C
for i=1:size(p.FunctionTable,1)
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size1_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(7,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(7,loop));
fprintf('#epochs : %d \n',length(kkt1c)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size2_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(8,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(8,loop));
fprintf('#epochs : %d \n',length(kkt2c)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'CBCD_size3_gc')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_c(9,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_c(9,loop));
fprintf('#epochs : %d \n',length(kkt3c)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD_size1_gc_u')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(7,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(7,loop));
fprintf('#epochs : %d \n',length(kktr1c)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD2')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(8,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(8,loop));
fprintf('#epochs : %d \n',length(kktr2c)-1);
end
if strcmp(p.FunctionTable(i,1).FunctionName , 'RBCD3')
fprintf('Function: %s\n',p.FunctionTable(i,1).FunctionName);
T_r(9,loop)= p.FunctionTable(i,1).TotalTime;
fprintf('Runtime : %.4f seconds. ',T_r(9,loop));
fprintf('#epochs : %d \n',length(kktr3c)-1);
end
end
% save objective
KKT_C{loop} = kkt1c;
epoch3(loop) = length(kkt1c);
KKT_C{loop+EXP.n_loop} = kkt2c;
epoch3(loop+EXP.n_loop) = length(kkt2c);
KKT_C{loop+EXP.n_loop*2} = kkt3c;
epoch3(loop+EXP.n_loop*2) = length(kkt3c);
% save objective of RBCD
KKTr_C{loop} = kktr1c;
epoch3r(loop) = length(kktr1c);
KKTr_C{loop+EXP.n_loop} = kktr2c;
epoch3r(loop+EXP.n_loop) = length(kktr2c);
KKTr_C{loop+EXP.n_loop*2} = kktr3c;
epoch3r(loop+EXP.n_loop*2) = length(kktr3c);
% run again to get objective(function value)
[~, obj1c] = CBCD_size1_fx(C, Cb, d, iters,pre,l,u,x0);
[~, obj2c] = CBCD_size2_fx(C, Cb, d, iters,pre,l,u,x0);
[~, obj3c] = CBCD_size3_fx(C, Cb, d, iters,pre,l,u,x0);
% save objective, which is function value
OBJ_C{loop} = obj1c;
OBJ_C{loop+EXP.n_loop} = obj2c;
OBJ_C{loop+EXP.n_loop*2} = obj3c;
end
%% this part shows the mean of ratio of number of epochs
% taken by size2/3 over size1, for matrix A,B and C
% we don't take the experiments that reaches the max iters
% when calculating the #2/#1 and #3/#1.
% However they are counted when plotting in function plot4EXP()
index2remove = find(epoch1(1:EXP.n_loop)==iters);
% construct the index of size 123
vector2remove = [index2remove;index2remove+EXP.n_loop;index2remove+EXP.n_loop*2];
temp_epoch1 = epoch1;
temp_epoch2 = epoch2;
temp_epoch3 = epoch3;
temp_epoch1(vector2remove)=[];
temp_epoch2(vector2remove)=[];
temp_epoch3(vector2remove)=[];
temp_exp_loop=EXP.n_loop;
EXP.n_loop = size(temp_epoch1,1)/3;
EXP.A_ep2_OVER_ep1 = mean(temp_epoch1(1+EXP.n_loop:2*EXP.n_loop)./...
temp_epoch1(1:EXP.n_loop));
EXP.A_ep3_OVER_ep1 = mean(temp_epoch1(1+2*EXP.n_loop:3*EXP.n_loop)./...
temp_epoch1(1:EXP.n_loop));
EXP.B_ep2_OVER_ep1 = mean(temp_epoch2(1+EXP.n_loop:2*EXP.n_loop)./...
temp_epoch2(1:EXP.n_loop));
EXP.B_ep3_OVER_ep1 = mean(temp_epoch2(1+2*EXP.n_loop:3*EXP.n_loop)./...
temp_epoch2(1:EXP.n_loop));
EXP.C_ep2_OVER_ep1 = mean(temp_epoch3(1+EXP.n_loop:2*EXP.n_loop)./...
temp_epoch3(1:EXP.n_loop));
EXP.C_ep3_OVER_ep1 = mean(temp_epoch3(1+2*EXP.n_loop:3*EXP.n_loop)./...
temp_epoch3(1:EXP.n_loop));
% to check whether reaches the max number of iterations or not
EXP.check_iter = max([temp_epoch1;temp_epoch2;temp_epoch3]);
% set the number of loops back to original
EXP.n_loop=temp_exp_loop;
%% save the parameters to EXP
EXP.T_c = T_c;
EXP.T_r = T_r;
EXP.KKT_A = KKT_A;
EXP.KKT_B = KKT_B;
EXP.KKT_C = KKT_C;
EXP.KKTr_A = KKTr_A;
EXP.KKTr_B = KKTr_B;
EXP.KKTr_C = KKTr_C;
EXP.OBJ_A = OBJ_A;
EXP.OBJ_B = OBJ_B;
EXP.OBJ_C = OBJ_C;
EXP.B = B;
EXP.C = C;
EXP.epoch1 = epoch1;
EXP.epoch2 = epoch2;
EXP.epoch3 = epoch3;
EXP.epoch1r = epoch1r;
EXP.epoch2r = epoch2r;
EXP.epoch3r = epoch3r;
EXP.tRCM=mean(tRCM);
% to save EXP
if EXP.save==1
save([EXP.output_dir 'EXP.mat'],'EXP');
end
% if plot the convergence
if EXP.plot_convergence ==1
plot4EXP(eidx,EXP);
end
end