comparison toolboxes/FullBNT-1.0.7/netlab3.3/quasinew.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 [x, options, flog, pointlog] = quasinew(f, x, options, gradf, ...
2 varargin)
3 %QUASINEW Quasi-Newton optimization.
4 %
5 % Description
6 % [X, OPTIONS, FLOG, POINTLOG] = QUASINEW(F, X, OPTIONS, GRADF) uses a
7 % quasi-Newton algorithm to find a local minimum of the function F(X)
8 % whose gradient is given by GRADF(X). Here X is a row vector and F
9 % returns a scalar value. The point at which F has a local minimum is
10 % returned as X. The function value at that point is returned in
11 % OPTIONS(8). A log of the function values after each cycle is
12 % (optionally) returned in FLOG, and a log of the points visited is
13 % (optionally) returned in POINTLOG.
14 %
15 % QUASINEW(F, X, OPTIONS, GRADF, P1, P2, ...) allows additional
16 % arguments to be passed to F() and GRADF().
17 %
18 % The optional parameters have the following interpretations.
19 %
20 % OPTIONS(1) is set to 1 to display error values; also logs error
21 % values in the return argument ERRLOG, and the points visited in the
22 % return argument POINTSLOG. If OPTIONS(1) is set to 0, then only
23 % warning messages are displayed. If OPTIONS(1) is -1, then nothing is
24 % displayed.
25 %
26 % OPTIONS(2) is a measure of the absolute precision required for the
27 % value of X at the solution. If the absolute difference between the
28 % values of X between two successive steps is less than OPTIONS(2),
29 % then this condition is satisfied.
30 %
31 % OPTIONS(3) is a measure of the precision required of the objective
32 % function at the solution. If the absolute difference between the
33 % objective function values between two successive steps is less than
34 % OPTIONS(3), then this condition is satisfied. Both this and the
35 % previous condition must be satisfied for termination.
36 %
37 % OPTIONS(9) should be set to 1 to check the user defined gradient
38 % function.
39 %
40 % OPTIONS(10) returns the total number of function evaluations
41 % (including those in any line searches).
42 %
43 % OPTIONS(11) returns the total number of gradient evaluations.
44 %
45 % OPTIONS(14) is the maximum number of iterations; default 100.
46 %
47 % OPTIONS(15) is the precision in parameter space of the line search;
48 % default 1E-2.
49 %
50 % See also
51 % CONJGRAD, GRADDESC, LINEMIN, MINBRACK, SCG
52 %
53
54 % Copyright (c) Ian T Nabney (1996-2001)
55
56 % Set up the options.
57 if length(options) < 18
58 error('Options vector too short')
59 end
60
61 if(options(14))
62 niters = options(14);
63 else
64 niters = 100;
65 end
66
67 % Set up options for line search
68 line_options = foptions;
69 % Don't need a very precise line search
70 if options(15) > 0
71 line_options(2) = options(15);
72 else
73 line_options(2) = 1e-2; % Default
74 end
75 % Minimal fractional change in f from Newton step: otherwise do a line search
76 min_frac_change = 1e-4;
77
78 display = options(1);
79
80 % Next two lines allow quasinew to work with expression strings
81 f = fcnchk(f, length(varargin));
82 gradf = fcnchk(gradf, length(varargin));
83
84 % Check gradients
85 if (options(9))
86 feval('gradchek', x, f, gradf, varargin{:});
87 end
88
89 nparams = length(x);
90 fnew = feval(f, x, varargin{:});
91 options(10) = options(10) + 1;
92 gradnew = feval(gradf, x, varargin{:});
93 options(11) = options(11) + 1;
94 p = -gradnew; % Search direction
95 hessinv = eye(nparams); % Initialise inverse Hessian to be identity matrix
96 j = 1;
97 if nargout >= 3
98 flog(j, :) = fnew;
99 if nargout == 4
100 pointlog(j, :) = x;
101 end
102 end
103
104 while (j <= niters)
105
106 xold = x;
107 fold = fnew;
108 gradold = gradnew;
109
110 x = xold + p;
111 fnew = feval(f, x, varargin{:});
112 options(10) = options(10) + 1;
113
114 % This shouldn't occur, but rest of code depends on sd being downhill
115 if (gradnew*p' >= 0)
116 p = -p;
117 if options(1) >= 0
118 warning('search direction uphill in quasinew');
119 end
120 end
121
122 % Does the Newton step reduce the function value sufficiently?
123 if (fnew >= fold + min_frac_change * (gradnew*p'))
124 % No it doesn't
125 % Minimize along current search direction: must be less than Newton step
126 [lmin, line_options] = feval('linemin', f, xold, p, fold, ...
127 line_options, varargin{:});
128 options(10) = options(10) + line_options(10);
129 options(11) = options(11) + line_options(11);
130 % Correct x and fnew to be the actual search point we have found
131 x = xold + lmin * p;
132 p = x - xold;
133 fnew = line_options(8);
134 end
135
136 % Check for termination
137 if (max(abs(x - xold)) < options(2) & max(abs(fnew - fold)) < options(3))
138 options(8) = fnew;
139 return;
140 end
141 gradnew = feval(gradf, x, varargin{:});
142 options(11) = options(11) + 1;
143 v = gradnew - gradold;
144 vdotp = v*p';
145
146 % Skip update to inverse Hessian if fac not sufficiently positive
147 if (vdotp*vdotp > eps*sum(v.^2)*sum(p.^2))
148 Gv = (hessinv*v')';
149 vGv = sum(v.*Gv);
150 u = p./vdotp - Gv./vGv;
151 % Use BFGS update rule
152 hessinv = hessinv + (p'*p)/vdotp - (Gv'*Gv)/vGv + vGv*(u'*u);
153 end
154
155 p = -(hessinv * gradnew')';
156
157 if (display > 0)
158 fprintf(1, 'Cycle %4d Function %11.6f\n', j, fnew);
159 end
160
161 j = j + 1;
162 if nargout >= 3
163 flog(j, :) = fnew;
164 if nargout == 4
165 pointlog(j, :) = x;
166 end
167 end
168 end
169
170 % If we get here, then we haven't terminated in the given number of
171 % iterations.
172
173 options(8) = fold;
174 if (options(1) >= 0)
175 disp(maxitmess);
176 end