Unconstrained optimization with MINTOOLKIT for GNU octave
Abstract
The paper documents MINTOOLKIT for GNU Octave. MINTOOLKIT provides functions for minimization and numeric differentiation. The main algorithms are BFGS, LBFGS, and simulated annealing. Examples are given.
Full text
Unconstrained Optimization with MINTOOLKIT for GNU Octave Michael Creel∗ March 2004 Abstract The paper documents MINTOOLKIT for GNU Octave. MINTOOLKIT provides functions for minimization and numeric differentiation. The main algorithms are BFGS, LBFGS, and simulated annealing. Examples are given. 1 Introduction Unconstrained optimization is a basic tool in many disciplines, and there is no need to discuss its importance. This paper discusses how unconstrained optimization may be done using GNU Octave (Eaton, www.octave.org) using the package MINTOOLKIT (Creel, http://pareto.uab.es/mcreel/MINTOOLKIT). If you would just like to see some examples of how to use the algorithms, skip to section 4. Otherwise, here’s some introductory information that explains how algorithms we selected for inclusion into MINTOOLKIT. 1.1 Types of problems We first briefly discuss types of optimization problems, in order to identify the cases where GNU Octave will be a good platform for analysis, and which of the algorithms in MINTOOLKIT will likely work well for given cases. 1.1.1 Large/small What is the number of parameters to be minimized? Let kbe the number of parameters. Memory usage of an algorithm will be some function f(k). If this ∗Dept. of Economics and Economic History, Universitat Autònoma de Barcelona. [email protected] 1
function is growing rapidly for a certain algorithm, that algorithm will cease to be useful for ”large” problems, since memory will be exhausted. Of course, ”large” is a relative term that increases over time as memory becomes cheaper. For ”small” problems, the speed of convergence of the algorithm will be of primary importance, since memory resources will not be a bottleneck. 1.1.2 Continuous/discontinuous Gradient-based methods such as Newton’s algorithm or quasi-Newton methods rely on the function being differentiable. This will not hold if the function is not continuous. Search-type algorithms will be appropriate here. 1.1.3 Convex/nonconvex A convex objective function will have a single global minimizer, whereas nonconvex functions may have additional local minima. Quasi-Newton methods only use local information in their updates, so they may well converge to a non-global minimum, depending upon starting values. A possible solution is to try a number of starting values. This is likely to work well if the nonconvexity problem is not too severe. When there are many local minima, a search-type algorithm may become more efficient, since the problem of local minima is dealt with automatically and doesn’t require the analysts’ intervention. After all, who’s time is more important, yours, or your computer’s? 1.1.4 Costly/cheap Can the objective function be evaluated quickly, or is it time-consuming? Octave is an interpreted language, and is in general slower than FORTRAN, C, or similar. So expensive objective functions are best not implemented in pure Octave. One might go to a different environment for analysis, but in fact it is relatively easy to convert objective functions written in Octave to C++, and call them dynamically from Octave scripts. In this way, the expensive calculations are done using a fast language, while the user deals with the convenient, friendly Octave environment. In fact, C++ functions may make use of the Octave classes, so converting an Octave function to C++ is not very difficult. The algorithms in MINTOOLKIT serve as examples of how this may be done. 2
2 Algorithms The algorithms in MINTOOLKIT were chosen based upon many sources of information, two of which are Nocedal (1992), for continuous, convex problems, and Mittelmann for global minimization. The goal of MINTOOLKIT is to be able to solve well-posed problems quickly and robustly, using the smallest set of algorithms possible. ”Well-posed” is an important adjective here - MINTOOLKIT is not meant to be able to solve poorly conditioned problems or problems that can easily fall into numeric precision traps. Please pay attention to how your data is scaled, try to ”bullet-proof” your objective function to avoid divisions by zero, etc. Nevertheless, if you find bugs, or have suggestions or comments, please contact the author. 2.1 BFGSMIN The BFGS algorithm is probably the most widely-used quasi-Newton method for moderately-sized continuous problems that are not extremely nonconvex. It is more robust than other quasi-Newton methods such as DFP (Nocedal, 1992), and it is faster than Newton’s method, since the Hessian matrix need not be calculated. One could easily create a Newton algorithm using the source code for bfgsmin. 2.2 LBFGSMIN For large problems, the BFGS algorithm may not be feasible, since it requires storing a k×kmatrix. The LBFGS (”L” is for ”limited memory”) method is able to store all information for updates in vectors, which substantially reduced memory requirements. It may also be faster than the BFGS algorithm in some circumstances, since it may use fewer floating point operations per iteration, depending upon the size of the problem. While it will usually require more iterations that BFGS, it may be faster if each iteration is faster. Liu and Nocecal 1989 is a reference. 2.3 SAMIN When problems are mildly nonconvex, the quasi-Newton methods above, combined with a number of trial start values, may be the fastest way to find the global minimum. This solution is implemented in battery.m. But when the problem becomes less smooth, with many local minima, this solution may fail. Simulated annealing is one of the algorithms that works well in this case. The 3
implementation by Goffe has been used widely, and is the basis for the version included in MINTOOLKIT. An additional reference is Goffe (1996). samin differs from the Goffe code in two important ways: 1. The ”temperature” is determined automatically. In a first stage, the temperature is increased until the active search region covers the entire parameter space defined as the k-dimensional rectangle ×k j=1(lbj,ubj)where lbjand ubjare the jth elements of the LB and UB vectors that are the lower and upper bounds for the parameters (these are user-specified arguments to samin. Once this is achieved, the temperature decreases as usual. 2. Convergence is defined as two conditions holding simultaneously. (a) The last NEPS best function values cannot differ by more than FUNCTOL. This is as in Goffe’s code. (b) The width of the search interval must be less than PARAMTOL for each parameter. This allows to avoid accepting points on a flat plateau. 3 Obtaining the code •MINTOOLKIT is available directly from the author, at http://pareto.uab.es/mcreel/MINTOOLKIT. If you get it this way, uncompress the file where you like, change to the MINTOOLKIT directory, and compile by typing ”make all” (this supposes that you have Octave installed already). Make sure that Octave knows where the MINTOOLKIT directory is. This option guarantees that you have the most recent version. •Otherwise, you can obtain MINTOOLKIT as part of the octave-forge package. •If you happen to be running Debian Linux, you can install a pre-compiled version of octave-forge and all required files by typing ”apt-get install octave-forge”. This is the easiest (and recommended) option. 4 Examples MINTOOLKIT contains some functions for use by users, and some other functions that users can ignore. The functions for users are 4
Function Purpose bfgsmin Ordinary BFGS algorithm battery Calls bfgsmin with a set of starting values lbfgsmin Limited-memory BFGS, for large problems samin Simulated annealing, for global minimization numgradient numeric first derivative of vector-valued function numhessian numeric second derivative matrix This section gives some very simple examples of the use of the algorithms and functions in MINTOOLKIT. The first examples are intended to clearly illustrate how to use the algorithms. Realism is not important. Then some more difficult problems are considered. The functions in MINTOOLKIT allow minimization or differentiation with respect to any of the arguments of a function, holding the other arguments fixed. The other arguments can include data or fixed parameters of the function, for example. The argument with respect to which minimization or differentiation is done is denoted by minarg, which by default is equal to 1. Any function to be minimized or differentiated by algorithms in MINTOOLKIT must follow one of the forms value =f(arg1,arg2, ..., argp) [value,return2, ..., returnn] = f(arg1,arg2, ..., argp) Special case: If the second form is used and return2is a k×1 vector, where kis the dimension of minarg, then is assumed to be the gradient of fwith respect to minarg, if the algorithm called uses the gradient. Otherwise, it (and any other returns from f) are ignored by MINTOOLKIT. 4.1 Minimization 4.1.1 bfgsmin bfgsmin is called as [theta,value,convergence] = b f gsmin(”f”, {args},{control}) The first argument ” f” is a string variable that holds the name of the function to be minimized. The second argument, args, is a cell array that hold the arguments of f. The third argument control is an optional cell array of 4 elements. The elements of control are described in Table 1. The outputs of bfgsmin are 5
Table 1: Controls for bfgsmin Element Purpose Default Value Other possible values 1 maxiters -1 (infinity) any positive integer 2 verbosity 0 1: summary every iteration; 2: only final summary 3 criterion 1: strict convergence (f, g, ∆p) 2: weak convergence (only f) 4 minarg 1: first argument int: 1 ≤minarg ≤k,k=#args obvious, except the code values that convergence can take on. These are -1 for no convergence, maxiters exceeded; 1: convergence according to the specified strong or weak criterion; 2: no convergence due to failure of the algorithm (e.g., the gradient calculation fails, or a stepsize cannot be found). Consider a simple example - minimizing a quadratic function. The program bfgsmin-example.m follows: 1; # This shows how to call bfgsmin.m # example obj. fn function obj_value = objective1(x, y) obj_value = x’*x + y’*y; endfunction # example obj. fn.: with gradient function [obj_value, gradient] = objective2(x, y) obj_value = x’*x + y’*y; gradient = 2*x; endfunction # Check bfgsmin, illustrating variations printf("EXAMPLE 1: Numeric gradient"); x0 = ones(2,1); y0 = 2*ones(2,1); control = {20,2,1,1}; # maxiters, verbosity, conv. reg., arg_to_min [theta, obj_value, convergence] = bfgsmin("objective1", {x0, y0}, control); printf("\n"); printf("EXAMPLE 2: Analytic gradient"); control = {20,2,1,1}; # maxiters, verbosity, conv. reg., arg_to_min 6
[theta, obj_value, convergence] = bfgsmin("objective2", {x0, y0}, control); printf("\n"); printf("EXAMPLE 3: Funny case - min wrt 2nd arg."); control = {20,2,1,2}; # maxiters, verbosity, conv. reg., arg_to_min [theta, obj_value, convergence] = bfgsmin("objective1", {x0, y0}, control); •The first example uses numeric derivatives, and minimizes with respect to x, the first argument of the objective function. The second argument, y, is treated as fixed. •The second example uses analytic derivatives, since it calls objective2, and minimizes with respect to x, the first argument of the objective function. The second argument, y, is treated as fixed. •The third example uses numeric derivatives, and minimizes with respect to y, the second argument of the objective function, since the 4th element of control,minarg, is 2 . The first argument, x, is treated as fixed. The output of running this example is EXAMPLE: Numeric gradient ================================================ BFGSMIN final results STRONG CONVERGENCE Used numeric gradient Obj. fn. value 8.000000 Iteration 1 Function conv 1 Param conv 1 Gradient conv 1 param gradient change 0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 ================================================ EXAMPLE: Analytic gradient ================================================ BFGSMIN final results STRONG CONVERGENCE 7
Used analytic gradient Obj. fn. value 8.000000 Iteration 1 Function conv 1 Param conv 1 Gradient conv 1 param gradient change 0.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 ================================================ EXAMPLE: Funny case - min wrt 2nd arg. ================================================ BFGSMIN final results STRONG CONVERGENCE Used numeric gradient Obj. fn. value 2.000000 Iteration 1 Function conv 1 Param conv 1 Gradient conv 1 param gradient change 0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 ================================================ Notice that analytic gradients lead to faster convergence that do numeric gradients. Also note in the third example, where minarg=2, that minimization can be with respect to any of the arguments of the objective function. 4.1.2 lbfgsmin When the problem is very large, a limited-memory bfgs algorithm may be needed, if bfgsmin is not feasible due to memory limitations. lbfgsmin is called as [theta,value,convergence] = lb f gsmin(”f”, {args},{control}) The first argument ” f” is a string variable that holds the name of the function to be minimized. The second argument, args, is a cell array that hold the arguments of f. The third argument control is an optional cell array of 5 ele- 8
Table 2: Controls for lbfgsmin Element Purpose Default Value Other possible values 1 maxiters -1 (infinity) any positive integer 2 verbosity 0 1: summary every iteration; 2: only final summary 3 criterion 1: strict convergence (f, g, ∆p) 2: weak convergence (only f) 4 minarg 1: first argument int: 1 ≤minarg ≤k,k=#args 5 memory 5 any positive integer ments. The elements of control are the same as for bfgsmin, except that there is one more element that controls how many iterations are used to form the quasi-Hessian matrix (this is the memory of the method). The control vector is fully described in Table 3. You can easily modify the above example to use the lbfgsmin method. It is possible that lbfgsmin can outperform bfgsmin even when memory is not an issue. Remember that both of these algorithms are approximating the Hessian matrix using previous gradient evaluations. If the true Hessian is changing rapidly, then a limited memory approximation may be better than a long memory approximation. The Rosenbrock function is such a case. The program lbfgsmin-example.m minimizes a 200-dimensional Rosenbrock function using both algorithms. The output EXAMPLE: LBFGSMIN ================================================ LBFGSMIN final results STRONG CONVERGENCE Used analytic gradient Obj. fn. value 0.000000 Iteration 1117 Function conv 1 Param conv 1 Gradient conv 1 param gradient change 1.0000 -0.0000 0.0000 1.0000 0.0000 -0.0000 1.0000 -0.0000 0.0000 1.0000 0.0000 -0.0000 < 196 similar lines snipped> ================================================ If it converged to true minimizer, print a 1, else a 0: 1 9
0.0000 -7699999.9763 99.9789 0.0000 -9240000.0188 119.9747 0.0000 -10779999.9825 139.9705 0.0000 -12319999.9463 159.9663 0.0000 -13859999.9888 179.9621 0.0000 -15399999.9526 199.9578 BFGSMIN intermediate results: Iteration 67 Stepsize 0.0000000 Using numeric gradient Objective function value 0.0000000006 Function conv 1 Param conv 1 Gradient conv 1 params gradient change 9.9989 -0.0000 0.0000 20.0013 0.0000 0.0000 30.0011 0.0000 -0.0000 39.9991 -0.0000 0.0000 49.9974 -0.0000 0.0000 60.0010 0.0000 -0.0000 69.9972 -0.0000 -0.0000 80.0018 0.0000 0.0000 90.0004 0.0000 0.0000 99.9988 -0.0000 0.0000 PROPERLY SCALED DATA BFGSMIN intermediate results: Iteration 0 Stepsize 0.0083444 Using numeric gradient Objective function value 100.0000000000 Function conv 0 Param conv 0 Gradient conv 0 params gradient change 0.0000 -40.0000 0.3338 0.0000 -40.0000 0.3338 0.0000 -40.0000 0.3338 0.0000 -40.0000 0.3338 0.0000 -40.0000 0.3338 16
0.0000 -40.0000 0.3338 0.0000 -40.0000 0.3338 0.0000 -40.0000 0.3338 0.0000 -40.0000 0.3338 0.0000 -40.0000 0.3338 BFGSMIN intermediate results: Iteration 25 Stepsize 0.0000000 Using numeric gradient Objective function value 0.0000000000 Function conv 1 Param conv 1 Gradient conv 1 params gradient change 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000 -0.0000 0.0000 You can see that the scaled data gives a more accurate solution, using less than half the iterations. Bullet-proofing Writing your objective function so that it cannot return NaN or otherwise crash can save a lot of grief. Insert something like if (((abs(obj_value) == Inf)) || (isnan(obj_value))) obj_value = realmax; endif at the end of the objective function, and then return obj_value. This way, parameter values that lead to crashes are penalized, and will be avoided automatically. 17
4.2 Numeric differentiation numgradient and numhessian can be used for numeric differentiation. numgradient returns the derivative of an n×1 vector-valued function with respect to a k×1 vector in a n×kmatrix. numhessian returns the derivative of a real-valued function with respect to a k×1 vector in a k×kmatrix. Both functions are quite accurate. numderivatives.m, which follows, shows how it can be done. function obj_value = vectorvalued(x) obj_value = sin(x); endfunction function obj_value = realvalued(x) obj_value = sum(sin(x)); endfunction function obj_value = twoargs(x,y) obj_value = x’*x + y’*y; endfunction x = [0;pi/2; pi; 3*pi/2; 2*pi]; printf("gradient of vector-valued f_i = sin(x_i)\n"); g = numgradient("vectorvalued", {x}); g H = numhessian("realvalued", {x}); H x = 1:3; x = x’; y = 4:6; y = y’; printf("gradient of function of two args\n"); g = numgradient("twoargs", {x,y}, 1); g g = numgradient("twoargs", {x,y}, 2); g The results are: gradient of vector-valued f_i = sin(x_i) 18
g = 1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 -1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 1.00000 H = -0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 -1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 -0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 gradient of function of two args g = 2.0000 4.0000 6.0000 g = 8.0000 10.0000 12.0000 5 Testing the code The program mgh-test.m allows testing the algorithms using the Moré-Garbow- Hillstrom test suite, obtained from the SolvOpt source code. You can compare the output with these results, if you like. Note that simply applying BFGS with a single start value will sometimes lead to a failure of convergence, or convergence to a non-global minimum. This is expected, considering the nature of the problems. See section 4.1.4 for an appropriate means of proceeding with these problems. If you find any bugs in the code, please contact me. 19
References [1] Eaton, J.W., http://www.octave.org/ [2] Liu and Nocedal, http://www.ece.northwestern.edu/~nocedal/PDFfiles/limited-memory.pdf [3] Mittelmann, http://plato.asu.edu/topics/problems/global.html [4] Nocedal (1992), http://www.ece.northwestern.edu/~nocedal/PDFfiles/acta.pdf [5] Goffe, http://www.netlib.no/netlib/opt/simann.f [6] Goffe, "SIMANN: A Global Optimization Algorithm using Simulated Annealing " Studies in Nonlinear Dynamics & Econometrics, Oct96, Vol. 1 Issue 3. 20