-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathampl_interface_mex.cpp
More file actions
287 lines (246 loc) · 9.46 KB
/
ampl_interface_mex.cpp
File metadata and controls
287 lines (246 loc) · 9.46 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
#include "class_handle.hpp"
#undef printf
#include "asl/asl_pfgh.h"
#include "mex.h"
// The class that we are interfacing to
class dummy {
public:
ASL *asl;
real *J;
fint n, nc, nz;
size_t Jsize;
private:
};
extern "C" {
double ddot_(
size_t *n,
double *dx,
size_t *incx,
double *dy,
size_t *incy
);
};
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[])
{
dummy* AC;
ASL *asl;
char *buf1, buf[512], msgbuf[256];
fint nerror = 0;
mwSize i, n, nc, nz;
// 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)) {
FILE *nl;
// Return a handle to a new C++ instance
AC = new dummy;
plhs[0] = convertPtr2Mat<dummy>(AC);
// Allocate the ASL object.
if (mxGetString(prhs[1], buf1 = buf, sizeof(buf)))
mexErrMsgTxt("Expected 'stub' as argument\n");
asl = AC->asl = ASL_alloc(ASL_read_pfgh);
return_nofile = 1;
if (!(nl = jac0dim(buf1,strlen(buf)))) {
sprintf(msgbuf, "Can't open %.*s\n", sizeof(msgbuf)-20, buf);
mexErrMsgTxt(msgbuf);
}
if (n_obj <= 0)
printf("Warning: objective == 0\n");
// Save the sizes into the ampl context. The sizes n, nc, and
// nz are useful, so make local copies.
n = AC->n = n_var;
nc = AC->nc = n_con;
nz = AC->nz = nzc;
// Allocate space for the Jacobian. M1alloc is an ampl macro
// that actually allocates space within the ASL object; no
// need to deallocate this, since the ASL_free function
// (later) will take care to delete this.
AC->J = (real *)M1alloc(nz*sizeof(real));
// Create the RHSs.
X0 = mxGetPr(plhs[1] = mxCreateDoubleMatrix(n , 1, mxREAL));
LUv = mxGetPr(plhs[2] = mxCreateDoubleMatrix(n , 1, mxREAL));
Uvx = mxGetPr(plhs[3] = mxCreateDoubleMatrix(n , 1, mxREAL));
pi0 = mxGetPr(plhs[4] = mxCreateDoubleMatrix(nc, 1, mxREAL));
LUrhs = mxGetPr(plhs[5] = mxCreateDoubleMatrix(nc, 1, mxREAL));
Urhsx = mxGetPr(plhs[6] = mxCreateDoubleMatrix(nc, 1, mxREAL));
plhs[7] = mxCreateDoubleScalar(nlc);
// pfgh_read reads and then closes the open file nl.
pfgh_read(nl, ASL_findgroups);
AC->Jsize = nc*n*sizeof(real);
return;
}
// -----------------------------------------------------------------
// Retrieve C++ object, and unpack it.
// -----------------------------------------------------------------
AC = convertMat2Ptr<dummy>(prhs[1]);
asl = AC->asl;
n = AC->n;
nc = AC->nc;
nz = AC->nz;
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// Call the various class methods
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// Command: Delete
// -----------------------------------------------------------------
if (!strcmp("delete", cmd)) {
// Destroy the C++ object
ASL_free(&(AC->asl));
destroyObject<dummy>(prhs[1]);
// Warn if other commands 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", n);
double *f = mxGetPr(plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL));
*f = objval(0, x, &nerror);
if (nerror)
mexErrMsgTxt("Trouble evaluating f\n");
return;
}
// -----------------------------------------------------------------
// Command: gradient
// -----------------------------------------------------------------
if (!strcmp("grad", cmd)) {
double *x = getDense(prhs[2], "x", n);
double *g = mxGetPr(plhs[0] = mxCreateDoubleMatrix(n, 1, mxREAL));
objgrd(0, x, g, &nerror);
if (nerror)
mexErrMsgTxt("Trouble evaluating g\n");
return;
}
// -----------------------------------------------------------------
// Command: constraint
// -----------------------------------------------------------------
if (!strcmp("con", cmd)) {
double *x = getDense(prhs[2], "x", n);
double *c = mxGetPr(plhs[0] = mxCreateDoubleMatrix(nc, 1, mxREAL));
conval(x, c, &nerror);
if (nerror)
mexErrMsgTxt("Trouble evaluating c\n");
return;
}
// -----------------------------------------------------------------
// Command: jacobian
// -----------------------------------------------------------------
if (!strcmp("jac", cmd)) {
cgrad *cg, **cgp, **cgpe;
double *x = getDense(prhs[2], "x", n);
double *J1 = mxGetPr(plhs[0] = mxCreateDoubleMatrix(nc, n, mxREAL));
if (nc) {
memset(J1, 0, AC->Jsize);
jacval(x, AC->J, &nerror);
if (nerror)
mexErrMsgTxt("Trouble evaluating J\n");
cgp = Cgrad;
for(cgpe = cgp + nc; cgp < cgpe; J1++)
for(cg = *cgp++; cg; cg = cg->next)
J1[nc*cg->varno] = AC->J[cg->goff];
}
return;
}
// -----------------------------------------------------------------
// Command: Hessian of Lagrangian
// -----------------------------------------------------------------
if (!strcmp("hessobj", cmd)) {
double *H = mxGetPr(plhs[0] = mxCreateDoubleMatrix(n, n, mxREAL));
fullhes(H, n, 0, 0, NULL);
return;
}
// -----------------------------------------------------------------
// Command: Hessian of Lagrangian
// -----------------------------------------------------------------
if (!strcmp("hesslag", cmd)) {
double *y = getDense(prhs[2], "y", nc);
double *H = mxGetPr(plhs[0] = mxCreateDoubleMatrix(n, n, mxREAL));
fullhes(H, n, 0, 0, y);
return;
}
// -----------------------------------------------------------------
// Command: Hessian of constraint, sum_i H_i y_i.
// -----------------------------------------------------------------
if (!strcmp("hesscon", cmd)) {
double *y = getDense(prhs[2], "y", nc);
double *H = mxGetPr(plhs[0] = mxCreateDoubleMatrix(n, n, mxREAL));
fullhes(H, n, -1, NULL, y);
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]);
lagscale(sigma, &nerror);
if (nerror)
mexErrMsgTxt("Failed to set sign of Lagrangian Hessian.");
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", n);
double *g = getDense(prhs[3], "g", n);
double *v = getDense(prhs[4], "v", n);
double *hv = (double*)mxMalloc(n*sizeof(double));
double *gHiv = mxGetPr(plhs[0] = mxCreateDoubleMatrix(nlc, 1, mxREAL));
size_t one = 1;
size_t nn = n;
xknown(x);
for (i = 0; i < nlc; i++) {
hvcompd(hv, v, i);
gHiv[i] = ddot_(&nn, hv, &one, g, &one);
}
xunknown();
mxFree(hv);
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", n);
double *y = getDense(prhs[4], "y", nc);
write_sol(buf, x, y, 0);
return;
}
// -----------------------------------------------------------------
// Got here, so command not recognized
// -----------------------------------------------------------------
mexErrMsgTxt("Command not recognized.");
}