-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanial.m
More file actions
358 lines (257 loc) · 8.31 KB
/
manial.m
File metadata and controls
358 lines (257 loc) · 8.31 KB
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
function [X,Z,ret]=manial(A, manifold, f, h, opts)
if ~isfield(opts, 'max_iter'); opts.max_iter = 1e3; end
if ~isfield(opts, 'rho'); opts.rho = 2; end
if ~isfield(opts, 'nu0'); opts.nu0 = 10; end
if ~isfield(opts, 'nu_max'); opts.nu_max = opts.nu0*1e3; end
if ~isfield(opts, 'nu_min'); opts.nu_min = 1e-2; end
if ~isfield(opts, 'tol'); opts.tol = 1e-6; end
if ~isfield(opts, 'tau'); opts.tau = 0.9; end
if ~isfield(opts, 'verbosity'); opts.verbosity = 1; end
if ~isfield(opts, 'ALM_step'); opts.ALM_step = 1; end
if ~isfield(opts, 'sub_solver');opts.sub_solver = 3; end
if ~isfield(opts, 'gtol_ratio0'); opts.gtol_ratio0 = 1e0; end
if ~isfield(opts, 'record_file'); opts.record_file = ''; end
if ~isfield(opts, 'gtol0'); opts.gtol0 = 1e-1; end
if ~isfield(opts, 'maxitersub'); opts.maxitersub = 10; end
if ~isfield(opts, 'gtol_decrease'); opts.gtol_decrease = 0.8; end
if ~isfield(opts, 'extra_iter'); opts.extra_iter = 40; end
prefix = 'log';
tol = opts.tol;
rho = opts.rho;
sub_solver = opts.sub_solver;
deltak_cnt = 0;
opts.debug = 1;
%construct scale parameter
% construct Aop
if isnumeric(A)
Aop = struct();
Aop.applyA = @(X) A*X;
Aop.applyAT = @(y) A'*y;
elseif isstruct(A) % A is struct, only validate its usability
Aop = A;
if ~isfield(A, 'applyA')
error('A.applyA is not defined.');
end
if ~isfield(A, 'applyAT')
error('A.applyAT is not defined.');
end
else
error('unsupported input of A.');
end
if ~isfield(f, 'cost_grad')
error('f is not defined.');
end
if ~isfield(f, 'data')
f.data = {};
end
% check h (structure of function handle)
% [f, prox_h, prox_h_norm] = h.obj_prox(R, Z, nuk, data)
% h.hess(R, Z, nuk, data) returns R h''(R'R - Z/nuk)[U'R + R'U]
% different from f, h can be empty (Zero)
if isempty(h)
h.cost = @(X,~) 0;
h.prox = @(X,nuk,~) 0;
h.is_empty = true;
else
if ~isfield(h, 'prox') || ~isfield(h, 'cost')
error('h is not defined.');
end
h.is_empty = true;
end
if ~isfield(h, 'data')
h.data = {};
end
if ~isfield(opts, 'X0')
X0 = manifold.rand();
else
X0 = opts.X0;
end
nuk = opts.nu0;
gtol_ratio = opts.gtol_ratio0;
cstop = 0;
t = tic;
Y = Aop.applyA(X0);
Z = zeros(size(Y));
X = X0;
iter = 0;
if opts.verbosity > 0
str0 = ' %6s';
str2 = ' %6s';
stra = ['\n%6s',str0,str2,str0,str0,str0, ' %4s', ' %3s', ' %6s',' %6s',' %6s'];
str_head = sprintf(stra,...
'iter','obj', 'deltak','kkt_X','kkt_Y','error', 'nuk', 'siter','snrmG', 'time', 'smsg');
str_head_debug = sprintf(' %10s',' gtol_tgt');
str_num = ['\n %4d',' %+9.5e',' %+7.2e',' %+7.2e',' %+7.2e',' %7.2e',' %.1f',' %4d',' %8.2e',' %6.2f',' %-12s'];
str_debug = ['%4.2e'];
if ~isempty(opts.record_file)
if ~exist(prefix, 'dir')
mkdir(prefix);
end
record_fname = [prefix '/' opts.record_file];
fid = fopen(record_fname, 'w');
else
fid = 1;
end
end
%ftol_inc_step = 0;
RGBB_extra_iter = 0;
AXy = Aop.applyA(X) - Y;
deltak = norm(AXy);
delta0 = deltak;
out.nrmG = 1;
sub_iter = 0;
gtol_bnd = opts.gtol0;
maxitersub = opts.maxitersub;
% initialize output
ret = struct();
ret.flag = 99;
ret.msg = 'exceed max iteration';
time_arr = zeros(opts.max_iter,1);
obj_arr = zeros(opts.max_iter,1);
error_arr = zeros(opts.max_iter,3);
iter_arr = zeros(opts.max_iter,1);
[fcost,~] = f.cost_grad(X, f.data{:});
obj_arr(1) = fcost + h.cost(Aop.applyA(X),h.data{:});
while iter<opts.max_iter && ~cstop
iter = iter+1;
X0 = X;
ALM_step = opts.ALM_step;
% set gtol
gtol_min = 1e-6;
gtol = max([gtol_ratio*1e-6 * sqrt(deltak), gtol_bnd, gtol_min]);
gtol = gtol_min;
t = tic;
switch sub_solver
case 2
problem.M = manifold;
options_manopt.verbosity = 0;
options_manopt.maxiter = 100;
options_manopt.tolgradnorm = gtol;
[X, ~, out] = trustregions(problem, X0, options_manopt);
case 3
optRGB = opts;
optRGB.xtol = 1e-5; optRGB.ftol = 1e-8; optRGB.gtol = gtol;
optRGB.alpha = 1e-3; optRGB.rhols = 1e-6; optRGB.gamma = 0.85;
optRGB.nt = 5; optRGB.eta = 0.2; optRGB.STPEPS = 1e-10;
optRGB.maxit = maxitersub + RGBB_extra_iter;
optRGB.record = opts.verbosity > 1;
if optRGB.record
optRGB.record_fid = fid;
end
if optRGB.flag == 1
optRGB.maxit = 100; optRGB.gtol = max(0.5^iter,gtol_min);
else
optRGB.maxit = 2^iter; optRGB.gtol = gtol_min;
end
[X, ~, out] = RGBB(X0, @fun_ARNT, manifold, optRGB, Aop, f, h, Z, nuk);
if out.iter == optRGB.maxit
RGBB_extra_iter = min(opts.extra_iter, max(2 * RGBB_extra_iter, 10));
else
gtol_bnd = gtol_bnd * opts.gtol_decrease;
end
sub_iter = sub_iter + out.iter;
iter_arr(iter+1) = sub_iter;
end
AX = Aop.applyA(X); AXZ = AX - Z/nuk;
Y = h.prox(AXZ, 1/nuk, h.data{:});
deltak_p =deltak;
deltak = norm(AX - Y,'fro')/(1+norm(AX,'fro') + norm(Y,'fro'));
if iter==1
delta0 =deltak; deltak_p = norm(Aop.applyA(X0) - Y,'fro');
end
deltak_ratio = deltak/deltak_p;
%beta = min(1,delta0*log(2)^2/(sqrt(iter) * deltak * log(iter+2)));
% multiplier update: Z
Z = Z-ALM_step*nuk*(AX - Y);
time_arr(iter+1) = time_arr(iter) + toc(t);
acc_time = sum(time_arr);
[fcost,fgrad] = f.cost_grad(X, f.data{:});
hprox = h.prox( AX - Z, 1, h.data{:});
kkt_X = norm(manifold.proj(X, fgrad - Aop.applyAT(Z)), 'fro')/(1 + norm(fgrad,'fro'));
kkt_Y = norm(AX - hprox, 'fro')/(1+norm(AX,'fro'));
kkt_error = max(kkt_X, kkt_Y);
obj = fcost + h.cost(Aop.applyA(X),h.data{:});
obj_arr(iter+1) = obj;
error_arr(iter,:) = [deltak,kkt_X,kkt_Y];
% adjust sigmak such that deltak & etaK2 decrease at the same rate
sigtol = opts.tau;
if deltak_ratio > sigtol
deltak_cnt = deltak_cnt + 1;
else
deltak_cnt = 0;
end
%nuk = rho * nuk;
if deltak_ratio > sigtol && deltak >= tol
nuk = min(rho * nuk, opts.nu_max);
elseif deltak < tol && kkt_error > tol*10
nuk = max((delta0*log10(2)) /(deltak*(iter+1)^2*log10(iter)), opts.nu_min);
end
cstop = (kkt_error<tol && deltak < tol ) || obj<opts.obj+1e-10 ;
if cstop
ret.flag = 0;
ret.msg = 'converge';
end
% print
if opts.verbosity > 0
% print header
if iter == 1 || opts.verbosity > 1
fprintf(fid, str_head);
if opts.debug
fprintf(fid, str_head_debug);
end
end
% print iteration info
switch sub_solver
case 2
fprintf(fid, str_num,iter,deltak,kkt_error, nuk, out(end).iter, out(end).gradnorm, acc_time, '');
if opts.debug
fprintf(fid, str_debug, gtol);
end
case 3
fprintf(fid, str_num,iter,obj,deltak,kkt_X,kkt_Y,kkt_error, nuk, out.iter,out.nrmG, acc_time, out.msg);
if opts.debug
fprintf(fid, str_debug, gtol);
end
end
end
end
sub_iter = sub_iter/iter;
tsolve_ALM = toc(t);
if iter < opts.max_iter
ret.flag = 1;
else
ret.flag = 0;
end
ret.obj_arr = obj_arr;
ret.time_arr = time_arr;
ret.error_arr = error_arr;
ret.iter_arr = iter_arr;
ret.time = acc_time;
ret.iter = iter;
ret.deltak = deltak;
ret.X = X;
ret.Z = Z;
ret.Y = Y;
ret.nu = nuk;
ret.obj = f.cost_grad(X,f.data{:}) + h.cost(Aop.applyA(X),h.data{:});
ret.sub_iter = sub_iter;
ret.nrmG = out.nrmG;
ret.etaD = kkt_X;
ret.etaC = kkt_Y;
[n,k] = size(AX);
ret.sparsity = sum(sum(abs(AX)<=1e-4))/(n*k);
if opts.verbosity > 0 && ~isempty(opts.record_file)
fclose(fid);
end
function [f,g] = fun_ARNT(X, Aop, f, h, Z,nuk)
% apply A
AXZ = Aop.applyA(X) - Z/nuk;
AXZ_prox = h.prox(AXZ,1/nuk,h.data{:});
% apply AT (handle)
AAXZ = Aop.applyAT(AXZ - AXZ_prox);
[f1,g1] = f.cost_grad(X,f.data{:});
%[f1,g1] = f.cost_grad(X,f.data{:});
g = g1 + nuk*AAXZ;
f = f1 + h.cost(AXZ_prox,h.data{:}) + nuk/2*norm(AXZ - AXZ_prox, 'fro')^2;
end
end