-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspam_interface_mex.cpp
297 lines (234 loc) · 9.76 KB
/
spam_interface_mex.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
#include "class_handle.hpp"
#undef printf
#include "asl/aslinterface.h"
#include "mex.h"
// The class that we are interfacing to
class ASLMex {
public:
ASL *asl;
mwSize nvar, ncon, nnzj, nnzh;
private:
};
static double*
getDense(const mxArray *mp, const char *who, mwSize m)
{
char msgbuf[256];
mwSize m1, n1;
if (mxIsSparse(mp)) {
sprintf(msgbuf,"Expected %s to be a dense matrix",who);
mexErrMsgTxt(msgbuf);
}
m1 = mxGetM(mp);
n1 = mxGetN(mp);
if (m1 != m || (n1 != 1 && m1)) {
sprintf(msgbuf,
"Expected %s to be %d x 1 rather than %d x %d\n",
who, m, m1, n1);
mexErrMsgTxt(msgbuf);
}
return mxGetPr(mp);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
ASLMex* AC;
ASL *asl;
char *buf1, buf[512];
fint err = 0;
mwSize i, j, nvar, ncon, nlcon, nnzj, nnzh;
// Get the command string.
char cmd[64];
if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
mexErrMsgTxt("First input should be a command string less than 64 characters long.");
// -----------------------------------------------------------------
// Command: New
// -----------------------------------------------------------------
if (!strcmp("new", cmd)) {
if (mxGetString(prhs[1], buf1 = buf, sizeof(buf)))
mexErrMsgTxt("Expected 'stub' as argument\n");
// Return a handle to a new C++ instance
AC = new ASLMex;
asl = AC->asl = asl_init(buf);
AC->nvar = nvar = asl_nvar(asl);
AC->ncon = ncon = asl_ncon(asl);
AC->nnzj = nnzj = asl_nnzj(asl);
AC->nnzh = nnzh = asl_nnzh(asl);
nlcon = asl_nlc(asl);
plhs[7] = mxCreateDoubleScalar(nlcon);
// Left-hand sides
plhs[0] = convertPtr2Mat<ASLMex>(AC);
double *x0p = mxGetPr(plhs[1] = mxCreateDoubleMatrix(nvar, 1, mxREAL));
double *blp = mxGetPr(plhs[2] = mxCreateDoubleMatrix(nvar, 1, mxREAL));
double *bup = mxGetPr(plhs[3] = mxCreateDoubleMatrix(nvar, 1, mxREAL));
double *y0p = mxGetPr(plhs[4] = mxCreateDoubleMatrix(ncon, 1, mxREAL));
double *clp = mxGetPr(plhs[5] = mxCreateDoubleMatrix(ncon, 1, mxREAL));
double *cup = mxGetPr(plhs[6] = mxCreateDoubleMatrix(ncon, 1, mxREAL));
// Obtain pointers to arrays inside ASL data structure
double *x0 = asl_x0(asl);
double *bl = asl_lvar(asl);
double *bu = asl_uvar(asl);
double *y0 = asl_y0(asl);
double *cl = asl_lcon(asl);
double *cu = asl_ucon(asl);
// Copy arrays over to Matlab
for (i = 0; i < AC->nvar; i++) {
x0p[i] = x0[i];
blp[i] = bl[i];
bup[i] = bu[i];
}
for (i = 0; i < AC->ncon; i++) {
y0p[i] = y0[i];
clp[i] = cl[i];
cup[i] = cu[i];
}
return;
}
// -----------------------------------------------------------------
// Retrieve C++ object, and unpack it.
// -----------------------------------------------------------------
AC = convertMat2Ptr<ASLMex>(prhs[1]);
asl = AC->asl;
nvar = AC->nvar;
ncon = AC->ncon;
nnzj = AC->nnzj;
nnzh = AC->nnzh;
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// Call the various class methods
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// Command: Delete
// -----------------------------------------------------------------
if (!strcmp("delete", cmd)) {
// Destroy the C++ object
ASL_free(&(AC->asl));
destroyObject<ASLMex>(prhs[1]);
// Warn if other arguments were ignored
if (nlhs != 0 || nrhs != 2)
mexWarnMsgTxt("Delete: Unexpected arguments ignored.");
return;
}
// -----------------------------------------------------------------
// Command: objective
// -----------------------------------------------------------------
if (!strcmp("obj", cmd)) {
double *x = getDense(prhs[2], "x", nvar);
double *f = mxGetPr(plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL));
*f = asl_obj(asl, x, &err);
if (err) mexErrMsgTxt("Trouble evaluating objective value\n");
return;
}
// -----------------------------------------------------------------
// Command: gradient
// -----------------------------------------------------------------
if (!strcmp("grad", cmd)) {
double *x = getDense(prhs[2], "x", nvar);
double *g = mxGetPr(plhs[0] = mxCreateDoubleMatrix(nvar, 1, mxREAL));
asl_grad(asl, x, g, &err);
if (err) mexErrMsgTxt("Trouble evaluating objective gradient\n");
return;
}
// -----------------------------------------------------------------
// Command: constraint
// -----------------------------------------------------------------
if (!strcmp("con", cmd)) {
double *x = getDense(prhs[2], "x", nvar);
double *c = mxGetPr(plhs[0] = mxCreateDoubleMatrix(ncon, 1, mxREAL));
asl_cons(asl, x, c, &err);
if (err) mexErrMsgTxt("Trouble evaluating constraints\n");
return;
}
// -----------------------------------------------------------------
// Command: jacobian in coordinate format
// -----------------------------------------------------------------
if (!strcmp("jac_coord", cmd)) {
double *x = getDense(prhs[2], "x", nvar);
double *rows = mxGetPr(plhs[0] = mxCreateDoubleMatrix(nnzj, 1, mxREAL));
double *cols = mxGetPr(plhs[1] = mxCreateDoubleMatrix(nnzj, 1, mxREAL));
double *vals = mxGetPr(plhs[2] = mxCreateDoubleMatrix(nnzj, 1, mxREAL));
// is there a more elegant way?!
int64_t *irows = (int64_t*)malloc(nnzj * sizeof(int64_t));
int64_t *jcols = (int64_t*)malloc(nnzj * sizeof(int64_t));
asl_jac(asl, x, irows, jcols, vals, &err);
if (err) mexErrMsgTxt("Trouble evaluating constraints Jacobian\n");
for (i = 0; i < nnzj; i++) {
rows[i] = irows[i] + 1;
cols[i] = jcols[i] + 1; // 1-based indexing
}
free(irows);
free(jcols);
return;
}
// -----------------------------------------------------------------
// Command: Hessian of Lagrangian
// -----------------------------------------------------------------
if (!strcmp("hesslag", cmd)) {
double *y = getDense(prhs[2], "y", ncon);
double ow = mxGetScalar(prhs[3]);
double *rows = mxGetPr(plhs[0] = mxCreateDoubleMatrix(nnzh, 1, mxREAL));
double *cols = mxGetPr(plhs[1] = mxCreateDoubleMatrix(nnzh, 1, mxREAL));
double *vals = mxGetPr(plhs[2] = mxCreateDoubleMatrix(nnzh, 1, mxREAL));
int64_t *irows = (int64_t*)malloc(nnzh * sizeof(int64_t));
int64_t *jcols = (int64_t*)malloc(nnzh * sizeof(int64_t));
ow = asl->i.objtype_[0] ? -ow : ow; // Objective weight.
asl_hess(asl, y, ow, irows, jcols, vals);
for (i = 0; i < nnzh; i++) {
rows[i] = irows[i] + 1;
cols[i] = jcols[i] + 1;
}
free(irows);
free(jcols);
return;
}
// -----------------------------------------------------------------
// Command: lagscale
// -----------------------------------------------------------------
if (!strcmp("lagscale", cmd)) {
// Specify that the sign of the Lagrangian is as follows:
// L(x,y) = H(x) - sum_i y_i H_i(x).
double sigma = mxGetScalar(prhs[2]);
asl_lagscale(asl, sigma, &err);
if (err) mexErrMsgTxt("Failed to set sign of Lagrangian.");
return;
}
// -----------------------------------------------------------------
// Command: ghivprod. Vector of dot products <g, Hi*v>, where Hi
// are Hessians of the constraints.
// -----------------------------------------------------------------
if (!strcmp("ghivprod", cmd)) {
double *x = getDense(prhs[2], "x", nvar);
double *g = getDense(prhs[3], "g", nvar);
double *v = getDense(prhs[4], "v", nvar);
// double *hv = (double*)mxMalloc(n*sizeof(double));
double *gHiv = mxGetPr(plhs[0] = mxCreateDoubleMatrix(nlc, 1, mxREAL));
asl_ghjvprod(asl, g, v, gHiv);
return;
}
// -----------------------------------------------------------------
// Command: hesslagprod. (H - sum_i H_i y_i) v
// -----------------------------------------------------------------
if (!strcmp("hesslagprod", cmd)) {
double *y = getDense(prhs[2], "y", ncon);
double *v = getDense(prhs[3], "v", nvar);
double ow = mxGetScalar(prhs[4]);
ow = asl->i.objtype_[0] ? -ow : ow; // Objective weight.
double *hv = mxGetPr(plhs[0] = mxCreateDoubleMatrix(nvar, 1, mxREAL));
asl_hprod(asl, y, v, hv, ow);
return;
}
// -----------------------------------------------------------------
// Command: write_sol. Write solution to file.
// -----------------------------------------------------------------
if (!strcmp("write_sol", cmd)) {
if (mxGetString(prhs[2], buf, sizeof(buf)))
mexErrMsgTxt("Error while retrieving message.");
double *x = getDense(prhs[3], "x", nvar);
double *y = getDense(prhs[4], "y", ncon);
asl_write_sol(asl, buf, x, y);
return;
}
// -----------------------------------------------------------------
// Got here, so command not recognized
// -----------------------------------------------------------------
mexErrMsgTxt("Command not recognized.");
}