Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/netlab3.3/demopt1.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function demopt1(xinit) | |
2 %DEMOPT1 Demonstrate different optimisers on Rosenbrock's function. | |
3 % | |
4 % Description | |
5 % The four general optimisers (quasi-Newton, conjugate gradients, | |
6 % scaled conjugate gradients, and gradient descent) are applied to the | |
7 % minimisation of Rosenbrock's well known `banana' function. Each | |
8 % optimiser is run for at most 100 cycles, and a stopping criterion of | |
9 % 1.0e-4 is used for both position and function value. At the end, the | |
10 % trajectory of each algorithm is shown on a contour plot of the | |
11 % function. | |
12 % | |
13 % DEMOPT1(XINIT) allows the user to specify a row vector with two | |
14 % columns as the starting point. The default is the point [-1 1]. Note | |
15 % that the contour plot has an x range of [-1.5, 1.5] and a y range of | |
16 % [-0.5, 2.1], so it is best to choose a starting point in the same | |
17 % region. | |
18 % | |
19 % See also | |
20 % CONJGRAD, GRADDESC, QUASINEW, SCG, ROSEN, ROSEGRAD | |
21 % | |
22 | |
23 % Copyright (c) Ian T Nabney (1996-2001) | |
24 | |
25 % Initialise start point for search | |
26 if nargin < 1 | size(xinit) ~= [1 2] | |
27 xinit = [-1 1]; % Traditional start point | |
28 end | |
29 | |
30 % Find out if flops is available (i.e. pre-version 6 Matlab) | |
31 v = version; | |
32 if (str2num(strtok(v, '.')) >= 6) | |
33 flops_works = logical(0); | |
34 else | |
35 flops_works = logical(1); | |
36 end | |
37 | |
38 % Set up options | |
39 options = foptions; % Standard options | |
40 options(1) = -1; % Turn off printing completely | |
41 options(3) = 1e-8; % Tolerance in value of function | |
42 options(14) = 100; % Max. 100 iterations of algorithm | |
43 | |
44 clc | |
45 disp('This demonstration compares the performance of four generic') | |
46 disp('optimisation routines when finding the minimum of Rosenbrock''s') | |
47 disp('function y = 100*(x2-x1^2)^2 + (1-x1)^2.') | |
48 disp(' ') | |
49 disp('The global minimum of this function is at [1 1].') | |
50 disp(['Each algorithm starts at the point [' num2str(xinit(1))... | |
51 ' ' num2str(xinit(2)) '].']) | |
52 disp(' ') | |
53 disp('Press any key to continue.') | |
54 pause | |
55 | |
56 % Generate a contour plot of the function | |
57 a = -1.5:.02:1.5; | |
58 b = -0.5:.02:2.1; | |
59 [A, B] = meshgrid(a, b); | |
60 Z = rosen([A(:), B(:)]); | |
61 Z = reshape(Z, length(b), length(a)); | |
62 l = -1:6; | |
63 v = 2.^l; | |
64 fh1 = figure; | |
65 contour(a, b, Z, v) | |
66 title('Contour plot of Rosenbrock''s function') | |
67 hold on | |
68 | |
69 clc | |
70 disp('We now use quasi-Newton, conjugate gradient, scaled conjugate') | |
71 disp('gradient, and gradient descent with line search algorithms') | |
72 disp('to find a local minimum of this function. Each algorithm is stopped') | |
73 disp('when 100 cycles have elapsed, or if the change in function value') | |
74 disp('is less than 1.0e-8 or the change in the input vector is less than') | |
75 disp('1.0e-4 in magnitude.') | |
76 disp(' ') | |
77 disp('Press any key to continue.') | |
78 pause | |
79 | |
80 clc | |
81 x = xinit; | |
82 flops(0) | |
83 [x, options, errlog, pointlog] = quasinew('rosen', x, options, 'rosegrad'); | |
84 fprintf(1, 'For quasi-Newton method:\n') | |
85 fprintf(1, 'Final point is (%f, %f), value is %f\n', x(1), x(2), options(8)) | |
86 fprintf(1, 'Number of function evaluations is %d\n', options(10)) | |
87 fprintf(1, 'Number of gradient evaluations is %d\n', options(11)) | |
88 if flops_works | |
89 opt_flops = flops; | |
90 fprintf(1, 'Number of floating point operations is %d\n', opt_flops) | |
91 end | |
92 fprintf(1, 'Number of cycles is %d\n', size(pointlog, 1) - 1); | |
93 disp(' ') | |
94 | |
95 x = xinit; | |
96 flops(0) | |
97 [x, options, errlog2, pointlog2] = conjgrad('rosen', x, options, 'rosegrad'); | |
98 fprintf(1, 'For conjugate gradient method:\n') | |
99 fprintf(1, 'Final point is (%f, %f), value is %f\n', x(1), x(2), options(8)) | |
100 fprintf(1, 'Number of function evaluations is %d\n', options(10)) | |
101 fprintf(1, 'Number of gradient evaluations is %d\n', options(11)) | |
102 if flops_works | |
103 opt_flops = flops; | |
104 fprintf(1, 'Number of floating point operations is %d\n', ... | |
105 opt_flops) | |
106 end | |
107 fprintf(1, 'Number of cycles is %d\n', size(pointlog2, 1) - 1); | |
108 disp(' ') | |
109 | |
110 x = xinit; | |
111 flops(0) | |
112 [x, options, errlog3, pointlog3] = scg('rosen', x, options, 'rosegrad'); | |
113 fprintf(1, 'For scaled conjugate gradient method:\n') | |
114 fprintf(1, 'Final point is (%f, %f), value is %f\n', x(1), x(2), options(8)) | |
115 fprintf(1, 'Number of function evaluations is %d\n', options(10)) | |
116 fprintf(1, 'Number of gradient evaluations is %d\n', options(11)) | |
117 if flops_works | |
118 opt_flops = flops; | |
119 fprintf(1, 'Number of floating point operations is %d\n', opt_flops) | |
120 end | |
121 fprintf(1, 'Number of cycles is %d\n', size(pointlog3, 1) - 1); | |
122 disp(' ') | |
123 | |
124 x = xinit; | |
125 options(7) = 1; % Line minimisation used | |
126 flops(0) | |
127 [x, options, errlog4, pointlog4] = graddesc('rosen', x, options, 'rosegrad'); | |
128 fprintf(1, 'For gradient descent method:\n') | |
129 fprintf(1, 'Final point is (%f, %f), value is %f\n', x(1), x(2), options(8)) | |
130 fprintf(1, 'Number of function evaluations is %d\n', options(10)) | |
131 fprintf(1, 'Number of gradient evaluations is %d\n', options(11)) | |
132 if flops_works | |
133 opt_flops = flops; | |
134 fprintf(1, 'Number of floating point operations is %d\n', opt_flops) | |
135 end | |
136 fprintf(1, 'Number of cycles is %d\n', size(pointlog4, 1) - 1); | |
137 disp(' ') | |
138 disp('Note that gradient descent does not reach a local minimum in') | |
139 disp('100 cycles.') | |
140 disp(' ') | |
141 disp('On this problem, where the function is cheap to evaluate, the') | |
142 disp('computational effort is dominated by the algorithm overhead.') | |
143 disp('However on more complex optimisation problems (such as those') | |
144 disp('involving neural networks), computational effort is dominated by') | |
145 disp('the number of function and gradient evaluations. Counting these,') | |
146 disp('we can rank the algorithms: quasi-Newton (the best), conjugate') | |
147 disp('gradient, scaled conjugate gradient, gradient descent (the worst)') | |
148 disp(' ') | |
149 disp('Press any key to continue.') | |
150 pause | |
151 clc | |
152 disp('We now plot the trajectory of search points for each algorithm') | |
153 disp('superimposed on the contour plot.') | |
154 disp(' ') | |
155 disp('Press any key to continue.') | |
156 pause | |
157 plot(pointlog4(:,1), pointlog4(:,2), 'bd', 'MarkerSize', 6) | |
158 plot(pointlog3(:,1), pointlog3(:,2), 'mx', 'MarkerSize', 6, 'LineWidth', 2) | |
159 plot(pointlog(:,1), pointlog(:,2), 'k.', 'MarkerSize', 18) | |
160 plot(pointlog2(:,1), pointlog2(:,2), 'g+', 'MarkerSize', 6, 'LineWidth', 2) | |
161 lh = legend( 'Gradient Descent', 'Scaled Conjugate Gradients', ... | |
162 'Quasi Newton', 'Conjugate Gradients'); | |
163 | |
164 hold off | |
165 | |
166 clc | |
167 disp('Press any key to end.') | |
168 pause | |
169 close(fh1); | |
170 clear all; |