annotate toolboxes/FullBNT-1.0.7/netlab3.3/conjgrad.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [x, options, flog, pointlog] = conjgrad(f, x, options, gradf, ...
Daniel@0 2 varargin)
Daniel@0 3 %CONJGRAD Conjugate gradients optimization.
Daniel@0 4 %
Daniel@0 5 % Description
Daniel@0 6 % [X, OPTIONS, FLOG, POINTLOG] = CONJGRAD(F, X, OPTIONS, GRADF) uses a
Daniel@0 7 % conjugate gradients algorithm to find the minimum of the function
Daniel@0 8 % F(X) whose gradient is given by GRADF(X). Here X is a row vector and
Daniel@0 9 % F returns a scalar value. The point at which F has a local minimum
Daniel@0 10 % is returned as X. The function value at that point is returned in
Daniel@0 11 % OPTIONS(8). A log of the function values after each cycle is
Daniel@0 12 % (optionally) returned in FLOG, and a log of the points visited is
Daniel@0 13 % (optionally) returned in POINTLOG.
Daniel@0 14 %
Daniel@0 15 % CONJGRAD(F, X, OPTIONS, GRADF, P1, P2, ...) allows additional
Daniel@0 16 % arguments to be passed to F() and GRADF().
Daniel@0 17 %
Daniel@0 18 % The optional parameters have the following interpretations.
Daniel@0 19 %
Daniel@0 20 % OPTIONS(1) is set to 1 to display error values; also logs error
Daniel@0 21 % values in the return argument ERRLOG, and the points visited in the
Daniel@0 22 % return argument POINTSLOG. If OPTIONS(1) is set to 0, then only
Daniel@0 23 % warning messages are displayed. If OPTIONS(1) is -1, then nothing is
Daniel@0 24 % displayed.
Daniel@0 25 %
Daniel@0 26 % OPTIONS(2) is a measure of the absolute precision required for the
Daniel@0 27 % value of X at the solution. If the absolute difference between the
Daniel@0 28 % values of X between two successive steps is less than OPTIONS(2),
Daniel@0 29 % then this condition is satisfied.
Daniel@0 30 %
Daniel@0 31 % OPTIONS(3) is a measure of the precision required of the objective
Daniel@0 32 % function at the solution. If the absolute difference between the
Daniel@0 33 % objective function values between two successive steps is less than
Daniel@0 34 % OPTIONS(3), then this condition is satisfied. Both this and the
Daniel@0 35 % previous condition must be satisfied for termination.
Daniel@0 36 %
Daniel@0 37 % OPTIONS(9) is set to 1 to check the user defined gradient function.
Daniel@0 38 %
Daniel@0 39 % OPTIONS(10) returns the total number of function evaluations
Daniel@0 40 % (including those in any line searches).
Daniel@0 41 %
Daniel@0 42 % OPTIONS(11) returns the total number of gradient evaluations.
Daniel@0 43 %
Daniel@0 44 % OPTIONS(14) is the maximum number of iterations; default 100.
Daniel@0 45 %
Daniel@0 46 % OPTIONS(15) is the precision in parameter space of the line search;
Daniel@0 47 % default 1E-4.
Daniel@0 48 %
Daniel@0 49 % See also
Daniel@0 50 % GRADDESC, LINEMIN, MINBRACK, QUASINEW, SCG
Daniel@0 51 %
Daniel@0 52
Daniel@0 53 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 54
Daniel@0 55 % Set up the options.
Daniel@0 56 if length(options) < 18
Daniel@0 57 error('Options vector too short')
Daniel@0 58 end
Daniel@0 59
Daniel@0 60 if(options(14))
Daniel@0 61 niters = options(14);
Daniel@0 62 else
Daniel@0 63 niters = 100;
Daniel@0 64 end
Daniel@0 65
Daniel@0 66 % Set up options for line search
Daniel@0 67 line_options = foptions;
Daniel@0 68 % Need a precise line search for success
Daniel@0 69 if options(15) > 0
Daniel@0 70 line_options(2) = options(15);
Daniel@0 71 else
Daniel@0 72 line_options(2) = 1e-4;
Daniel@0 73 end
Daniel@0 74
Daniel@0 75 display = options(1);
Daniel@0 76
Daniel@0 77 % Next two lines allow conjgrad to work with expression strings
Daniel@0 78 f = fcnchk(f, length(varargin));
Daniel@0 79 gradf = fcnchk(gradf, length(varargin));
Daniel@0 80
Daniel@0 81 % Check gradients
Daniel@0 82 if (options(9))
Daniel@0 83 feval('gradchek', x, f, gradf, varargin{:});
Daniel@0 84 end
Daniel@0 85
Daniel@0 86 options(10) = 0;
Daniel@0 87 options(11) = 0;
Daniel@0 88 nparams = length(x);
Daniel@0 89 fnew = feval(f, x, varargin{:});
Daniel@0 90 options(10) = options(10) + 1;
Daniel@0 91 gradnew = feval(gradf, x, varargin{:});
Daniel@0 92 options(11) = options(11) + 1;
Daniel@0 93 d = -gradnew; % Initial search direction
Daniel@0 94 br_min = 0;
Daniel@0 95 br_max = 1.0; % Initial value for maximum distance to search along
Daniel@0 96 tol = sqrt(eps);
Daniel@0 97
Daniel@0 98 j = 1;
Daniel@0 99 if nargout >= 3
Daniel@0 100 flog(j, :) = fnew;
Daniel@0 101 if nargout == 4
Daniel@0 102 pointlog(j, :) = x;
Daniel@0 103 end
Daniel@0 104 end
Daniel@0 105
Daniel@0 106 while (j <= niters)
Daniel@0 107
Daniel@0 108 xold = x;
Daniel@0 109 fold = fnew;
Daniel@0 110 gradold = gradnew;
Daniel@0 111
Daniel@0 112 gg = gradold*gradold';
Daniel@0 113 if (gg == 0.0)
Daniel@0 114 % If the gradient is zero then we are done.
Daniel@0 115 options(8) = fnew;
Daniel@0 116 return;
Daniel@0 117 end
Daniel@0 118
Daniel@0 119 % This shouldn't occur, but rest of code depends on d being downhill
Daniel@0 120 if (gradnew*d' > 0)
Daniel@0 121 d = -d;
Daniel@0 122 if options(1) >= 0
Daniel@0 123 warning('search direction uphill in conjgrad');
Daniel@0 124 end
Daniel@0 125 end
Daniel@0 126
Daniel@0 127 line_sd = d./norm(d);
Daniel@0 128 [lmin, line_options] = feval('linemin', f, xold, line_sd, fold, ...
Daniel@0 129 line_options, varargin{:});
Daniel@0 130 options(10) = options(10) + line_options(10);
Daniel@0 131 options(11) = options(11) + line_options(11);
Daniel@0 132 % Set x and fnew to be the actual search point we have found
Daniel@0 133 x = xold + lmin * line_sd;
Daniel@0 134 fnew = line_options(8);
Daniel@0 135
Daniel@0 136 % Check for termination
Daniel@0 137 if (max(abs(x - xold)) < options(2) & max(abs(fnew - fold)) < options(3))
Daniel@0 138 options(8) = fnew;
Daniel@0 139 return;
Daniel@0 140 end
Daniel@0 141
Daniel@0 142 gradnew = feval(gradf, x, varargin{:});
Daniel@0 143 options(11) = options(11) + 1;
Daniel@0 144
Daniel@0 145 % Use Polak-Ribiere formula to update search direction
Daniel@0 146 gamma = ((gradnew - gradold)*(gradnew)')/gg;
Daniel@0 147 d = (d .* gamma) - gradnew;
Daniel@0 148
Daniel@0 149 if (display > 0)
Daniel@0 150 fprintf(1, 'Cycle %4d Function %11.6f\n', j, line_options(8));
Daniel@0 151 end
Daniel@0 152
Daniel@0 153 j = j + 1;
Daniel@0 154 if nargout >= 3
Daniel@0 155 flog(j, :) = fnew;
Daniel@0 156 if nargout == 4
Daniel@0 157 pointlog(j, :) = x;
Daniel@0 158 end
Daniel@0 159 end
Daniel@0 160 end
Daniel@0 161
Daniel@0 162 % If we get here, then we haven't terminated in the given number of
Daniel@0 163 % iterations.
Daniel@0 164
Daniel@0 165 options(8) = fold;
Daniel@0 166 if (options(1) >= 0)
Daniel@0 167 disp(maxitmess);
Daniel@0 168 end