chiark / gitweb /
added NEWUOA, unified starting step-size for derivative-free algorithms
[nlopt.git] / api / nlopt_minimize_constrained.3
1 .\" 
2 .\" Copyright (c) 2007 Massachusetts Institute of Technology
3 .\" 
4 .\" Copying and distribution of this file, with or without modification,
5 .\" are permitted in any medium without royalty provided the copyright
6 .\" notice and this notice are preserved.
7 .\"
8 .TH NLOPT_MINIMIZE_CONSTRAINED 3  2007-08-23 "MIT" "NLopt programming manual"
9 .SH NAME
10 nlopt_minimize_constrained \- Minimize a multivariate nonlinear function subject to nonlinear constraints
11 .SH SYNOPSIS
12 .nf
13 .B #include <nlopt.h>
14 .sp
15 .BI "nlopt_result nlopt_minimize_constrained(nlopt_algorithm " "algorithm" ,
16 .br
17 .BI "                            int " "n" ,
18 .BI "                            nlopt_func " "f" ,
19 .BI "                            void* " "f_data" ,
20 .BI "                            int " "m" ,
21 .BI "                            nlopt_func " "fc" ,
22 .BI "                            void* " "fc_data" ,
23 .BI "                            ptrdiff_t " "fc_datum_size" ,
24 .BI "                            const double* " "lb" ,
25 .BI "                            const double* " "ub" ,
26 .BI "                            double* " "x" ,
27 .BI "                            double* " "minf" ,
28 .BI "                            double " "minf_max" ,
29 .BI "                            double " "ftol_rel" ,
30 .BI "                            double " "ftol_abs" ,
31 .BI "                            double " "xtol_rel" ,
32 .BI "                            const double* " "xtol_abs" ,
33 .BI "                            int " "maxeval" ,
34 .BI "                            double " "maxtime" );
35 .sp
36 You should link the resulting program with the linker flags
37 -lnlopt -lm on Unix.
38 .fi
39 .SH DESCRIPTION
40 .BR nlopt_minimize_constrained ()
41 attempts to minimize a nonlinear function
42 .I f
43 of
44 .I n
45 design variables, subject to 
46 .I m
47 nonlinear constraints described by the function
48 .I fc
49 (see below), using the specified
50 .IR algorithm .
51 The minimum function value found is returned in
52 .IR minf ,
53 with the corresponding design variable values returned in the array
54 .I x
55 of length
56 .IR n .
57 The input values in
58 .I x
59 should be a starting guess for the optimum.
60 The inputs
61 .I lb
62 and
63 .I ub
64 are arrays of length
65 .I n
66 containing lower and upper bounds, respectively, on the design variables
67 .IR x .
68 The other parameters specify stopping criteria (tolerances, the maximum
69 number of function evaluations, etcetera) and other information as described
70 in more detail below.  The return value is a integer code indicating success
71 (positive) or failure (negative), as described below.
72 .PP
73 By changing the parameter
74 .I algorithm
75 among several predefined constants described below, one can switch easily
76 between a variety of minimization algorithms.  Some of these algorithms
77 require the gradient (derivatives) of the function to be supplied via
78 .IR f ,
79 and other algorithms do not require derivatives.  Some of the
80 algorithms attempt to find a global minimum within the given bounds,
81 and others find only a local minimum.  Most of the algorithms only handle the
82 case where
83 .I m
84 is zero (no explicit nonlinear constraints); the only algorithms that
85 currently support positive
86 .I m
87 are 
88 .B NLOPT_LD_MMA
89 and
90 .BR NLOPT_LN_COBYLA .
91 .PP
92 The
93 .B nlopt_minimize_constrained
94 function is a wrapper around several free/open-source minimization packages,
95 as well as some new implementations of published optimization algorithms.
96 You could, of course, compile and call these packages separately, and in
97 some cases this will provide greater flexibility than is available via the
98 .B nlopt_minimize_constrained
99 interface.  However, depending upon the specific function being minimized,
100 the different algorithms will vary in effectiveness.  The intent of
101 .B nlopt_minimize_constrained
102 is to allow you to quickly switch between algorithms in order to experiment
103 with them for your problem, by providing a simple unified interface to
104 these subroutines.
105 .SH OBJECTIVE FUNCTION
106 .BR nlopt_minimize_constrained ()
107 minimizes an objective function
108 .I f
109 of the form:
110 .sp
111 .BI "      double f(int " "n" , 
112 .br
113 .BI "               const double* " "x" , 
114 .br
115 .BI "               double* " "grad" , 
116 .br
117 .BI "               void* " "f_data" );
118 .sp
119 The return value should be the value of the function at the point
120 .IR x ,
121 where
122 .I x
123 points to an array of length
124 .I n
125 of the design variables.  The dimension
126 .I n
127 is identical to the one passed to
128 .BR nlopt_minimize_constrained ().
129 .sp
130 In addition, if the argument
131 .I grad
132 is not NULL, then
133 .I grad
134 points to an array of length
135 .I n
136 which should (upon return) be set to the gradient of the function with
137 respect to the design variables at
138 .IR x .
139 That is,
140 .IR grad[i]
141 should upon return contain the partial derivative df/dx[i],
142 for 0 <= i < n, if
143 .I grad
144 is non-NULL.
145 Not all of the optimization algorithms (below) use the gradient information:
146 for algorithms listed as "derivative-free," the 
147 .I grad
148 argument will always be NULL and need never be computed.  (For
149 algorithms that do use gradient information, however,
150 .I grad
151 may still be NULL for some calls.)
152 .sp
153 The 
154 .I f_data
155 argument is the same as the one passed to 
156 .BR nlopt_minimize_constrained (),
157 and may be used to pass any additional data through to the function.
158 (That is, it may be a pointer to some caller-defined data
159 structure/type containing information your function needs, which you
160 convert from void* by a typecast.)
161 .sp
162 .SH BOUND CONSTRAINTS
163 Most of the algorithms in NLopt are designed for minimization of functions
164 with simple bound constraints on the inputs.  That is, the input vectors
165 x[i] are constrainted to lie in a hyperrectangle lb[i] <= x[i] <= ub[i] for
166 0 <= i < n, where
167 .I lb
168 and
169 .I ub
170 are the two arrays passed to
171 .BR nlopt_minimize_constrained ().
172 .sp
173 However, a few of the algorithms support partially or totally
174 unconstrained optimization, as noted below, where a (totally or
175 partially) unconstrained design variable is indicated by a lower bound
176 equal to -Inf and/or an upper bound equal to +Inf.  Here, Inf is the
177 IEEE-754 floating-point infinity, which (in ANSI C99) is represented by
178 the macro INFINITY in math.h.  Alternatively, for older C versions
179 you may also use the macro HUGE_VAL (also in math.h).
180 .sp
181 With some of the algorithms, especially those that do not require
182 derivative information, a simple (but not especially efficient) way
183 to implement arbitrary nonlinear constraints is to return Inf (see
184 above) whenever the constraints are violated by a given input
185 .IR x .
186 More generally, there are various ways to implement constraints
187 by adding "penalty terms" to your objective function, which are
188 described in the optimization literature.
189 A much more efficient way to specify nonlinear constraints is described
190 below, but is only supported by a small subset of the algorithms.
191 .SH NONLINEAR CONSTRAINTS
192 The
193 .B nlopt_minimize_constrained
194 function also allows you to specify
195 .I m
196 nonlinear constraints via the function
197 .IR fc ,
198 where
199 .I m
200 is any nonnegative integer.  However, nonzero
201 .I m
202 is currently only supported by the
203 .B NLOPT_LD_MMA
204 and
205 .B NLOPT_LN_COBYLA
206 algorithms below.
207 .sp
208 In particular, the nonlinear constraints are of the form 
209 \fIfc\fR(\fIx\fR) <= 0, where the function
210 .I fc
211 is of the same form as the objective function described above:
212 .sp
213 .BI "      double fc(int " "n" , 
214 .br
215 .BI "                const double* " "x" , 
216 .br
217 .BI "                double* " "grad" , 
218 .br
219 .BI "                void* " "fc_datum" );
220 .sp
221 The return value should be the value of the constraint at the point
222 .IR x ,
223 where
224 the dimension
225 .I n
226 is identical to the one passed to
227 .BR nlopt_minimize_constrained ().
228 As for the objective function, if the argument
229 .I grad
230 is not NULL, then
231 .I grad
232 points to an array of length
233 .I n
234 which should (upon return) be set to the gradient of the function with
235 respect to 
236 .IR x .
237 (For any algorithm listed as "derivative-free" below, the
238 .I grad
239 argument will always be NULL and need never be computed.)
240 .sp
241 The 
242 .I fc_datum
243 argument is based on the
244 .I fc_data
245 argument passed to
246 .BR nlopt_minimize_constrained (),
247 and may be used to pass any additional data through to the function,
248 and is used to distinguish between different constraints.
249 .sp
250 In particular, the constraint function
251 .I fc
252 will be called (at most)
253 .I m
254 times for each
255 .IR x ,
256 and the i-th constraint (0 <= i <
257 .IR m )
258 will be passed an
259 .I fc_datum
260 argument equal to
261 .I fc_data
262 offset by i * 
263 .IR fc_datum_size .
264 For example, suppose that you have a data structure of type "foo"
265 that describes the data needed by each constraint, and you store
266 the information for the constraints in an array "foo data[m]".  In
267 this case, you would pass "data" as the
268 .I fc_data
269 parameter to
270 .BR nlopt_minimize_constrained ,
271 and "sizeof(foo)" as the 
272 .I fc_datum_size
273 parameter.  Then, your 
274 .I fc
275 function would be called 
276 .I m
277 times for each point, and be passed &data[0] through &data[m-1] in sequence.
278 .SH ALGORITHMS
279 The 
280 .I algorithm
281 parameter specifies the optimization algorithm (for more detail on
282 these, see the README files in the source-code subdirectories), and
283 can take on any of the following constant values.  Constants with
284 .B _G{N,D}_
285 in their names
286 refer to global optimization methods, whereas
287 .B _L{N,D}_
288 refers to local optimization methods (that try to find a local minimum
289 starting from the starting guess
290 .IR x ).
291 Constants with
292 .B _{G,L}N_
293 refer to non-gradient (derivative-free) algorithms that do not require the
294 objective function to supply a gradient, whereas
295 .B _{G,L}D_
296 refers to derivative-based algorithms that require the objective
297 function to supply a gradient.  (Especially for local optimization,
298 derivative-based algorithms are generally superior to derivative-free
299 ones: the gradient is good to have 
300 .I if 
301 you can compute it cheaply, e.g. via an adjoint method.)
302 .TP 
303 .B NLOPT_GN_DIRECT_L
304 Perform a global (G) derivative-free (N) optimization using the
305 DIRECT-L search algorithm by Jones et al. as modified by Gablonsky et
306 al. to be more weighted towards local search.  Does not support
307 unconstrainted optimization.  There are also several other variants of
308 the DIRECT algorithm that are supported:
309 .BR NLOPT_GLOBAL_DIRECT ,
310 which is the original DIRECT algorithm;
311 .BR NLOPT_GLOBAL_DIRECT_L_RAND ,
312 a slightly randomized version of DIRECT-L that may be better in
313 high-dimensional search spaces;
314 .BR NLOPT_GLOBAL_DIRECT_NOSCAL ,
315 .BR NLOPT_GLOBAL_DIRECT_L_NOSCAL ,
316 and
317 .BR NLOPT_GLOBAL_DIRECT_L_RAND_NOSCAL ,
318 which are versions of DIRECT where the dimensions are not rescaled to
319 a unit hypercube (which means that dimensions with larger bounds are
320 given more weight).
321 .TP 
322 .B NLOPT_GN_ORIG_DIRECT_L
323 A global (G) derivative-free optimization using the DIRECT-L algorithm
324 as above, along with
325 .B NLOPT_GN_ORIG_DIRECT
326 which is the original DIRECT algorithm.  Unlike 
327 .B NLOPT_GN_DIRECT_L 
328 above, these two algorithms refer to code based on the original
329 Fortran code of Gablonsky et al., which has some hard-coded
330 limitations on the number of subdivisions etc. and does not support
331 all of the NLopt stopping criteria, but on the other hand supports
332 arbitrary nonlinear constraints as described above.
333 .TP 
334 .B NLOPT_GD_STOGO
335 Global (G) optimization using the StoGO algorithm by Madsen et al.  StoGO
336 exploits gradient information (D) (which must be supplied by the
337 objective) for its local searches, and performs the global search by a
338 branch-and-bound technique.  Only bound-constrained optimization
339 is supported.  There is also another variant of this algorithm,
340 .BR NLOPT_GD_STOGO_RAND ,
341 which is a randomized version of the StoGO search scheme.  The StoGO
342 algorithms are only available if NLopt is compiled with C++ enabled,
343 and should be linked via -lnlopt_cxx (via a C++ compiler, in order
344 to link the C++ standard libraries).
345 .TP 
346 .B NLOPT_LN_SUBPLEX
347 Perform a local (L) derivative-free (N) optimization, starting at
348 .IR x ,
349 using the Subplex algorithm of Rowan et al., which is an improved
350 variant of Nelder-Mead simplex algorithm.  (Like Nelder-Mead, Subplex
351 often works well in practice, even for discontinuous objectives, but
352 there is no rigorous guarantee that it will converge.)  Subplex is
353 best for unconstrained optimization, but constrained optimization also
354 works (both for simple bound constraints via
355 .I lb
356 and
357 .I ub
358 as well as nonlinear constraints via the crude technique of returning
359 +Inf when the constraints are violated, as explained above).
360 .TP
361 .B NLOPT_LN_PRAXIS
362 Local (L) derivative-free (N) optimization using the principal-axis
363 method, based on code by Richard Brent.  Designed for unconstrained
364 optimization, although bound constraints are supported too (via the
365 inefficient method of returning +Inf when the constraints are violated).
366 .TP
367 .B NLOPT_LD_LBFGS
368 Local (L) gradient-based (D) optimization using the limited-memory BFGS
369 (L-BFGS) algorithm.  (The objective function must supply the
370 gradient.)  Unconstrained optimization is supported in addition to
371 simple bound constraints (see above).  Based on an implementation by
372 Luksan et al.
373 .TP
374 .B NLOPT_LD_VAR2
375 Local (L) gradient-based (D) optimization using a shifted limited-memory
376 variable-metric method based on code by Luksan et al., supporting both
377 unconstrained and bound-constrained optimization.  
378 .B NLOPT_LD_VAR2
379 uses a rank-2 method, while 
380 .B .B NLOPT_LD_VAR1
381 is another variant using a rank-1 method.
382 .TP
383 .B NLOPT_LD_TNEWTON_PRECOND_RESTART
384 Local (L) gradient-based (D) optimization using an
385 LBFGS-preconditioned truncated Newton method with steepest-descent
386 restarting, based on code by Luksan et al., supporting both
387 unconstrained and bound-constrained optimization.  There are several
388 other variants of this algorithm:
389 .B NLOPT_LD_TNEWTON_PRECOND 
390 (same without restarting), 
391 .B NLOPT_LD_TNEWTON_RESTART
392 (same without preconditioning), and
393 .B NLOPT_LD_TNEWTON
394 (same without restarting or preconditioning).
395 .TP
396 .B NLOPT_GN_CRS2_LM
397 Global (G) derivative-free (N) optimization using the controlled random
398 search (CRS2) algorithm of Price, with the "local mutation" (LM)
399 modification suggested by Kaelo and Ali.
400 .TP
401 \fBNLOPT_GD_MLSL_LDS\fR, \fBNLOPT_GN_MLSL_LDS\fR
402 Global (G) derivative-based (D) or derivative-free (N) optimization
403 using the multi-level single-linkage (MLSL) algorithm with a
404 low-discrepancy sequence (LDS).  This algorithm executes a quasi-random
405 (LDS) sequence of local searches, with a clustering heuristic to
406 avoid multiple local searches for the same local minimum.  The local
407 search uses the derivative/nonderivative algorithm set by
408 .I nlopt_set_local_search_algorithm
409 (currently defaulting to
410 .I NLOPT_LD_LBFGS
411 and
412 .I NLOPT_LN_SUBPLEX
413 for derivative/nonderivative searches, respectively).  There are also
414 two other variants, \fBNLOPT_GD_MLSL\fR and \fBNLOPT_GN_MLSL\fR, which use
415 pseudo-random numbers (instead of an LDS) as in the original MLSL algorithm.
416 .TP
417 .B NLOPT_LD_MMA
418 Local (L) gradient-based (D) optimization using the method of moving
419 asymptotes (MMA), or rather a refined version of the algorithm as
420 published by Svanberg (2002).  (NLopt uses an independent free-software/open-source
421 implementation of Svanberg's algorithm.)  The
422 .B NLOPT_LD_MMA
423 algorithm supports both bound-constrained and unconstrained optimization,
424 and also supports an arbitrary number (\fIm\fR) of nonlinear constraints
425 as described above.
426 .TP
427 .B NLOPT_LN_COBYLA
428 Local (L) derivative-free (N) optimization using the COBYLA algorithm
429 of Powell (Constrained Optimization BY Linear Approximations).
430 The
431 .B NLOPT_LN_COBYLA
432 algorithm supports both bound-constrained and unconstrained optimization,
433 and also supports an arbitrary number (\fIm\fR) of nonlinear constraints
434 as described above.
435 .TP
436 .B NLOPT_LN_NEWUOA
437 Local (L) derivative-free (N) optimization using the NEWUOA algorithm
438 of Powell, based on successive quadratic approximations of the objective
439 function. The
440 .B NLOPT_LN_NEWUOA
441 algorithm was originally designed only for unconstrained optimization,
442 and we only support bound constraints by an inefficient algorithm.
443 .SH STOPPING CRITERIA
444 Multiple stopping criteria for the optimization are supported, as
445 specified by the following arguments to
446 .BR nlopt_minimize_constrained ().
447 The optimization halts whenever any one of these criteria is
448 satisfied.  In some cases, the precise interpretation of the stopping
449 criterion depends on the optimization algorithm above (although we
450 have tried to make them as consistent as reasonably possible), and
451 some algorithms do not support all of the stopping criteria.
452 .sp
453 Important: you do not need to use all of the stopping criteria!  In most
454 cases, you only need one or two, and can set the remainder to values where
455 they do nothing (as described below).
456 .TP
457 .B minf_max
458 Stop when a function value less than or equal to
459 .I minf_max
460 is found.  Set to -Inf or NaN (see constraints section above) to disable.
461 .TP
462 .B ftol_rel
463 Relative tolerance on function value: stop when an optimization step
464 (or an estimate of the minimum) changes the function value by less
465 than
466 .I ftol_rel
467 multiplied by the absolute value of the function value.  (If there is any chance that your minimum function value is close to zero, you might want to set an absolute tolerance with
468 .I ftol_abs
469 as well.)  Disabled if non-positive.
470 .TP
471 .B ftol_abs
472 Absolute tolerance on function value: stop when an optimization step
473 (or an estimate of the minimum) changes the function value by less
474 than
475 .IR ftol_abs .
476 Disabled if non-positive.
477 .TP
478 .B xtol_rel
479 Relative tolerance on design variables: stop when an optimization step
480 (or an estimate of the minimum) changes every design variable by less
481 than
482 .I xtol_rel
483 multiplied by the absolute value of the design variable.  (If there is
484 any chance that an optimal design variable is close to zero, you
485 might want to set an absolute tolerance with
486 .I xtol_abs
487 as well.)  Disabled if non-positive.
488 .TP
489 .B xtol_abs
490 Pointer to an array of length
491 .I
492 n giving absolute tolerances on design variables: stop when an
493 optimization step (or an estimate of the minimum) changes every design
494 variable
495 .IR x [i]
496 by less than
497 .IR xtol_abs [i].
498 Disabled if non-positive, or if
499 .I xtol_abs
500 is NULL.
501 .TP
502 .B maxeval
503 Stop when the number of function evaluations exceeds
504 .IR maxeval .
505 (This is not a strict maximum: the number of function evaluations may
506 exceed
507 .I maxeval 
508 slightly, depending upon the algorithm.)  Disabled
509 if non-positive.
510 .TP
511 .B maxtime
512 Stop when the optimization time (in seconds) exceeds
513 .IR maxtime .
514 (This is not a strict maximum: the time may
515 exceed
516 .I maxtime
517 slightly, depending upon the algorithm and on how slow your function
518 evaluation is.)  Disabled if non-positive.
519 .SH RETURN VALUE
520 The value returned is one of the following enumerated constants.
521 .SS Successful termination (positive return values):
522 .TP
523 .B NLOPT_SUCCESS
524 Generic success return value.
525 .TP
526 .B NLOPT_MINF_MAX_REACHED
527 Optimization stopped because
528 .I minf_max
529 (above) was reached.
530 .TP
531 .B NLOPT_FTOL_REACHED
532 Optimization stopped because
533 .I ftol_rel
534 or
535 .I ftol_abs
536 (above) was reached.
537 .TP
538 .B NLOPT_XTOL_REACHED
539 Optimization stopped because
540 .I xtol_rel
541 or
542 .I xtol_abs
543 (above) was reached.
544 .TP
545 .B NLOPT_MAXEVAL_REACHED
546 Optimization stopped because
547 .I maxeval
548 (above) was reached.
549 .TP
550 .B NLOPT_MAXTIME_REACHED
551 Optimization stopped because
552 .I maxtime
553 (above) was reached.
554 .SS Error codes (negative return values):
555 .TP
556 .B NLOPT_FAILURE
557 Generic failure code.
558 .TP
559 .B NLOPT_INVALID_ARGS
560 Invalid arguments (e.g. lower bounds are bigger than upper bounds, an
561 unknown algorithm was specified, etcetera).
562 .TP
563 .B NLOPT_OUT_OF_MEMORY
564 Ran out of memory.
565 .SH PSEUDORANDOM NUMBERS
566 For stochastic optimization algorithms, we use pseudorandom numbers generated
567 by the Mersenne Twister algorithm, based on code from Makoto Matsumoto.
568 By default, the seed for the random numbers is generated from the system
569 time, so that they will be different each time you run the program.  If
570 you want to use deterministic random numbers, you can set the seed by
571 calling:
572 .sp
573 .BI "            void nlopt_srand(unsigned long " "seed" );
574 .sp
575 Some of the algorithms also support using low-discrepancy sequences (LDS),
576 sometimes known as quasi-random numbers.  NLopt uses the Sobol LDS, which
577 is implemented for up to 1111 dimensions.
578 .SH BUGS
579 Currently the NLopt library is in pre-alpha stage.  Most algorithms
580 currently do not support all termination conditions: the only
581 termination condition that is consistently supported right now is
582 .BR maxeval .
583 .SH AUTHORS
584 Written by Steven G. Johnson.
585 .PP
586 Copyright (c) 2007-2008 Massachusetts Institute of Technology.
587 .SH "SEE ALSO"
588 nlopt_minimize(3)