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 all 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: 11 additions & 1 deletion 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 @@ -31,12 +32,14 @@ Comp & Applied Math).
# and the like. This way we could not only merge the two functions, but also have a convenient
# 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,
time_limit::Real=NaN,
lambda = T(10),
tau = T(Inf),
lambda_increase::Real = 10.0,
Expand All @@ -49,6 +52,8 @@ function levenberg_marquardt(
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 @@ -88,6 +93,7 @@ function levenberg_marquardt(
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 @@ -116,7 +122,7 @@ function levenberg_marquardt(
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 @@ -235,6 +241,10 @@ function levenberg_marquardt(
x_converged = true
end
converged = g_converged | x_converged

# Check time_limit; if time_limit=NaN (the default) the condition is false.
stopped_by_time_limit = (time_limit > 0) && (time() - t0 > time_limit)
show_trace && stopped_by_time_limit #&& warn("Stopping due to time limit")
end

LsqFitResults(
Expand Down