Skip to content

Commit 624820d

Browse files
authored
Merge pull request #37 from JuliaNLSolvers/newdf2
Use NLSolversBase
2 parents 0c05761 + a8d1e1a commit 624820d

16 files changed

+115
-191
lines changed

README.md

+19-14
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ We solve the Rosenbrock problem with two different line search algorithms.
2222

2323
First, run `Newton` with the default line search algorithm:
2424
```julia
25-
using Optim
25+
using Optim, LineSearches
2626
prob = Optim.UnconstrainedProblems.examples["Rosenbrock"]
2727

28-
algo_hz = Newton(;linesearch! = hagerzhang!)
28+
algo_hz = Newton(linesearch = hagerzhang!)
2929
res_hz = Optim.optimize(prob.f, prob.g!, prob.h!, prob.initial_x, method=algo_hz)
3030
```
3131

@@ -34,38 +34,43 @@ This gives the result
3434
Results of Optimization Algorithm
3535
* Algorithm: Newton's Method
3636
* Starting Point: [0.0,0.0]
37-
* Minimizer: [0.9999999999979515,0.9999999999960232]
38-
* Minimum: 5.639268e-24
39-
* Iterations: 13
37+
* Minimizer: [0.9999999999999994,0.9999999999999989]
38+
* Minimum: 3.081488e-31
39+
* Iterations: 14
4040
* Convergence: true
4141
* |x - x'| < 1.0e-32: false
4242
* |f(x) - f(x')| / |f(x)| < 1.0e-32: false
4343
* |g(x)| < 1.0e-08: true
44+
* f(x) > f(x'): false
4445
* Reached Maximum Number of Iterations: false
45-
* Objective Function Calls: 54
46-
* Gradient Calls: 54
46+
* Objective Calls: 44
47+
* Gradient Calls: 44
48+
* Hessian Calls: 14
4749
```
4850

49-
Now we can try `Newton` with the Moré Thuente line search:
51+
Now we can try `Newton` with the cubic backtracking line search:
5052
``` julia
51-
algo_mt = Newton(;linesearch! = morethuente!)
52-
res_mt = Optim.optimize(prob.f, prob.g!, prob.h!, prob.initial_x, method=algo_mt)
53+
algo_bt3 = Newton(linesearch = bt3!)
54+
res_bt3 = Optim.optimize(prob.f, prob.g!, prob.h!, prob.initial_x, method=algo_bt3)
5355
```
5456

5557
This gives the following result, reducing the number of function and gradient calls:
5658
``` julia
59+
Results of Optimization Algorithm
5760
* Algorithm: Newton's Method
5861
* Starting Point: [0.0,0.0]
59-
* Minimizer: [0.9999999999999992,0.999999999999998]
60-
* Minimum: 2.032549e-29
62+
* Minimizer: [0.9999999959215587,0.9999999918223065]
63+
* Minimum: 1.667699e-17
6164
* Iterations: 14
6265
* Convergence: true
6366
* |x - x'| < 1.0e-32: false
6467
* |f(x) - f(x')| / |f(x)| < 1.0e-32: false
6568
* |g(x)| < 1.0e-08: true
69+
* f(x) > f(x'): false
6670
* Reached Maximum Number of Iterations: false
67-
* Objective Function Calls: 45
68-
* Gradient Calls: 45
71+
* Objective Calls: 19
72+
* Gradient Calls: 15
73+
* Hessian Calls: 14
6974
```
7075

7176
## References

REQUIRE

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
julia 0.5
2+
NLSolversBase

src/LineSearches.jl

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ isdefined(Base, :__precompile__) && __precompile__()
22

33
module LineSearches
44

5+
import NLSolversBase
56
import Base.clear!
67

78
export LineSearchResults, LineSearchException

src/api.jl

+4-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ function alphatry{T}(alpha::T,
1313
iterfinitemax::Integer = ceil(Integer, -log2(eps(T))),
1414
alphamax::Real = convert(T, Inf),
1515
verbose::Bool = false)
16-
f_calls = 0
17-
g_calls = 0
1816

1917
phi0 = lsr.value[1]
2018
dphi0 = lsr.slope[1]
@@ -23,19 +21,17 @@ function alphatry{T}(alpha::T,
2321
alphatest = min(alphatest, alphamax)
2422

2523
# Use xtmp here
26-
phitest = df.f(x + alphatest * s)
27-
f_calls += 1
24+
phitest = NLSolversBase.value!(df, x + alphatest * s)
2825

2926
iterfinite = 1
3027
while !isfinite(phitest)
3128
alphatest = psi3 * alphatest
3229
# Use xtmp here
33-
phitest = df.f(x + alphatest * s)
34-
f_calls += 1
30+
phitest = NLSolversBase.value!(df, x + alphatest * s)
3531
lsr.nfailures += 1
3632
iterfinite += 1
3733
if iterfinite >= iterfinitemax
38-
return zero(T), true, f_calls, g_calls
34+
return zero(T), true
3935
# error("Failed to achieve finite test value; alphatest = ", alphatest)
4036
end
4137
end
@@ -73,7 +69,7 @@ function alphatry{T}(alpha::T,
7369
if verbose == true
7470
println("alpha guess (expand): ", alpha)
7571
end
76-
return alpha, mayterminate, f_calls, g_calls
72+
return alpha, mayterminate
7773
end
7874

7975

src/backtracking.jl

+4-10
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ function backtracking!{T}(df,
6666
# Count the total number of iterations
6767
iteration = 0
6868

69-
# Track calls to function and gradient
70-
f_calls = 0
71-
g_calls = 0
72-
7369
# Count number of parameters
7470
n = length(x)
7571

@@ -84,17 +80,16 @@ function backtracking!{T}(df,
8480
push!(lsr.alpha, alpha)
8581

8682
# Backtrack until we satisfy sufficient decrease condition
87-
f_x_scratch = df.f(x_scratch)
83+
f_x_scratch = NLSolversBase.value!(df, x_scratch)
8884
push!(lsr.value, f_x_scratch)
89-
f_calls += 1
9085
while f_x_scratch > f_x + c1 * alpha * gxp
9186
# Increment the number of steps we've had to perform
9287
iteration += 1
9388

9489
# Ensure termination
9590
if iteration > iterations
9691
throw(LineSearchException("Linesearch failed to converge, reached maximum iterations $(iterations).",
97-
lsr.alpha[end], f_calls, g_calls,lsr))
92+
lsr.alpha[end], lsr))
9893
end
9994

10095
# Shrink proposed step-size:
@@ -135,10 +130,9 @@ function backtracking!{T}(df,
135130
end
136131

137132
# Evaluate f(x) at proposed position
138-
f_x_scratch = df.f(x_scratch)
139-
f_calls += 1
133+
f_x_scratch = NLSolversBase.value!(df, x_scratch)
140134
push!(lsr.value, f_x_scratch)
141135
end
142136

143-
return alpha, f_calls, g_calls
137+
return alpha
144138
end

src/basic.jl

+2-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ function basic!{T}(df,
2121
end
2222

2323
# Evaluate f(x) at new position
24-
f_x_scratch = df.f(x_scratch)
24+
f_x_scratch = NLSolversBase.value!(df, x_scratch)
2525
push!(lsr.value, f_x_scratch)
26-
f_calls = 1
2726

28-
g_calls = 0
29-
30-
return alpha, f_calls, g_calls
27+
return alpha
3128
end

src/deprecate.jl

-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
11
# Deprecation warnings
2-
3-
@deprecate hz_linesearch! hagerzhang!
4-
@deprecate mt_linesearch! morethuente!
5-
@deprecate backtracking_linesearch! backtracking!
6-
@deprecate interpbacktrack_linesearch! bt2!
7-
@deprecate interpolating_linesearch! strongwolfe!
8-
@deprecate LinesearchException LineSearchException
9-
@deprecate interpbacktrack! bt2!

0 commit comments

Comments
 (0)