]> Creatis software - CreaPhase.git/blob - octave_packages/optim-1.2.0/bfgsmin_example.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / optim-1.2.0 / bfgsmin_example.m
1 ## Copyright (C) 2004,2005,2006 Michael Creel <michael.creel@uab.es>
2 ##
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
6 ## version.
7 ##
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
11 ## details.
12 ##
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/>.
15
16 # usage: bfgsmin_example (to run) or edit bfgsmin_example (to examine)
17 ##
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.
23 ##
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
31 1;
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);
36 endfunction
37
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);
42 endfunction
43
44 # initial values
45 dim = 20; # dimension of Rosenbrock function
46 theta0 = zeros(dim+1,1);  # starting values
47 location = (0:dim)/dim; # true values
48 location = location';
49
50 printf("EXAMPLE 1: Ordinary BFGS, using analytic gradient\n");
51 t=cputime();
52 control = {Inf,1};  # maxiters, verbosity
53 [theta, obj_value, convergence] = bfgsmin("objective", {theta0, location}, control);
54 fflush(1);
55 t1 = cputime() - t;
56 conv = norm(theta-location, 'inf');
57 test1 = conv < 1e-5;
58
59 printf("EXAMPLE 2: Same as Example 1, but verbose\n");
60 t=cputime();
61 control = {-1;3};  # maxiters, verbosity
62 [theta, obj_value, convergence] = bfgsmin("objective", {theta0, location}, control);
63 fflush(1);
64 t2 = cputime() - t;
65 conv = norm(theta-location, 'inf');
66 test2 = conv < 1e-5;
67
68 printf("EXAMPLE 3: Limited memory BFGS, using analytic gradient\n");
69 t=cputime();
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);
72 fflush(1);
73 t3 = cputime() - t;
74 conv = norm(theta-location, 'inf');
75 test3 = conv < 1e-5;
76
77 printf("EXAMPLE 4: Ordinary BFGS, using numeric gradient\n");
78 t=cputime();
79 control = {-1;1};  # maxiters, verbosity
80 [theta, obj_value, convergence] = bfgsmin("objective2", {theta0, location}, control);
81 fflush(1);
82 t4 = cputime() - t;
83 conv = norm(theta-location, 'inf');
84 test4 = conv < 1e-5;
85
86 printf("EXAMPLE 5: Limited memory BFGS, using numeric gradient\n");
87 t=cputime();
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);
90 fflush(1);
91 t5 = cputime() - t;
92 conv = norm(theta-location, 'inf');
93 test5 = conv < 1e-5;
94
95
96 printf("EXAMPLE 6: Funny case: minimize w.r.t. second argument, Ordinary BFGS, using numeric gradient\n");
97 t=cputime();
98 control = {-1;1;1;2};  # maxiters, verbosity, conv. reg., arg_to_min
99 [theta, obj_value, convergence] = bfgsmin("objective2", {location, theta0}, control);
100 fflush(1);
101 t6 = cputime() - t;
102 conv = norm(theta-location, 'inf');
103 test6 = conv < 1e-5;
104
105 printf("EXAMPLE 7: Ordinary BFGS, using numeric gradient, using non-default tolerances\n");
106 t=cputime();
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);
109 fflush(1);
110 t7 = cputime() - t;
111 conv = norm(theta-location, 'inf');
112 test7 = conv < 1e-2;
113
114
115 printf("EXAMPLE 1: Ordinary BFGS, using analytic gradient\n");
116 if test1
117         printf("Success!! :-)\n");
118 else
119         printf("Failure?! :-(\n");
120 endif
121 printf("Elapsed time = %f\n\n\n\n",t1);
122
123 printf("EXAMPLE 2: Same as Example 1, but verbose\n");
124 if test2
125         printf("Success!! :-)\n");
126 else
127         printf("Failure?! :-(\n");
128 endif
129 printf("Elapsed time = %f\n\n\n\n",t2);
130
131 printf("EXAMPLE 3: Limited memory BFGS, using analytic gradient\n");
132 if test3
133         printf("Success!! :-)\n");
134 else
135         printf("Failure?! :-(\n");
136 endif
137 printf("Elapsed time = %f\n\n\n\n",t3);
138
139 printf("EXAMPLE 4: Ordinary BFGS, using numeric gradient\n");
140 if test4
141         printf("Success!! :-)\n");
142 else
143         printf("Failure?! :-(\n");
144 endif
145 printf("Elapsed time = %f\n\n\n\n",t4);
146
147 printf("EXAMPLE 5: Limited memory BFGS, using numeric gradient\n");
148 if test5
149         printf("Success!! :-)\n");
150 else
151         printf("Failure?! :-(\n");
152 endif
153 printf("Elapsed time = %f\n\n\n\n",t5);
154
155 printf("EXAMPLE 6: Funny case: minimize w.r.t. second argument, Ordinary BFGS, using numeric gradient\n");
156 if test6
157         printf("Success!! :-)\n");
158 else
159         printf("Failure?! :-(\n");
160 endif
161 printf("Elapsed time = %f\n\n\n\n",t6);
162
163 printf("EXAMPLE 7: Ordinary BFGS, using numeric gradient, using non-default tolerances\n");
164 if test7
165         printf("Success!! :-)\n");
166 else
167         printf("Failure?! :-(\n");
168 endif
169 printf("Elapsed time = %f\n\n\n\n",t7);
170