1 ## Copyright (C) 2004,2005,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 # usage: bfgsmin_example (to run) or edit bfgsmin_example (to examine)
18 # Shows how to call bfgsmin. There are two objective functions, the first
19 # supplies the analytic gradient, and the second does not. The true minimizer
20 # is at "location", a 50x1 vector (0.00, 0.02, 0.04 ..., 1.00).
21 # Note that limited memory bfgs is faster when the dimension is high.
22 # Also note that supplying analytic derivatives gives a speedup.
24 # Six examples are given:
25 # Example 1: regular bfgs, analytic gradient
26 # Example 2: same as Example 1, but verbose
27 # Example 3: limited memory bfgs, analytic gradient
28 # Example 4: regular bfgs, numeric gradient
29 # Example 5: limited memory bfgs, numeric gradient
30 # Example 6: regular bfgs, analytic gradient, minimize wrt second argument
32 # example obj. fn.: supplies analytic gradient
33 function [obj_value, gradient] = objective(theta, location)
34 x = theta - location + ones(rows(theta),1); # move minimizer to "location"
35 [obj_value, gradient] = rosenbrock(x);
38 # example obj. fn.: no gradient
39 function obj_value = objective2(theta, location)
40 x = theta - location + ones(rows(theta),1); # move minimizer to "location"
41 obj_value = rosenbrock(x);
45 dim = 20; # dimension of Rosenbrock function
46 theta0 = zeros(dim+1,1); # starting values
47 location = (0:dim)/dim; # true values
50 printf("EXAMPLE 1: Ordinary BFGS, using analytic gradient\n");
52 control = {Inf,1}; # maxiters, verbosity
53 [theta, obj_value, convergence] = bfgsmin("objective", {theta0, location}, control);
56 conv = norm(theta-location, 'inf');
59 printf("EXAMPLE 2: Same as Example 1, but verbose\n");
61 control = {-1;3}; # maxiters, verbosity
62 [theta, obj_value, convergence] = bfgsmin("objective", {theta0, location}, control);
65 conv = norm(theta-location, 'inf');
68 printf("EXAMPLE 3: Limited memory BFGS, using analytic gradient\n");
70 control = {-1;1;1;1;3}; # maxiters, verbosity, conv. requirement., arg_to_min, lbfgs memory
71 [theta, obj_value, convergence] = bfgsmin("objective", {theta0, location}, control);
74 conv = norm(theta-location, 'inf');
77 printf("EXAMPLE 4: Ordinary BFGS, using numeric gradient\n");
79 control = {-1;1}; # maxiters, verbosity
80 [theta, obj_value, convergence] = bfgsmin("objective2", {theta0, location}, control);
83 conv = norm(theta-location, 'inf');
86 printf("EXAMPLE 5: Limited memory BFGS, using numeric gradient\n");
88 control = {-1;1;1;1;3}; # maxiters, verbosity, conv. reg., arg_to_min, lbfgs memory
89 [theta, obj_value, convergence] = bfgsmin("objective2", {theta0, location}, control);
92 conv = norm(theta-location, 'inf');
96 printf("EXAMPLE 6: Funny case: minimize w.r.t. second argument, Ordinary BFGS, using numeric gradient\n");
98 control = {-1;1;1;2}; # maxiters, verbosity, conv. reg., arg_to_min
99 [theta, obj_value, convergence] = bfgsmin("objective2", {location, theta0}, control);
102 conv = norm(theta-location, 'inf');
105 printf("EXAMPLE 7: Ordinary BFGS, using numeric gradient, using non-default tolerances\n");
107 control = {-1;1;1;1;0;1e-6;1e-2;1e-2}; # maxiters, verbosity, conv. reg., arg_to_min, lbfgs memory, fun. tol., param. tol., gradient tol.
108 [theta, obj_value, convergence] = bfgsmin("objective2", {theta0, location}, control);
111 conv = norm(theta-location, 'inf');
115 printf("EXAMPLE 1: Ordinary BFGS, using analytic gradient\n");
117 printf("Success!! :-)\n");
119 printf("Failure?! :-(\n");
121 printf("Elapsed time = %f\n\n\n\n",t1);
123 printf("EXAMPLE 2: Same as Example 1, but verbose\n");
125 printf("Success!! :-)\n");
127 printf("Failure?! :-(\n");
129 printf("Elapsed time = %f\n\n\n\n",t2);
131 printf("EXAMPLE 3: Limited memory BFGS, using analytic gradient\n");
133 printf("Success!! :-)\n");
135 printf("Failure?! :-(\n");
137 printf("Elapsed time = %f\n\n\n\n",t3);
139 printf("EXAMPLE 4: Ordinary BFGS, using numeric gradient\n");
141 printf("Success!! :-)\n");
143 printf("Failure?! :-(\n");
145 printf("Elapsed time = %f\n\n\n\n",t4);
147 printf("EXAMPLE 5: Limited memory BFGS, using numeric gradient\n");
149 printf("Success!! :-)\n");
151 printf("Failure?! :-(\n");
153 printf("Elapsed time = %f\n\n\n\n",t5);
155 printf("EXAMPLE 6: Funny case: minimize w.r.t. second argument, Ordinary BFGS, using numeric gradient\n");
157 printf("Success!! :-)\n");
159 printf("Failure?! :-(\n");
161 printf("Elapsed time = %f\n\n\n\n",t6);
163 printf("EXAMPLE 7: Ordinary BFGS, using numeric gradient, using non-default tolerances\n");
165 printf("Success!! :-)\n");
167 printf("Failure?! :-(\n");
169 printf("Elapsed time = %f\n\n\n\n",t7);