-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas_zsl_matlab.cpp
210 lines (197 loc) · 8.21 KB
/
as_zsl_matlab.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
// ------------------------------------------------------------------------
//
// 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: andrea.cristofari@uniroma2.it)
//
// 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 "mex.h"
#include "as_zsl.h"
#include <string>
#include <cmath>
#ifndef mxGetDoubles
#define mxGetDoubles mxGetPr
typedef double mxDouble;
#endif
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
unsigned int m,n,n_lambda,t;
std::vector<double> lambda;
const double *cdbl_ptr;
double *A,*y;
mxDouble *mdbl_ptr;
size_t *irs,*jcs;
// check the number of inupts and outputs
if (nrhs<3 || nrhs>4) {
mexErrMsgTxt("The number of inputs must be three or four.");
}
if (nlhs<1 || nlhs>3) {
mexErrMsgTxt("The number of outputs must be between one and three.");
}
// check inputs
if (mxIsScalar(prhs[0]) || mxGetNumberOfDimensions(prhs[0])>2 || !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])) {
mexErrMsgTxt("The first input must be a real matrix.");
}
m = (unsigned int) mxGetM(prhs[0]); // number of samples
n = (unsigned int) mxGetN(prhs[0]); // number of variables
if (mxGetNumberOfDimensions(prhs[0])>2 || mxGetN(prhs[1])!=1 || !mxIsDouble(prhs[1]) || mxIsComplex(prhs[1])) {
mexErrMsgTxt("The second input must be a real column vector.");
}
if ((unsigned int) mxGetM(prhs[1]) != m) {
mexErrMsgTxt("The dimension of A and y in the system Ax = y must agree.");
}
if (mxGetNumberOfDimensions(prhs[0])>2 || !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2])) {
mexErrMsgTxt("The third input must be a non-negative number or a column vector sorted in descending order.");
}
// get matrix A
A = mxGetDoubles(prhs[0]);
if (mxIsSparse(prhs[0])) {
irs = mxGetIr(prhs[0]);
jcs = mxGetJc(prhs[0]);
for (unsigned int i=0; i<jcs[n]; i++) {
if (mxIsNaN(A[i])) {
mexErrMsgTxt("The first input must be a real matrix (NaNs are not allowed).");
}
}
} else {
for (unsigned int i=0; i<m*n; i++) {
if (mxIsNaN(A[i])) {
mexErrMsgTxt("The first input must be a real matrix (NaNs are not allowed).");
}
}
irs = jcs = NULL;
}
// get vector y
y = mxGetDoubles(prhs[1]);
for (unsigned int i=0; i<m; i++) {
if (mxIsNaN(y[i])) {
mexErrMsgTxt("The second input must be a real column vector (NaNs are not allowed).");
}
}
// get lambda(s)
n_lambda = (unsigned int) mxGetN(prhs[2]);
if (n_lambda > 1) {
if (mxGetM(prhs[2]) > 1) {
mexErrMsgTxt("The third input must be a non-negative number or a column vector sorted in descending order.");
}
} else {
n_lambda = (unsigned int) mxGetM(prhs[2]);
}
lambda.resize(n_lambda);
cdbl_ptr = mxGetDoubles(prhs[2]);
lambda[0] = *cdbl_ptr;
if (lambda[0]<0 || mxIsNaN(lambda[0])) {
mexErrMsgTxt("The third input must be a non-negative number or a column vector sorted in descending order (NaNs are not allowed).");
}
for (unsigned int r=1; r<n_lambda; r++) {
lambda[r] = cdbl_ptr[r];
if (lambda[r]<0 || mxIsNaN(lambda[r]) || lambda[r]>lambda[r-1]) {
mexErrMsgTxt("The third input must be a non-negative number or a column vector sorted in descending order (NaNs are not allowed).");
}
}
// get options
as_zsl_options opts;
if (nrhs > 3) {
if (!mxIsStruct(prhs[3]) || mxGetNumberOfElements(prhs[3])>1) {
mexErrMsgTxt("The fourth input argument (which is optional) must be a structure.");
}
for (int i=0; i<mxGetNumberOfFields(prhs[3]); i++) {
mxArray *tmp_mxArray = mxGetFieldByNumber(prhs[3],0,i);
const char *tmp_char = mxGetFieldNameByNumber(prhs[3],i);
if (std::string(tmp_char).compare(std::string("eps_opt")) == 0) {
if (!mxIsScalar(tmp_mxArray) || !mxIsDouble(tmp_mxArray) || mxIsComplex(tmp_mxArray)) {
mexErrMsgTxt("In the options, 'eps_opt' must be a non-negative number.");
}
opts.eps_opt = *mxGetDoubles(tmp_mxArray);
} else if (std::string(tmp_char).compare(std::string("max_it")) == 0) {
if (!mxIsScalar(tmp_mxArray) || !mxIsDouble(tmp_mxArray) || mxIsComplex(tmp_mxArray)) {
mexErrMsgTxt("In the options, 'max_it' must be a number greater than or equal to 1.");
}
opts.max_it = (int)floor(*mxGetDoubles(tmp_mxArray));
} else if (std::string(tmp_char).compare(std::string("verbosity")) == 0) {
if (!mxIsScalar(tmp_mxArray) || !mxIsLogical(tmp_mxArray)) {
mexErrMsgTxt("In the options, 'verbosity' must a logical.");
}
opts.verbosity = *mxGetLogicals(tmp_mxArray);
} else {
mexErrMsgTxt("Not valid field name in the structure of options.");
}
}
}
// call the solver
As_zsl alg(m,n,A,irs,jcs,y,lambda,&opts);
alg.solve();
// set outputs
plhs[0] = mxCreateDoubleMatrix(n,n_lambda,mxREAL);
mdbl_ptr = mxGetDoubles(plhs[0]);
t = 0;
for (unsigned int r=0; r<n_lambda; r++) {
cdbl_ptr = &alg.get_x()[r][0];
for (unsigned int h=0; h<n; h++) {
mdbl_ptr[t] = cdbl_ptr[h];
t++;
}
}
if (nlhs > 1) {
plhs[1] = mxCreateDoubleMatrix(1,n_lambda,mxREAL);
mdbl_ptr = mxGetDoubles(plhs[1]);
cdbl_ptr = &alg.get_f()[0];
for (unsigned int r=0; r<n_lambda; r++) {
mdbl_ptr[r] = cdbl_ptr[r];
}
if (nlhs > 2) {
mxArray *pit = mxCreateDoubleMatrix(1,n_lambda,mxREAL);
mxArray *pflag = mxCreateDoubleMatrix(1,n_lambda,mxREAL);
const unsigned int *cuint_ptr;
const char* as_zsl_field_names[] = {"it","flag"};
mwSize as_zsl_info_dims[2] = {1,1};
plhs[2] = mxCreateStructArray(2,as_zsl_info_dims,2,as_zsl_field_names);
mdbl_ptr = mxGetDoubles(pit);
cuint_ptr = &alg.get_it()[0];
for (unsigned int r=0; r<n_lambda; r++) {
mdbl_ptr[r] = (double) cuint_ptr[r];
}
mxSetField(plhs[2],0,as_zsl_field_names[0],pit);
mdbl_ptr = mxGetDoubles(pflag);
cuint_ptr = &alg.get_flag()[0];
for (unsigned int r=0; r<n_lambda; r++) {
mdbl_ptr[r] = (double) cuint_ptr[r];
}
mxSetField(plhs[2],0,as_zsl_field_names[1],pflag);
}
}
}