Skip to content

Commit

Permalink
Merge pull request #51 from JuliaNLSolvers/pkm/optimbase
Browse files Browse the repository at this point in the history
Remove Optim as dependency and add OptimBase instead.
  • Loading branch information
blakejohnson authored Jun 1, 2017
2 parents 1f9abe5 + 335ab03 commit 4ecb0ec
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: julia
julia:
- 0.4
- 0.5
- 0.6
- nightly
notifications:
email: false
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.4
julia 0.5
Calculus
Distributions
Optim 0.7.5
Compat 0.9.1
OptimBase
4 changes: 3 additions & 1 deletion src/LsqFit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module LsqFit
estimate_errors,
estimate_covar

using Optim
using Calculus
using Distributions
using Compat
using OptimBase

import Base.summary

include("levenberg_marquardt.jl")
include("curve_fit.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/curve_fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ end
# provide a method for those who have their own Jacobian function
function lmfit(f::Function, g::Function, p0, wt; kwargs...)
results = levenberg_marquardt(f, g, p0; kwargs...)
p = Optim.minimizer(results)
p = minimizer(results)
resid = f(p)
dof = length(resid) - length(p)
return LsqFitResult(dof, p, f(p), g(p), Optim.converged(results), wt)
return LsqFitResult(dof, p, f(p), g(p), converged(results), wt)
end

function lmfit(f::Function, p0, wt; kwargs...)
Expand Down
17 changes: 10 additions & 7 deletions src/levenberg_marquardt.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
immutable LevenbergMarquardt <: Optim.Optimizer end

immutable LevenbergMarquardt <: Optimizer end
Base.summary(::LevenbergMarquardt) = "Levenberg-Marquardt"
"""
`levenberg_marquardt(f, g, initial_x; <keyword arguments>`
Expand Down Expand Up @@ -69,10 +69,10 @@ function levenberg_marquardt{T}(f::Function, g::Function, initial_x::AbstractVec
n_buffer = Vector{T}(n)

# Maintain a trace of the system.
tr = Optim.OptimizationTrace{LevenbergMarquardt}()
tr = OptimizationTrace{LevenbergMarquardt}()
if show_trace
d = Dict("lambda" => lambda)
os = Optim.OptimizationState{LevenbergMarquardt}(iterCt, sum(abs2, fcur), NaN, d)
os = OptimizationState{LevenbergMarquardt}(iterCt, sum(abs2, fcur), NaN, d)
push!(tr, os)
println(os)
end
Expand Down Expand Up @@ -145,7 +145,7 @@ function levenberg_marquardt{T}(f::Function, g::Function, initial_x::AbstractVec
if show_trace
g_norm = norm(J' * fcur, Inf)
d = Dict("g(x)" => g_norm, "dx" => delta_x, "lambda" => lambda)
os = Optim.OptimizationState{LevenbergMarquardt}(iterCt, sum(abs2, fcur), g_norm, d)
os = OptimizationState{LevenbergMarquardt}(iterCt, sum(abs2, fcur), g_norm, d)
push!(tr, os)
println(os)
end
Expand All @@ -161,19 +161,22 @@ function levenberg_marquardt{T}(f::Function, g::Function, initial_x::AbstractVec
converged = g_converged | x_converged
end

Optim.MultivariateOptimizationResults(
"Levenberg-Marquardt", # method
MultivariateOptimizationResults(
LevenbergMarquardt(), # method
initial_x, # initial_x
x, # minimizer
sum(abs2, fcur), # minimum
iterCt, # iterations
!converged, # iteration_converged
x_converged, # x_converged
0.0, # x_tol
0.0,
false, # f_converged
0.0, # f_tol
0.0,
g_converged, # g_converged
tolG, # g_tol
0.0,
false, # f_increased
tr, # trace
f_calls, # f_calls
Expand Down
22 changes: 11 additions & 11 deletions test/levenberg_marquardt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let
initial_x = [100.0, 100.0]

results = LsqFit.levenberg_marquardt(f_lm, g_lm, initial_x)
@assert norm(Optim.minimizer(results) - [0.0, 2.0]) < 0.01
@assert norm(OptimBase.minimizer(results) - [0.0, 2.0]) < 0.01


function rosenbrock_res(x, r)
Expand All @@ -36,12 +36,12 @@ let

results = LsqFit.levenberg_marquardt(frb, grb, initial_xrb)

@assert norm(Optim.minimizer(results) - [1.0, 1.0]) < 0.01
@assert norm(OptimBase.minimizer(results) - [1.0, 1.0]) < 0.01

# check estimate is within the bound PR #278
result = LsqFit.levenberg_marquardt(frb, grb, [150.0, 150.0]; lower = [10.0, 10.0], upper = [200.0, 200.0])
@test Optim.minimizer(result)[1] >= 10.0
@test Optim.minimizer(result)[2] >= 10.0
@test OptimBase.minimizer(result)[1] >= 10.0
@test OptimBase.minimizer(result)[2] >= 10.0



Expand All @@ -60,7 +60,7 @@ let
g_lsq = Calculus.jacobian(f_lsq)
results = LsqFit.levenberg_marquardt(f_lsq, g_lsq, [0.5, 0.5])

@assert norm(Optim.minimizer(results) - [1.0, 2.0]) < 0.05
@assert norm(OptimBase.minimizer(results) - [1.0, 2.0]) < 0.05
end

let
Expand All @@ -84,15 +84,15 @@ let

lower=[5.0, 11.0, 5.0]
results = LsqFit.levenberg_marquardt(f_lsq, g_lsq, [15.0, 15.0, 15.0], lower=lower)
Optim.minimizer(results)
@test Optim.converged(results)
@test all(Optim.minimizer(results) .>= lower)
OptimBase.minimizer(results)
@test OptimBase.converged(results)
@test all(OptimBase.minimizer(results) .>= lower)

upper=[15.0, 9.0, 15.0]
results = LsqFit.levenberg_marquardt(f_lsq, g_lsq, [5.0, 5.0, 5.0], upper=upper)
Optim.minimizer(results)
@test Optim.converged(results)
@test all(Optim.minimizer(results) .<= upper)
OptimBase.minimizer(results)
@test OptimBase.converged(results)
@test all(OptimBase.minimizer(results) .<= upper)

# tests for PR #267
LsqFit.levenberg_marquardt(f_lsq, g_lsq, [15.0, 15.0, 15.0], show_trace=true)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Correctness Tests
#

using LsqFit, Optim, Base.Test, Compat
using LsqFit, Base.Test, Compat

my_tests = [ "curve_fit.jl", "levenberg_marquardt.jl"]

Expand Down

0 comments on commit 4ecb0ec

Please sign in to comment.