-
Notifications
You must be signed in to change notification settings - Fork 0
/
as_zsl.cpp
573 lines (491 loc) · 18 KB
/
as_zsl.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// ------------------------------------------------------------------------
//
// This file is part of AS-ZSL, which is a solver for the following lasso
// problem with zero-sum constraint:
//
// min 0.5*||Ax-y||^2 + lambda*||x||_1
// s.t. sum(x) = 0
//
// with given matrix A, vector y and non-negative scalar lambda.
//
// ------------------------------------------------------------------------
//
// Reference paper:
//
// A. Cristofari (2022). A decomposition method for lasso problems with
// zero-sum constraint. European Journal of Operational Research 306(1),
// 358–369
//
// ------------------------------------------------------------------------
//
// Author:
// Andrea Cristofari (e-mail: [email protected])
//
// Last update of this file:
// July 26th, 2024
//
// Licensing:
// This file is part of AS-ZSL.
// AS-ZSL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// AS-ZSL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with AS-ZSL. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2022-2024 Andrea Cristofari.
//
// ------------------------------------------------------------------------
#include "as_zsl.h"
#include <iostream>
#include <numeric>
#include <random>
#include <algorithm>
// constructor
//-------------------------------------------------------------------------------------
As_zsl::As_zsl(unsigned int n_row, unsigned int n_col, double * const mat,
size_t * const row, size_t * const col, double * const label, const std::vector<double>& lam,
const as_zsl_options * opts) {
if (opts == NULL) {
as_zsl_options as_zsl_opts;
opts = &as_zsl_opts;
}
eps_opt = opts->eps_opt;
max_it = (unsigned int) opts->max_it;
verbosity = opts->verbosity;
if (eps_opt < 0e0) {
std::cout << "In the options, 'eps_opt' must be a non-negative number.\n";
exit(-1);
}
if (opts->max_it < 1) {
std::cout << "In the options, 'max_it' must be a number greater than or equal to 1.\n";
exit(-1);
}
m = n_row; // number of columns of A
n = n_col; // number of rows of A
A = mat;
y = label;
lambda_vec = &lam[0];
n_lambda = (unsigned int) lam.size();
x_vec.resize(n_lambda);
f_vec.resize(n_lambda);
it_vec.resize(n_lambda);
flag_vec.resize(n_lambda);
if (row==NULL || col==NULL) {
if (row!=NULL || col!=NULL) {
std::cout << "The matrix of covariates must be either full or sparse.\n";
exit(-1);
}
A_is_full = true;
} else {
A_is_full = false;
irs = row;
jcs = col;
}
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
void As_zsl::solve() {
unsigned int seed,h_non_act;
double opt_viol,eta_min,eta_max,f_old,mu,theta,tmp;
double *A_col;
bool it_mvp;
std::vector<double> g,c,pi;
if (verbosity) {
std::cout.precision(4);
std::cout.setf(std::ios::scientific,std::ios::floatfield);
}
is_fixed.assign(n,false);
c.resize(n);
g.resize(n);
pi.resize(n);
// set c = A'*y
if (A_is_full) {
for (unsigned int h=0; h<n; h++) {
tmp = 0e0;
A_col = A + h*m;
for (unsigned int t=0; t<m; t++) {
tmp += A_col[t]*y[t];
}
c[h] = tmp;
}
} else {
for (unsigned int h=0; h<n; h++) {
tmp = 0e0;
for (size_t t=jcs[h]; t<jcs[h+1]; t++) {
tmp += A[t]*y[irs[t]];
}
c[h] = tmp;
}
}
// non-active indices
seed = 1;
srand(seed);
std::minstd_rand0 eng(seed);
n_non_act = n;
ind_non_act.resize(n);
std::iota(ind_non_act.begin(),ind_non_act.end(),0);
for (unsigned int r=0; r<n_lambda; r++) {
x_vec[r].resize(n);
lambda = lambda_vec[r];
if (verbosity && n_lambda>1) {
std::cout << "\n*** lambda = " << lambda << " ***\n";
}
// warm start in case of multiple values of lambda
if (r > 0) {
if (f_norm1 > 0e0) {
f = f_quad + lambda*f_norm1;
it = 0;
if (verbosity) {
std::cout << "it = 0, f = " << f << "\n";
}
} else {
if (verbosity) {
std::cout << "it = 0, f = " << f << "\n";
}
eta_min -= lambda_vec[r-1];
eta_min += lambda;
eta_max += lambda_vec[r-1];
eta_max -= lambda;
opt_viol = eta_max - eta_min;
if (opt_viol <= eps_opt) {
x_vec[r] = x;
f_vec[r] = f;
it_vec[r] = 0;
flag_vec[r] = 0;
continue;
}
// first mvp iteration
solve_subproblem();
if (verbosity) {
std::cout << "it = 1, f = " << f << " (mvp)\n";
}
it = 1;
}
} else {
x.assign(n,0e0);
f_quad = 0e0;
for (unsigned int h=0; h<m; h++) {
f_quad += y[h]*y[h];
}
f_quad *= 5e-1;
f = f_quad;
f_norm1 = 0e0;
for (unsigned int h=0; h<n; h++) {
g[h] = -c[h];
}
u.assign(m,0e0);
if (verbosity) {
std::cout << "it = 0, f = " << f << "\n";
}
// compute the mvp
i = j = 0;
eta_min = eta_max = g[0];
for (unsigned int h=1; h<n; h++) {
if (g[h] < eta_min) {
eta_min = g[h];
i = h;
} else if (g[h] > eta_max) {
eta_max = g[h];
j = h;
}
}
eta_min += lambda;
eta_max -= lambda;
opt_viol = eta_max - eta_min;
if (opt_viol <= eps_opt) {
x_vec[r] = x;
f_vec[r] = f;
it_vec[r] = 0;
flag_vec[r] = 0;
continue;
}
// first mvp iteration
g_i = g[i];
g_j = g[j];
solve_subproblem();
if (verbosity) {
std::cout << "it = 1, f = " << f << " (mvp)\n";
}
it = 1;
}
theta = 1e-2;
flag = 1;
// fake values for the 1st iteration
it_mvp = false;
f_old = 0e0;
while (it < max_it) {
// choose between strategy mvp and strategy ac2cd
if (!it_mvp && f_old-f<=theta*std::max(f_old,1e0)) {
it_mvp = true;
theta = std::max(5e-1*theta,1e-6);
} else {
if (it_mvp && f_old-f<=1e-9*std::max(f_old,1e0)) {
flag = 2;
break;
}
it_mvp = false;
}
if (it_mvp) {
// compute the gradient
if (A_is_full) {
for (unsigned int h=0; h<n; h++) {
tmp = 0e0;
A_col = A + h*m;
for (unsigned int t=0; t<m; t++) {
tmp += A_col[t]*u[t];
}
g[h] = tmp - c[h];
}
} else {
for (unsigned int h=0; h<n; h++) {
tmp = 0e0;
for (size_t t=jcs[h]; t<jcs[h+1]; t++) {
tmp += A[t]*u[irs[t]];
}
g[h] = tmp - c[h];
}
}
// compute the multiplier
mu = 0e0;
tmp = 0e0;
for (unsigned int h=0; h<n; h++) {
if (x[h]!=0e0) {
mu += fabs(x[h])*(g[h]+(x[h]>0e0?lambda:-lambda));
tmp += fabs(x[h]);
}
}
mu /= tmp;
for (unsigned int h=0; h<n; h++) {
pi[h] = g[h] - mu;
}
}
// active-set estimate
n_non_act = 0;
for (unsigned int h=0; h<n; h++) {
if (x[h]!=0 || (fabs(pi[h])>lambda && !is_fixed[i])) {
ind_non_act[n_non_act] = h;
n_non_act++;
}
}
f_old = f;
if (it_mvp) {
eta_min = std::numeric_limits<double>::max();
eta_max = std::numeric_limits<double>::lowest();
for (unsigned int h=0; h<n_non_act; h++) {
h_non_act = ind_non_act[h];
if (x[h_non_act] == 0e0) {
if (g[h_non_act]+lambda < eta_min) {
eta_min = g[h_non_act] + lambda;
i = h_non_act;
}
if (g[h_non_act]-lambda > eta_max) {
eta_max = g[h_non_act] - lambda;
j = h_non_act;
}
} else {
tmp = g[h_non_act] + (x[h_non_act]>0e0?lambda:-lambda);
if (tmp < eta_min) {
eta_min = tmp;
i = h_non_act;
}
if (tmp > eta_max) {
eta_max = tmp;
j = h_non_act;
}
}
}
opt_viol = eta_max - eta_min; // this is the optimality violation in N^k
if (opt_viol <= eps_opt) {
// compute the overall optimality violation
if (x[0] == 0e0) {
eta_min = g[0] + lambda;
eta_max = g[0] - lambda;
} else {
eta_min = eta_max = g[0] + (x[0]>0?lambda:-lambda);
}
for (unsigned int h=1; h<n; h++) {
if (x[h] == 0e0) {
if (g[h]+lambda < eta_min) {
eta_min = g[h] + lambda;
i = h;
}
if (g[h]-lambda > eta_max) {
eta_max = g[h] - lambda;
j = h;
}
} else {
tmp = g[h] + (x[h]>0e0?lambda:-lambda);
if (tmp < eta_min) {
eta_min = tmp;
i = h;
}
if (tmp > eta_max) {
eta_max = tmp;
j = h;
}
}
}
}
opt_viol = eta_max - eta_min;
if (opt_viol <= eps_opt) {
flag = 0;
break;
}
g_i = g[i];
g_j = g[j];
solve_subproblem();
} else {
// shuffle variables
shuffle(ind_non_act.begin(),ind_non_act.begin()+n_non_act,std::default_random_engine(seed=eng()));
// select index j
tmp = fabs(x[ind_non_act[0]]);
j = 0;
for (unsigned int h=1; h<n_non_act; h++) {
if (fabs(x[ind_non_act[h]]) > tmp) {
tmp = fabs(x[ind_non_act[h]]);
j = h;
}
}
// solve subproblems
j = ind_non_act[j];
ind_non_act[j] = ind_non_act[0];
for (unsigned int h=1; h<n_non_act; h++) {
i = ind_non_act[h];
if (A_is_full) {
tmp = 0e0;
A_col = A + i*m;
for (unsigned int t=0; t<m; t++) {
tmp += A_col[t]*u[t];
}
g_i = tmp - c[i];
tmp = 0e0;
A_col = A + j*m;
for (unsigned int t=0; t<m; t++) {
tmp += A_col[t]*u[t];
}
g_j = tmp - c[j];
} else {
tmp = 0e0;
for (size_t t=jcs[i]; t<jcs[i+1]; t++) {
tmp += A[t]*u[irs[t]];
}
g_i = tmp - c[i];
tmp = 0e0;
for (size_t t=jcs[j]; t<jcs[j+1]; t++) {
tmp += A[t]*u[irs[t]];
}
g_j = tmp - c[j];
}
solve_subproblem();
}
}
it++;
if (verbosity) {
std::cout << "it = " << it << ", f = " << f;
if (it_mvp) {
std::cout << " (mvp)\n";
} else {
std::cout << " (ac2cd)\n";
}
}
}
x_vec[r] = x;
f_vec[r] = f;
it_vec[r] = it;
flag_vec[r] = flag;
}
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
void As_zsl::solve_subproblem() {
double x_i,alpha,beta,s;
std::vector<double> A_ij;
double sum_xi_xj = x[i] + x[j];
bool sol_found = false;
alpha = 0e0;
if (A_is_full) {
A_ij.resize(n);
double *A_col_i = A + m*i;
double *A_col_j = A + m*j;
for (unsigned int h=0; h<m; h++) {
A_ij[h] = A_col_i[h] - A_col_j[h];
alpha += A_ij[h]*A_ij[h];
}
} else {
A_ij.assign(n,0e0);
for (size_t h=jcs[i]; h<jcs[i+1]; h++) {
A_ij[irs[h]] = A[h];
}
for (size_t h=jcs[j]; h<jcs[j+1]; h++) {
A_ij[irs[h]] -= A[h];
}
alpha = std::inner_product(A_ij.begin(),A_ij.end(),A_ij.begin(),0e0);
}
if (alpha > 0e0) {
beta = alpha*x[i] + g_j - g_i;
// seek a stationary point
x_i = (beta-2e0*lambda)/alpha;
if (x_i > std::max(0e0,sum_xi_xj)) {
sol_found = true;
} else {
x_i = (beta+2e0*lambda)/alpha;
if (x_i < std::min(0e0,sum_xi_xj)) {
sol_found = true;
} else {
x_i = beta/alpha;
if (x_i*(x_i-sum_xi_xj)<0e0) {
sol_found = true;
}
}
}
// stationary point not found -> the minimizer is a point of non-differentiability
if (!sol_found) {
if (lambda*fabs(sum_xi_xj) <= 5e-1*alpha*sum_xi_xj*sum_xi_xj-beta*sum_xi_xj+lambda*fabs(sum_xi_xj)) {
x_i = 0e0;
} else {
x_i = sum_xi_xj;
}
}
} else {
x_i = 0e0;
is_fixed[i] = true;
}
// update x, f and u
s = x_i - x[i];
if (s != 0e0) {
f_quad += s*(g_i-g_j+5e-1*s*alpha);
f_norm1 += fabs(x_i) + fabs(x[j]-s) - fabs(x[i]) - fabs(x[j]);
f = f_quad + lambda*f_norm1;
for (unsigned int h=0; h<m; h++) {
u[h] += s*A_ij[h];
}
x[i] = x_i;
x[j] = x[j] - s;
}
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
const std::vector<std::vector<double>>& As_zsl::get_x() {
return x_vec;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
const std::vector<double>& As_zsl::get_f() {
return f_vec;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
const std::vector<unsigned int>& As_zsl::get_it() {
return it_vec;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
const std::vector<unsigned int>& As_zsl::get_flag() {
return flag_vec;
}
//-------------------------------------------------------------------------------------