-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathnlopt_optimize-mex.c
379 lines (331 loc) · 12.2 KB
/
nlopt_optimize-mex.c
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
/* Copyright (c) 2007-2014 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* Matlab MEX interface to NLopt, and in particular to nlopt_optimize */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mex.h>
#include "nlopt.h"
#define CHECK0(cond, msg) if (!(cond)) mexErrMsgTxt(msg);
static double struct_val_default(const mxArray *s, const char *name, double dflt)
{
mxArray *val = mxGetField(s, 0, name);
if (val) {
CHECK0(mxIsNumeric(val) && !mxIsComplex(val)
&& mxGetM(val) * mxGetN(val) == 1,
"opt fields, other than xtol_abs, must be real scalars");
return mxGetScalar(val);
}
return dflt;
}
static double *struct_arrval(const mxArray *s, const char *name, unsigned n,
double *dflt)
{
mxArray *val = mxGetField(s, 0, name);
if (val) {
CHECK0(mxIsNumeric(val) && !mxIsComplex(val)
&& mxGetM(val) * mxGetN(val) == n,
"opt vector field is not of length n");
return mxGetPr(val);
}
return dflt;
}
static mxArray *struct_funcval(const mxArray *s, const char *name)
{
mxArray *val = mxGetField(s, 0, name);
if (val) {
CHECK0(mxIsChar(val) || mxIsFunctionHandle(val),
"opt function field is not a function handle/name");
return val;
}
return NULL;
}
static double *fill(double *arr, unsigned n, double val)
{
unsigned i;
for (i = 0; i < n; ++i) arr[i] = val;
return arr;
}
#define FLEN 128 /* max length of user function name */
#define MAXRHS 3 /* max nrhs for user function */
typedef struct user_function_data_s {
char f[FLEN];
mxArray *plhs[2];
mxArray *prhs[MAXRHS];
int xrhs, nrhs;
int verbose, neval;
struct user_function_data_s *dpre;
nlopt_opt opt;
} user_function_data;
static double user_function(unsigned n, const double *x,
double *gradient, /* NULL if not needed */
void *d_)
{
user_function_data *d = (user_function_data *) d_;
double f;
d->plhs[0] = d->plhs[1] = NULL;
memcpy(mxGetPr(d->prhs[d->xrhs]), x, n * sizeof(double));
CHECK0(0 == mexCallMATLAB(gradient ? 2 : 1, d->plhs,
d->nrhs, d->prhs, d->f),
"error calling user function");
CHECK0(mxIsNumeric(d->plhs[0]) && !mxIsComplex(d->plhs[0])
&& mxGetM(d->plhs[0]) * mxGetN(d->plhs[0]) == 1,
"user function must return real scalar");
f = mxGetScalar(d->plhs[0]);
mxDestroyArray(d->plhs[0]);
if (gradient) {
CHECK0(mxIsDouble(d->plhs[1]) && !mxIsComplex(d->plhs[1])
&& (mxGetM(d->plhs[1]) == 1 || mxGetN(d->plhs[1]) == 1)
&& mxGetM(d->plhs[1]) * mxGetN(d->plhs[1]) == n,
"gradient vector from user function is the wrong size");
memcpy(gradient, mxGetPr(d->plhs[1]), n * sizeof(double));
mxDestroyArray(d->plhs[1]);
}
d->neval++;
if (d->verbose) mexPrintf("nlopt_optimize eval #%d: %g\n", d->neval, f);
if (mxIsNaN(f)) nlopt_force_stop(d->opt);
return f;
}
static void user_pre(unsigned n, const double *x, const double *v,
double *vpre, void *d_)
{
user_function_data *d = ((user_function_data *) d_)->dpre;
d->plhs[0] = d->plhs[1] = NULL;
memcpy(mxGetPr(d->prhs[d->xrhs]), x, n * sizeof(double));
memcpy(mxGetPr(d->prhs[d->xrhs + 1]), v, n * sizeof(double));
CHECK0(0 == mexCallMATLAB(1, d->plhs,
d->nrhs, d->prhs, d->f),
"error calling user function");
CHECK0(mxIsDouble(d->plhs[0]) && !mxIsComplex(d->plhs[0])
&& (mxGetM(d->plhs[0]) == 1 || mxGetN(d->plhs[0]) == 1)
&& mxGetM(d->plhs[0]) * mxGetN(d->plhs[0]) == n,
"vpre vector from user function is the wrong size");
memcpy(vpre, mxGetPr(d->plhs[0]), n * sizeof(double));
mxDestroyArray(d->plhs[0]);
d->neval++;
if (d->verbose) mexPrintf("nlopt_optimize precond eval #%d\n", d->neval);
}
#define CHECK1(cond, msg) if (!(cond)) { mxFree(tmp); nlopt_destroy(opt); nlopt_destroy(local_opt); mexWarnMsgTxt(msg); return NULL; };
nlopt_opt make_opt(const mxArray *opts, unsigned n)
{
nlopt_opt opt = NULL, local_opt = NULL;
nlopt_algorithm algorithm;
double *tmp = NULL;
unsigned i;
algorithm = (nlopt_algorithm)
struct_val_default(opts, "algorithm", NLOPT_NUM_ALGORITHMS);
CHECK1(((int)algorithm) >= 0 && algorithm < NLOPT_NUM_ALGORITHMS,
"invalid opt.algorithm");
tmp = (double *) mxCalloc(n, sizeof(double));
opt = nlopt_create(algorithm, n);
CHECK1(opt, "nlopt: out of memory");
nlopt_set_lower_bounds(opt, struct_arrval(opts, "lower_bounds", n,
fill(tmp, n, -HUGE_VAL)));
nlopt_set_upper_bounds(opt, struct_arrval(opts, "upper_bounds", n,
fill(tmp, n, +HUGE_VAL)));
nlopt_set_stopval(opt, struct_val_default(opts, "stopval", -HUGE_VAL));
nlopt_set_ftol_rel(opt, struct_val_default(opts, "ftol_rel", 0.0));
nlopt_set_ftol_abs(opt, struct_val_default(opts, "ftol_abs", 0.0));
nlopt_set_xtol_rel(opt, struct_val_default(opts, "xtol_rel", 0.0));
nlopt_set_xtol_abs(opt, struct_arrval(opts, "xtol_abs", n,
fill(tmp, n, 0.0)));
nlopt_set_x_weights(opt, struct_arrval(opts, "x_weights", n,
fill(tmp, n, 1.0)));
nlopt_set_maxeval(opt, struct_val_default(opts, "maxeval", 0.0) < 0 ?
0 : struct_val_default(opts, "maxeval", 0.0));
nlopt_set_maxtime(opt, struct_val_default(opts, "maxtime", 0.0));
nlopt_set_population(opt, struct_val_default(opts, "population", 0));
nlopt_set_vector_storage(opt, struct_val_default(opts, "vector_storage", 0));
if (struct_arrval(opts, "initial_step", n, NULL))
nlopt_set_initial_step(opt,
struct_arrval(opts, "initial_step", n, NULL));
if (mxGetField(opts, 0, "local_optimizer")) {
const mxArray *local_opts = mxGetField(opts, 0, "local_optimizer");
CHECK1(mxIsStruct(local_opts),
"opt.local_optimizer must be a structure");
CHECK1(local_opt = make_opt(local_opts, n),
"error initializing local optimizer");
nlopt_set_local_optimizer(opt, local_opt);
nlopt_destroy(local_opt); local_opt = NULL;
}
mxFree(tmp);
return opt;
}
#define CHECK(cond, msg) if (!(cond)) { mxFree(dh); mxFree(dfc); nlopt_destroy(opt); mexErrMsgTxt(msg); }
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
unsigned n;
double *x, *x0, opt_f;
nlopt_result ret;
mxArray *x_mx, *mx;
user_function_data d, dpre, *dfc = NULL, *dh = NULL;
nlopt_opt opt = NULL;
CHECK(nrhs == 2 && nlhs <= 3, "wrong number of arguments");
/* options = prhs[0] */
CHECK(mxIsStruct(prhs[0]), "opt must be a struct");
/* x0 = prhs[1] */
CHECK(mxIsDouble(prhs[1]) && !mxIsComplex(prhs[1])
&& (mxGetM(prhs[1]) == 1 || mxGetN(prhs[1]) == 1),
"x must be real row or column vector");
n = mxGetM(prhs[1]) * mxGetN(prhs[1]),
x0 = mxGetPr(prhs[1]);
CHECK(opt = make_opt(prhs[0], n), "error initializing nlopt options");
d.neval = 0;
d.verbose = (int) struct_val_default(prhs[0], "verbose", 0);
d.opt = opt;
/* function f = prhs[1] */
mx = struct_funcval(prhs[0], "min_objective");
if (!mx) mx = struct_funcval(prhs[0], "max_objective");
CHECK(mx, "either opt.min_objective or opt.max_objective must exist");
if (mxIsChar(mx)) {
CHECK(mxGetString(mx, d.f, FLEN) == 0,
"error reading function name string (too long?)");
d.nrhs = 1;
d.xrhs = 0;
}
else {
d.prhs[0] = mx;
strcpy(d.f, "feval");
d.nrhs = 2;
d.xrhs = 1;
}
d.prhs[d.xrhs] = mxCreateDoubleMatrix(1, n, mxREAL);
if ((mx = struct_funcval(prhs[0], "pre"))) {
CHECK(mxIsChar(mx) || mxIsFunctionHandle(mx),
"pre must contain function handles or function names");
if (mxIsChar(mx)) {
CHECK(mxGetString(mx, dpre.f, FLEN) == 0,
"error reading function name string (too long?)");
dpre.nrhs = 2;
dpre.xrhs = 0;
}
else {
dpre.prhs[0] = mx;
strcpy(dpre.f, "feval");
dpre.nrhs = 3;
dpre.xrhs = 1;
}
dpre.verbose = d.verbose > 2;
dpre.opt = opt;
dpre.neval = 0;
dpre.prhs[dpre.xrhs] = d.prhs[d.xrhs];
dpre.prhs[d.xrhs+1] = mxCreateDoubleMatrix(1, n, mxREAL);
d.dpre = &dpre;
if (struct_funcval(prhs[0], "min_objective"))
nlopt_set_precond_min_objective(opt, user_function,user_pre,&d);
else
nlopt_set_precond_max_objective(opt, user_function,user_pre,&d);
}
else {
dpre.nrhs = 0;
if (struct_funcval(prhs[0], "min_objective"))
nlopt_set_min_objective(opt, user_function, &d);
else
nlopt_set_max_objective(opt, user_function, &d);
}
if ((mx = mxGetField(prhs[0], 0, "fc"))) {
int j, m;
double *fc_tol;
CHECK(mxIsCell(mx), "fc must be a Cell array");
m = mxGetM(mx) * mxGetN(mx);;
dfc = (user_function_data *) mxCalloc(m, sizeof(user_function_data));
fc_tol = struct_arrval(prhs[0], "fc_tol", m, NULL);
for (j = 0; j < m; ++j) {
mxArray *fc = mxGetCell(mx, j);
CHECK(mxIsChar(fc) || mxIsFunctionHandle(fc),
"fc must contain function handles or function names");
if (mxIsChar(fc)) {
CHECK(mxGetString(fc, dfc[j].f, FLEN) == 0,
"error reading function name string (too long?)");
dfc[j].nrhs = 1;
dfc[j].xrhs = 0;
}
else {
dfc[j].prhs[0] = fc;
strcpy(dfc[j].f, "feval");
dfc[j].nrhs = 2;
dfc[j].xrhs = 1;
}
dfc[j].verbose = d.verbose > 1;
dfc[j].opt = opt;
dfc[j].neval = 0;
dfc[j].prhs[dfc[j].xrhs] = d.prhs[d.xrhs];
CHECK(nlopt_add_inequality_constraint(opt, user_function,
dfc + j,
fc_tol ? fc_tol[j] : 0)
> 0, "nlopt error adding inequality constraint");
}
}
if ((mx = mxGetField(prhs[0], 0, "h"))) {
int j, m;
double *h_tol;
CHECK(mxIsCell(mx), "h must be a Cell array");
m = mxGetM(mx) * mxGetN(mx);;
dh = (user_function_data *) mxCalloc(m, sizeof(user_function_data));
h_tol = struct_arrval(prhs[0], "h_tol", m, NULL);
for (j = 0; j < m; ++j) {
mxArray *h = mxGetCell(mx, j);
CHECK(mxIsChar(h) || mxIsFunctionHandle(h),
"h must contain function handles or function names");
if (mxIsChar(h)) {
CHECK(mxGetString(h, dh[j].f, FLEN) == 0,
"error reading function name string (too long?)");
dh[j].nrhs = 1;
dh[j].xrhs = 0;
}
else {
dh[j].prhs[0] = h;
strcpy(dh[j].f, "feval");
dh[j].nrhs = 2;
dh[j].xrhs = 1;
}
dh[j].verbose = d.verbose > 1;
dh[j].opt = opt;
dh[j].neval = 0;
dh[j].prhs[dh[j].xrhs] = d.prhs[d.xrhs];
CHECK(nlopt_add_equality_constraint(opt, user_function,
dh + j,
h_tol ? h_tol[j] : 0)
> 0, "nlopt error adding equality constraint");
}
}
x_mx = mxCreateDoubleMatrix(mxGetM(prhs[1]), mxGetN(prhs[1]), mxREAL);
x = mxGetPr(x_mx);
memcpy(x, x0, sizeof(double) * n);
ret = nlopt_optimize(opt, x, &opt_f);
mxFree(dh);
mxFree(dfc);
mxDestroyArray(d.prhs[d.xrhs]);
if (dpre.nrhs > 0) mxDestroyArray(dpre.prhs[d.xrhs+1]);
nlopt_destroy(opt);
plhs[0] = x_mx;
if (nlhs > 1) {
plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
*(mxGetPr(plhs[1])) = opt_f;
}
if (nlhs > 2) {
plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
*(mxGetPr(plhs[2])) = (int) ret;
}
}