chiark / gitweb /
more nlopt_opt introspection
[nlopt.git] / api / nlopt.h
1 /* Copyright (c) 2007-2009 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
23 #ifndef NLOPT_H
24 #define NLOPT_H
25
26 #include <stddef.h> /* for ptrdiff_t */
27
28 /* use stdcall convention under Windows, since this seems to
29    be more standard there and is important for calling from .NET */
30 #if defined(_WIN32) || defined(__WIN32__)
31 #  if defined(__GNUC__)
32 #    define NLOPT_STDCALL __attribute__((stdcall))
33 #  elif defined(_MSC_VER) || defined(_ICC) || defined(_STDCALL_SUPPORTED)
34 #    define NLOPT_STDCALL __stdcall
35 #  else
36 #    define NLOPT_STDCALL
37 #  endif
38 #else
39 #  define NLOPT_STDCALL
40 #endif
41
42 /* for Windows compilers, you should add a line
43            #define NLOPT_DLL
44    when using NLopt from a DLL, in order to do the proper
45    Windows importing nonsense. */
46 #if defined(NLOPT_DLL) && (defined(_WIN32) || defined(__WIN32__))
47 /* annoying Windows syntax for calling functions in a DLL */
48 #  define NLOPT_EXTERN extern __declspec(dllimport) NLOPT_STDCALL
49 #else
50 #  define NLOPT_EXTERN extern NLOPT_STDCALL
51 #endif
52
53 #ifdef __cplusplus
54 extern "C"
55 {
56 #endif /* __cplusplus */
57
58 typedef double (*nlopt_func)(int n, const double *x,
59                              double *gradient, /* NULL if not needed */
60                              void *func_data);
61
62 typedef enum {
63      /* Naming conventions:
64
65         NLOPT_{G/L}{D/N}_* 
66             = global/local derivative/no-derivative optimization, 
67               respectively 
68  
69         *_RAND algorithms involve some randomization.
70
71         *_NOSCAL algorithms are *not* scaled to a unit hypercube
72                  (i.e. they are sensitive to the units of x)
73         */
74
75      NLOPT_GN_DIRECT = 0,
76      NLOPT_GN_DIRECT_L,
77      NLOPT_GN_DIRECT_L_RAND,
78      NLOPT_GN_DIRECT_NOSCAL,
79      NLOPT_GN_DIRECT_L_NOSCAL,
80      NLOPT_GN_DIRECT_L_RAND_NOSCAL,
81
82      NLOPT_GN_ORIG_DIRECT,
83      NLOPT_GN_ORIG_DIRECT_L,
84
85      NLOPT_GD_STOGO,
86      NLOPT_GD_STOGO_RAND,
87
88      NLOPT_LD_LBFGS_NOCEDAL,
89
90      NLOPT_LD_LBFGS,
91
92      NLOPT_LN_PRAXIS,
93
94      NLOPT_LD_VAR1,
95      NLOPT_LD_VAR2,
96
97      NLOPT_LD_TNEWTON,
98      NLOPT_LD_TNEWTON_RESTART,
99      NLOPT_LD_TNEWTON_PRECOND,
100      NLOPT_LD_TNEWTON_PRECOND_RESTART,
101
102      NLOPT_GN_CRS2_LM,
103
104      NLOPT_GN_MLSL,
105      NLOPT_GD_MLSL,
106      NLOPT_GN_MLSL_LDS,
107      NLOPT_GD_MLSL_LDS,
108
109      NLOPT_LD_MMA,
110
111      NLOPT_LN_COBYLA,
112
113      NLOPT_LN_NEWUOA,
114      NLOPT_LN_NEWUOA_BOUND,
115
116      NLOPT_LN_NELDERMEAD,
117      NLOPT_LN_SBPLX,
118
119      NLOPT_LN_AUGLAG,
120      NLOPT_LD_AUGLAG,
121      NLOPT_LN_AUGLAG_EQ,
122      NLOPT_LD_AUGLAG_EQ,
123
124      NLOPT_LN_BOBYQA,
125
126      NLOPT_GN_ISRES,
127
128      NLOPT_NUM_ALGORITHMS /* not an algorithm, just the number of them */
129 } nlopt_algorithm;
130
131 extern const char *nlopt_algorithm_name(nlopt_algorithm a);
132
133 typedef enum {
134      NLOPT_FAILURE = -1, /* generic failure code */
135      NLOPT_INVALID_ARGS = -2,
136      NLOPT_OUT_OF_MEMORY = -3,
137      NLOPT_ROUNDOFF_LIMITED = -4,
138
139      NLOPT_SUCCESS = 1, /* generic success code */
140      NLOPT_MINF_MAX_REACHED = 2,
141      NLOPT_FTOL_REACHED = 3,
142      NLOPT_XTOL_REACHED = 4,
143      NLOPT_MAXEVAL_REACHED = 5,
144      NLOPT_MAXTIME_REACHED = 6
145 } nlopt_result;
146
147
148 NLOPT_EXTERN void nlopt_srand(unsigned long seed);
149 NLOPT_EXTERN void nlopt_srand_time(void);
150
151 NLOPT_EXTERN void nlopt_version(int *major, int *minor, int *bugfix);
152
153 /*************************** OBJECT-ORIENTED API **************************/
154 /* The style here is that we create an nlopt_opt "object" (an opaque pointer),
155    then set various optimization parameters, and then execute the
156    algorithm.  In this way, we can add more and more optimization parameters
157    (including algorithm-specific ones) without breaking backwards
158    compatibility, having functions with zillions of parameters, or
159    relying non-reentrantly on global variables.*/
160
161 struct nlopt_opt_s; /* opaque structure, defined internally */
162 typedef struct nlopt_opt_s *nlopt_opt;
163
164 /* the only immutable parameters of an optimization are the algorithm and
165    the dimension n of the problem, since changing either of these could
166    have side-effects on lots of other parameters */
167 NLOPT_EXTERN nlopt_opt nlopt_create(nlopt_algorithm algorithm, int n);
168 NLOPT_EXTERN void nlopt_destroy(nlopt_opt opt);
169 NLOPT_EXTERN nlopt_opt nlopt_copy(const nlopt_opt opt);
170
171 NLOPT_EXTERN nlopt_result nlopt_optimize(nlopt_opt opt, double *x,
172                                          double *opt_f);
173
174 NLOPT_EXTERN nlopt_result nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, 
175                                                   void *f_data);
176
177 NLOPT_EXTERN nlopt_algorithm nlopt_get_algorithm(const nlopt_opt opt);
178 NLOPT_EXTERN int nlopt_get_dimension(const nlopt_opt opt);
179
180 /* constraints: */
181
182 NLOPT_EXTERN nlopt_result nlopt_set_lower_bounds(nlopt_opt opt, 
183                                                  const double *lb);
184 NLOPT_EXTERN nlopt_result nlopt_set_lower_bounds1(nlopt_opt opt, double lb);
185 NLOPT_EXTERN void nlopt_get_lower_bounds(const nlopt_opt opt, double *lb);
186 NLOPT_EXTERN nlopt_result nlopt_set_upper_bounds(nlopt_opt opt, 
187                                                  const double *ub);
188 NLOPT_EXTERN nlopt_result nlopt_set_upper_bounds1(nlopt_opt opt, double ub);
189 NLOPT_EXTERN void nlopt_get_upper_bounds(const nlopt_opt opt, double *ub);
190
191 NLOPT_EXTERN nlopt_result nlopt_remove_inequality_constraints(nlopt_opt opt);
192 NLOPT_EXTERN nlopt_result nlopt_add_inequality_constraint(nlopt_opt opt,
193                                                           nlopt_func fc,
194                                                           void *fc_data,
195                                                           double tol);
196
197 NLOPT_EXTERN nlopt_result nlopt_remove_equality_constraints(nlopt_opt opt);
198 NLOPT_EXTERN nlopt_result nlopt_add_equality_constraint(nlopt_opt opt,
199                                                         nlopt_func h,
200                                                         void *h_data,
201                                                         double tol);
202
203 /* stopping criteria: */
204
205 NLOPT_EXTERN nlopt_result nlopt_set_stopval(nlopt_opt opt, double minf_max);
206 NLOPT_EXTERN double nlopt_get_stopval(const nlopt_opt opt);
207
208 NLOPT_EXTERN nlopt_result nlopt_set_ftol_rel(nlopt_opt opt, double tol);
209 NLOPT_EXTERN double nlopt_get_ftol_rel(const nlopt_opt opt);
210 NLOPT_EXTERN nlopt_result nlopt_set_ftol_abs(nlopt_opt opt, double tol);
211 NLOPT_EXTERN double nlopt_get_ftol_abs(const nlopt_opt opt);
212
213 NLOPT_EXTERN nlopt_result nlopt_set_xtol_rel(nlopt_opt opt, double tol);
214 NLOPT_EXTERN double nlopt_get_xtol_rel(const nlopt_opt opt);
215 NLOPT_EXTERN nlopt_result nlopt_set_xtol_abs1(nlopt_opt opt, double tol);
216 NLOPT_EXTERN nlopt_result nlopt_set_xtol_abs(nlopt_opt opt, const double *tol);
217 NLOPT_EXTERN void nlopt_get_xtol_abs(const nlopt_opt opt, double *tol);
218
219 NLOPT_EXTERN nlopt_result nlopt_set_maxeval(nlopt_opt opt, int maxeval);
220 NLOPT_EXTERN int nlopt_get_maxeval(nlopt_opt opt);
221
222 NLOPT_EXTERN nlopt_result nlopt_set_maxtime(nlopt_opt opt, double maxtime);
223 NLOPT_EXTERN double nlopt_get_maxtime(nlopt_opt opt);
224
225 /* more algorithm-specific parameters */
226
227 NLOPT_EXTERN nlopt_result nlopt_set_local_optimizer(nlopt_opt opt, 
228                                                     const nlopt_opt local_opt);
229
230 NLOPT_EXTERN nlopt_result nlopt_set_population(nlopt_opt opt, int pop);
231 NLOPT_EXTERN int nlopt_get_population(const nlopt_opt opt);
232
233 NLOPT_EXTERN nlopt_result nlopt_set_default_initial_step(nlopt_opt opt, 
234                                                          const double *x);
235 NLOPT_EXTERN nlopt_result nlopt_set_initial_step(nlopt_opt opt, 
236                                                  const double *dx);
237 NLOPT_EXTERN nlopt_result nlopt_set_initial_step1(nlopt_opt opt, double dx);
238 NLOPT_EXTERN nlopt_result nlopt_get_initial_step(const nlopt_opt opt, 
239                                                  const double *x, double *dx);
240
241 /*************************** DEPRECATED API **************************/
242 /* The new "object-oriented" API is preferred, since it allows us to
243    gracefully add new features and algorithm-specific options in a
244    re-entrant way, and we can automatically assume reasonable defaults
245    for unspecified parameters. */
246
247 /* Where possible (e.g. for gcc >= 3.1), enable a compiler warning
248    for code that uses a deprecated function */
249 #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__==3 && __GNUC_MINOR__ > 0))
250 #  define NLOPT_DEPRECATED __attribute__((deprecated))
251 #else
252 #  define NLOPT_DEPRECATED 
253 #endif
254
255 NLOPT_EXTERN nlopt_result nlopt_minimize(
256      nlopt_algorithm algorithm,
257      int n, nlopt_func f, void *f_data,
258      const double *lb, const double *ub, /* bounds */
259      double *x, /* in: initial guess, out: minimizer */
260      double *minf, /* out: minimum */
261      double minf_max, double ftol_rel, double ftol_abs,
262      double xtol_rel, const double *xtol_abs,
263      int maxeval, double maxtime) NLOPT_DEPRECATED;
264
265 NLOPT_EXTERN nlopt_result nlopt_minimize_constrained(
266      nlopt_algorithm algorithm,
267      int n, nlopt_func f, void *f_data,
268      int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size,
269      const double *lb, const double *ub, /* bounds */
270      double *x, /* in: initial guess, out: minimizer */
271      double *minf, /* out: minimum */
272      double minf_max, double ftol_rel, double ftol_abs,
273      double xtol_rel, const double *xtol_abs,
274      int maxeval, double maxtime) NLOPT_DEPRECATED;
275
276 NLOPT_EXTERN nlopt_result nlopt_minimize_econstrained(
277      nlopt_algorithm algorithm,
278      int n, nlopt_func f, void *f_data,
279      int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size,
280      int p, nlopt_func h, void *h_data, ptrdiff_t h_datum_size,
281      const double *lb, const double *ub, /* bounds */
282      double *x, /* in: initial guess, out: minimizer */
283      double *minf, /* out: minimum */
284      double minf_max, double ftol_rel, double ftol_abs,
285      double xtol_rel, const double *xtol_abs,
286      double htol_rel, double htol_abs,
287      int maxeval, double maxtime) NLOPT_DEPRECATED;
288
289 NLOPT_EXTERN void nlopt_get_local_search_algorithm(nlopt_algorithm *deriv,
290                                              nlopt_algorithm *nonderiv,
291                                              int *maxeval) NLOPT_DEPRECATED;
292 NLOPT_EXTERN void nlopt_set_local_search_algorithm(nlopt_algorithm deriv,
293                                              nlopt_algorithm nonderiv,
294                                              int maxeval) NLOPT_DEPRECATED;
295
296 NLOPT_EXTERN int nlopt_get_stochastic_population(void) NLOPT_DEPRECATED;
297 NLOPT_EXTERN void nlopt_set_stochastic_population(int pop) NLOPT_DEPRECATED;
298
299 /*********************************************************************/
300
301 #ifdef __cplusplus
302 }  /* extern "C" */
303 #endif /* __cplusplus */
304
305 #endif