-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add iterator interface #745
base: master
Are you sure you want to change the base?
Changes from 1 commit
b10f27e
cc594a8
d6a6d75
2117364
b7b63e8
8a64efb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,36 +27,73 @@ function initial_convergence(d, state, method::AbstractOptimizer, initial_x, opt | |
end | ||
initial_convergence(d, state, method::ZerothOrderOptimizer, initial_x, options) = false | ||
|
||
function optimize(d::D, initial_x::Tx, method::M, | ||
options::Options{T, TCallback} = Options(;default_options(method)...), | ||
state = initial_state(method, options, d, initial_x)) where {D<:AbstractObjective, M<:AbstractOptimizer, Tx <: AbstractArray, T, TCallback} | ||
if length(initial_x) == 1 && typeof(method) <: NelderMead | ||
error("You cannot use NelderMead for univariate problems. Alternatively, use either interval bound univariate optimization, or another method such as BFGS or Newton.") | ||
end | ||
struct OptimIterator{D <: AbstractObjective, M <: AbstractOptimizer, Tx <: AbstractArray, O <: Options, S} | ||
d::D | ||
initial_x::Tx | ||
method::M | ||
options::O | ||
state::S | ||
end | ||
|
||
Base.IteratorSize(::Type{<:OptimIterator}) = Base.SizeUnknown() | ||
Base.IteratorEltype(::Type{<:OptimIterator}) = Base.HasEltype() | ||
Base.eltype(::Type{<:OptimIterator}) = IteratorState | ||
|
||
@with_kw struct IteratorState{IT <: OptimIterator, TR <: OptimizationTrace} | ||
# Put `OptimIterator` in iterator state so that `OptimizationResults` can | ||
# be constructed from `IteratorState`. | ||
iter::IT | ||
|
||
t0::Float64 | ||
tr::TR | ||
tracing::Bool | ||
stopped::Bool | ||
stopped_by_callback::Bool | ||
stopped_by_time_limit::Bool | ||
f_limit_reached::Bool | ||
g_limit_reached::Bool | ||
h_limit_reached::Bool | ||
x_converged::Bool | ||
f_converged::Bool | ||
f_increased::Bool | ||
counter_f_tol::Int | ||
g_converged::Bool | ||
converged::Bool | ||
iteration::Int | ||
ls_success::Bool | ||
end | ||
|
||
function Base.iterate(iter::OptimIterator, istate = nothing) | ||
@unpack d, initial_x, method, options, state = iter | ||
if istate === nothing | ||
t0 = time() # Initial time stamp used to control early stopping by options.time_limit | ||
|
||
tr = OptimizationTrace{typeof(value(d)), typeof(method)}() | ||
tracing = options.store_trace || options.show_trace || options.extended_trace || options.callback != nothing | ||
stopped, stopped_by_callback, stopped_by_time_limit = false, false, false | ||
f_limit_reached, g_limit_reached, h_limit_reached = false, false, false | ||
x_converged, f_converged, f_increased, counter_f_tol = false, false, false, 0 | ||
|
||
t0 = time() # Initial time stamp used to control early stopping by options.time_limit | ||
g_converged = initial_convergence(d, state, method, initial_x, options) | ||
converged = g_converged | ||
|
||
tr = OptimizationTrace{typeof(value(d)), typeof(method)}() | ||
tracing = options.store_trace || options.show_trace || options.extended_trace || options.callback != nothing | ||
stopped, stopped_by_callback, stopped_by_time_limit = false, false, false | ||
f_limit_reached, g_limit_reached, h_limit_reached = false, false, false | ||
x_converged, f_converged, f_increased, counter_f_tol = false, false, false, 0 | ||
# prepare iteration counter (used to make "initial state" trace entry) | ||
iteration = 0 | ||
|
||
g_converged = initial_convergence(d, state, method, initial_x, options) | ||
converged = g_converged | ||
options.show_trace && print_header(method) | ||
trace!(tr, d, state, iteration, method, options, time()-t0) | ||
ls_success::Bool = true | ||
else | ||
@unpack_IteratorState istate | ||
|
||
# prepare iteration counter (used to make "initial state" trace entry) | ||
iteration = 0 | ||
!converged && !stopped && iteration < options.iterations || return nothing | ||
|
||
options.show_trace && print_header(method) | ||
trace!(tr, d, state, iteration, method, options, time()-t0) | ||
ls_success::Bool = true | ||
while !converged && !stopped && iteration < options.iterations | ||
iteration += 1 | ||
|
||
ls_failed = update_state!(d, state, method) | ||
if !ls_success | ||
break # it returns true if it's forced by something in update! to stop (eg dx_dg == 0.0 in BFGS, or linesearch errors) | ||
# it returns true if it's forced by something in update! to stop (eg dx_dg == 0.0 in BFGS, or linesearch errors) | ||
return nothing | ||
end | ||
update_g!(d, state, method) # TODO: Should this be `update_fg!`? | ||
|
||
|
@@ -85,7 +122,35 @@ function optimize(d::D, initial_x::Tx, method::M, | |
stopped_by_time_limit || f_limit_reached || g_limit_reached || h_limit_reached | ||
stopped = true | ||
end | ||
end # while | ||
end | ||
|
||
new_istate = IteratorState( | ||
iter, | ||
t0, | ||
tr, | ||
tracing, | ||
stopped, | ||
stopped_by_callback, | ||
stopped_by_time_limit, | ||
f_limit_reached, | ||
g_limit_reached, | ||
h_limit_reached, | ||
x_converged, | ||
f_converged, | ||
f_increased, | ||
counter_f_tol, | ||
g_converged, | ||
converged, | ||
iteration, | ||
ls_success, | ||
) | ||
|
||
return new_istate, new_istate | ||
end | ||
|
||
function OptimizationResults(istate::IteratorState) | ||
@unpack_IteratorState istate | ||
@unpack d, initial_x, method, options, state = iter | ||
|
||
after_while!(d, state, method, options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Executing a mutating-function But using a function like |
||
|
||
|
@@ -94,6 +159,9 @@ function optimize(d::D, initial_x::Tx, method::M, | |
Tf = typeof(value(d)) | ||
f_incr_pick = f_increased && !options.allow_f_increases | ||
|
||
T = (_tmp(::Options{T}) where T = T)(options) | ||
Tx = typeof(initial_x) | ||
|
||
return MultivariateOptimizationResults{typeof(method),T,Tx,typeof(x_abschange(state)),Tf,typeof(tr), Bool}(method, | ||
initial_x, | ||
pick_best_x(f_incr_pick, state), | ||
|
@@ -120,3 +188,22 @@ function optimize(d::D, initial_x::Tx, method::M, | |
h_calls(d), | ||
!ls_success) | ||
end | ||
|
||
function optimizing(d::D, initial_x::Tx, method::M, | ||
options::Options = Options(;default_options(method)...), | ||
state = initial_state(method, options, d, initial_x)) where {D<:AbstractObjective, M<:AbstractOptimizer, Tx <: AbstractArray} | ||
if length(initial_x) == 1 && typeof(method) <: NelderMead | ||
error("You cannot use NelderMead for univariate problems. Alternatively, use either interval bound univariate optimization, or another method such as BFGS or Newton.") | ||
end | ||
return OptimIterator(d, initial_x, method, options, state) | ||
end | ||
|
||
function optimize(d::D, initial_x::Tx, method::M, | ||
options::Options = Options(;default_options(method)...), | ||
state = initial_state(method, options, d, initial_x)) where {D<:AbstractObjective, M<:AbstractOptimizer, Tx <: AbstractArray} | ||
local istate | ||
for istate′ in optimizing(d, initial_x, method, options, state) | ||
istate = istate′ | ||
end | ||
return OptimizationResults(istate) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can drop
iter
field fromIteratorState
if we change the API to:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessor functions like
iteration_limit_reached(istate)
and(f|g|h)_calls(istate)
need.iter
, too.