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

Add constructor to convert NLLS to OptimizationProblem #538

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions src/problems/basic_problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,21 @@
OptimizationProblem{true}(OptimizationFunction{true}(f), args...; kwargs...)
end

function OptimizationFunction(f::NonlinearFunction, adtype::AbstractADType = NoAD(); kwargs...)
if isinplace(f)
throw(ArgumentError("Converting NonlinearFunction to OptimizationFunction is not supported with in-place functions yet."))

Check warning on line 693 in src/problems/basic_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/basic_problems.jl#L691-L693

Added lines #L691 - L693 were not covered by tests
end
OptimizationFunction((u, p) -> sum(abs2, f(u, p)), adtype; kwargs...)

Check warning on line 695 in src/problems/basic_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/basic_problems.jl#L695

Added line #L695 was not covered by tests
end

function OptimizationProblem(prob::NonlinearLeastSquaresProblem, adtype::AbstractADType = NoAD(); kwargs...)
if isinplace(prob)
throw(ArgumentError("Converting NonlinearLeastSquaresProblem to OptimizationProblem is not supported with in-place functions yet."))

Check warning on line 700 in src/problems/basic_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/basic_problems.jl#L698-L700

Added lines #L698 - L700 were not covered by tests
end
optf = OptimizationFunction(prob.f, adtype; kwargs...)
return OptimizationProblem(optf, prob.u0, prob.p; prob.kwargs..., kwargs...)

Check warning on line 703 in src/problems/basic_problems.jl

View check run for this annotation

Codecov / codecov/patch

src/problems/basic_problems.jl#L702-L703

Added lines #L702 - L703 were not covered by tests
end

isinplace(f::OptimizationFunction{iip}) where {iip} = iip
isinplace(f::OptimizationProblem{iip}) where {iip} = iip

Expand Down
29 changes: 29 additions & 0 deletions test/downstream/nllsopt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using NonlinearSolve, Optimization, OptimizationNLopt, ForwardDiff
import FastLevenbergMarquardt, LeastSquaresOptim
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These downstream deps would be required. But just remove FastLevenbergMarquardt and LeastSquaresOptim?

Vaibhavdixit02 marked this conversation as resolved.
Show resolved Hide resolved

true_function(x, θ) = @. θ[1] * exp(θ[2] * x) * cos(θ[3] * x + θ[4])

θ_true = [1.0, 0.1, 2.0, 0.5]

x = [-1.0, -0.5, 0.0, 0.5, 1.0]

y_target = true_function(x, θ_true)

function loss_function(θ, p)
ŷ = true_function(p, θ)
return ŷ .- y_target
end

θ_init = θ_true .+ randn!(similar(θ_true)) * 0.1
prob_oop = NonlinearLeastSquaresProblem{false}(loss_function, θ_init, x)

solver = LevenbergMarquardt()

@time sol = solve(prob, solver; maxiters = 10000, abstol = 1e-8)

optf = OptimizationFunction(prob_oop.f, AutoForwardDiff())
optprob = OptimizationProblem(optf, prob_oop.u0, prob_oop.p)
@time sol = solve(optprob, NLopt.LD_LBFGS(); maxiters = 10000, abstol = 1e-8)

optprob = OptimizationProblem(prob_oop, AutoForwardDiff())
@time sol = solve(optprob, NLopt.LD_LBFGS(); maxiters = 10000, abstol = 1e-8)
Loading