1. 程式人生 > >使用非精確線搜尋Armijo演算法確定步長的最速下降法(MATLAB)

使用非精確線搜尋Armijo演算法確定步長的最速下降法(MATLAB)

Armijo演算法實現:

function mk = armijo( fun, xk, rho, sigma, gk )

assert( rho > 0 && rho < 1 );
assert( sigma > 0 && sigma < 0.5 );

mk = 0; max_mk = 100;

while mk <= max_mk
    x = xk - rho^mk * gk;
    if feval( fun, x ) <= feval( fun, xk ) - sigma * rho^mk * norm( gk )^2
        break;
    end
    mk = mk + 1;
end

return;
最速下降法實現:
function [opt_x, opt_f, k] = grad_descent( fun_obj, fun_grad, x0 )

max_iter = 5000;    % max number of iterations
EPS = 1e-5;         % threshold of gradient norm

% Armijo parameters
rho = 0.5; sigma = 0.2;

% initialization
k = 0; xk = x0;

while k < max_iter
    
    k = k + 1;
    
    gk = feval( fun_grad, xk ); % gradient vector
    dk = -1 * gk;               % search direction
    
    if norm( dk ) < EPS
        break;
    end
    
    yk = feval( fun_obj, xk );
    fprintf( '#iter = %5d, xk = %.5f, F = %.5f\n', k, xk, yk );
    
    mk = armijo( fun_obj, xk, rho, sigma, gk );   
    xk = xk + rho^mk * dk;
end

fprintf( '----------------------\n' );
if k == max_iter
    fprintf( 'Problem Not solved!\n' );
else
    fprintf( 'Problem solved!\n' );
end

% record results
opt_x = xk;
opt_f = feval( fun_obj, xk );

return;