chiark / gitweb /
61ae819b165d3393f2d823eedf45938e5168aea6
[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 NLOPT_EXTERN nlopt_result nlopt_minimize(
148      nlopt_algorithm algorithm,
149      int n, nlopt_func f, void *f_data,
150      const double *lb, const double *ub, /* bounds */
151      double *x, /* in: initial guess, out: minimizer */
152      double *minf, /* out: minimum */
153      double minf_max, double ftol_rel, double ftol_abs,
154      double xtol_rel, const double *xtol_abs,
155      int maxeval, double maxtime);
156
157 NLOPT_EXTERN nlopt_result nlopt_minimize_constrained(
158      nlopt_algorithm algorithm,
159      int n, nlopt_func f, void *f_data,
160      int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size,
161      const double *lb, const double *ub, /* bounds */
162      double *x, /* in: initial guess, out: minimizer */
163      double *minf, /* out: minimum */
164      double minf_max, double ftol_rel, double ftol_abs,
165      double xtol_rel, const double *xtol_abs,
166      int maxeval, double maxtime);
167
168 NLOPT_EXTERN nlopt_result nlopt_minimize_econstrained(
169      nlopt_algorithm algorithm,
170      int n, nlopt_func f, void *f_data,
171      int m, nlopt_func fc, void *fc_data, ptrdiff_t fc_datum_size,
172      int p, nlopt_func h, void *h_data, ptrdiff_t h_datum_size,
173      const double *lb, const double *ub, /* bounds */
174      double *x, /* in: initial guess, out: minimizer */
175      double *minf, /* out: minimum */
176      double minf_max, double ftol_rel, double ftol_abs,
177      double xtol_rel, const double *xtol_abs,
178      double htol_rel, double htol_abs,
179      int maxeval, double maxtime);
180
181 NLOPT_EXTERN void nlopt_srand(unsigned long seed);
182 NLOPT_EXTERN void nlopt_srand_time(void);
183
184 NLOPT_EXTERN void nlopt_version(int *major, int *minor, int *bugfix);
185
186 NLOPT_EXTERN void nlopt_get_local_search_algorithm(nlopt_algorithm *deriv,
187                                              nlopt_algorithm *nonderiv,
188                                              int *maxeval);
189 NLOPT_EXTERN void nlopt_set_local_search_algorithm(nlopt_algorithm deriv,
190                                              nlopt_algorithm nonderiv,
191                                              int maxeval);
192
193 NLOPT_EXTERN int nlopt_get_stochastic_population(void);
194 NLOPT_EXTERN void nlopt_set_stochastic_population(int pop);
195
196 #ifdef __cplusplus
197 }  /* extern "C" */
198 #endif /* __cplusplus */
199
200 #endif