-
Notifications
You must be signed in to change notification settings - Fork 46
/
libnlopt.jl
418 lines (324 loc) · 15.4 KB
/
libnlopt.jl
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Copyright (c) 2013: Steven G. Johnson and contributors
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
#! format: off
using CEnum
# typedef double ( * nlopt_func ) ( unsigned n , const double * x , double * gradient , /* NULL if not needed */ void * func_data )
const nlopt_func = Ptr{Cvoid}
# typedef void ( * nlopt_mfunc ) ( unsigned m , double * result , unsigned n , const double * x , double * gradient , /* NULL if not needed */ void * func_data )
const nlopt_mfunc = Ptr{Cvoid}
# typedef void ( * nlopt_precond ) ( unsigned n , const double * x , const double * v , double * vpre , void * data )
const nlopt_precond = Ptr{Cvoid}
@cenum nlopt_algorithm::UInt32 begin
NLOPT_GN_DIRECT = 0
NLOPT_GN_DIRECT_L = 1
NLOPT_GN_DIRECT_L_RAND = 2
NLOPT_GN_DIRECT_NOSCAL = 3
NLOPT_GN_DIRECT_L_NOSCAL = 4
NLOPT_GN_DIRECT_L_RAND_NOSCAL = 5
NLOPT_GN_ORIG_DIRECT = 6
NLOPT_GN_ORIG_DIRECT_L = 7
NLOPT_GD_STOGO = 8
NLOPT_GD_STOGO_RAND = 9
NLOPT_LD_LBFGS_NOCEDAL = 10
NLOPT_LD_LBFGS = 11
NLOPT_LN_PRAXIS = 12
NLOPT_LD_VAR1 = 13
NLOPT_LD_VAR2 = 14
NLOPT_LD_TNEWTON = 15
NLOPT_LD_TNEWTON_RESTART = 16
NLOPT_LD_TNEWTON_PRECOND = 17
NLOPT_LD_TNEWTON_PRECOND_RESTART = 18
NLOPT_GN_CRS2_LM = 19
NLOPT_GN_MLSL = 20
NLOPT_GD_MLSL = 21
NLOPT_GN_MLSL_LDS = 22
NLOPT_GD_MLSL_LDS = 23
NLOPT_LD_MMA = 24
NLOPT_LN_COBYLA = 25
NLOPT_LN_NEWUOA = 26
NLOPT_LN_NEWUOA_BOUND = 27
NLOPT_LN_NELDERMEAD = 28
NLOPT_LN_SBPLX = 29
NLOPT_LN_AUGLAG = 30
NLOPT_LD_AUGLAG = 31
NLOPT_LN_AUGLAG_EQ = 32
NLOPT_LD_AUGLAG_EQ = 33
NLOPT_LN_BOBYQA = 34
NLOPT_GN_ISRES = 35
NLOPT_AUGLAG = 36
NLOPT_AUGLAG_EQ = 37
NLOPT_G_MLSL = 38
NLOPT_G_MLSL_LDS = 39
NLOPT_LD_SLSQP = 40
NLOPT_LD_CCSAQ = 41
NLOPT_GN_ESCH = 42
NLOPT_GN_AGS = 43
NLOPT_NUM_ALGORITHMS = 44
end
function nlopt_algorithm_name(a)
ccall((:nlopt_algorithm_name, libnlopt), Ptr{Cchar}, (nlopt_algorithm,), a)
end
function nlopt_algorithm_to_string(algorithm)
ccall((:nlopt_algorithm_to_string, libnlopt), Ptr{Cchar}, (nlopt_algorithm,), algorithm)
end
function nlopt_algorithm_from_string(name)
ccall((:nlopt_algorithm_from_string, libnlopt), nlopt_algorithm, (Ptr{Cchar},), name)
end
@cenum nlopt_result::Int32 begin
NLOPT_FAILURE = -1
NLOPT_INVALID_ARGS = -2
NLOPT_OUT_OF_MEMORY = -3
NLOPT_ROUNDOFF_LIMITED = -4
NLOPT_FORCED_STOP = -5
NLOPT_NUM_FAILURES = -6
NLOPT_SUCCESS = 1
NLOPT_STOPVAL_REACHED = 2
NLOPT_FTOL_REACHED = 3
NLOPT_XTOL_REACHED = 4
NLOPT_MAXEVAL_REACHED = 5
NLOPT_MAXTIME_REACHED = 6
NLOPT_NUM_RESULTS = 7
end
function nlopt_result_to_string(algorithm)
ccall((:nlopt_result_to_string, libnlopt), Ptr{Cchar}, (nlopt_result,), algorithm)
end
function nlopt_result_from_string(name)
ccall((:nlopt_result_from_string, libnlopt), nlopt_result, (Ptr{Cchar},), name)
end
function nlopt_srand(seed)
ccall((:nlopt_srand, libnlopt), Cvoid, (Culong,), seed)
end
function nlopt_srand_time()
ccall((:nlopt_srand_time, libnlopt), Cvoid, ())
end
function nlopt_version(major, minor, bugfix)
ccall((:nlopt_version, libnlopt), Cvoid, (Ptr{Cint}, Ptr{Cint}, Ptr{Cint}), major, minor, bugfix)
end
mutable struct nlopt_opt_s end
const nlopt_opt = Ptr{Cvoid}
function nlopt_create(algorithm, n)
ccall((:nlopt_create, libnlopt), nlopt_opt, (nlopt_algorithm, Cuint), algorithm, n)
end
function nlopt_destroy(opt)
ccall((:nlopt_destroy, libnlopt), Cvoid, (nlopt_opt,), opt)
end
function nlopt_copy(opt)
ccall((:nlopt_copy, libnlopt), nlopt_opt, (nlopt_opt,), opt)
end
function nlopt_optimize(opt, x, opt_f)
ccall((:nlopt_optimize, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}, Ptr{Cdouble}), opt, x, opt_f)
end
function nlopt_set_min_objective(opt, f, f_data)
ccall((:nlopt_set_min_objective, libnlopt), nlopt_result, (nlopt_opt, nlopt_func, Ptr{Cvoid}), opt, f, f_data)
end
function nlopt_set_max_objective(opt, f, f_data)
ccall((:nlopt_set_max_objective, libnlopt), nlopt_result, (nlopt_opt, nlopt_func, Ptr{Cvoid}), opt, f, f_data)
end
function nlopt_set_precond_min_objective(opt, f, pre, f_data)
ccall((:nlopt_set_precond_min_objective, libnlopt), nlopt_result, (nlopt_opt, nlopt_func, nlopt_precond, Ptr{Cvoid}), opt, f, pre, f_data)
end
function nlopt_set_precond_max_objective(opt, f, pre, f_data)
ccall((:nlopt_set_precond_max_objective, libnlopt), nlopt_result, (nlopt_opt, nlopt_func, nlopt_precond, Ptr{Cvoid}), opt, f, pre, f_data)
end
function nlopt_get_algorithm(opt)
ccall((:nlopt_get_algorithm, libnlopt), nlopt_algorithm, (nlopt_opt,), opt)
end
function nlopt_get_dimension(opt)
ccall((:nlopt_get_dimension, libnlopt), Cuint, (nlopt_opt,), opt)
end
function nlopt_get_errmsg(opt)
ccall((:nlopt_get_errmsg, libnlopt), Ptr{Cchar}, (nlopt_opt,), opt)
end
function nlopt_set_param(opt, name, val)
ccall((:nlopt_set_param, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cchar}, Cdouble), opt, name, val)
end
function nlopt_get_param(opt, name, defaultval)
ccall((:nlopt_get_param, libnlopt), Cdouble, (nlopt_opt, Ptr{Cchar}, Cdouble), opt, name, defaultval)
end
function nlopt_has_param(opt, name)
ccall((:nlopt_has_param, libnlopt), Cint, (nlopt_opt, Ptr{Cchar}), opt, name)
end
function nlopt_num_params(opt)
ccall((:nlopt_num_params, libnlopt), Cuint, (nlopt_opt,), opt)
end
function nlopt_nth_param(opt, n)
ccall((:nlopt_nth_param, libnlopt), Ptr{Cchar}, (nlopt_opt, Cuint), opt, n)
end
function nlopt_set_lower_bounds(opt, lb)
ccall((:nlopt_set_lower_bounds, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, lb)
end
function nlopt_set_lower_bounds1(opt, lb)
ccall((:nlopt_set_lower_bounds1, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, lb)
end
function nlopt_set_lower_bound(opt, i, lb)
ccall((:nlopt_set_lower_bound, libnlopt), nlopt_result, (nlopt_opt, Cint, Cdouble), opt, i, lb)
end
function nlopt_get_lower_bounds(opt, lb)
ccall((:nlopt_get_lower_bounds, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, lb)
end
function nlopt_set_upper_bounds(opt, ub)
ccall((:nlopt_set_upper_bounds, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, ub)
end
function nlopt_set_upper_bounds1(opt, ub)
ccall((:nlopt_set_upper_bounds1, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, ub)
end
function nlopt_set_upper_bound(opt, i, ub)
ccall((:nlopt_set_upper_bound, libnlopt), nlopt_result, (nlopt_opt, Cint, Cdouble), opt, i, ub)
end
function nlopt_get_upper_bounds(opt, ub)
ccall((:nlopt_get_upper_bounds, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, ub)
end
function nlopt_remove_inequality_constraints(opt)
ccall((:nlopt_remove_inequality_constraints, libnlopt), nlopt_result, (nlopt_opt,), opt)
end
function nlopt_add_inequality_constraint(opt, fc, fc_data, tol)
ccall((:nlopt_add_inequality_constraint, libnlopt), nlopt_result, (nlopt_opt, nlopt_func, Ptr{Cvoid}, Cdouble), opt, fc, fc_data, tol)
end
function nlopt_add_precond_inequality_constraint(opt, fc, pre, fc_data, tol)
ccall((:nlopt_add_precond_inequality_constraint, libnlopt), nlopt_result, (nlopt_opt, nlopt_func, nlopt_precond, Ptr{Cvoid}, Cdouble), opt, fc, pre, fc_data, tol)
end
function nlopt_add_inequality_mconstraint(opt, m, fc, fc_data, tol)
ccall((:nlopt_add_inequality_mconstraint, libnlopt), nlopt_result, (nlopt_opt, Cuint, nlopt_mfunc, Ptr{Cvoid}, Ptr{Cdouble}), opt, m, fc, fc_data, tol)
end
function nlopt_remove_equality_constraints(opt)
ccall((:nlopt_remove_equality_constraints, libnlopt), nlopt_result, (nlopt_opt,), opt)
end
function nlopt_add_equality_constraint(opt, h, h_data, tol)
ccall((:nlopt_add_equality_constraint, libnlopt), nlopt_result, (nlopt_opt, nlopt_func, Ptr{Cvoid}, Cdouble), opt, h, h_data, tol)
end
function nlopt_add_precond_equality_constraint(opt, h, pre, h_data, tol)
ccall((:nlopt_add_precond_equality_constraint, libnlopt), nlopt_result, (nlopt_opt, nlopt_func, nlopt_precond, Ptr{Cvoid}, Cdouble), opt, h, pre, h_data, tol)
end
function nlopt_add_equality_mconstraint(opt, m, h, h_data, tol)
ccall((:nlopt_add_equality_mconstraint, libnlopt), nlopt_result, (nlopt_opt, Cuint, nlopt_mfunc, Ptr{Cvoid}, Ptr{Cdouble}), opt, m, h, h_data, tol)
end
function nlopt_set_stopval(opt, stopval)
ccall((:nlopt_set_stopval, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, stopval)
end
function nlopt_get_stopval(opt)
ccall((:nlopt_get_stopval, libnlopt), Cdouble, (nlopt_opt,), opt)
end
function nlopt_set_ftol_rel(opt, tol)
ccall((:nlopt_set_ftol_rel, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, tol)
end
function nlopt_get_ftol_rel(opt)
ccall((:nlopt_get_ftol_rel, libnlopt), Cdouble, (nlopt_opt,), opt)
end
function nlopt_set_ftol_abs(opt, tol)
ccall((:nlopt_set_ftol_abs, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, tol)
end
function nlopt_get_ftol_abs(opt)
ccall((:nlopt_get_ftol_abs, libnlopt), Cdouble, (nlopt_opt,), opt)
end
function nlopt_set_xtol_rel(opt, tol)
ccall((:nlopt_set_xtol_rel, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, tol)
end
function nlopt_get_xtol_rel(opt)
ccall((:nlopt_get_xtol_rel, libnlopt), Cdouble, (nlopt_opt,), opt)
end
function nlopt_set_xtol_abs1(opt, tol)
ccall((:nlopt_set_xtol_abs1, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, tol)
end
function nlopt_set_xtol_abs(opt, tol)
ccall((:nlopt_set_xtol_abs, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, tol)
end
function nlopt_get_xtol_abs(opt, tol)
ccall((:nlopt_get_xtol_abs, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, tol)
end
function nlopt_set_x_weights1(opt, w)
ccall((:nlopt_set_x_weights1, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, w)
end
function nlopt_set_x_weights(opt, w)
ccall((:nlopt_set_x_weights, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, w)
end
function nlopt_get_x_weights(opt, w)
ccall((:nlopt_get_x_weights, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, w)
end
function nlopt_set_maxeval(opt, maxeval)
ccall((:nlopt_set_maxeval, libnlopt), nlopt_result, (nlopt_opt, Cint), opt, maxeval)
end
function nlopt_get_maxeval(opt)
ccall((:nlopt_get_maxeval, libnlopt), Cint, (nlopt_opt,), opt)
end
function nlopt_get_numevals(opt)
ccall((:nlopt_get_numevals, libnlopt), Cint, (nlopt_opt,), opt)
end
function nlopt_set_maxtime(opt, maxtime)
ccall((:nlopt_set_maxtime, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, maxtime)
end
function nlopt_get_maxtime(opt)
ccall((:nlopt_get_maxtime, libnlopt), Cdouble, (nlopt_opt,), opt)
end
function nlopt_force_stop(opt)
ccall((:nlopt_force_stop, libnlopt), nlopt_result, (nlopt_opt,), opt)
end
function nlopt_set_force_stop(opt, val)
ccall((:nlopt_set_force_stop, libnlopt), nlopt_result, (nlopt_opt, Cint), opt, val)
end
function nlopt_get_force_stop(opt)
ccall((:nlopt_get_force_stop, libnlopt), Cint, (nlopt_opt,), opt)
end
function nlopt_set_local_optimizer(opt, local_opt)
ccall((:nlopt_set_local_optimizer, libnlopt), nlopt_result, (nlopt_opt, nlopt_opt), opt, local_opt)
end
function nlopt_set_population(opt, pop)
ccall((:nlopt_set_population, libnlopt), nlopt_result, (nlopt_opt, Cuint), opt, pop)
end
function nlopt_get_population(opt)
ccall((:nlopt_get_population, libnlopt), Cuint, (nlopt_opt,), opt)
end
function nlopt_set_vector_storage(opt, dim)
ccall((:nlopt_set_vector_storage, libnlopt), nlopt_result, (nlopt_opt, Cuint), opt, dim)
end
function nlopt_get_vector_storage(opt)
ccall((:nlopt_get_vector_storage, libnlopt), Cuint, (nlopt_opt,), opt)
end
function nlopt_set_default_initial_step(opt, x)
ccall((:nlopt_set_default_initial_step, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, x)
end
function nlopt_set_initial_step(opt, dx)
ccall((:nlopt_set_initial_step, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}), opt, dx)
end
function nlopt_set_initial_step1(opt, dx)
ccall((:nlopt_set_initial_step1, libnlopt), nlopt_result, (nlopt_opt, Cdouble), opt, dx)
end
function nlopt_get_initial_step(opt, x, dx)
ccall((:nlopt_get_initial_step, libnlopt), nlopt_result, (nlopt_opt, Ptr{Cdouble}, Ptr{Cdouble}), opt, x, dx)
end
# typedef void * ( * nlopt_munge ) ( void * p )
const nlopt_munge = Ptr{Cvoid}
function nlopt_set_munge(opt, munge_on_destroy, munge_on_copy)
ccall((:nlopt_set_munge, libnlopt), Cvoid, (nlopt_opt, nlopt_munge, nlopt_munge), opt, munge_on_destroy, munge_on_copy)
end
# typedef void * ( * nlopt_munge2 ) ( void * p , void * data )
const nlopt_munge2 = Ptr{Cvoid}
function nlopt_munge_data(opt, munge, data)
ccall((:nlopt_munge_data, libnlopt), Cvoid, (nlopt_opt, nlopt_munge2, Ptr{Cvoid}), opt, munge, data)
end
# typedef double ( * nlopt_func_old ) ( int n , const double * x , double * gradient , /* NULL if not needed */ void * func_data )
const nlopt_func_old = Ptr{Cvoid}
function nlopt_minimize(algorithm, n, f, f_data, lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, maxeval, maxtime)
ccall((:nlopt_minimize, libnlopt), nlopt_result, (nlopt_algorithm, Cint, nlopt_func_old, Ptr{Cvoid}, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}, Cdouble, Cdouble, Cdouble, Cdouble, Ptr{Cdouble}, Cint, Cdouble), algorithm, n, f, f_data, lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, maxeval, maxtime)
end
function nlopt_minimize_constrained(algorithm, n, f, f_data, m, fc, fc_data, fc_datum_size, lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, maxeval, maxtime)
ccall((:nlopt_minimize_constrained, libnlopt), nlopt_result, (nlopt_algorithm, Cint, nlopt_func_old, Ptr{Cvoid}, Cint, nlopt_func_old, Ptr{Cvoid}, Cptrdiff_t, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}, Cdouble, Cdouble, Cdouble, Cdouble, Ptr{Cdouble}, Cint, Cdouble), algorithm, n, f, f_data, m, fc, fc_data, fc_datum_size, lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, maxeval, maxtime)
end
function nlopt_minimize_econstrained(algorithm, n, f, f_data, m, fc, fc_data, fc_datum_size, p, h, h_data, h_datum_size, lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, htol_rel, htol_abs, maxeval, maxtime)
ccall((:nlopt_minimize_econstrained, libnlopt), nlopt_result, (nlopt_algorithm, Cint, nlopt_func_old, Ptr{Cvoid}, Cint, nlopt_func_old, Ptr{Cvoid}, Cptrdiff_t, Cint, nlopt_func_old, Ptr{Cvoid}, Cptrdiff_t, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}, Ptr{Cdouble}, Cdouble, Cdouble, Cdouble, Cdouble, Ptr{Cdouble}, Cdouble, Cdouble, Cint, Cdouble), algorithm, n, f, f_data, m, fc, fc_data, fc_datum_size, p, h, h_data, h_datum_size, lb, ub, x, minf, minf_max, ftol_rel, ftol_abs, xtol_rel, xtol_abs, htol_rel, htol_abs, maxeval, maxtime)
end
function nlopt_get_local_search_algorithm(deriv, nonderiv, maxeval)
ccall((:nlopt_get_local_search_algorithm, libnlopt), Cvoid, (Ptr{nlopt_algorithm}, Ptr{nlopt_algorithm}, Ptr{Cint}), deriv, nonderiv, maxeval)
end
function nlopt_set_local_search_algorithm(deriv, nonderiv, maxeval)
ccall((:nlopt_set_local_search_algorithm, libnlopt), Cvoid, (nlopt_algorithm, nlopt_algorithm, Cint), deriv, nonderiv, maxeval)
end
function nlopt_get_stochastic_population()
ccall((:nlopt_get_stochastic_population, libnlopt), Cint, ())
end
function nlopt_set_stochastic_population(pop)
ccall((:nlopt_set_stochastic_population, libnlopt), Cvoid, (Cint,), pop)
end
const NLOPT_MINF_MAX_REACHED = NLOPT_STOPVAL_REACHED
# Skipping MacroDefinition: NLOPT_DEPRECATED __attribute__ ( ( deprecated ) )