-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlopt_wrapper.nim
353 lines (331 loc) · 17 KB
/
nlopt_wrapper.nim
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
## 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.
##
{.deadCodeElim: on.}
const
libname* = "libnlopt.so"
## for now comment out NLOPT macros, since they are only needed for windows
## Change 0 to 1 to use stdcall convention under Win32
## #if 0 && (defined(_WIN32) || defined(__WIN32__))
## # if defined(__GNUC__)
## # define NLOPT_STDCALL __attribute__((stdcall))
## # elif defined(_MSC_VER) || defined(_ICC) || defined(_STDCALL_SUPPORTED)
## # define NLOPT_STDCALL __stdcall
## # else
## # define NLOPT_STDCALL
## # endif
## #else
## # define NLOPT_STDCALL
## #endif
## /\* for Windows compilers, you should add a line
## #define NLOPT_DLL
## when using NLopt from a DLL, in order to do the proper
## Windows importing nonsense. *\/
## #if defined(NLOPT_DLL) && (defined(_WIN32) || defined(__WIN32__)) && !defined(__LCC__)
## /\* annoying Windows syntax for calling functions in a DLL *\/
## # if defined(NLOPT_DLL_EXPORT)
## # define NLOPT_EXTERN(T) extern __declspec(dllexport) T NLOPT_STDCALL
## # else
## # define NLOPT_EXTERN(T) extern __declspec(dllimport) T NLOPT_STDCALL
## # endif
## #else
## # define NLOPT_EXTERN(T) extern T NLOPT_STDCALL
## #endif
type
nlopt_func* = proc (n: cuint; x: ptr cdouble; gradient: ptr cdouble; func_data: pointer): cdouble {.cdecl.} ## NULL if not needed
nlopt_mfunc* = proc (m: cuint; result: ptr cdouble; n: cuint; x: ptr cdouble;
gradient: ptr cdouble; func_data: pointer) {.cdecl.} ## NULL if not needed
## A preconditioner, which preconditions v at x to return vpre.
## (The meaning of "preconditioning" is algorithm-dependent.)
type
## Naming conventions:
##
## NLOPT_{G/L}{D/N}_*
## = global/local derivative/no-derivative optimization,
## respectively
##
## _RAND algorithms involve some randomization.
##
## _NOSCAL algorithms are *not* scaled to a unit hypercube
## (i.e. they are sensitive to the units of x)
##
nlopt_precond* = proc (n: cuint; x: ptr cdouble; v: ptr cdouble; vpre: ptr cdouble; data: pointer) {.cdecl.}
nlopt_algorithm* {.size: sizeof(cint).} = enum
GN_DIRECT = 0,
GN_DIRECT_L,
GN_DIRECT_L_RAND,
GN_DIRECT_NOSCAL,
GN_DIRECT_L_NOSCAL,
GN_DIRECT_L_RAND_NOSCAL,
GN_ORIG_DIRECT,
GN_ORIG_DIRECT_L,
GD_STOGO,
GD_STOGO_RAND,
LD_LBFGS_NOCEDAL,
LD_LBFGS,
LN_PRAXIS,
LD_VAR1,
LD_VAR2,
LD_TNEWTON,
LD_TNEWTON_RESTART,
LD_TNEWTON_PRECOND,
LD_TNEWTON_PRECOND_RESTART,
GN_CRS2_LM,
GN_MLSL,
GD_MLSL,
GN_MLSL_LDS,
GD_MLSL_LDS,
LD_MMA,
LN_COBYLA,
LN_NEWUOA,
LN_NEWUOA_BOUND,
LN_NELDERMEAD,
LN_SBPLX,
LN_AUGLAG,
LD_AUGLAG,
LN_AUGLAG_EQ,
LD_AUGLAG_EQ,
LN_BOBYQA,
GN_ISRES, ## new variants that require local_optimizer to be set,
## not with older constants for backwards compatibility
AUGLAG,
AUGLAG_EQ,
G_MLSL,
G_MLSL_LDS,
LD_SLSQP,
LD_CCSAQ,
GN_ESCH,
NUM_ALGORITHMS ## not an algorithm, just the number of them
proc nlopt_algorithm_name*(a: nlopt_algorithm): cstring {.cdecl,
importc: "nlopt_algorithm_name", dynlib: libname.}
type
nlopt_result* {.size: sizeof(cint).} = enum
NLOPT_FORCED_STOP = - 5, NLOPT_ROUNDOFF_LIMITED = - 4, NLOPT_OUT_OF_MEMORY = - 3,
NLOPT_INVALID_ARGS = - 2, NLOPT_FAILURE = - 1, ## generic failure code
NLOPT_SUCCESS = 1, ## generic success code
NLOPT_STOPVAL_REACHED = 2, NLOPT_FTOL_REACHED = 3, NLOPT_XTOL_REACHED = 4,
NLOPT_MAXEVAL_REACHED = 5, NLOPT_MAXTIME_REACHED = 6
const
NLOPT_MINF_MAX_REACHED* = NLOPT_STOPVAL_REACHED
proc nlopt_srand*(seed: culong) {.cdecl, importc: "nlopt_srand", dynlib: libname.}
proc nlopt_srand_time*() {.cdecl, importc: "nlopt_srand_time", dynlib: libname.}
proc nlopt_version*(major: ptr cint; minor: ptr cint; bugfix: ptr cint) {.cdecl,
importc: "nlopt_version", dynlib: libname.}
## ************************** OBJECT-ORIENTED API *************************
## The style here is that we create an nlopt_opt "object" (an opaque pointer),
## then set various optimization parameters, and then execute the
## algorithm. In this way, we can add more and more optimization parameters
## (including algorithm-specific ones) without breaking backwards
## compatibility, having functions with zillions of parameters, or
## relying non-reentrantly on global variables.
type
nlopt_opt_s* = object
## opaque structure, defined internally
type
nlopt_opt* = ptr nlopt_opt_s
## the only immutable parameters of an optimization are the algorithm and
## the dimension n of the problem, since changing either of these could
## have side-effects on lots of other parameters
proc nlopt_create*(algorithm: nlopt_algorithm; n: cuint): nlopt_opt {.cdecl,
importc: "nlopt_create", dynlib: libname.}
proc nlopt_destroy*(opt: nlopt_opt) {.cdecl, importc: "nlopt_destroy", dynlib: libname.}
proc nlopt_copy*(opt: nlopt_opt): nlopt_opt {.cdecl, importc: "nlopt_copy",
dynlib: libname.}
proc nlopt_optimize*(opt: nlopt_opt; x: ptr cdouble; opt_f: ptr cdouble): nlopt_result {.
cdecl, importc: "nlopt_optimize", dynlib: libname.}
proc nlopt_set_min_objective*(opt: nlopt_opt; f: nlopt_func; f_data: pointer): nlopt_result {.
cdecl, importc: "nlopt_set_min_objective", dynlib: libname.}
proc nlopt_set_max_objective*(opt: nlopt_opt; f: nlopt_func; f_data: pointer): nlopt_result {.
cdecl, importc: "nlopt_set_max_objective", dynlib: libname.}
proc nlopt_set_precond_min_objective*(opt: nlopt_opt; f: nlopt_func;
pre: nlopt_precond; f_data: pointer): nlopt_result {.
cdecl, importc: "nlopt_set_precond_min_objective", dynlib: libname.}
proc nlopt_set_precond_max_objective*(opt: nlopt_opt; f: nlopt_func;
pre: nlopt_precond; f_data: pointer): nlopt_result {.
cdecl, importc: "nlopt_set_precond_max_objective", dynlib: libname.}
proc nlopt_get_algorithm*(opt: nlopt_opt): nlopt_algorithm {.cdecl,
importc: "nlopt_get_algorithm", dynlib: libname.}
proc nlopt_get_dimension*(opt: nlopt_opt): cuint {.cdecl,
importc: "nlopt_get_dimension", dynlib: libname.}
proc nlopt_get_errmsg*(opt: nlopt_opt): cstring {.cdecl, importc: "nlopt_get_errmsg",
dynlib: libname.}
## constraints:
proc nlopt_set_lower_bounds*(opt: nlopt_opt; lb: ptr cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_lower_bounds", dynlib: libname.}
proc nlopt_set_lower_bounds1*(opt: nlopt_opt; lb: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_lower_bounds1", dynlib: libname.}
proc nlopt_get_lower_bounds*(opt: nlopt_opt; lb: ptr cdouble): nlopt_result {.cdecl,
importc: "nlopt_get_lower_bounds", dynlib: libname.}
proc nlopt_set_upper_bounds*(opt: nlopt_opt; ub: ptr cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_upper_bounds", dynlib: libname.}
proc nlopt_set_upper_bounds1*(opt: nlopt_opt; ub: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_upper_bounds1", dynlib: libname.}
proc nlopt_get_upper_bounds*(opt: nlopt_opt; ub: ptr cdouble): nlopt_result {.cdecl,
importc: "nlopt_get_upper_bounds", dynlib: libname.}
proc nlopt_remove_inequality_constraints*(opt: nlopt_opt): nlopt_result {.cdecl,
importc: "nlopt_remove_inequality_constraints", dynlib: libname.}
proc nlopt_add_inequality_constraint*(opt: nlopt_opt; fc: nlopt_func;
fc_data: pointer; tol: cdouble): nlopt_result {.
cdecl, importc: "nlopt_add_inequality_constraint", dynlib: libname.}
proc nlopt_add_precond_inequality_constraint*(opt: nlopt_opt; fc: nlopt_func;
pre: nlopt_precond; fc_data: pointer; tol: cdouble): nlopt_result {.cdecl,
importc: "nlopt_add_precond_inequality_constraint", dynlib: libname.}
proc nlopt_add_inequality_mconstraint*(opt: nlopt_opt; m: cuint; fc: nlopt_mfunc;
fc_data: pointer; tol: ptr cdouble): nlopt_result {.
cdecl, importc: "nlopt_add_inequality_mconstraint", dynlib: libname.}
proc nlopt_remove_equality_constraints*(opt: nlopt_opt): nlopt_result {.cdecl,
importc: "nlopt_remove_equality_constraints", dynlib: libname.}
proc nlopt_add_equality_constraint*(opt: nlopt_opt; h: nlopt_func; h_data: pointer;
tol: cdouble): nlopt_result {.cdecl,
importc: "nlopt_add_equality_constraint", dynlib: libname.}
proc nlopt_add_precond_equality_constraint*(opt: nlopt_opt; h: nlopt_func;
pre: nlopt_precond; h_data: pointer; tol: cdouble): nlopt_result {.cdecl,
importc: "nlopt_add_precond_equality_constraint", dynlib: libname.}
proc nlopt_add_equality_mconstraint*(opt: nlopt_opt; m: cuint; h: nlopt_mfunc;
h_data: pointer; tol: ptr cdouble): nlopt_result {.
cdecl, importc: "nlopt_add_equality_mconstraint", dynlib: libname.}
## stopping criteria:
proc nlopt_set_stopval*(opt: nlopt_opt; stopval: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_stopval", dynlib: libname.}
proc nlopt_get_stopval*(opt: nlopt_opt): cdouble {.cdecl,
importc: "nlopt_get_stopval", dynlib: libname.}
proc nlopt_set_ftol_rel*(opt: nlopt_opt; tol: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_ftol_rel", dynlib: libname.}
proc nlopt_get_ftol_rel*(opt: nlopt_opt): cdouble {.cdecl,
importc: "nlopt_get_ftol_rel", dynlib: libname.}
proc nlopt_set_ftol_abs*(opt: nlopt_opt; tol: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_ftol_abs", dynlib: libname.}
proc nlopt_get_ftol_abs*(opt: nlopt_opt): cdouble {.cdecl,
importc: "nlopt_get_ftol_abs", dynlib: libname.}
proc nlopt_set_xtol_rel*(opt: nlopt_opt; tol: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_xtol_rel", dynlib: libname.}
proc nlopt_get_xtol_rel*(opt: nlopt_opt): cdouble {.cdecl,
importc: "nlopt_get_xtol_rel", dynlib: libname.}
proc nlopt_set_xtol_abs1*(opt: nlopt_opt; tol: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_xtol_abs1", dynlib: libname.}
proc nlopt_set_xtol_abs*(opt: nlopt_opt; tol: ptr cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_xtol_abs", dynlib: libname.}
proc nlopt_get_xtol_abs*(opt: nlopt_opt; tol: ptr cdouble): nlopt_result {.cdecl,
importc: "nlopt_get_xtol_abs", dynlib: libname.}
proc nlopt_set_maxeval*(opt: nlopt_opt; maxeval: cint): nlopt_result {.cdecl,
importc: "nlopt_set_maxeval", dynlib: libname.}
proc nlopt_get_maxeval*(opt: nlopt_opt): cint {.cdecl, importc: "nlopt_get_maxeval",
dynlib: libname.}
proc nlopt_get_numevals*(opt: nlopt_opt): cint {.cdecl,
importc: "nlopt_get_numevals", dynlib: libname.}
proc nlopt_set_maxtime*(opt: nlopt_opt; maxtime: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_maxtime", dynlib: libname.}
proc nlopt_get_maxtime*(opt: nlopt_opt): cdouble {.cdecl,
importc: "nlopt_get_maxtime", dynlib: libname.}
proc nlopt_force_stop*(opt: nlopt_opt): nlopt_result {.cdecl,
importc: "nlopt_force_stop", dynlib: libname.}
proc nlopt_set_force_stop*(opt: nlopt_opt; val: cint): nlopt_result {.cdecl,
importc: "nlopt_set_force_stop", dynlib: libname.}
proc nlopt_get_force_stop*(opt: nlopt_opt): cint {.cdecl,
importc: "nlopt_get_force_stop", dynlib: libname.}
## more algorithm-specific parameters
proc nlopt_set_local_optimizer*(opt: nlopt_opt; local_opt: nlopt_opt): nlopt_result {.
cdecl, importc: "nlopt_set_local_optimizer", dynlib: libname.}
proc nlopt_set_population*(opt: nlopt_opt; pop: cuint): nlopt_result {.cdecl,
importc: "nlopt_set_population", dynlib: libname.}
proc nlopt_get_population*(opt: nlopt_opt): cuint {.cdecl,
importc: "nlopt_get_population", dynlib: libname.}
proc nlopt_set_vector_storage*(opt: nlopt_opt; dim: cuint): nlopt_result {.cdecl,
importc: "nlopt_set_vector_storage", dynlib: libname.}
proc nlopt_get_vector_storage*(opt: nlopt_opt): cuint {.cdecl,
importc: "nlopt_get_vector_storage", dynlib: libname.}
proc nlopt_set_default_initial_step*(opt: nlopt_opt; x: ptr cdouble): nlopt_result {.
cdecl, importc: "nlopt_set_default_initial_step", dynlib: libname.}
proc nlopt_set_initial_step*(opt: nlopt_opt; dx: ptr cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_initial_step", dynlib: libname.}
proc nlopt_set_initial_step1*(opt: nlopt_opt; dx: cdouble): nlopt_result {.cdecl,
importc: "nlopt_set_initial_step1", dynlib: libname.}
proc nlopt_get_initial_step*(opt: nlopt_opt; x: ptr cdouble; dx: ptr cdouble): nlopt_result {.
cdecl, importc: "nlopt_get_initial_step", dynlib: libname.}
## the following are functions mainly designed to be used internally
## by the Fortran and SWIG wrappers, allow us to tel nlopt_destroy and
## nlopt_copy to do something to the f_data pointers (e.g. free or
## duplicate them, respectively)
type
nlopt_munge* = proc (p: pointer): pointer {.cdecl.}
proc nlopt_set_munge*(opt: nlopt_opt; munge_on_destroy: nlopt_munge;
munge_on_copy: nlopt_munge) {.cdecl,
importc: "nlopt_set_munge", dynlib: libname.}
type
nlopt_munge2* = proc (p: pointer; data: pointer): pointer {.cdecl.}
proc nlopt_munge_data*(opt: nlopt_opt; munge: nlopt_munge2; data: pointer) {.cdecl,
importc: "nlopt_munge_data", dynlib: libname.}
## The deprecated API is not supported. No need for it.
## ************************** DEPRECATED API *************************
## The new "object-oriented" API is preferred, since it allows us to
## gracefully add new features and algorithm-specific options in a
## re-entrant way, and we can automatically assume reasonable defaults
## for unspecified parameters.
## Where possible (e.g. for gcc >= 3.1), enable a compiler warning
## for code that uses a deprecated function
## #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__==3 && __GNUC_MINOR__ > 0))
## # define NLOPT_DEPRECATED __attribute__((deprecated))
## #else
## # define NLOPT_DEPRECATED
## #endif
## typedef double (*nlopt_func_old)(int n, const double *x,
## double *gradient, /\* NULL if not needed *\/
## void *func_data);
## nlopt_result nlopt_minimize(
## nlopt_algorithm algorithm,
## int n, nlopt_func_old f, void *f_data,
## const double *lb, const double *ub, /\* bounds *\/
## double *x, /\* in: initial guess, out: minimizer *\/
## double *minf, /\* out: minimum *\/
## double minf_max, double ftol_rel, double ftol_abs,
## double xtol_rel, const double *xtol_abs,
## int maxeval, double maxtime) NLOPT_DEPRECATED;
## nlopt_result nlopt_minimize_constrained(
## nlopt_algorithm algorithm,
## int n, nlopt_func_old f, void *f_data,
## int m, nlopt_func_old fc, void *fc_data, ptrdiff_t fc_datum_size,
## const double *lb, const double *ub, /\* bounds *\/
## double *x, /\* in: initial guess, out: minimizer *\/
## double *minf, /\* out: minimum *\/
## double minf_max, double ftol_rel, double ftol_abs,
## double xtol_rel, const double *xtol_abs,
## int maxeval, double maxtime) NLOPT_DEPRECATED;
## nlopt_result nlopt_minimize_econstrained(
## nlopt_algorithm algorithm,
## int n, nlopt_func_old f, void *f_data,
## int m, nlopt_func_old fc, void *fc_data, ptrdiff_t fc_datum_size,
## int p, nlopt_func_old h, void *h_data, ptrdiff_t h_datum_size,
## const double *lb, const double *ub, /\* bounds *\/
## double *x, /\* in: initial guess, out: minimizer *\/
## double *minf, /\* out: minimum *\/
## double minf_max, double ftol_rel, double ftol_abs,
## double xtol_rel, const double *xtol_abs,
## double htol_rel, double htol_abs,
## int maxeval, double maxtime) NLOPT_DEPRECATED;
## void nlopt_get_local_search_algorithm(nlopt_algorithm *deriv,
## nlopt_algorithm *nonderiv,
## int *maxeval) NLOPT_DEPRECATED;
## void nlopt_set_local_search_algorithm(nlopt_algorithm deriv,
## nlopt_algorithm nonderiv,
## int maxeval) NLOPT_DEPRECATED;
## int nlopt_get_stochastic_population(void) NLOPT_DEPRECATED;
## void nlopt_set_stochastic_population(int pop) NLOPT_DEPRECATED;
## *******************************************************************