Skip to content

Commit 7b66048

Browse files
authored
Add simple InitialStatic alpha choice by providing just a number to t… (#812)
* Add simple InitialStatic alpha choice by providing just a number to the alphaguess keyword in method constructors. * Add Optim qaulifyier.
1 parent 990c1b3 commit 7b66048

File tree

10 files changed

+25
-9
lines changed

10 files changed

+25
-9
lines changed

src/multivariate/solvers/first_order/accelerated_gradient_descent.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function AcceleratedGradientDescent(;
1818
alphaguess = LineSearches.InitialPrevious(), # TODO: investigate good defaults
1919
linesearch = LineSearches.HagerZhang(), # TODO: investigate good defaults
2020
manifold::Manifold=Flat())
21-
AcceleratedGradientDescent(alphaguess, linesearch, manifold)
21+
AcceleratedGradientDescent(_alphaguess(alphaguess), linesearch, manifold)
2222
end
2323

2424
mutable struct AcceleratedGradientDescentState{T, Tx} <: AbstractOptimizerState

src/multivariate/solvers/first_order/bfgs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function BFGS(; alphaguess = LineSearches.InitialStatic(), # TODO: benchmark def
4242
initial_invH = nothing,
4343
initial_stepnorm = nothing,
4444
manifold::Manifold=Flat())
45-
BFGS(alphaguess, linesearch, initial_invH, initial_stepnorm, manifold)
45+
BFGS(_alphaguess(alphaguess), linesearch, initial_invH, initial_stepnorm, manifold)
4646
end
4747

4848
mutable struct BFGSState{Tx, Tm, T,G} <: AbstractOptimizerState

src/multivariate/solvers/first_order/cg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function ConjugateGradient(; alphaguess = LineSearches.InitialHagerZhang(),
8888

8989
ConjugateGradient(eta,
9090
P, precondprep,
91-
alphaguess, linesearch,
91+
_alphaguess(alphaguess), linesearch,
9292
manifold)
9393
end
9494

src/multivariate/solvers/first_order/gradient_descent.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function GradientDescent(; alphaguess = LineSearches.InitialPrevious(), # TODO:
3333
P = nothing,
3434
precondprep = (P, x) -> nothing,
3535
manifold::Manifold=Flat())
36-
GradientDescent(alphaguess, linesearch, P, precondprep, manifold)
36+
GradientDescent(_alphaguess(alphaguess), linesearch, P, precondprep, manifold)
3737
end
3838

3939
mutable struct GradientDescentState{Tx, T} <: AbstractOptimizerState

src/multivariate/solvers/first_order/l_bfgs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function LBFGS(; m::Integer = 10,
126126
precondprep = (P, x) -> nothing,
127127
manifold::Manifold=Flat(),
128128
scaleinvH0::Bool = true && (typeof(P) <: Nothing) )
129-
LBFGS(Int(m), alphaguess, linesearch, P, precondprep, manifold, scaleinvH0)
129+
LBFGS(Int(m), _alphaguess(alphaguess), linesearch, P, precondprep, manifold, scaleinvH0)
130130
end
131131

132132
Base.summary(::LBFGS) = "L-BFGS"

src/multivariate/solvers/first_order/momentum_gradient_descent.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function MomentumGradientDescent(; mu::Real = 0.01,
1414
alphaguess = LineSearches.InitialPrevious(), # TODO: investigate good defaults
1515
linesearch = LineSearches.HagerZhang(), # TODO: investigate good defaults
1616
manifold::Manifold=Flat())
17-
MomentumGradientDescent(mu, alphaguess, linesearch, manifold)
17+
MomentumGradientDescent(mu, _alphaguess(alphaguess), linesearch, manifold)
1818
end
1919

2020
mutable struct MomentumGradientDescentState{Tx, T} <: AbstractOptimizerState

src/multivariate/solvers/first_order/ngmres.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function NGMRES(;manifold::Manifold = Flat(),
7575
ϵ0 = 1e-12, # ϵ0 = 1e-12 -- number was an arbitrary choice#
7676
wmax::Int = 10) # wmax = 10 -- number was an arbitrary choice to match L-BFGS field `m`
7777
@assert manifold == nlprecon.manifold
78-
NGMRES(alphaguess, linesearch, manifold, nlprecon, nlpreconopts, ϵ0, wmax)
78+
NGMRES(_alphaguess(alphaguess), linesearch, manifold, nlprecon, nlpreconopts, ϵ0, wmax)
7979
end
8080

8181
"""
@@ -116,7 +116,7 @@ function OACCEL(;manifold::Manifold = Flat(),
116116
ϵ0 = 1e-12, # ϵ0 = 1e-12 -- number was an arbitrary choice
117117
wmax::Int = 10) # wmax = 10 -- number was an arbitrary choice to match L-BFGS field `m`
118118
@assert manifold == nlprecon.manifold
119-
OACCEL(alphaguess, linesearch, manifold, nlprecon, nlpreconopts, ϵ0, wmax)
119+
OACCEL(_alphaguess(alphaguess), linesearch, manifold, nlprecon, nlpreconopts, ϵ0, wmax)
120120
end
121121

122122

src/multivariate/solvers/second_order/newton.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Wright (ch. 6, 1999) for a discussion of Newton's method in practice.
2222
"""
2323
function Newton(; alphaguess = LineSearches.InitialStatic(), # Good default for Newton
2424
linesearch = LineSearches.HagerZhang()) # Good default for Newton
25-
Newton(alphaguess, linesearch)
25+
Newton(_alphaguess(alphaguess), linesearch)
2626
end
2727

2828
Base.summary(::Newton) = "Newton's Method"

src/utilities/perform_linesearch.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# return true if the direction was changed
33
reset_search_direction!(state, d, method) = false # no-op
44

5+
_alphaguess(a) = a
6+
_alphaguess(a::Number) = LineSearches.InitialStatic(alpha=a)
7+
58
function reset_search_direction!(state, d, method::BFGS)
69
n = length(state.x)
710
T = eltype(state.x)

test/multivariate/optimize/interface.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@
3636
@test norm(Optim.minimum(ref)-Optim.minimum(r)) < 1e-6
3737
end
3838
end
39+
# simple tests for https://github.com/JuliaNLSolvers/Optim.jl/issues/805
40+
@test AcceleratedGradientDescent(alphaguess=1.0).alphaguess! isa Optim.LineSearches.InitialStatic
41+
@test BFGS(alphaguess=1.0).alphaguess! isa Optim.LineSearches.InitialStatic
42+
@test ConjugateGradient(alphaguess=1.0).alphaguess! isa Optim.LineSearches.InitialStatic
43+
@test GradientDescent(alphaguess=1.0).alphaguess! isa Optim.LineSearches.InitialStatic
44+
@test MomentumGradientDescent(alphaguess=1.0).alphaguess! isa Optim.LineSearches.InitialStatic
45+
@test Newton(alphaguess=1.0).alphaguess! isa Optim.LineSearches.InitialStatic
46+
optimize(od, problem.initial_x, AcceleratedGradientDescent(alphaguess=1.0))
47+
optimize(od, problem.initial_x, BFGS(alphaguess=1.0))
48+
optimize(od, problem.initial_x, ConjugateGradient(alphaguess=1.0))
49+
optimize(od, problem.initial_x, GradientDescent(alphaguess=1.0))
50+
optimize(od, problem.initial_x, MomentumGradientDescent(alphaguess=1.0))
51+
optimize(td, problem.initial_x, Newton(alphaguess=1.0))
3952
end
4053

4154
@testset "only_fg!, only_fgh!" begin

0 commit comments

Comments
 (0)