Skip to content
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

Adding time limit to levenberg_marquardt.jl #204

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/levenberg_marquardt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Comp & Applied Math).
* `x_tol::Real=1e-8`: search tolerance in x
* `g_tol::Real=1e-12`: search tolerance in gradient
* `maxIter::Integer=1000`: maximum number of iterations
* `time_limit::Integer=-1`: maximum running time in seconds (-1 for no limit)
* `min_step_quality=1e-3`: for steps below this quality, the trust region is shrinked
* `good_step_quality=0.75`: for steps above this quality, the trust region is expanded
* `lambda::Real=10`: (inverse of) initial trust region radius
Expand All @@ -32,12 +33,14 @@ Comp & Applied Math).
# way to provide an autodiff-made acceleration when someone doesn't provide an `avv`.
# it would probably be very inefficient performace-wise for most cases, but it wouldn't hurt to have it somewhere
function levenberg_marquardt(df::OnceDifferentiable, initial_x::AbstractVector{T};
x_tol::Real = 1e-8, g_tol::Real = 1e-12, maxIter::Integer = 1000,
x_tol::Real = 1e-8, g_tol::Real = 1e-12, maxIter::Integer = 1000, time_limit::Integer=-1,
pkofod marked this conversation as resolved.
Show resolved Hide resolved
lambda = T(10), tau=T(Inf), lambda_increase::Real = 10.0, lambda_decrease::Real = 0.1,
min_step_quality::Real = 1e-3, good_step_quality::Real = 0.75,
show_trace::Bool = false, lower::AbstractVector{T} = Array{T}(undef, 0), upper::AbstractVector{T} = Array{T}(undef, 0), avv!::Union{Function,Nothing,Avv} = nothing
) where T

t0 = time() # Initial time stamp used to control early stopping by time_limit

# First evaluation
value_jacobian!!(df, initial_x)

Expand Down Expand Up @@ -66,6 +69,7 @@ function levenberg_marquardt(df::OnceDifferentiable, initial_x::AbstractVector{T
x_converged = false
g_converged = false
iterCt = 0
stopped_by_time_limit = false
x = copy(initial_x)
delta_x = copy(initial_x)
a = similar(x)
Expand Down Expand Up @@ -94,7 +98,7 @@ function levenberg_marquardt(df::OnceDifferentiable, initial_x::AbstractVector{T
println(os)
end

while (~converged && iterCt < maxIter)
while (~converged && ~stopped_by_time_limit && iterCt < maxIter)
# jacobian! will check if x is new or not, so it is only actually
# evaluated if x was updated last iteration.
jacobian!(df, x) # has alias J
Expand Down Expand Up @@ -213,6 +217,10 @@ function levenberg_marquardt(df::OnceDifferentiable, initial_x::AbstractVector{T
x_converged = true
end
converged = g_converged | x_converged

# Check time_limit; if time_limit=-1 (the default) the condition is false.
pkofod marked this conversation as resolved.
Show resolved Hide resolved
stopped_by_time_limit = (time_limit > 0) && (time() - t0 > time_limit)
show_trace && stopped_by_time_limit && println("Stopping due to time limit")
pkofod marked this conversation as resolved.
Show resolved Hide resolved
end

MultivariateOptimizationResults(
Expand Down