Switch to unified view

a b/combinedDeepLearningActiveContour/minFunc/ArmijoBacktrack.m
1
function [t,x_new,f_new,g_new,funEvals,H] = ArmijoBacktrack(...
2
    x,t,d,f,fr,g,gtd,c1,LS,tolX,debug,doPlot,saveHessianComp,funObj,varargin)
3
%
4
% Backtracking linesearch to satisfy Armijo condition
5
%
6
% Inputs:
7
%   x: starting location
8
%   t: initial step size
9
%   d: descent direction
10
%   f: function value at starting location
11
%   fr: reference function value (usually funObj(x))
12
%   gtd: directional derivative at starting location
13
%   c1: sufficient decrease parameter
14
%   debug: display debugging information
15
%   LS: type of interpolation
16
%   tolX: minimum allowable step length
17
%   doPlot: do a graphical display of interpolation
18
%   funObj: objective function
19
%   varargin: parameters of objective function
20
%
21
% Outputs:
22
%   t: step length
23
%   f_new: function value at x+t*d
24
%   g_new: gradient value at x+t*d
25
%   funEvals: number function evaluations performed by line search
26
%   H: Hessian at initial guess (only computed if requested
27
28
% Evaluate the Objective and Gradient at the Initial Step
29
if nargout == 6
30
    [f_new,g_new,H] = feval(funObj, x + t*d, varargin{:}); 
31
else
32
    [f_new,g_new] = feval(funObj, x + t*d, varargin{:}); 
33
end
34
funEvals = 1;
35
36
while f_new > fr + c1*t*gtd || ~isLegal(f_new)
37
38
    temp = t;
39
    if LS == 0 || ~isLegal(f_new)
40
        % Backtrack w/ fixed backtracking rate
41
        if debug
42
            fprintf('Fixed BT\n');
43
        end
44
        t = 0.5*t;
45
    elseif LS == 2 && isLegal(g_new)
46
        % Backtracking w/ cubic interpolation w/ derivative
47
        if debug
48
            fprintf('Grad-Cubic BT\n');
49
        end
50
        t = polyinterp([0 f gtd; t f_new g_new'*d],doPlot);
51
    elseif funEvals < 2 || ~isLegal(f_prev)
52
        % Backtracking w/ quadratic interpolation (no derivative at new point)
53
        if debug
54
            fprintf('Quad BT\n');
55
        end
56
        t = polyinterp([0 f gtd; t f_new sqrt(-1)],doPlot);
57
    else%if LS == 1
58
        % Backtracking w/ cubic interpolation (no derivatives at new points)
59
        if debug
60
            fprintf('Cubic BT\n');
61
        end
62
        t = polyinterp([0 f gtd; t f_new sqrt(-1); t_prev f_prev sqrt(-1)],doPlot);
63
    end
64
65
    % Adjust if change in t is too small/large
66
67
    if t < temp*1e-3
68
        if debug
69
            fprintf('Interpolated Value Too Small, Adjusting\n');
70
        end
71
        t = temp*1e-3;
72
    elseif t > temp*0.6
73
        if debug
74
            fprintf('Interpolated Value Too Large, Adjusting\n');
75
        end
76
        t = temp*0.6;
77
    end
78
79
    f_prev = f_new;
80
    t_prev = temp;
81
    if ~saveHessianComp && nargout == 6
82
        [f_new,g_new,H] = feval(funObj, x + t*d, varargin{:}); 
83
    else
84
        [f_new,g_new] = feval(funObj, x + t*d, varargin{:}); 
85
    end
86
    funEvals = funEvals+1;
87
88
    % Check whether step size has become too small
89
    if sum(abs(t*d)) <= tolX
90
        if debug
91
            fprintf('Backtracking Line Search Failed\n');
92
        end
93
        t = 0;
94
        f_new = f;
95
        g_new = g;
96
        break;
97
    end
98
end
99
100
% Evaluate Hessian at new point
101
if nargout == 6 && funEvals > 1 && saveHessianComp
102
    [f_new,g_new,H] = feval(funObj, x + t*d, varargin{:}); 
103
    funEvals = funEvals+1;
104
end
105
106
x_new = x + t*d;
107
108
end