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