pdsco.m

来自「% Atomizer Main Directory, Version .802 」· M 代码 · 共 923 行 · 第 1/3 页

M
923 行
字号

    elseif LSproblem == 32
       %-----------------------------------------------------------------
       % Solve 2x2 system symmetrized by X^{1/2}.
       % Equilibrate first n rows and cols of K2.
       % Also rows of AX^{1/2} if A is explicit.
       %-----------------------------------------------------------------
       xroot = sqrt(x);
       h     = z  +  Q.*x;
       if explicit
          DD  = sparse( 1:n, 1:n, xroot, n, n );
          AD  = AAA*DD;
          AD2 = AD.*AD;
          wD  = sum( (AD2') )';   % Sum of squares of each row of AD
          wD  = sqrt( wD + LSdamp2 );
          R   = sqrt(1 ./ wD);
       else
          R   = ones(m,1);
       end

       S     = sqrt( h.*h + x );    % Norms of first n columns of K
       S2    = 1 ./ S;
       S     = sqrt(S2);            % Left and right scaling
       DDD1  = - S2.* h;
       DDD2  =   S .* xroot;
       DDD3  =   R;

       if direct   % No scaling with R yet.
          rhs   = [ (DDD2.*w); r ];
          AD2   = AAA * sparse( 1:n, 1:n, DDD2, n, n );
          d2I   = LSdamp2 * em;

          K2    = [ sparse( 1:n, 1:n, DDD1, n, n )       AD2'
                    AD2                    sparse( 1:m, 1:m, d2I, m, m ) ];

          conds = 0;   %%%%%%%% TEST
          if conds
             AX2 = AAA * sparse( 1:n, 1:n, xroot, n, n );
             KX  = [ sparse( 1:n, 1:n, (-h), n, n )       AX2'
                    AX2                    sparse( 1:m, 1:m, d2I, m, m ) ];
             ev1 = sort(abs(eig(full(KX))));   nev = length(ev1);
             emax1 = ev1(nev);  emin1 = ev1(1);  condKX = emax1 / emin1;
             ev2 = sort(abs(eig(full(K2))));
             emax2 = ev2(nev);  emin2 = ev2(1);  condK2 = emax2 / emin2;
             fprintf('\nK with AX^1/2   e1,en,cond: %9.1e%9.1e%9.1e',...
                           emax1, emin1, condKX)
             fprintf('\nK scaled        e1,en,cond: %9.1e%9.1e%9.1e',...
                           emax2, emin2, condK2)
             K2 = full(K2);  %%%% To fix erroneous divide by 0 in K2 \ rhs;
          end          %%%%%%%% END TEST

          soln  = K2 \ rhs;
          dx    = DDD2 .* soln(1:n);
          dy    = soln(n+1:nkkt);
          Adx   = AAA*dx;
          Ady   = AAA'*dy;
       else

          % Call SYMMLQ.   (Use feval in case this option is never used.
          % Then it doesn't matter if the user doesn't have symmlq.)

          rhs   = [ (DDD2.*w); (DDD3.*r) ];
          rtol  = atol;  % from LSQR

          [ soln, istop, itncg, anorm, acond, rnorm, ynorm ] = ...
              feval( 'symmlq', nkkt, rhs, 'pdsyyy', 'dummy', Aname, rw,  ...
                      precon, shift, showsym, check, itnsym, rtol );

          dx    = DDD2 .* soln(1:n);
          dy    = DDD3 .* soln(n+1:nkkt);
          Adx   = feval( Aname, 1, m, n, dx );
          Ady   = feval( Aname, 2, m, n, dy );
       end

    else
       disp( 'This LSproblem not yet implemented' )
       break
    end
%-----------------------------------------------------------------------

    cgitns = cgitns + itncg;

%-----------------------------------------------------------------------
%  dx and dy are now known.  Get dz.
%  dz  = xinv.*(v - z.*dx);  % is the classical formula -- no good???
%-----------------------------------------------------------------------
    if LSproblem ~= 31
       dz  = t - Ady + Q.*dx;
    end

%-----------------------------------------------------------------------
%  Find the maximum step.
%-----------------------------------------------------------------------
    stepx = infinity;
    stepz = infinity;

    blocking  =  find( dx < 0 );
    if length( blocking ) > 0
       steps  =  x(blocking) ./ (- dx(blocking));
       stepx  =  min( steps );
    end

    blocking  =  find( dz < 0 );
    if length( blocking ) > 0
       steps  =  z(blocking) ./ (- dz(blocking));
       stepz  =  min( steps );
    end

    stepxmax =  stepx;
    stepzmax =  stepz;
    stepx    =  min( steptol * stepx, 1 );
    stepz    =  min( steptol * stepz, 1 );

%  Backtracking linesearch.

    fail     =  true;
    nf       =  0;

    while nf < maxf
       nf       =   nf + 1;
       xnew     =   x  +  stepx * dx;
       ynew     =   y  +  stepz * dy;
       znew     =   z  +  stepz * dz;
       [obj,grad,hess] = feval( Fname, xnew );
       rlinnew  =   rlin     -  stepx * Adx;
       tlinnew  =   tlin     -  stepz *(Ady + dz);
       rnew     =   rlinnew  -  LSdamp2 * ynew;
       tnew     =   tlinnew  +  (gamma2 * xnew  +  grad);
       Xznew    =   xnew .* znew;
       vnew     =   mu  -  Xznew;
       fnew     =   [ rnew;  tnew;  vnew ];
       normfnew =   norm( fnew );     % Must be 2-norm here.
       step     =   min( stepx, stepz );
       if normfnew >= normf,
          fratio = normfnew / normf;
          % fprintf( 'fnew / f%8.1e', fratio)
       end
       if normfnew <= (1 - eta*step)*normf
          fail = false;
          break;
       end

%     If the first attempt doesn't work,
%     make stepx and stepz the same.

       if nf == 1
          stepx   = min( stepx, stepz );
          stepz   = stepx;
       elseif nf < maxf
          stepx   = stepx/2;
          stepz   = stepx;
       end;
    end

    if fail
       fprintf('\n     Linesearch failed (nf too big)');
       nfail = nfail + 1;
    else
       nfail = 0;
    end

    x     = xnew;
    y     = ynew;
    z     = znew;
    rlin  = rlinnew;
    tlin  = tlinnew;
    r     = rnew;
    t     = tnew;
    v     = vnew;
    normf = normfnew;
    Xz    = Xznew;

%%%%%%%%%%%%%%%%%%%%%%%%%

%  Iteration log.

    normr   = norm(r);
    normt   = norm(t);
    normv   = norm(v);
    normx   = norm(x);
    normy   = norm(y);
    normz   = norm(z);
    regterm = gamma2 * normx^2  +  LSdamp2 * normy^2;
    objtrue = obj  +  0.5*regterm;
    bsize   = 1 + normb;
    fsize   = 1 + abs(objtrue);
    gsize   = 1 + norm(grad,inf);
    center  = max(Xz) / min(Xz);

    str1    = sprintf('\n%3g%5.1f' , itn         , log10(mu)   );
    str2    = sprintf('%6.3f%6.3f' , stepx       , stepz       );
    if stepx < 0.001 | stepz < 0.001
       str2 = sprintf('%6.1e%6.1e' , stepx       , stepz       );
    end
    str3    = sprintf('%6.1f%6.1f' , log10(normr), log10(normt));
    str4    = sprintf('%6.1f%15.7e', log10(normv), objtrue     );
    str5    = sprintf('%3g%7.1f'   , nf          , center      );
    if center > 99999
       str5 = sprintf('%3g%7.1e'   , nf          , center      );
    end
    if direct
       str6 = ' ';
    else
       str6 = sprintf(' %5.1f%7g', log10(atol), itncg);
    end
    fprintf( [str1 str2 str3 str4 str5 str6] )

%-----------------------------------------------------------------------
%  Test for termination.
%-----------------------------------------------------------------------
    gap           = max(Xz);
    primalfeas    = norm(r,inf) / bsize  <=  featol;
    dualfeas      = norm(t,inf) / gsize  <=  featol;
    complementary = gap         / fsize  <=  opttol;
    converged     = primalfeas  &  dualfeas  &  complementary;

    if kminor
       fprintf( 'Start of next minor itn...\n')
       keyboard
    end

    if converged,
       fprintf('\n   Converged')
    elseif itn >= maxitn
       fprintf('\n   Too many iterations')
       inform = 1;
       break
    elseif nfail >= maxfail
       fprintf('\n   Too many linesearch failures')
       inform = 2;
       break
    else

%     Reduce mu and LSdamp, and reset certain residuals.

       stepmu  = min( stepx , stepz   );
       stepmu  = min( stepmu, steptol );
       muold   = mu;
       mu      = mu   -  stepmu * mu;
       if center >= 100,  mu = muold;  end

%     mutrad  = mu0 * (sum(Xz) / n);   % 24 May 1998:  Tradition value.  But --
%     mu      = min( mu, mutrad );     % this seemed to decrease mu too much.

       mu      = max( mu, mulast );     % 13 Jun 1998:  No need for smaller mu.

       LSdamp  = max( sqrt(mu), delta );
       LSdamp  = min( LSdamp  , delta0);
       LSdamp2 = LSdamp^2;
       r       = rlin  -  LSdamp2 * y;
       Xz      = x.*z;
       v       = mu   -  Xz;
       f       = [ r; t; v ];
       normf   = norm(f);

%     Reduce atol for LSQR (and SYMMLQ).

       atolold = atol;
       if atol > atol2
          atolfac = (mu/mufirst)^0.25;
          atol    = max( atol*atolfac, atol2 );
       end

%     If the linesearch took more than one function (nf > 1),
%     we assume the search direction needed more accuracy
%     (though this may be true only for LPs).
%
%     12 Jun 1998: Ask for more accuracy only if nf > 2.

       if nf > 2
          atol    = max( atolold*0.1, atolmin );
       end
    end
end
%-----------------------------------------------------------------------
% End of main loop.
%-----------------------------------------------------------------------

fprintf('\n\nmax( x ) =%10.3f', max(x)     )
fprintf('    max(|y|) =%10.3f', max(abs(y)))
fprintf('    max( z ) =%10.3f', max(z)     )

flops1 = flops - flops0;
str1   = sprintf('\nMflops   =%10.1f', (flops1/1.0e+6) );
str2   = sprintf(     ' itns =%10g'  , cgitns );
fprintf( [str1 ' ' solver str2] )
fprintf('    time     =%10.1f', toc);

fprintf('\n\nDistribution of solution:  x         z')
x1 = 10^(floor(log10(max([x; z]))) + 1);
for k = 1:10
    x2 = x1;   x1 = x1/10;
    nx = length(find(x>=x1 & x<x2));
    nz = length(find(z>=x1 & z<x2));
    fprintf('\n[%7.3g,%7.3g )%10g%10g', x1, x2, nx, nz)
end
fprintf('\n')

if wait
    keyboard
end
%-----------------------------------------------------------------------
% End of pdsco.m
%-----------------------------------------------------------------------


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?