Skip to content

Commit

Permalink
Use namedtuple for default options. (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkofod committed Jan 24, 2023
1 parent 7fa215a commit 1f1258c
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/multivariate/optimize/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function check_kwargs(kwargs, fallback_method)
kws, method
end

default_options(method::AbstractOptimizer) = Dict{Symbol, Any}()
default_options(method::AbstractOptimizer) = NamedTuple()

function add_default_opts!(opts::Dict{Symbol, Any}, method::AbstractOptimizer)
for newopt in default_options(method)
Expand Down Expand Up @@ -83,6 +83,7 @@ promote_objtype(method::SecondOrderOptimizer, x, autodiff::Symbol, inplace::Bool
function optimize(f, initial_x::AbstractArray; inplace = true, autodiff = :finite, kwargs...)
method = fallback_method(f)
checked_kwargs, method = check_kwargs(kwargs, method)

d = promote_objtype(method, initial_x, autodiff, inplace, f)
add_default_opts!(checked_kwargs, method)

Expand Down
2 changes: 1 addition & 1 deletion src/multivariate/solvers/constrained/ipnewton/ipnewton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ function is_smaller_eps(ref, step)
end

function default_options(method::ConstrainedOptimizer)
Dict(:allow_f_increases => true, :successive_f_tol => 2)
(; allow_f_increases = true, successive_f_tol = 2)
end


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ function trace!(tr, d, state, iteration, method::AcceleratedGradientDescent, opt
end

function default_options(method::AcceleratedGradientDescent)
Dict(:allow_f_increases => true)
(; allow_f_increases = true)
end
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ function trace!(tr, d, state, iteration, method::MomentumGradientDescent, option
end

function default_options(method::MomentumGradientDescent)
Dict(:allow_f_increases => true)
(; allow_f_increases = true)
end
2 changes: 1 addition & 1 deletion src/multivariate/solvers/first_order/ngmres.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,5 +457,5 @@ end
# end

function default_options(method::AbstractNGMRES)
Dict(:allow_f_increases => true)
(;allow_f_increases = true)
end
4 changes: 2 additions & 2 deletions test/multivariate/solvers/first_order/ngmres.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using Optim, Test
@test solver.nlpreconopts.allow_f_increases == true

defopts = Optim.default_options(solver)
@test defopts == Dict(:allow_f_increases => true)
@test defopts == (; allow_f_increases = true)

state = Optim.initial_state(solver, Optim.Options(;defopts...), df,
prob.initial_x)
Expand Down Expand Up @@ -96,7 +96,7 @@ end
@test solver.nlpreconopts.allow_f_increases == true

defopts = Optim.default_options(solver)
@test defopts == Dict(:allow_f_increases => true)
@test defopts == (;allow_f_increases = true)

state = Optim.initial_state(solver, Optim.Options(;defopts...), df,
prob.initial_x)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function run_optim_tests(method; convergence_exceptions = (),
dopts = Optim.default_options(method)
if haskey(dopts, :allow_f_increases)
allow_f_increases = allow_f_increases || dopts[:allow_f_increases]
delete!(dopts, :allow_f_increases)
dopts = (;dopts..., allow_f_increases = allow_f_increases)
end
options = Optim.Options(allow_f_increases = allow_f_increases,
iterations = iters, show_trace = show_trace;
Expand Down

0 comments on commit 1f1258c

Please sign in to comment.