X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=blobdiff_plain;f=optimization%2Fpreconditioned_conjugate_gradient_method.m;h=e2f841aefea06ec92e8bb1e8dbe85eb5f2b65747;hp=eb2089f5b4e50d367c8237be9c166d5065f70982;hb=92116b34e755b3ef5de14a1777676bc09180f007;hpb=af1b47d92cb94b9289987babefd34633f3bbe804 diff --git a/optimization/preconditioned_conjugate_gradient_method.m b/optimization/preconditioned_conjugate_gradient_method.m index eb2089f..e2f841a 100644 --- a/optimization/preconditioned_conjugate_gradient_method.m +++ b/optimization/preconditioned_conjugate_gradient_method.m @@ -1,8 +1,8 @@ -function [x, k] = preconditioned_conjugate_gradient_method(Q, - M, - b, - x0, - tolerance, +function [x, k] = preconditioned_conjugate_gradient_method(Q, ... + M, ... + b, ... + x0, ... + tolerance, ... max_iterations) % % Solve, @@ -58,8 +58,16 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, % REFERENCES: % % 1. Guler, Osman. Foundations of Optimization. New York, Springer, - % 2010. + % 2010. % + % 2. Shewchuk, Jonathan Richard. An Introduction to the Conjugate + % Gradient Method Without the Agonizing Pain, Edition 1.25. + % August 4, 1994. + % + + % We use this in the inner loop. + n = length(x0); + sqrt_n = floor(sqrt(n)); % Set k=0 first, that way the references to xk,rk,zk,dk which % immediately follow correspond (semantically) to x0,r0,z0,d0. @@ -70,14 +78,7 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, zk = M \ rk; dk = -zk; - for k = [ 0 : max_iterations ] - - if (norm(rk) < tolerance) - % Check our stopping condition. This should catch the k=0 case. - x = xk; - return; - end - + while (k <= max_iterations && norm(rk, 'inf') > tolerance) % Used twice, avoid recomputation. rkzk = rk' * zk; @@ -85,15 +86,36 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, % do them both, so we precompute the more expensive operation. Qdk = Q * dk; - % After substituting the two previously-created variables, the - % following algorithm occurs verbatim in the reference. - alpha_k = rkzk/(dk' * Qdk); + % We're going to divide by this quantity... + dkQdk = dk' * Qdk; + + % So if it's too close to zero, we replace it with something + % comparable but non-zero. + if (dkQdk < eps) + dkQdk = eps; + end + + alpha_k = rkzk/dkQdk; x_next = xk + (alpha_k * dk); - r_next = rk + (alpha_k * Qdk); + + % The recursive definition of r_next is prone to accumulate + % roundoff error. When sqrt(n) divides k, we recompute the + % residual to minimize this error. This modification was suggested + % by the second reference. + if (mod(k, sqrt_n) == 0) + r_next = Q*x_next - b; + else + r_next = rk + (alpha_k * Qdk); + end + z_next = M \ r_next; beta_next = (r_next' * z_next)/rkzk; d_next = -z_next + beta_next*dk; + % We potentially just performed one more iteration than necessary + % in order to simplify the loop. Note that due to the structure of + % our loop, we will have k > max_iterations when we fail to + % converge. k = k + 1; xk = x_next; rk = r_next; @@ -101,7 +123,6 @@ function [x, k] = preconditioned_conjugate_gradient_method(Q, dk = d_next; end - % The algorithm didn't converge, but we still want to return the - % terminal value of xk. + % If we make it here, one of the two stopping conditions was met. x = xk; end