-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinesearch_backtrack.m
105 lines (85 loc) · 2.28 KB
/
linesearch_backtrack.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function [ x, g, f, info, count, img] = linesearch_backtrack(fg, xp, f, g, s, stp, ftol,wolfe, max_linesearch, min_step, max_step,linesearch_type)
x=xp;
ret=0;
width=0;
dg=0;
norm=0;
finit=0;
dginit=0;
dgtest=0;
dec=.5;
inc=2.1;
count=0;
info=0;
% /* Check the input parameters for errors. */
if (stp <= 0.)
info=999;
return;
end
% /* Compute the initial gradient in the search direction. */
dginit=sum(g.*s);
% /* Make sure that s points to a descent direction. */
if (0 < dginit)
info=998;
return;
end
% /* The initial value of the objective function. */
finit = f;
dgtest = ftol * dginit;
while (1)
x=xp;
x=x+ stp*s;
if (nargout>5)
[f,g,img]=feval(fg,x);
else
[f,g]=feval(fg,x);
end
count=count+1;
if (f > finit + stp * dgtest)
width = dec;
else
% display('testing linesearch');
% /* The sufficient decrease condition (Armijo condition). */
if (linesearch_type == 1)
% /* Exit with the Armijo condition. */
% display('Armijo');
return;
end
% /* Check the Wolfe condition. */
dg=sum(g.*s);
if (dg < wolfe * dginit)
width = inc;
else
if(linesearch_type == 2)
% /* Exit with the regular Wolfe condition. */
% display('Wolfe');
return;
end
end
% /* Check the strong Wolfe condition. */
if(dg > -wolfe * dginit)
width = dec;
else
% /* Exit with the strong Wolfe condition. */
% display('Strong wolfe');
return;
end
end
if (stp < min_step)
% /* The step is the minimum value. */
info=997;
return;
end
if (stp > max_step)
% /* The step is the maximum value. */
info=996;
return;
end
if (max_linesearch <= count)
% /* Maximum number of iteration. */
info=995;
return;
end
stp = stp*width;
end
end