python-libnest2d 0.1.0__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
include/nlopt.hpp ADDED
@@ -0,0 +1,619 @@
1
+ /* Copyright (c) 2007-2011 Massachusetts Institute of Technology
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining
4
+ * a copy of this software and associated documentation files (the
5
+ * "Software"), to deal in the Software without restriction, including
6
+ * without limitation the rights to use, copy, modify, merge, publish,
7
+ * distribute, sublicense, and/or sell copies of the Software, and to
8
+ * permit persons to whom the Software is furnished to do so, subject to
9
+ * the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be
12
+ * included in all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ */
22
+ // C++ style wrapper around NLopt API
23
+ // nlopt.hpp is AUTOMATICALLY GENERATED from nlopt-in.hpp - edit the latter!
24
+ #ifndef NLOPT_HPP
25
+ #define NLOPT_HPP
26
+ #include <nlopt.h>
27
+ #include <vector>
28
+ #include <stdexcept>
29
+ #include <new>
30
+ #include <cstdlib>
31
+ #include <cstring>
32
+ #include <cmath>
33
+ #include <utility> // std::move
34
+ #include <functional> // std::function
35
+ // convenience overloading for below (not in nlopt:: since has nlopt_ prefix)
36
+ inline nlopt_result nlopt_get_initial_step(const nlopt_opt opt, double *dx) {
37
+ return nlopt_get_initial_step(opt, (const double *) NULL, dx);
38
+ }
39
+ namespace nlopt {
40
+ //////////////////////////////////////////////////////////////////////
41
+ // nlopt::* namespace versions of the C enumerated types
42
+ // AUTOMATICALLY GENERATED, DO NOT EDIT
43
+ // GEN_ENUMS_HERE
44
+ enum algorithm {
45
+ GN_DIRECT = 0,
46
+ GN_DIRECT_L,
47
+ GN_DIRECT_L_RAND,
48
+ GN_DIRECT_NOSCAL,
49
+ GN_DIRECT_L_NOSCAL,
50
+ GN_DIRECT_L_RAND_NOSCAL,
51
+ GN_ORIG_DIRECT,
52
+ GN_ORIG_DIRECT_L,
53
+ GD_STOGO,
54
+ GD_STOGO_RAND,
55
+ LD_LBFGS,
56
+ LN_PRAXIS,
57
+ LD_VAR1,
58
+ LD_VAR2,
59
+ LD_TNEWTON,
60
+ LD_TNEWTON_RESTART,
61
+ LD_TNEWTON_PRECOND,
62
+ LD_TNEWTON_PRECOND_RESTART,
63
+ GN_CRS2_LM,
64
+ GN_MLSL,
65
+ GD_MLSL,
66
+ GN_MLSL_LDS,
67
+ GD_MLSL_LDS,
68
+ LD_MMA,
69
+ LN_COBYLA,
70
+ LN_NEWUOA,
71
+ LN_NEWUOA_BOUND,
72
+ LN_NELDERMEAD,
73
+ LN_SBPLX,
74
+ LN_AUGLAG,
75
+ LD_AUGLAG,
76
+ LN_AUGLAG_EQ,
77
+ LD_AUGLAG_EQ,
78
+ LN_BOBYQA,
79
+ GN_ISRES,
80
+ AUGLAG,
81
+ AUGLAG_EQ,
82
+ G_MLSL,
83
+ G_MLSL_LDS,
84
+ LD_SLSQP,
85
+ LD_CCSAQ,
86
+ GN_ESCH,
87
+ GN_AGS,
88
+ NUM_ALGORITHMS /* not an algorithm, just the number of them */
89
+ };
90
+ enum result {
91
+ FAILURE = -1, /* generic failure code */
92
+ INVALID_ARGS = -2,
93
+ OUT_OF_MEMORY = -3,
94
+ ROUNDOFF_LIMITED = -4,
95
+ FORCED_STOP = -5,
96
+ NUM_FAILURES = -6, /* not a result, just the number of possible failures */
97
+ SUCCESS = 1, /* generic success code */
98
+ STOPVAL_REACHED = 2,
99
+ FTOL_REACHED = 3,
100
+ XTOL_REACHED = 4,
101
+ MAXEVAL_REACHED = 5,
102
+ MAXTIME_REACHED = 6,
103
+ NUM_RESULTS /* not a result, just the number of possible successes */
104
+ };
105
+ //////////////////////////////////////////////////////////////////////
106
+ typedef nlopt_func func; // nlopt::func synoynm
107
+ typedef nlopt_mfunc mfunc; // nlopt::mfunc synoynm
108
+ // alternative to nlopt_func that takes std::vector<double>
109
+ // ... unfortunately requires a data copy
110
+ typedef double (*vfunc)(const std::vector<double> &x,
111
+ std::vector<double> &grad, void *data);
112
+ // OOP alternative to nlopt_func that stores the data inside,
113
+ // hence no need to pass (void*) data
114
+ // functor can be a regular function, C++ lambda, a class with `operator()`
115
+ // defined with the following signature:
116
+ // double operator()(unsigned, const double*, double*);
117
+ typedef std::function<double(unsigned, const double*, double*)> functor_type;
118
+ //////////////////////////////////////////////////////////////////////
119
+ // NLopt-specific exceptions (corresponding to error codes):
120
+ class roundoff_limited : public std::runtime_error {
121
+ public:
122
+ roundoff_limited() : std::runtime_error("nlopt roundoff-limited") {}
123
+ };
124
+ class forced_stop : public std::runtime_error {
125
+ public:
126
+ forced_stop() : std::runtime_error("nlopt forced stop") {}
127
+ };
128
+ //////////////////////////////////////////////////////////////////////
129
+ class opt {
130
+ private:
131
+ nlopt_opt o;
132
+ void mythrow(nlopt_result ret) const {
133
+ switch (ret) {
134
+ case NLOPT_FAILURE: throw std::runtime_error(get_errmsg() ? get_errmsg() : "nlopt failure");
135
+ case NLOPT_OUT_OF_MEMORY: throw std::bad_alloc();
136
+ case NLOPT_INVALID_ARGS: throw std::invalid_argument(get_errmsg() ? get_errmsg() : "nlopt invalid argument");
137
+ case NLOPT_ROUNDOFF_LIMITED: throw roundoff_limited();
138
+ case NLOPT_FORCED_STOP: throw forced_stop();
139
+ default: break;
140
+ }
141
+ }
142
+ typedef struct {
143
+ opt *o;
144
+ mfunc mf; func f; void *f_data;
145
+ functor_type functor;
146
+ vfunc vf;
147
+ nlopt_munge munge_destroy, munge_copy; // non-NULL for SWIG wrappers
148
+ } myfunc_data;
149
+ static myfunc_data* alloc_myfunc_data_with_nulls() {
150
+ myfunc_data *d = new myfunc_data(); // zero-initialize all pointers
151
+ if (!d) throw std::bad_alloc();
152
+ return d;
153
+ }
154
+ myfunc_data* alloc_and_init_myfunc_data() {
155
+ myfunc_data *d = alloc_myfunc_data_with_nulls();
156
+ d->o = this;
157
+ return d;
158
+ }
159
+ // free/destroy f_data in nlopt_destroy and nlopt_copy, respectively
160
+ static void *free_myfunc_data(void *p) {
161
+ myfunc_data *d = (myfunc_data *) p;
162
+ if (d) {
163
+ if (d->f_data && d->munge_destroy) d->munge_destroy(d->f_data);
164
+ delete d;
165
+ }
166
+ return NULL;
167
+ }
168
+ static void *dup_myfunc_data(void *p) {
169
+ myfunc_data *d = (myfunc_data *) p;
170
+ if (d) {
171
+ void *f_data;
172
+ if (d->f_data && d->munge_copy) {
173
+ f_data = d->munge_copy(d->f_data);
174
+ if (!f_data) return NULL;
175
+ }
176
+ else
177
+ f_data = d->f_data;
178
+ myfunc_data *dnew = alloc_myfunc_data_with_nulls();
179
+ *dnew = *d;
180
+ dnew->f_data = f_data;
181
+ return (void*) dnew;
182
+ }
183
+ else return NULL;
184
+ }
185
+ // nlopt_func wrapper that catches exceptions
186
+ static double myfunc(unsigned n, const double *x, double *grad, void *d_) {
187
+ myfunc_data *d = reinterpret_cast<myfunc_data*>(d_);
188
+ try {
189
+ return d->f(n, x, grad, d->f_data);
190
+ }
191
+ catch (std::bad_alloc&)
192
+ { d->o->forced_stop_reason = NLOPT_OUT_OF_MEMORY; }
193
+ catch (std::invalid_argument&)
194
+ { d->o->forced_stop_reason = NLOPT_INVALID_ARGS; }
195
+ catch (roundoff_limited&)
196
+ { d->o->forced_stop_reason = NLOPT_ROUNDOFF_LIMITED; }
197
+ catch (forced_stop&)
198
+ { d->o->forced_stop_reason = NLOPT_FORCED_STOP; }
199
+ catch (...)
200
+ { d->o->forced_stop_reason = NLOPT_FAILURE; }
201
+ d->o->force_stop(); // stop gracefully, opt::optimize will re-throw
202
+ return HUGE_VAL;
203
+ }
204
+ // nlopt_mfunc wrapper that catches exceptions
205
+ static void mymfunc(unsigned m, double *result,
206
+ unsigned n, const double *x, double *grad, void *d_) {
207
+ myfunc_data *d = reinterpret_cast<myfunc_data*>(d_);
208
+ try {
209
+ d->mf(m, result, n, x, grad, d->f_data);
210
+ return;
211
+ }
212
+ catch (std::bad_alloc&)
213
+ { d->o->forced_stop_reason = NLOPT_OUT_OF_MEMORY; }
214
+ catch (std::invalid_argument&)
215
+ { d->o->forced_stop_reason = NLOPT_INVALID_ARGS; }
216
+ catch (roundoff_limited&)
217
+ { d->o->forced_stop_reason = NLOPT_ROUNDOFF_LIMITED; }
218
+ catch (forced_stop&)
219
+ { d->o->forced_stop_reason = NLOPT_FORCED_STOP; }
220
+ catch (...)
221
+ { d->o->forced_stop_reason = NLOPT_FAILURE; }
222
+ d->o->force_stop(); // stop gracefully, opt::optimize will re-throw
223
+ for (unsigned i = 0; i < m; ++i) result[i] = HUGE_VAL;
224
+ }
225
+ std::vector<double> xtmp, gradtmp, gradtmp0; // scratch for myvfunc
226
+ // nlopt_func wrapper, using std::vector<double>
227
+ static double myvfunc(unsigned n, const double *x, double *grad, void *d_){
228
+ myfunc_data *d = reinterpret_cast<myfunc_data*>(d_);
229
+ try {
230
+ std::vector<double> &xv = d->o->xtmp;
231
+ if (n) std::memcpy(&xv[0], x, n * sizeof(double));
232
+ double val=d->vf(xv, grad ? d->o->gradtmp : d->o->gradtmp0, d->f_data);
233
+ if (grad && n) {
234
+ std::vector<double> &gradv = d->o->gradtmp;
235
+ std::memcpy(grad, &gradv[0], n * sizeof(double));
236
+ }
237
+ return val;
238
+ }
239
+ catch (std::bad_alloc&)
240
+ { d->o->forced_stop_reason = NLOPT_OUT_OF_MEMORY; }
241
+ catch (std::invalid_argument&)
242
+ { d->o->forced_stop_reason = NLOPT_INVALID_ARGS; }
243
+ catch (roundoff_limited&)
244
+ { d->o->forced_stop_reason = NLOPT_ROUNDOFF_LIMITED; }
245
+ catch (forced_stop&)
246
+ { d->o->forced_stop_reason = NLOPT_FORCED_STOP; }
247
+ catch (...)
248
+ { d->o->forced_stop_reason = NLOPT_FAILURE; }
249
+ d->o->force_stop(); // stop gracefully, opt::optimize will re-throw
250
+ return HUGE_VAL;
251
+ }
252
+ // nlopt_func wrapper, using std::function object
253
+ static double functor_wrapper(unsigned n, const double *x,
254
+ double *grad, void *d_) {
255
+ myfunc_data *d = reinterpret_cast<myfunc_data*>(d_);
256
+ try {
257
+ // since functor can be a callable object and contain its own data,
258
+ // passing data as (void*) is not needed
259
+ return d->functor(n, x, grad);
260
+ }
261
+ catch (std::bad_alloc&)
262
+ { d->o->forced_stop_reason = NLOPT_OUT_OF_MEMORY; }
263
+ catch (std::invalid_argument&)
264
+ { d->o->forced_stop_reason = NLOPT_INVALID_ARGS; }
265
+ catch (roundoff_limited&)
266
+ { d->o->forced_stop_reason = NLOPT_ROUNDOFF_LIMITED; }
267
+ catch (forced_stop&)
268
+ { d->o->forced_stop_reason = NLOPT_FORCED_STOP; }
269
+ catch (...)
270
+ { d->o->forced_stop_reason = NLOPT_FAILURE; }
271
+ d->o->force_stop(); // stop gracefully, opt::optimize will re-throw
272
+ return HUGE_VAL;
273
+ }
274
+ void alloc_tmp() {
275
+ if (xtmp.size() != nlopt_get_dimension(o)) {
276
+ xtmp = std::vector<double>(nlopt_get_dimension(o));
277
+ gradtmp = std::vector<double>(nlopt_get_dimension(o));
278
+ }
279
+ }
280
+ result last_result;
281
+ double last_optf;
282
+ nlopt_result forced_stop_reason;
283
+ public:
284
+ // Constructors etc.
285
+ opt() : o(NULL), xtmp(0), gradtmp(0), gradtmp0(0),
286
+ last_result(nlopt::FAILURE), last_optf(HUGE_VAL),
287
+ forced_stop_reason(NLOPT_FORCED_STOP) {}
288
+ ~opt() { nlopt_destroy(o); }
289
+ opt(algorithm a, unsigned n) :
290
+ o(nlopt_create(nlopt_algorithm(a), n)),
291
+ xtmp(0), gradtmp(0), gradtmp0(0),
292
+ last_result(nlopt::FAILURE), last_optf(HUGE_VAL),
293
+ forced_stop_reason(NLOPT_FORCED_STOP) {
294
+ if (!o) throw std::bad_alloc();
295
+ nlopt_set_munge(o, free_myfunc_data, dup_myfunc_data);
296
+ }
297
+ opt(const char * algo_str, unsigned n) :
298
+ o(NULL), xtmp(0), gradtmp(0), gradtmp0(0),
299
+ last_result(nlopt::FAILURE), last_optf(HUGE_VAL),
300
+ forced_stop_reason(NLOPT_FORCED_STOP) {
301
+ const nlopt_algorithm a = nlopt_algorithm_from_string(algo_str);
302
+ if (a < 0)
303
+ throw std::invalid_argument("wrong algorithm string");
304
+ o = nlopt_create(a, n);
305
+ if (!o) throw std::bad_alloc();
306
+ nlopt_set_munge(o, free_myfunc_data, dup_myfunc_data);
307
+ }
308
+ opt(const opt& f) : o(nlopt_copy(f.o)),
309
+ xtmp(f.xtmp), gradtmp(f.gradtmp), gradtmp0(0),
310
+ last_result(f.last_result), last_optf(f.last_optf),
311
+ forced_stop_reason(f.forced_stop_reason) {
312
+ if (f.o && !o) throw std::bad_alloc();
313
+ }
314
+ opt& operator=(opt const& f) {
315
+ if (this == &f) return *this; // self-assignment
316
+ nlopt_destroy(o);
317
+ o = nlopt_copy(f.o);
318
+ if (f.o && !o) throw std::bad_alloc();
319
+ xtmp = f.xtmp; gradtmp = f.gradtmp;
320
+ last_result = f.last_result; last_optf = f.last_optf;
321
+ forced_stop_reason = f.forced_stop_reason;
322
+ return *this;
323
+ }
324
+ // Do the optimization:
325
+ result optimize(std::vector<double> &x, double &opt_f) {
326
+ if (o && nlopt_get_dimension(o) != x.size())
327
+ throw std::invalid_argument("dimension mismatch");
328
+ forced_stop_reason = NLOPT_FORCED_STOP;
329
+ nlopt_result ret = nlopt_optimize(o, x.empty() ? NULL : &x[0], &opt_f);
330
+ last_result = result(ret);
331
+ last_optf = opt_f;
332
+ if (ret == NLOPT_FORCED_STOP)
333
+ mythrow(forced_stop_reason);
334
+ mythrow(ret);
335
+ return last_result;
336
+ }
337
+ // variant mainly useful for SWIG wrappers:
338
+ std::vector<double> optimize(const std::vector<double> &x0) {
339
+ std::vector<double> x(x0);
340
+ last_result = optimize(x, last_optf);
341
+ return x;
342
+ }
343
+ result last_optimize_result() const { return last_result; }
344
+ double last_optimum_value() const { return last_optf; }
345
+ // accessors:
346
+ algorithm get_algorithm() const {
347
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt");
348
+ return algorithm(nlopt_get_algorithm(o));
349
+ }
350
+ const char *get_algorithm_name() const {
351
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt");
352
+ return nlopt_algorithm_name(nlopt_get_algorithm(o));
353
+ }
354
+ unsigned get_dimension() const {
355
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt");
356
+ return nlopt_get_dimension(o);
357
+ }
358
+ // Set the objective function
359
+ void set_min_objective(func f, void *f_data) {
360
+ myfunc_data *d = alloc_and_init_myfunc_data();
361
+ d->f = f;
362
+ d->f_data = f_data;
363
+ mythrow(nlopt_set_min_objective(o, myfunc, d)); // d freed via o
364
+ }
365
+ void set_min_objective(vfunc vf, void *f_data) {
366
+ myfunc_data *d = alloc_and_init_myfunc_data();
367
+ d->vf = vf;
368
+ d->f_data = f_data;
369
+ mythrow(nlopt_set_min_objective(o, myvfunc, d)); // d freed via o
370
+ alloc_tmp();
371
+ }
372
+ void set_min_objective(functor_type functor) {
373
+ myfunc_data *d = alloc_and_init_myfunc_data();
374
+ d->functor = std::move(functor);
375
+ mythrow(nlopt_set_min_objective(o, functor_wrapper, d)); // d freed via o
376
+ }
377
+ void set_max_objective(func f, void *f_data) {
378
+ myfunc_data *d = alloc_and_init_myfunc_data();
379
+ d->f = f;
380
+ d->f_data = f_data;
381
+ mythrow(nlopt_set_max_objective(o, myfunc, d)); // d freed via o
382
+ }
383
+ void set_max_objective(vfunc vf, void *f_data) {
384
+ myfunc_data *d = alloc_and_init_myfunc_data();
385
+ d->vf = vf;
386
+ d->f_data = f_data;
387
+ mythrow(nlopt_set_max_objective(o, myvfunc, d)); // d freed via o
388
+ alloc_tmp();
389
+ }
390
+ void set_max_objective(functor_type functor) {
391
+ myfunc_data *d = alloc_and_init_myfunc_data();
392
+ d->functor = std::move(functor);
393
+ mythrow(nlopt_set_max_objective(o, functor_wrapper, d)); // d freed via o
394
+ }
395
+ // for internal use in SWIG wrappers -- variant that
396
+ // takes ownership of f_data, with munging for destroy/copy
397
+ void set_min_objective(func f, void *f_data,
398
+ nlopt_munge md, nlopt_munge mc) {
399
+ myfunc_data *d = alloc_and_init_myfunc_data();
400
+ d->f = f;
401
+ d->f_data = f_data;
402
+ d->munge_destroy = md;
403
+ d->munge_copy = mc;
404
+ mythrow(nlopt_set_min_objective(o, myfunc, d)); // d freed via o
405
+ }
406
+ void set_max_objective(func f, void *f_data,
407
+ nlopt_munge md, nlopt_munge mc) {
408
+ myfunc_data *d = alloc_and_init_myfunc_data();
409
+ d->f = f;
410
+ d->f_data = f_data;
411
+ d->munge_destroy = md;
412
+ d->munge_copy = mc;
413
+ mythrow(nlopt_set_max_objective(o, myfunc, d)); // d freed via o
414
+ }
415
+ // Nonlinear constraints:
416
+ void remove_inequality_constraints() {
417
+ nlopt_result ret = nlopt_remove_inequality_constraints(o);
418
+ mythrow(ret);
419
+ }
420
+ void add_inequality_constraint(func f, void *f_data, double tol=0) {
421
+ myfunc_data *d = alloc_and_init_myfunc_data();
422
+ d->f = f;
423
+ d->f_data = f_data;
424
+ mythrow(nlopt_add_inequality_constraint(o, myfunc, d, tol));
425
+ }
426
+ void add_inequality_constraint(vfunc vf, void *f_data, double tol=0) {
427
+ myfunc_data *d = alloc_and_init_myfunc_data();
428
+ d->vf = vf;
429
+ d->f_data = f_data;
430
+ mythrow(nlopt_add_inequality_constraint(o, myvfunc, d, tol));
431
+ alloc_tmp();
432
+ }
433
+ void add_inequality_mconstraint(mfunc mf, void *f_data,
434
+ const std::vector<double> &tol) {
435
+ myfunc_data *d = alloc_and_init_myfunc_data();
436
+ d->mf = mf;
437
+ d->f_data = f_data;
438
+ mythrow(nlopt_add_inequality_mconstraint(o, static_cast<unsigned int>(tol.size()), mymfunc, d,
439
+ tol.empty() ? NULL : &tol[0]));
440
+ }
441
+ void remove_equality_constraints() {
442
+ nlopt_result ret = nlopt_remove_equality_constraints(o);
443
+ mythrow(ret);
444
+ }
445
+ void add_equality_constraint(func f, void *f_data, double tol=0) {
446
+ myfunc_data *d = alloc_and_init_myfunc_data();
447
+ d->f = f;
448
+ d->f_data = f_data;
449
+ mythrow(nlopt_add_equality_constraint(o, myfunc, d, tol));
450
+ }
451
+ void add_equality_constraint(vfunc vf, void *f_data, double tol=0) {
452
+ myfunc_data *d = alloc_and_init_myfunc_data();
453
+ d->vf = vf;
454
+ d->f_data = f_data;
455
+ mythrow(nlopt_add_equality_constraint(o, myvfunc, d, tol));
456
+ alloc_tmp();
457
+ }
458
+ void add_equality_mconstraint(mfunc mf, void *f_data,
459
+ const std::vector<double> &tol) {
460
+ myfunc_data *d = alloc_and_init_myfunc_data();
461
+ d->mf = mf;
462
+ d->f_data = f_data;
463
+ mythrow(nlopt_add_equality_mconstraint(o, static_cast<unsigned int>(tol.size()), mymfunc, d,
464
+ tol.empty() ? NULL : &tol[0]));
465
+ }
466
+ // For internal use in SWIG wrappers (see also above)
467
+ void add_inequality_constraint(func f, void *f_data,
468
+ nlopt_munge md, nlopt_munge mc,
469
+ double tol=0) {
470
+ myfunc_data *d = alloc_and_init_myfunc_data();
471
+ d->f = f;
472
+ d->f_data = f_data;
473
+ d->munge_destroy = md;
474
+ d->munge_copy = mc;
475
+ mythrow(nlopt_add_inequality_constraint(o, myfunc, d, tol));
476
+ }
477
+ void add_equality_constraint(func f, void *f_data,
478
+ nlopt_munge md, nlopt_munge mc,
479
+ double tol=0) {
480
+ myfunc_data *d = alloc_and_init_myfunc_data();
481
+ d->f = f;
482
+ d->f_data = f_data;
483
+ d->munge_destroy = md;
484
+ d->munge_copy = mc;
485
+ mythrow(nlopt_add_equality_constraint(o, myfunc, d, tol));
486
+ }
487
+ void add_inequality_mconstraint(mfunc mf, void *f_data,
488
+ nlopt_munge md, nlopt_munge mc,
489
+ const std::vector<double> &tol) {
490
+ myfunc_data *d = alloc_and_init_myfunc_data();
491
+ d->mf = mf;
492
+ d->f_data = f_data;
493
+ d->munge_destroy = md;
494
+ d->munge_copy = mc;
495
+ mythrow(nlopt_add_inequality_mconstraint(o, static_cast<unsigned int>(tol.size()), mymfunc, d,
496
+ tol.empty() ? NULL : &tol[0]));
497
+ }
498
+ void add_equality_mconstraint(mfunc mf, void *f_data,
499
+ nlopt_munge md, nlopt_munge mc,
500
+ const std::vector<double> &tol) {
501
+ myfunc_data *d = alloc_and_init_myfunc_data();
502
+ d->mf = mf;
503
+ d->f_data = f_data;
504
+ d->munge_destroy = md;
505
+ d->munge_copy = mc;
506
+ mythrow(nlopt_add_equality_mconstraint(o, static_cast<unsigned int>(tol.size()), mymfunc, d,
507
+ tol.empty() ? NULL : &tol[0]));
508
+ }
509
+ void set_param(const char *name, double val) { mythrow(nlopt_set_param(o, name, val)); }
510
+ double get_param(const char *name, double defaultval) const { return nlopt_get_param(o, name, defaultval); }
511
+ bool has_param(const char *name) const { return bool(nlopt_has_param(o, name)); }
512
+ const char *nth_param(unsigned n) const { return nlopt_nth_param(o, n); }
513
+ unsigned num_params() const { return nlopt_num_params(o); }
514
+ #define NLOPT_GETSET_VEC(name) \
515
+ void set_##name(double val) { \
516
+ mythrow(nlopt_set_##name##1(o, val)); \
517
+ } \
518
+ void get_##name(std::vector<double> &v) const { \
519
+ if (o && nlopt_get_dimension(o) != v.size()) \
520
+ throw std::invalid_argument("dimension mismatch"); \
521
+ mythrow(nlopt_get_##name(o, v.empty() ? NULL : &v[0])); \
522
+ } \
523
+ std::vector<double> get_##name() const { \
524
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt"); \
525
+ std::vector<double> v(nlopt_get_dimension(o)); \
526
+ get_##name(v); \
527
+ return v; \
528
+ } \
529
+ void set_##name(const std::vector<double> &v) { \
530
+ if (o && nlopt_get_dimension(o) != v.size()) \
531
+ throw std::invalid_argument("dimension mismatch"); \
532
+ mythrow(nlopt_set_##name(o, v.empty() ? NULL : &v[0])); \
533
+ }
534
+ NLOPT_GETSET_VEC(lower_bounds)
535
+ NLOPT_GETSET_VEC(upper_bounds)
536
+ // stopping criteria:
537
+ #define NLOPT_GETSET(T, name) \
538
+ T get_##name() const { \
539
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt"); \
540
+ return nlopt_get_##name(o); \
541
+ } \
542
+ void set_##name(T name) { \
543
+ mythrow(nlopt_set_##name(o, name)); \
544
+ }
545
+ NLOPT_GETSET(double, stopval)
546
+ NLOPT_GETSET(double, ftol_rel)
547
+ NLOPT_GETSET(double, ftol_abs)
548
+ NLOPT_GETSET(double, xtol_rel)
549
+ NLOPT_GETSET_VEC(xtol_abs)
550
+ NLOPT_GETSET_VEC(x_weights)
551
+ NLOPT_GETSET(int, maxeval)
552
+ int get_numevals() const {
553
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt");
554
+ return nlopt_get_numevals(o);
555
+ }
556
+ NLOPT_GETSET(double, maxtime)
557
+ NLOPT_GETSET(int, force_stop)
558
+ void force_stop() { set_force_stop(1); }
559
+ const char *get_errmsg() const {
560
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt");
561
+ return nlopt_get_errmsg(o);
562
+ }
563
+ // algorithm-specific parameters:
564
+ void set_local_optimizer(const opt &lo) {
565
+ nlopt_result ret = nlopt_set_local_optimizer(o, lo.o);
566
+ mythrow(ret);
567
+ }
568
+ NLOPT_GETSET(unsigned, population)
569
+ NLOPT_GETSET(unsigned, vector_storage)
570
+ NLOPT_GETSET_VEC(initial_step)
571
+ void set_default_initial_step(const std::vector<double> &x) {
572
+ nlopt_result ret
573
+ = nlopt_set_default_initial_step(o, x.empty() ? NULL : &x[0]);
574
+ mythrow(ret);
575
+ }
576
+ void get_initial_step(const std::vector<double> &x, std::vector<double> &dx) const {
577
+ if (o && (nlopt_get_dimension(o) != x.size()
578
+ || nlopt_get_dimension(o) != dx.size()))
579
+ throw std::invalid_argument("dimension mismatch");
580
+ nlopt_result ret = nlopt_get_initial_step(o, x.empty() ? NULL : &x[0],
581
+ dx.empty() ? NULL : &dx[0]);
582
+ mythrow(ret);
583
+ }
584
+ std::vector<double> get_initial_step_(const std::vector<double> &x) const {
585
+ if (!o) throw std::runtime_error("uninitialized nlopt::opt");
586
+ std::vector<double> v(nlopt_get_dimension(o));
587
+ get_initial_step(x, v);
588
+ return v;
589
+ }
590
+ };
591
+ #undef NLOPT_GETSET
592
+ #undef NLOPT_GETSET_VEC
593
+ //////////////////////////////////////////////////////////////////////
594
+ inline void srand(unsigned long seed) { nlopt_srand(seed); }
595
+ inline void srand_time() { nlopt_srand_time(); }
596
+ inline void version(int &major, int &minor, int &bugfix) {
597
+ nlopt_version(&major, &minor, &bugfix);
598
+ }
599
+ inline int version_major() {
600
+ int major, minor, bugfix;
601
+ nlopt_version(&major, &minor, &bugfix);
602
+ return major;
603
+ }
604
+ inline int version_minor() {
605
+ int major, minor, bugfix;
606
+ nlopt_version(&major, &minor, &bugfix);
607
+ return minor;
608
+ }
609
+ inline int version_bugfix() {
610
+ int major, minor, bugfix;
611
+ nlopt_version(&major, &minor, &bugfix);
612
+ return bugfix;
613
+ }
614
+ inline const char *algorithm_name(algorithm a) {
615
+ return nlopt_algorithm_name(nlopt_algorithm(a));
616
+ }
617
+ //////////////////////////////////////////////////////////////////////
618
+ } // namespace nlopt
619
+ #endif /* NLOPT_HPP */