Daniel@0: function [x, options, flog, pointlog] = quasinew(f, x, options, gradf, ... Daniel@0: varargin) Daniel@0: %QUASINEW Quasi-Newton optimization. Daniel@0: % Daniel@0: % Description Daniel@0: % [X, OPTIONS, FLOG, POINTLOG] = QUASINEW(F, X, OPTIONS, GRADF) uses a Daniel@0: % quasi-Newton algorithm to find a local minimum of the function F(X) Daniel@0: % whose gradient is given by GRADF(X). Here X is a row vector and F Daniel@0: % returns a scalar value. The point at which F has a local minimum is Daniel@0: % returned as X. The function value at that point is returned in Daniel@0: % OPTIONS(8). A log of the function values after each cycle is Daniel@0: % (optionally) returned in FLOG, and a log of the points visited is Daniel@0: % (optionally) returned in POINTLOG. Daniel@0: % Daniel@0: % QUASINEW(F, X, OPTIONS, GRADF, P1, P2, ...) allows additional Daniel@0: % arguments to be passed to F() and GRADF(). Daniel@0: % Daniel@0: % The optional parameters have the following interpretations. Daniel@0: % Daniel@0: % OPTIONS(1) is set to 1 to display error values; also logs error Daniel@0: % values in the return argument ERRLOG, and the points visited in the Daniel@0: % return argument POINTSLOG. If OPTIONS(1) is set to 0, then only Daniel@0: % warning messages are displayed. If OPTIONS(1) is -1, then nothing is Daniel@0: % displayed. Daniel@0: % Daniel@0: % OPTIONS(2) is a measure of the absolute precision required for the Daniel@0: % value of X at the solution. If the absolute difference between the Daniel@0: % values of X between two successive steps is less than OPTIONS(2), Daniel@0: % then this condition is satisfied. Daniel@0: % Daniel@0: % OPTIONS(3) is a measure of the precision required of the objective Daniel@0: % function at the solution. If the absolute difference between the Daniel@0: % objective function values between two successive steps is less than Daniel@0: % OPTIONS(3), then this condition is satisfied. Both this and the Daniel@0: % previous condition must be satisfied for termination. Daniel@0: % Daniel@0: % OPTIONS(9) should be set to 1 to check the user defined gradient Daniel@0: % function. Daniel@0: % Daniel@0: % OPTIONS(10) returns the total number of function evaluations Daniel@0: % (including those in any line searches). Daniel@0: % Daniel@0: % OPTIONS(11) returns the total number of gradient evaluations. Daniel@0: % Daniel@0: % OPTIONS(14) is the maximum number of iterations; default 100. Daniel@0: % Daniel@0: % OPTIONS(15) is the precision in parameter space of the line search; Daniel@0: % default 1E-2. Daniel@0: % Daniel@0: % See also Daniel@0: % CONJGRAD, GRADDESC, LINEMIN, MINBRACK, SCG Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: % Set up the options. Daniel@0: if length(options) < 18 Daniel@0: error('Options vector too short') Daniel@0: end Daniel@0: Daniel@0: if(options(14)) Daniel@0: niters = options(14); Daniel@0: else Daniel@0: niters = 100; Daniel@0: end Daniel@0: Daniel@0: % Set up options for line search Daniel@0: line_options = foptions; Daniel@0: % Don't need a very precise line search Daniel@0: if options(15) > 0 Daniel@0: line_options(2) = options(15); Daniel@0: else Daniel@0: line_options(2) = 1e-2; % Default Daniel@0: end Daniel@0: % Minimal fractional change in f from Newton step: otherwise do a line search Daniel@0: min_frac_change = 1e-4; Daniel@0: Daniel@0: display = options(1); Daniel@0: Daniel@0: % Next two lines allow quasinew to work with expression strings Daniel@0: f = fcnchk(f, length(varargin)); Daniel@0: gradf = fcnchk(gradf, length(varargin)); Daniel@0: Daniel@0: % Check gradients Daniel@0: if (options(9)) Daniel@0: feval('gradchek', x, f, gradf, varargin{:}); Daniel@0: end Daniel@0: Daniel@0: nparams = length(x); Daniel@0: fnew = feval(f, x, varargin{:}); Daniel@0: options(10) = options(10) + 1; Daniel@0: gradnew = feval(gradf, x, varargin{:}); Daniel@0: options(11) = options(11) + 1; Daniel@0: p = -gradnew; % Search direction Daniel@0: hessinv = eye(nparams); % Initialise inverse Hessian to be identity matrix Daniel@0: j = 1; Daniel@0: if nargout >= 3 Daniel@0: flog(j, :) = fnew; Daniel@0: if nargout == 4 Daniel@0: pointlog(j, :) = x; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: while (j <= niters) Daniel@0: Daniel@0: xold = x; Daniel@0: fold = fnew; Daniel@0: gradold = gradnew; Daniel@0: Daniel@0: x = xold + p; Daniel@0: fnew = feval(f, x, varargin{:}); Daniel@0: options(10) = options(10) + 1; Daniel@0: Daniel@0: % This shouldn't occur, but rest of code depends on sd being downhill Daniel@0: if (gradnew*p' >= 0) Daniel@0: p = -p; Daniel@0: if options(1) >= 0 Daniel@0: warning('search direction uphill in quasinew'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % Does the Newton step reduce the function value sufficiently? Daniel@0: if (fnew >= fold + min_frac_change * (gradnew*p')) Daniel@0: % No it doesn't Daniel@0: % Minimize along current search direction: must be less than Newton step Daniel@0: [lmin, line_options] = feval('linemin', f, xold, p, fold, ... Daniel@0: line_options, varargin{:}); Daniel@0: options(10) = options(10) + line_options(10); Daniel@0: options(11) = options(11) + line_options(11); Daniel@0: % Correct x and fnew to be the actual search point we have found Daniel@0: x = xold + lmin * p; Daniel@0: p = x - xold; Daniel@0: fnew = line_options(8); Daniel@0: end Daniel@0: Daniel@0: % Check for termination Daniel@0: if (max(abs(x - xold)) < options(2) & max(abs(fnew - fold)) < options(3)) Daniel@0: options(8) = fnew; Daniel@0: return; Daniel@0: end Daniel@0: gradnew = feval(gradf, x, varargin{:}); Daniel@0: options(11) = options(11) + 1; Daniel@0: v = gradnew - gradold; Daniel@0: vdotp = v*p'; Daniel@0: Daniel@0: % Skip update to inverse Hessian if fac not sufficiently positive Daniel@0: if (vdotp*vdotp > eps*sum(v.^2)*sum(p.^2)) Daniel@0: Gv = (hessinv*v')'; Daniel@0: vGv = sum(v.*Gv); Daniel@0: u = p./vdotp - Gv./vGv; Daniel@0: % Use BFGS update rule Daniel@0: hessinv = hessinv + (p'*p)/vdotp - (Gv'*Gv)/vGv + vGv*(u'*u); Daniel@0: end Daniel@0: Daniel@0: p = -(hessinv * gradnew')'; Daniel@0: Daniel@0: if (display > 0) Daniel@0: fprintf(1, 'Cycle %4d Function %11.6f\n', j, fnew); Daniel@0: end Daniel@0: Daniel@0: j = j + 1; Daniel@0: if nargout >= 3 Daniel@0: flog(j, :) = fnew; Daniel@0: if nargout == 4 Daniel@0: pointlog(j, :) = x; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % If we get here, then we haven't terminated in the given number of Daniel@0: % iterations. Daniel@0: Daniel@0: options(8) = fold; Daniel@0: if (options(1) >= 0) Daniel@0: disp(maxitmess); Daniel@0: end