1 | using System;
|
---|
2 | using System.Runtime.InteropServices;
|
---|
3 |
|
---|
4 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
5 | public static class NLOpt {
|
---|
6 | public delegate double nlopt_func(uint n,
|
---|
7 | [In] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x, // double*
|
---|
8 | [In][Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] gradient, /* double* NULL if not needed */
|
---|
9 | IntPtr func_data);
|
---|
10 |
|
---|
11 | public delegate IntPtr nlopt_mfunc(uint m,
|
---|
12 | [Out][MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] result,
|
---|
13 | uint n,
|
---|
14 | [In][MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] double[] x,
|
---|
15 | ref double[] gradient, /* NULL if not needed */ // TODO: we need manual marshalling here (array is of length n*m) is stored in ∂c_i/∂x_j = grad[i*n + j]
|
---|
16 | IntPtr func_data);
|
---|
17 |
|
---|
18 | /* A preconditioner, which preconditions v at x to return vpre.
|
---|
19 | (The meaning of "preconditioning" is algorithm-dependent.) */
|
---|
20 | public delegate IntPtr nlopt_precond(uint n,
|
---|
21 | [In] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x,
|
---|
22 | [In] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] v,
|
---|
23 | [In][Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] vpre,
|
---|
24 | IntPtr data);
|
---|
25 |
|
---|
26 | public enum nlopt_algorithm {
|
---|
27 | /* Naming conventions:
|
---|
28 |
|
---|
29 | NLOPT_{G/L}{D/N}_*
|
---|
30 | = global/local derivative/no-derivative optimization,
|
---|
31 | respectively
|
---|
32 |
|
---|
33 | *_RAND algorithms involve some randomization.
|
---|
34 |
|
---|
35 | *_NOSCAL algorithms are *not* scaled to a unit hypercube
|
---|
36 | (i.e. they are sensitive to the units of x)
|
---|
37 | */
|
---|
38 |
|
---|
39 | NLOPT_GN_DIRECT = 0,
|
---|
40 | NLOPT_GN_DIRECT_L,
|
---|
41 | NLOPT_GN_DIRECT_L_RAND,
|
---|
42 | NLOPT_GN_DIRECT_NOSCAL,
|
---|
43 | NLOPT_GN_DIRECT_L_NOSCAL,
|
---|
44 | NLOPT_GN_DIRECT_L_RAND_NOSCAL,
|
---|
45 |
|
---|
46 | NLOPT_GN_ORIG_DIRECT,
|
---|
47 | NLOPT_GN_ORIG_DIRECT_L,
|
---|
48 |
|
---|
49 | NLOPT_GD_STOGO,
|
---|
50 | NLOPT_GD_STOGO_RAND,
|
---|
51 |
|
---|
52 | NLOPT_LD_LBFGS_NOCEDAL,
|
---|
53 |
|
---|
54 | NLOPT_LD_LBFGS,
|
---|
55 |
|
---|
56 | NLOPT_LN_PRAXIS,
|
---|
57 |
|
---|
58 | NLOPT_LD_VAR1,
|
---|
59 | NLOPT_LD_VAR2,
|
---|
60 |
|
---|
61 | NLOPT_LD_TNEWTON,
|
---|
62 | NLOPT_LD_TNEWTON_RESTART,
|
---|
63 | NLOPT_LD_TNEWTON_PRECOND,
|
---|
64 | NLOPT_LD_TNEWTON_PRECOND_RESTART,
|
---|
65 |
|
---|
66 | NLOPT_GN_CRS2_LM,
|
---|
67 |
|
---|
68 | NLOPT_GN_MLSL,
|
---|
69 | NLOPT_GD_MLSL,
|
---|
70 | NLOPT_GN_MLSL_LDS,
|
---|
71 | NLOPT_GD_MLSL_LDS,
|
---|
72 |
|
---|
73 | NLOPT_LD_MMA,
|
---|
74 |
|
---|
75 | NLOPT_LN_COBYLA,
|
---|
76 |
|
---|
77 | NLOPT_LN_NEWUOA,
|
---|
78 | NLOPT_LN_NEWUOA_BOUND,
|
---|
79 |
|
---|
80 | NLOPT_LN_NELDERMEAD,
|
---|
81 | NLOPT_LN_SBPLX,
|
---|
82 |
|
---|
83 | NLOPT_LN_AUGLAG,
|
---|
84 | NLOPT_LD_AUGLAG,
|
---|
85 | NLOPT_LN_AUGLAG_EQ,
|
---|
86 | NLOPT_LD_AUGLAG_EQ,
|
---|
87 |
|
---|
88 | NLOPT_LN_BOBYQA,
|
---|
89 |
|
---|
90 | NLOPT_GN_ISRES,
|
---|
91 |
|
---|
92 | /* new variants that require local_optimizer to be set,
|
---|
93 | not with older constants for backwards compatibility */
|
---|
94 | NLOPT_AUGLAG,
|
---|
95 | NLOPT_AUGLAG_EQ,
|
---|
96 | NLOPT_G_MLSL,
|
---|
97 | NLOPT_G_MLSL_LDS,
|
---|
98 |
|
---|
99 | NLOPT_LD_SLSQP,
|
---|
100 |
|
---|
101 | NLOPT_LD_CCSAQ,
|
---|
102 |
|
---|
103 | NLOPT_GN_ESCH,
|
---|
104 |
|
---|
105 | NLOPT_GN_AGS,
|
---|
106 |
|
---|
107 | NLOPT_NUM_ALGORITHMS /* not an algorithm, just the number of them */
|
---|
108 | };
|
---|
109 |
|
---|
110 | public enum nlopt_result {
|
---|
111 | NLOPT_FAILURE = -1, /* generic failure code */
|
---|
112 | NLOPT_INVALID_ARGS = -2,
|
---|
113 | NLOPT_OUT_OF_MEMORY = -3,
|
---|
114 | NLOPT_ROUNDOFF_LIMITED = -4,
|
---|
115 | NLOPT_FORCED_STOP = -5,
|
---|
116 | NLOPT_SUCCESS = 1, /* generic success code */
|
---|
117 | NLOPT_STOPVAL_REACHED = 2,
|
---|
118 | NLOPT_FTOL_REACHED = 3,
|
---|
119 | NLOPT_XTOL_REACHED = 4,
|
---|
120 | NLOPT_MAXEVAL_REACHED = 5,
|
---|
121 | NLOPT_MAXTIME_REACHED = 6
|
---|
122 | };
|
---|
123 |
|
---|
124 |
|
---|
125 | #region x86
|
---|
126 | [DllImport("nlopt_x86.dll", EntryPoint = "nlopt_version", CallingConvention = CallingConvention.Cdecl)]
|
---|
127 | public static extern IntPtr nlopt_version_x86(ref int major, ref int minor, ref int bugfix);
|
---|
128 |
|
---|
129 |
|
---|
130 | #endregion
|
---|
131 |
|
---|
132 | #region x64
|
---|
133 | [DllImport("nlopt_x64.dll", EntryPoint = "nlopt_version", CallingConvention = CallingConvention.Cdecl)]
|
---|
134 | public static extern IntPtr nlopt_version_x64(ref int major, ref int minor, ref int bugfix);
|
---|
135 |
|
---|
136 |
|
---|
137 | /*************************** OBJECT-ORIENTED API **************************/
|
---|
138 | /* The style here is that we create an nlopt_opt "object" (an opaque pointer),
|
---|
139 | then set various optimization parameters, and then execute the
|
---|
140 | algorithm. In this way, we can add more and more optimization parameters
|
---|
141 | (including algorithm-specific ones) without breaking backwards
|
---|
142 | compatibility, having functions with zillions of parameters, or
|
---|
143 | relying non-reentrantly on global variables.*/
|
---|
144 |
|
---|
145 |
|
---|
146 | /* the only immutable parameters of an optimization are the algorithm and
|
---|
147 | the dimension n of the problem, since changing either of these could
|
---|
148 | have side-effects on lots of other parameters */
|
---|
149 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
150 | public static extern IntPtr nlopt_create(nlopt_algorithm algorithm, uint n); // returns nlopt_opt
|
---|
151 |
|
---|
152 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
153 | public static extern IntPtr nlopt_destroy(IntPtr nlopt_opt);
|
---|
154 |
|
---|
155 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
156 | public static extern IntPtr /*nlopt_opt*/ nlopt_copy([In] IntPtr nlopt_opt);
|
---|
157 |
|
---|
158 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
159 | public static extern nlopt_result nlopt_optimize(IntPtr nlopt_opt,
|
---|
160 | [In][Out] double[] x,
|
---|
161 | ref double opt_f); // opt_f is optimal value
|
---|
162 |
|
---|
163 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
164 | public static extern nlopt_result nlopt_set_min_objective(IntPtr nlopt_opt,
|
---|
165 | [MarshalAs(UnmanagedType.FunctionPtr)] nlopt_func f,
|
---|
166 | IntPtr f_data);
|
---|
167 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
168 | public static extern nlopt_result nlopt_set_max_objective(IntPtr nlopt_opt, nlopt_func f, IntPtr f_data);
|
---|
169 |
|
---|
170 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
171 | public static extern nlopt_result nlopt_set_precond_min_objective(IntPtr nlopt_opt, nlopt_func f, nlopt_precond pre, IntPtr f_data); // only used by CCSAQ optimizer in NLOpt version 2.6.1
|
---|
172 |
|
---|
173 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
174 | public static extern nlopt_result nlopt_set_precond_max_objective(IntPtr nlopt_opt, nlopt_func f, nlopt_precond pre, IntPtr f_data); // only used by CCSAQ optimizer in NLOpt version 2.6.1
|
---|
175 |
|
---|
176 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
177 | public static extern nlopt_algorithm nlopt_get_algorithm([In] IntPtr nlopt_opt);
|
---|
178 |
|
---|
179 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
180 | public static extern uint nlopt_get_dimension([In] IntPtr nlopt_opt);
|
---|
181 |
|
---|
182 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
183 | public static extern string nlopt_get_errmsg(IntPtr nlopt_opt);
|
---|
184 |
|
---|
185 | /* constraints: */
|
---|
186 |
|
---|
187 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
188 | public static extern nlopt_result nlopt_set_lower_bounds(IntPtr nlopt_opt,
|
---|
189 | [In] double[] lb);
|
---|
190 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
191 | public static extern nlopt_result nlopt_set_lower_bounds1(IntPtr nlopt_opt, double lb);
|
---|
192 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
193 | public static extern nlopt_result nlopt_set_lower_bound(IntPtr nlopt_opt, int i, double lb);
|
---|
194 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
195 | public static extern nlopt_result nlopt_get_lower_bounds([In] IntPtr nlopt_opt,
|
---|
196 | IntPtr lb); // actually: double* TODO: manual marshalling
|
---|
197 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
198 | public static extern nlopt_result nlopt_set_upper_bounds(IntPtr nlopt_opt,
|
---|
199 | [In] double[] ub);
|
---|
200 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
201 | public static extern nlopt_result nlopt_set_upper_bounds1(IntPtr nlopt_opt, double ub);
|
---|
202 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
203 | public static extern nlopt_result nlopt_set_upper_bound(IntPtr nlopt_opt, int i, double ub);
|
---|
204 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
205 | public static extern nlopt_result nlopt_get_upper_bounds([In] IntPtr nlopt_opt,
|
---|
206 | IntPtr ub); // actually: double* TODO: manual marshalling
|
---|
207 |
|
---|
208 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
209 | public static extern nlopt_result nlopt_remove_inequality_constraints(IntPtr nlopt_opt);
|
---|
210 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
211 | public static extern nlopt_result nlopt_add_inequality_constraint(IntPtr nlopt_opt, nlopt_func fc, IntPtr fc_data, double tol);
|
---|
212 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
213 | public static extern nlopt_result nlopt_add_precond_inequality_constraint(IntPtr nlopt_opt, nlopt_func fc, nlopt_precond pre, IntPtr fc_data, double tol);
|
---|
214 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
215 | public static extern nlopt_result nlopt_add_inequality_mconstraint(IntPtr nlopt_opt, uint m, nlopt_mfunc fc, IntPtr fc_data, ref double[] tol);
|
---|
216 |
|
---|
217 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
218 | public static extern nlopt_result nlopt_remove_equality_constraints(IntPtr nlopt_opt);
|
---|
219 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
220 | public static extern nlopt_result nlopt_add_equality_constraint(IntPtr nlopt_opt, nlopt_func h, IntPtr h_data, double tol);
|
---|
221 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
222 | public static extern nlopt_result nlopt_add_precond_equality_constraint(IntPtr nlopt_opt, nlopt_func h, nlopt_precond pre, IntPtr h_data, double tol);
|
---|
223 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
224 | public static extern nlopt_result nlopt_add_equality_mconstraint(IntPtr nlopt_opt, uint m, nlopt_mfunc h, IntPtr h_data, ref double[] tol);
|
---|
225 |
|
---|
226 | /* stopping criteria: */
|
---|
227 |
|
---|
228 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
229 | public static extern nlopt_result nlopt_set_stopval(IntPtr nlopt_opt, double stopval);
|
---|
230 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
231 | public static extern double nlopt_get_stopval([In] IntPtr nlopt_opt);
|
---|
232 |
|
---|
233 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
234 | public static extern nlopt_result nlopt_set_ftol_rel(IntPtr nlopt_opt, double tol);
|
---|
235 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
236 | public static extern double nlopt_get_ftol_rel([In] IntPtr nlopt_opt);
|
---|
237 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
238 | public static extern nlopt_result nlopt_set_ftol_abs(IntPtr nlopt_opt, double tol);
|
---|
239 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
240 | public static extern double nlopt_get_ftol_abs([In] IntPtr nlopt_opt);
|
---|
241 |
|
---|
242 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
243 | public static extern nlopt_result nlopt_set_xtol_rel(IntPtr nlopt_opt, double tol);
|
---|
244 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
245 | public static extern double nlopt_get_xtol_rel([In] IntPtr nlopt_opt);
|
---|
246 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
247 | public static extern nlopt_result nlopt_set_xtol_abs1(IntPtr nlopt_opt, double tol);
|
---|
248 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
249 | public static extern nlopt_result nlopt_set_xtol_abs(IntPtr nlopt_opt,
|
---|
250 | [In] double[] tol);
|
---|
251 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
252 | public static extern nlopt_result nlopt_get_xtol_abs([In] IntPtr nlopt_opt,
|
---|
253 | IntPtr tol); // actually: double* TODO: manual marshalling
|
---|
254 |
|
---|
255 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
256 | public static extern nlopt_result nlopt_set_maxeval(IntPtr nlopt_opt, int maxeval);
|
---|
257 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
258 | public static extern int nlopt_get_maxeval([In] IntPtr nlopt_opt);
|
---|
259 |
|
---|
260 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
261 | public static extern int nlopt_get_numevals([In] IntPtr nlopt_opt);
|
---|
262 |
|
---|
263 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
264 | public static extern nlopt_result nlopt_set_maxtime(IntPtr nlopt_opt, double maxtime);
|
---|
265 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
266 | public static extern double nlopt_get_maxtime([In] IntPtr nlopt_opt);
|
---|
267 |
|
---|
268 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
269 | public static extern nlopt_result nlopt_force_stop(IntPtr nlopt_opt);
|
---|
270 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
271 | public static extern nlopt_result nlopt_set_force_stop(IntPtr nlopt_opt, int val);
|
---|
272 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
273 | public static extern int nlopt_get_force_stop([In] IntPtr nlopt_opt);
|
---|
274 |
|
---|
275 | /* more algorithm-specific parameters */
|
---|
276 |
|
---|
277 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
278 | public static extern nlopt_result nlopt_set_local_optimizer(IntPtr nlopt_opt, [In] IntPtr local_opt);
|
---|
279 |
|
---|
280 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
281 | public static extern nlopt_result nlopt_set_population(IntPtr nlopt_opt, uint pop);
|
---|
282 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
283 | public static extern uint nlopt_get_population([In] IntPtr nlopt_opt);
|
---|
284 |
|
---|
285 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
286 | public static extern nlopt_result nlopt_set_vector_storage(IntPtr nlopt_opt, uint dim);
|
---|
287 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
288 | public static extern uint nlopt_get_vector_storage([In] IntPtr nlopt_opt);
|
---|
289 |
|
---|
290 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
291 | public static extern nlopt_result nlopt_set_default_initial_step(IntPtr nlopt_opt,
|
---|
292 | [In] double[] x);
|
---|
293 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
294 | public static extern nlopt_result nlopt_set_initial_step(IntPtr nlopt_opt,
|
---|
295 | [In] double[] dx);
|
---|
296 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
297 | public static extern nlopt_result nlopt_set_initial_step1(IntPtr nlopt_opt, double dx);
|
---|
298 | [DllImport("nlopt_x64.dll", CallingConvention = CallingConvention.Cdecl)]
|
---|
299 | public static extern nlopt_result nlopt_get_initial_step([In] IntPtr nlopt_opt,
|
---|
300 | IntPtr x, // actually: double* TODO: manual marshalling
|
---|
301 | IntPtr dx // actually: double* TODO: manual marshalling
|
---|
302 | );
|
---|
303 | #endregion
|
---|
304 |
|
---|
305 | #region dispatch x86/x64
|
---|
306 | public static void nlopt_version(out int major, out int minor, out int bugfix) {
|
---|
307 | major = 0;
|
---|
308 | minor = 0;
|
---|
309 | bugfix = 0;
|
---|
310 | if (Environment.Is64BitProcess) {
|
---|
311 | nlopt_version_x64(ref major, ref minor, ref bugfix);
|
---|
312 | } else {
|
---|
313 | nlopt_version_x86(ref major, ref minor, ref bugfix);
|
---|
314 | }
|
---|
315 | }
|
---|
316 | #endregion
|
---|
317 | }
|
---|
318 | }
|
---|