1 ## Copyright (C) 2006 Michael Creel <michael.creel@uab.es>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## bfgsmin: bfgs or limited memory bfgs minimization of function
18 ## Usage: [x, obj_value, convergence, iters] = bfgsmin(f, args, control)
20 ## The function must be of the form
21 ## [value, return_2,..., return_m] = f(arg_1, arg_2,..., arg_n)
22 ## By default, minimization is w.r.t. arg_1, but it can be done
23 ## w.r.t. any argument that is a vector. Numeric derivatives are
24 ## used unless analytic derivatives are supplied. See bfgsmin_example.m
28 ## * f: name of function to minimize (string)
29 ## * args: a cell array that holds all arguments of the function
30 ## The argument with respect to which minimization is done
32 ## * control: an optional cell array of 1-8 elements. If a cell
33 ## array shorter than 8 elements is provided, the trailing elements
34 ## are provided with default values.
35 ## * elem 1: maximum iterations (positive integer, or -1 or Inf for unlimited (default))
36 ## * elem 2: verbosity
37 ## 0 = no screen output (default)
38 ## 1 = only final results
39 ## 2 = summary every iteration
40 ## 3 = detailed information
41 ## * elem 3: convergence criterion
42 ## 1 = strict (function, gradient and param change) (default)
43 ## 0 = weak - only function convergence required
44 ## * elem 4: arg in f_args with respect to which minimization is done (default is first)
45 ## * elem 5: (optional) Memory limit for lbfgs. If it's a positive integer
46 ## then lbfgs will be use. Otherwise ordinary bfgs is used
47 ## * elem 6: function change tolerance, default 1e-12
48 ## * elem 7: parameter change tolerance, default 1e-6
49 ## * elem 8: gradient tolerance, default 1e-5
53 ## * obj_value: the value of f() at x
54 ## * convergence: 1 if normal conv, other values if not
55 ## * iters: number of iterations performed
57 ## Example: see bfgsmin_example.m
59 function [parameter, obj, convergence, iters] = bfgsmin(f, f_args, control)
61 # check number and types of arguments
62 if ((nargin < 2) || (nargin > 3))
63 usage("bfgsmin: you must supply 2 or 3 arguments");
65 if (!ischar(f)) usage("bfgsmin: first argument must be string holding objective function name"); endif
66 if (!iscell(f_args)) usage("bfgsmin: second argument must cell array of function arguments"); endif
69 usage("bfgsmin: 3rd argument must be a cell array of 1-8 elements");
71 if (length(control) > 8)
72 usage("bfgsmin: 3rd argument must be a cell array of 1-8 elements");
77 # provide defaults for missing controls
78 if (length(control) == 0) control{1} = -1; endif # limit on iterations
79 if (length(control) == 1) control{2} = 0; endif # level of verbosity
80 if (length(control) == 2) control{3} = 1; endif # strong (function, gradient and parameter change) convergence required?
81 if (length(control) == 3) control{4} = 1; endif # argument with respect to which minimization is done
82 if (length(control) == 4) control{5} = 0; endif # memory for lbfgs: 0 uses ordinary bfgs
83 if (length(control) == 5) control{6} = 1e-10; endif # tolerance for function convergence
84 if (length(control) == 6) control{7} = 1e-6; endif # tolerance for parameter convergence
85 if (length(control) == 7) control{8} = 1e-5; endif # tolerance for gradient convergence
87 # validity checks on all controls
89 if (((tmp !=Inf) && (tmp != -1)) && (tmp > 0 && (mod(tmp,1) != 0)))
90 usage("bfgsmin: 1st element of 3rd argument (iteration limit) must be Inf or positive integer");
93 if ((tmp < 0) || (tmp > 3) || (mod(tmp,1) != 0))
94 usage("bfgsmin: 2nd element of 3rd argument (verbosity level) must be 0, 1, 2, or 3");
97 if ((tmp != 0) && (tmp != 1))
98 usage("bfgsmin: 3rd element of 3rd argument (strong/weak convergence) must be 0 (weak) or 1 (strong)");
101 if ((tmp < 1) || (tmp > length(f_args)) || (mod(tmp,1) != 0))
102 usage("bfgsmin: 4th element of 3rd argument (arg with respect to which minimization is done) must be an integer that indicates one of the elements of f_args");
105 if ((tmp < 0) || (mod(tmp,1) != 0))
106 usage("bfgsmin: 5th element of 3rd argument (memory for lbfgs must be zero (regular bfgs) or a positive integer");
110 usage("bfgsmin: 6th element of 3rd argument (tolerance for function convergence) must be a positive real number");
114 usage("bfgsmin: 7th element of 3rd argument (tolerance for parameter convergence) must be a positive real number");
118 usage("bfgsmin: 8th element of 3rd argument (tolerance for gradient convergence) must be a positive real number");
121 # check that the parameter we minimize w.r.t. is a vector
123 theta = f_args{minarg};
125 if (!isvector(theta)) usage("bfgsmin: minimization must be done with respect to a vector of parameters"); endif
126 f_args{minarg} = theta;
128 # now go ahead and do the minimization
129 [parameter, obj, convergence, iters] = __bfgsmin(f, f_args, control);