wolffd@0: function [x, options, flog, pointlog] = conjgrad(f, x, options, gradf, ... wolffd@0: varargin) wolffd@0: %CONJGRAD Conjugate gradients optimization. wolffd@0: % wolffd@0: % Description wolffd@0: % [X, OPTIONS, FLOG, POINTLOG] = CONJGRAD(F, X, OPTIONS, GRADF) uses a wolffd@0: % conjugate gradients algorithm to find the minimum of the function wolffd@0: % F(X) whose gradient is given by GRADF(X). Here X is a row vector and wolffd@0: % F returns a scalar value. The point at which F has a local minimum wolffd@0: % is returned as X. The function value at that point is returned in wolffd@0: % OPTIONS(8). A log of the function values after each cycle is wolffd@0: % (optionally) returned in FLOG, and a log of the points visited is wolffd@0: % (optionally) returned in POINTLOG. wolffd@0: % wolffd@0: % CONJGRAD(F, X, OPTIONS, GRADF, P1, P2, ...) allows additional wolffd@0: % arguments to be passed to F() and GRADF(). wolffd@0: % wolffd@0: % The optional parameters have the following interpretations. wolffd@0: % wolffd@0: % OPTIONS(1) is set to 1 to display error values; also logs error wolffd@0: % values in the return argument ERRLOG, and the points visited in the wolffd@0: % return argument POINTSLOG. If OPTIONS(1) is set to 0, then only wolffd@0: % warning messages are displayed. If OPTIONS(1) is -1, then nothing is wolffd@0: % displayed. wolffd@0: % wolffd@0: % OPTIONS(2) is a measure of the absolute precision required for the wolffd@0: % value of X at the solution. If the absolute difference between the wolffd@0: % values of X between two successive steps is less than OPTIONS(2), wolffd@0: % then this condition is satisfied. wolffd@0: % wolffd@0: % OPTIONS(3) is a measure of the precision required of the objective wolffd@0: % function at the solution. If the absolute difference between the wolffd@0: % objective function values between two successive steps is less than wolffd@0: % OPTIONS(3), then this condition is satisfied. Both this and the wolffd@0: % previous condition must be satisfied for termination. wolffd@0: % wolffd@0: % OPTIONS(9) is set to 1 to check the user defined gradient function. wolffd@0: % wolffd@0: % OPTIONS(10) returns the total number of function evaluations wolffd@0: % (including those in any line searches). wolffd@0: % wolffd@0: % OPTIONS(11) returns the total number of gradient evaluations. wolffd@0: % wolffd@0: % OPTIONS(14) is the maximum number of iterations; default 100. wolffd@0: % wolffd@0: % OPTIONS(15) is the precision in parameter space of the line search; wolffd@0: % default 1E-4. wolffd@0: % wolffd@0: % See also wolffd@0: % GRADDESC, LINEMIN, MINBRACK, QUASINEW, SCG wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: % Set up the options. wolffd@0: if length(options) < 18 wolffd@0: error('Options vector too short') wolffd@0: end wolffd@0: wolffd@0: if(options(14)) wolffd@0: niters = options(14); wolffd@0: else wolffd@0: niters = 100; wolffd@0: end wolffd@0: wolffd@0: % Set up options for line search wolffd@0: line_options = foptions; wolffd@0: % Need a precise line search for success wolffd@0: if options(15) > 0 wolffd@0: line_options(2) = options(15); wolffd@0: else wolffd@0: line_options(2) = 1e-4; wolffd@0: end wolffd@0: wolffd@0: display = options(1); wolffd@0: wolffd@0: % Next two lines allow conjgrad to work with expression strings wolffd@0: f = fcnchk(f, length(varargin)); wolffd@0: gradf = fcnchk(gradf, length(varargin)); wolffd@0: wolffd@0: % Check gradients wolffd@0: if (options(9)) wolffd@0: feval('gradchek', x, f, gradf, varargin{:}); wolffd@0: end wolffd@0: wolffd@0: options(10) = 0; wolffd@0: options(11) = 0; wolffd@0: nparams = length(x); wolffd@0: fnew = feval(f, x, varargin{:}); wolffd@0: options(10) = options(10) + 1; wolffd@0: gradnew = feval(gradf, x, varargin{:}); wolffd@0: options(11) = options(11) + 1; wolffd@0: d = -gradnew; % Initial search direction wolffd@0: br_min = 0; wolffd@0: br_max = 1.0; % Initial value for maximum distance to search along wolffd@0: tol = sqrt(eps); wolffd@0: wolffd@0: j = 1; wolffd@0: if nargout >= 3 wolffd@0: flog(j, :) = fnew; wolffd@0: if nargout == 4 wolffd@0: pointlog(j, :) = x; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: while (j <= niters) wolffd@0: wolffd@0: xold = x; wolffd@0: fold = fnew; wolffd@0: gradold = gradnew; wolffd@0: wolffd@0: gg = gradold*gradold'; wolffd@0: if (gg == 0.0) wolffd@0: % If the gradient is zero then we are done. wolffd@0: options(8) = fnew; wolffd@0: return; wolffd@0: end wolffd@0: wolffd@0: % This shouldn't occur, but rest of code depends on d being downhill wolffd@0: if (gradnew*d' > 0) wolffd@0: d = -d; wolffd@0: if options(1) >= 0 wolffd@0: warning('search direction uphill in conjgrad'); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: line_sd = d./norm(d); wolffd@0: [lmin, line_options] = feval('linemin', f, xold, line_sd, fold, ... wolffd@0: line_options, varargin{:}); wolffd@0: options(10) = options(10) + line_options(10); wolffd@0: options(11) = options(11) + line_options(11); wolffd@0: % Set x and fnew to be the actual search point we have found wolffd@0: x = xold + lmin * line_sd; wolffd@0: fnew = line_options(8); wolffd@0: wolffd@0: % Check for termination wolffd@0: if (max(abs(x - xold)) < options(2) & max(abs(fnew - fold)) < options(3)) wolffd@0: options(8) = fnew; wolffd@0: return; wolffd@0: end wolffd@0: wolffd@0: gradnew = feval(gradf, x, varargin{:}); wolffd@0: options(11) = options(11) + 1; wolffd@0: wolffd@0: % Use Polak-Ribiere formula to update search direction wolffd@0: gamma = ((gradnew - gradold)*(gradnew)')/gg; wolffd@0: d = (d .* gamma) - gradnew; wolffd@0: wolffd@0: if (display > 0) wolffd@0: fprintf(1, 'Cycle %4d Function %11.6f\n', j, line_options(8)); wolffd@0: end wolffd@0: wolffd@0: j = j + 1; wolffd@0: if nargout >= 3 wolffd@0: flog(j, :) = fnew; wolffd@0: if nargout == 4 wolffd@0: pointlog(j, :) = x; wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % If we get here, then we haven't terminated in the given number of wolffd@0: % iterations. wolffd@0: wolffd@0: options(8) = fold; wolffd@0: if (options(1) >= 0) wolffd@0: disp(maxitmess); wolffd@0: end