Skip to content

Commit 6c576e5

Browse files
committed
Add an attempted flatness-convergence test
The flatness check in HagerZhang was intended to prevent excessive iteration in cases where the objective function is flat to within numerical precision. This test is a poor attempt at capturing the issue. HZ takes more iterations than any other linesearch algorithm, but it's still a far cry from real-world cases I've seen where the linesearch exits from the iteration limit.
1 parent ba7217c commit 6c576e5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

test/issues.jl

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ gv = similar(x)
1919
ϕ0 = fg!(gv, x)
2020
s = -1*gv
2121
dϕ0 = dot(gv, s)
22-
println(ϕ0, ", ", dϕ0)
22+
# println(ϕ0, ", ", dϕ0)
2323

2424
# Univariate line search functions
2525
ϕ(α) = f(x .+ α.*s)
@@ -36,3 +36,35 @@ end
3636
res = (StrongWolfe())(ϕ, dϕ, ϕdϕ, α0, ϕ0, dϕ0)
3737
@test res[2] > 0
3838
@test res[2] == ϕ(res[1])
39+
40+
# Flatness check in HagerZhang
41+
function makeϕdϕ(a)
42+
@assert axes(a) == (1:2,)
43+
A = a*a'
44+
f(x) = x'*A*x/2
45+
df(x) = A*x
46+
x0 = [a[2], -a[1]]
47+
d = -x0 / 2
48+
ϕ(α) = f(x0 + α*d)
49+
(α) = dot(df(x0 + α*d), d)
50+
ϕdϕ(α) = (ϕ(α), (α))
51+
return ϕ, dϕ, ϕdϕ
52+
end
53+
54+
@testset "Flatness" begin
55+
cache = LineSearchCache{Float64}()
56+
lsalgs = (HagerZhang(; cache=cache), StrongWolfe(; cache=cache), MoreThuente(; cache=cache),
57+
BackTracking(; cache=cache), BackTracking(; order=2, cache=cache) )
58+
59+
n = 0
60+
while n < 10
61+
ϕ, dϕ, ϕdϕ = makeϕdϕ(randn(2))
62+
ϕ0, dϕ0 = ϕdϕ(0)
63+
dϕ0 < -eps(abs(ϕ0)) || continue
64+
n += 1
65+
for ls in lsalgs
66+
res = ls(ϕ, dϕ, ϕdϕ, 1.0, ϕ(0.0), (0.0))
67+
@test length(cache.alphas) < 10 # really should be < 5
68+
end
69+
end
70+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ my_tests = [
1515
"alphacalc.jl",
1616
"arbitrary_precision.jl",
1717
"examples.jl",
18+
"issues.jl",
1819
"captured.jl"
1920
]
2021

0 commit comments

Comments
 (0)