chiark / gitweb /
bff57abe18ea5ff7400cab96580fd14a32b0c795
[nlopt.git] / api / nlopt.h
1 /* Copyright (c) 2007-2010 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 and size_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__)) && !defined(__LCC__)
47 /* annoying Windows syntax for calling functions in a DLL */
48 #  if defined(NLOPT_DLL_EXPORT)
49 #    define NLOPT_EXTERN(T) extern __declspec(dllexport) T NLOPT_STDCALL
50 #  else
51 #    define NLOPT_EXTERN(T) extern __declspec(dllimport) T NLOPT_STDCALL
52 #  endif
53 #else
54 #  define NLOPT_EXTERN(T) extern T NLOPT_STDCALL
55 #endif
56
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif /* __cplusplus */
61
62 typedef double (*nlopt_func)(unsigned n, const double *x,
63                              double *gradient, /* NULL if not needed */
64                              void *func_data);
65
66 typedef void (*nlopt_mfunc)(unsigned m, double *result,
67                             unsigned n, const double *x,
68                              double *gradient, /* NULL if not needed */
69                              void *func_data);
70
71 typedef enum {
72      /* Naming conventions:
73
74         NLOPT_{G/L}{D/N}_* 
75             = global/local derivative/no-derivative optimization, 
76               respectively 
77  
78         *_RAND algorithms involve some randomization.
79
80         *_NOSCAL algorithms are *not* scaled to a unit hypercube
81                  (i.e. they are sensitive to the units of x)
82         */
83
84      NLOPT_GN_DIRECT = 0,
85      NLOPT_GN_DIRECT_L,
86      NLOPT_GN_DIRECT_L_RAND,
87      NLOPT_GN_DIRECT_NOSCAL,
88      NLOPT_GN_DIRECT_L_NOSCAL,
89      NLOPT_GN_DIRECT_L_RAND_NOSCAL,
90
91      NLOPT_GN_ORIG_DIRECT,
92      NLOPT_GN_ORIG_DIRECT_L,
93
94      NLOPT_GD_STOGO,
95      NLOPT_GD_STOGO_RAND,
96
97      NLOPT_LD_LBFGS_NOCEDAL,
98
99      NLOPT_LD_LBFGS,
100
101      NLOPT_LN_PRAXIS,
102
103      NLOPT_LD_VAR1,
104      NLOPT_LD_VAR2,
105
106      NLOPT_LD_TNEWTON,
107      NLOPT_LD_TNEWTON_RESTART,
108      NLOPT_LD_TNEWTON_PRECOND,
109      NLOPT_LD_TNEWTON_PRECOND_RESTART,
110
111      NLOPT_GN_CRS2_LM,
112
113      NLOPT_GN_MLSL,
114      NLOPT_GD_MLSL,
115      NLOPT_GN_MLSL_LDS,
116      NLOPT_GD_MLSL_LDS,
117
118      NLOPT_LD_MMA,
119
120      NLOPT_LN_COBYLA,
121
122      NLOPT_LN_NEWUOA,
123      NLOPT_LN_NEWUOA_BOUND,
124
125      NLOPT_LN_NELDERMEAD,
126      NLOPT_LN_SBPLX,
127
128      NLOPT_LN_AUGLAG,
129      NLOPT_LD_AUGLAG,
130      NLOPT_LN_AUGLAG_EQ,
131      NLOPT_LD_AUGLAG_EQ,
132
133      NLOPT_LN_BOBYQA,
134
135      NLOPT_GN_ISRES,
136
137      /* new variants that require local_optimizer to be set,
138         not with older constants for backwards compatibility */
139      NLOPT_AUGLAG,
140      NLOPT_AUGLAG_EQ,
141      NLOPT_G_MLSL,
142      NLOPT_G_MLSL_LDS,
143
144      NLOPT_LD_SLSQP,
145
146      NLOPT_NUM_ALGORITHMS /* not an algorithm, just the number of them */
147 } nlopt_algorithm;
148
149 NLOPT_EXTERN(const char *) nlopt_algorithm_name(nlopt_algorithm a);
150
151 typedef enum {
152      NLOPT_FAILURE = -1, /* generic failure code */
153      NLOPT_INVALID_ARGS = -2,
154      NLOPT_OUT_OF_MEMORY = -3,
155      NLOPT_ROUNDOFF_LIMITED = -4,
156      NLOPT_FORCED_STOP = -5,
157      NLOPT_SUCCESS = 1, /* generic success code */
158      NLOPT_STOPVAL_REACHED = 2,
159      NLOPT_FTOL_REACHED = 3,
160      NLOPT_XTOL_REACHED = 4,
161      NLOPT_MAXEVAL_REACHED = 5,
162      NLOPT_MAXTIME_REACHED = 6
163 } nlopt_result;
164
165 #define NLOPT_MINF_MAX_REACHED NLOPT_STOPVAL_REACHED
166
167 NLOPT_EXTERN(void) nlopt_srand(unsigned long seed);
168 NLOPT_EXTERN(void) nlopt_srand_time(void);
169
170 NLOPT_EXTERN(void) nlopt_version(int *major, int *minor, int *bugfix);
171
172 /*************************** OBJECT-ORIENTED API **************************/
173 /* The style here is that we create an nlopt_opt "object" (an opaque pointer),
174    then set various optimization parameters, and then execute the
175    algorithm.  In this way, we can add more and more optimization parameters
176    (including algorithm-specific ones) without breaking backwards
177    compatibility, having functions with zillions of parameters, or
178    relying non-reentrantly on global variables.*/
179
180 struct nlopt_opt_s; /* opaque structure, defined internally */
181 typedef struct nlopt_opt_s *nlopt_opt;
182
183 /* the only immutable parameters of an optimization are the algorithm and
184    the dimension n of the problem, since changing either of these could
185    have side-effects on lots of other parameters */
186 NLOPT_EXTERN(nlopt_opt) nlopt_create(nlopt_algorithm algorithm, unsigned n);
187 NLOPT_EXTERN(void) nlopt_destroy(nlopt_opt opt);
188 NLOPT_EXTERN(nlopt_opt) nlopt_copy(const nlopt_opt opt);
189
190 NLOPT_EXTERN(nlopt_result) nlopt_optimize(nlopt_opt opt, double *x,
191                                          double *opt_f);
192
193 NLOPT_EXTERN(nlopt_result) nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, 
194                                                   void *f_data);
195 NLOPT_EXTERN(nlopt_result) nlopt_set_max_objective(nlopt_opt opt, nlopt_func f, 
196                                                   void *f_data);
197
198 NLOPT_EXTERN(nlopt_algorithm) nlopt_get_algorithm(const nlopt_opt opt);
199 NLOPT_EXTERN(unsigned) nlopt_get_dimension(const nlopt_opt opt);
200
201 /* constraints: */
202
203 NLOPT_EXTERN(nlopt_result) nlopt_set_lower_bounds(nlopt_opt opt, 
204                                                  const double *lb);
205 NLOPT_EXTERN(nlopt_result) nlopt_set_lower_bounds1(nlopt_opt opt, double lb);
206 NLOPT_EXTERN(nlopt_result) nlopt_get_lower_bounds(const nlopt_opt opt, 
207                                                  double *lb);
208 NLOPT_EXTERN(nlopt_result) nlopt_set_upper_bounds(nlopt_opt opt, 
209                                                  const double *ub);
210 NLOPT_EXTERN(nlopt_result) nlopt_set_upper_bounds1(nlopt_opt opt, double ub);
211 NLOPT_EXTERN(nlopt_result) nlopt_get_upper_bounds(const nlopt_opt opt,
212                                                  double *ub);
213
214 NLOPT_EXTERN(nlopt_result) nlopt_remove_inequality_constraints(nlopt_opt opt);
215 NLOPT_EXTERN(nlopt_result) nlopt_add_inequality_constraint(nlopt_opt opt,
216                                                           nlopt_func fc,
217                                                           void *fc_data,
218                                                           double tol);
219 NLOPT_EXTERN(nlopt_result) nlopt_add_inequality_mconstraint(nlopt_opt opt,
220                                                             unsigned m,
221                                                             nlopt_mfunc fc,
222                                                             void *fc_data,
223                                                             const double *tol);
224
225 NLOPT_EXTERN(nlopt_result) nlopt_remove_equality_constraints(nlopt_opt opt);
226 NLOPT_EXTERN(nlopt_result) nlopt_add_equality_constraint(nlopt_opt opt,
227                                                         nlopt_func h,
228                                                         void *h_data,
229                                                         double tol);
230 NLOPT_EXTERN(nlopt_result) nlopt_add_equality_mconstraint(nlopt_opt opt,
231                                                           unsigned m,
232                                                           nlopt_mfunc h,
233                                                           void *h_data,
234                                                           const double *tol);
235
236 /* stopping criteria: */
237
238 NLOPT_EXTERN(nlopt_result) nlopt_set_stopval(nlopt_opt opt, double stopval);
239 NLOPT_EXTERN(double) nlopt_get_stopval(const nlopt_opt opt);
240
241 NLOPT_EXTERN(nlopt_result) nlopt_set_ftol_rel(nlopt_opt opt, double tol);
242 NLOPT_EXTERN(double) nlopt_get_ftol_rel(const nlopt_opt opt);
243 NLOPT_EXTERN(nlopt_result) nlopt_set_ftol_abs(nlopt_opt opt, double tol);
244 NLOPT_EXTERN(double) nlopt_get_ftol_abs(const nlopt_opt opt);
245
246 NLOPT_EXTERN(nlopt_result) nlopt_set_xtol_rel(nlopt_opt opt, double tol);
247 NLOPT_EXTERN(double) nlopt_get_xtol_rel(const nlopt_opt opt);
248 NLOPT_EXTERN(nlopt_result) nlopt_set_xtol_abs1(nlopt_opt opt, double tol);
249 NLOPT_EXTERN(nlopt_result) nlopt_set_xtol_abs(nlopt_opt opt, const double *tol);
250 NLOPT_EXTERN(nlopt_result) nlopt_get_xtol_abs(const nlopt_opt opt,
251                                              double *tol);
252
253 NLOPT_EXTERN(nlopt_result) nlopt_set_maxeval(nlopt_opt opt, int maxeval);
254 NLOPT_EXTERN(int) nlopt_get_maxeval(const nlopt_opt opt);
255
256 NLOPT_EXTERN(nlopt_result) nlopt_set_maxtime(nlopt_opt opt, double maxtime);
257 NLOPT_EXTERN(double) nlopt_get_maxtime(const nlopt_opt opt);
258
259 NLOPT_EXTERN(nlopt_result) nlopt_force_stop(nlopt_opt opt);
260 NLOPT_EXTERN(nlopt_result) nlopt_set_force_stop(nlopt_opt opt, int val);
261 NLOPT_EXTERN(int) nlopt_get_force_stop(const nlopt_opt opt);
262
263 /* more algorithm-specific parameters */
264
265 NLOPT_EXTERN(nlopt_result) nlopt_set_local_optimizer(nlopt_opt opt, 
266                                                     const nlopt_opt local_opt);
267
268 NLOPT_EXTERN(nlopt_result) nlopt_set_population(nlopt_opt opt, unsigned pop);
269 NLOPT_EXTERN(unsigned) nlopt_get_population(const nlopt_opt opt);
270
271 NLOPT_EXTERN(nlopt_result) nlopt_set_vector_storage(nlopt_opt opt, unsigned dim);
272 NLOPT_EXTERN(unsigned) nlopt_get_vector_storage(const nlopt_opt opt);
273
274 NLOPT_EXTERN(nlopt_result) nlopt_set_default_initial_step(nlopt_opt opt, 
275                                                          const double *x);
276 NLOPT_EXTERN(nlopt_result) nlopt_set_initial_step(nlopt_opt opt, 
277                                                  const double *dx);
278 NLOPT_EXTERN(nlopt_result) nlopt_set_initial_step1(nlopt_opt opt, double dx);
279 NLOPT_EXTERN(nlopt_result) nlopt_get_initial_step(const nlopt_opt opt, 
280                                                  const double *x, double *dx);
281
282 /* the following are functions mainly designed to be used internally
283    by the Fortran and SWIG wrappers, allow us to tel nlopt_destroy and
284    nlopt_copy to do something to the f_data pointers (e.g. free or
285    duplicate them, respectively) */
286 typedef void* (*nlopt_munge)(void *p);
287 NLOPT_EXTERN(void) nlopt_set_munge(nlopt_opt opt,
288                                   nlopt_munge munge_on_destroy,
289                                   nlopt_munge munge_on_copy);
290
291 /*************************** DEPRECATED API **************************/
292 /* The new "object-oriented" API is preferred, since it allows us to
293    gracefully add new features and algorithm-specific options in a
294    re-entrant way, and we can automatically assume reasonable defaults
295    for unspecified parameters. */
296
297 /* Where possible (e.g. for gcc >= 3.1), enable a compiler warning
298    for code that uses a deprecated function */
299 #if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__==3 && __GNUC_MINOR__ > 0))
300 #  define NLOPT_DEPRECATED __attribute__((deprecated))
301 #else
302 #  define NLOPT_DEPRECATED 
303 #endif
304
305 typedef double (*nlopt_func_old)(int n, const double *x,
306                                  double *gradient, /* NULL if not needed */
307                                  void *func_data);
308
309 NLOPT_EXTERN(nlopt_result) nlopt_minimize(
310      nlopt_algorithm algorithm,
311      int n, nlopt_func_old f, void *f_data,
312      const double *lb, const double *ub, /* bounds */
313      double *x, /* in: initial guess, out: minimizer */
314      double *minf, /* out: minimum */
315      double minf_max, double ftol_rel, double ftol_abs,
316      double xtol_rel, const double *xtol_abs,
317      int maxeval, double maxtime) NLOPT_DEPRECATED;
318
319 NLOPT_EXTERN(nlopt_result) nlopt_minimize_constrained(
320      nlopt_algorithm algorithm,
321      int n, nlopt_func_old f, void *f_data,
322      int m, nlopt_func_old fc, void *fc_data, ptrdiff_t fc_datum_size,
323      const double *lb, const double *ub, /* bounds */
324      double *x, /* in: initial guess, out: minimizer */
325      double *minf, /* out: minimum */
326      double minf_max, double ftol_rel, double ftol_abs,
327      double xtol_rel, const double *xtol_abs,
328      int maxeval, double maxtime) NLOPT_DEPRECATED;
329
330 NLOPT_EXTERN(nlopt_result) nlopt_minimize_econstrained(
331      nlopt_algorithm algorithm,
332      int n, nlopt_func_old f, void *f_data,
333      int m, nlopt_func_old fc, void *fc_data, ptrdiff_t fc_datum_size,
334      int p, nlopt_func_old h, void *h_data, ptrdiff_t h_datum_size,
335      const double *lb, const double *ub, /* bounds */
336      double *x, /* in: initial guess, out: minimizer */
337      double *minf, /* out: minimum */
338      double minf_max, double ftol_rel, double ftol_abs,
339      double xtol_rel, const double *xtol_abs,
340      double htol_rel, double htol_abs,
341      int maxeval, double maxtime) NLOPT_DEPRECATED;
342
343 NLOPT_EXTERN(void) nlopt_get_local_search_algorithm(nlopt_algorithm *deriv,
344                                              nlopt_algorithm *nonderiv,
345                                              int *maxeval) NLOPT_DEPRECATED;
346 NLOPT_EXTERN(void) nlopt_set_local_search_algorithm(nlopt_algorithm deriv,
347                                              nlopt_algorithm nonderiv,
348                                              int maxeval) NLOPT_DEPRECATED;
349
350 NLOPT_EXTERN(int) nlopt_get_stochastic_population(void) NLOPT_DEPRECATED;
351 NLOPT_EXTERN(void) nlopt_set_stochastic_population(int pop) NLOPT_DEPRECATED;
352
353 /*********************************************************************/
354
355 #ifdef __cplusplus
356 }  /* extern "C" */
357 #endif /* __cplusplus */
358
359 #endif