diff --git a/docs/make.jl b/docs/make.jl index 0c62bba..3a7ed11 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -3,7 +3,7 @@ using Documenter, DocumenterMarkdown, NLSolvers makedocs( doctest = false, sitename = "NLSolvers.jl", - pages = ["index.md", "optimization.md",], + pages = ["index.md", "optimization.md"], format = Markdown(), ) diff --git a/src/nlsolve/problem_types.jl b/src/nlsolve/problem_types.jl index 748f06a..3aaa4b5 100644 --- a/src/nlsolve/problem_types.jl +++ b/src/nlsolve/problem_types.jl @@ -36,7 +36,7 @@ end function jacobian(nleq::NEqProblem{<:ScalarObjective}, J, x) gradient(nleq.R, J, x) end -function value_jacobian(nleq::NEqProblem{<:ScalarObjective, <:Any, <:Any, OutOfPlace}, F, J, x) +function value_jacobian(nleq::NEqProblem{<:ScalarObjective,<:Any,<:Any,OutOfPlace}, F, J, x) nleq.R.fg(J, x) end diff --git a/src/nlsolve/spectral/dfsane.jl b/src/nlsolve/spectral/dfsane.jl index 3118657..f8dae8d 100644 --- a/src/nlsolve/spectral/dfsane.jl +++ b/src/nlsolve/spectral/dfsane.jl @@ -59,7 +59,7 @@ To re-implement the setup in the numerical section of [PAPER] use DFSANE(nexp=2,memory=10, sigma_limit=(1e-10, 1e10), decrease=1e-4, sigma_0=1.0)) """ -struct DFSANE{T,Σ,TAU,N, G, A} +struct DFSANE{T,Σ,TAU,N,G,A} memory::T sigma_limits::Σ taus::TAU @@ -67,7 +67,14 @@ struct DFSANE{T,Σ,TAU,N, G, A} γ::G σ0::A end -DFSANE(; memory = 4, sigma_limits = (1e-5, 1e5), taus=(1 / 10, 1 / 2), nexp=2,decrease = 1 / 10000, sigma_0 = 1.0) = DFSANE(memory, sigma_limits, taus,nexp, decrease, sigma_0) +DFSANE(; + memory = 4, + sigma_limits = (1e-5, 1e5), + taus = (1 / 10, 1 / 2), + nexp = 2, + decrease = 1 / 10000, + sigma_0 = 1.0, +) = DFSANE(memory, sigma_limits, taus, nexp, decrease, sigma_0) init(::NEqProblem, ::DFSANE; x, Fx = copy(x), y = copy(x), d = copy(x), z = copy(x)) = (; x, Fx, y, d, z) @@ -122,7 +129,7 @@ function solve( ηk = ρ2F0 / (1 + iter)^2 φ(α) = norm(F(Fx, (z .= x .+ α .* d)))^nexp φ0 = fx - α, φα = find_steplength(RNMS(method.γ,method.σ0), φ, φ0, fbar, ηk, τmin, τmax) + α, φα = find_steplength(RNMS(method.γ, method.σ0), φ, φ0, fbar, ηk, τmin, τmax) if isnan(α) || isnan(φα) status = :linesearch_failed break diff --git a/src/nlsolve/trustregions/newton.jl b/src/nlsolve/trustregions/newton.jl index af25da8..7d0ec16 100644 --- a/src/nlsolve/trustregions/newton.jl +++ b/src/nlsolve/trustregions/newton.jl @@ -48,7 +48,7 @@ function solve( approach::TrustRegion{<:Union{SR1,DBFGS,BFGS,Newton},<:Any,<:Any}, options::NEqOptions, ) - if !(mstyle(prob) === InPlace()) && !(approach.spsolve isa Dogleg) + if !(mstyle(prob) === InPlace()) && !(approach.spsolve isa Dogleg) throw( ErrorException( "solve() not defined for OutOfPlace() with Trustregion for NEqProblem", @@ -76,5 +76,5 @@ function solve( time = res.info.time, iter = res.info.iter, ) - return ConvergenceInfo(approach, newinfo, options) + return ConvergenceInfo(approach, newinfo, options) end diff --git a/src/objectives.jl b/src/objectives.jl index 8390e64..eb6fb07 100644 --- a/src/objectives.jl +++ b/src/objectives.jl @@ -20,15 +20,15 @@ struct ScalarObjective{Tf,Tg,Tfg,Tfgh,Th,Thv,Tbf,P} param::P end ScalarObjective(; - f = nothing, - g = nothing, - fg = nothing, - fgh = nothing, - h = nothing, - hv = nothing, - batched_f = nothing, - param = nothing, - ) = ScalarObjective(f, g, fg, fgh, h, hv, batched_f, param) + f = nothing, + g = nothing, + fg = nothing, + fgh = nothing, + h = nothing, + hv = nothing, + batched_f = nothing, + param = nothing, +) = ScalarObjective(f, g, fg, fgh, h, hv, batched_f, param) has_param(so::ScalarObjective) = so.param === nothing ? false : true function value(so::ScalarObjective, x) if has_param(so) @@ -118,7 +118,8 @@ struct VectorObjective{TF,TJ,TFJ,TJv} FJ::TFJ Jv::TJv end -VectorObjective(; F=nothing, J=nothing, FJ=nothing, Jv=nothing) = VectorObjective(F, J, FJ, Jv) +VectorObjective(; F = nothing, J = nothing, FJ = nothing, Jv = nothing) = + VectorObjective(F, J, FJ, Jv) ## If prob is a NEqProblem, then we can just dispatch to least squares MeritObjective # if fast JacVec exists then maybe even line searches that updates the gradient can be used??? diff --git a/test/nlsolve/interface.jl b/test/nlsolve/interface.jl index 55b9a2c..4df4f92 100644 --- a/test/nlsolve/interface.jl +++ b/test/nlsolve/interface.jl @@ -447,17 +447,14 @@ end @testset "scalar nlsolves" begin function ff(x) x^2 - end - - function fgg(Jx, x) - x^2, 2x - end - - prob_obj = NLSolvers.ScalarObjective( - f=ff, - fg=fgg, - ) - + end + + function fgg(Jx, x) + x^2, 2x + end + + prob_obj = NLSolvers.ScalarObjective(f = ff, fg = fgg) + prob = NEqProblem(prob_obj; inplace = false) x0 = 0.3 @@ -468,7 +465,7 @@ function solve_static() function F_rosenbrock_static(Fx, x) Fx1 = 1 - x[1] Fx2 = 10(x[2] - x[1]^2) - return @SVector([Fx1,Fx2]) + return @SVector([Fx1, Fx2]) end function J_rosenbrock_static(Jx, x) Jx11 = -1 @@ -489,11 +486,11 @@ function solve_static() nothing, ) - prob_static = NEqProblem(obj; inplace=false) + prob_static = NEqProblem(obj; inplace = false) x0_static = @SVector([-1.2, 1.0]) res = solve(prob_static, x0_static, TrustRegion(Newton(), Dogleg()), NEqOptions()) end solve_static() alloced = @allocated solve_static() -@test alloced == 0 \ No newline at end of file +@test alloced == 0 diff --git a/test/nlsolve/problems.jl b/test/nlsolve/problems.jl index 988f31a..f4b9693 100644 --- a/test/nlsolve/problems.jl +++ b/test/nlsolve/problems.jl @@ -45,7 +45,7 @@ NLE_PROBS["rosenbrock"]["static"] = Dict() function F_rosenbrock_static(Fx, x) Fx1 = 1 - x[1] Fx2 = 10(x[2] - x[1]^2) - return @SVector([Fx1,Fx2]) + return @SVector([Fx1, Fx2]) end function J_rosenbrock_static(Jx, x) Jx11 = -1 diff --git a/test/optimize/param.jl b/test/optimize/param.jl index 321a7f2..6bb730f 100644 --- a/test/optimize/param.jl +++ b/test/optimize/param.jl @@ -2,7 +2,7 @@ N_test = 9 w = rand(N_test) function obji(x, data) - s=sum((x .- data) .^ 2) + s = sum((x .- data) .^ 2) s end function g!(G, x, data) @@ -10,18 +10,18 @@ G end function h!(H, x, data) - H .= 2 * I + H .= 2 * I H end param1 = [1, 2, 3] sc = ScalarObjective(; f = obji, g = g!, h = h!, param = param1) op = OptimizationProblem(sc) sol1 = solve(op, [3.0, 4.0, 4.0], BFGS(), OptimizationOptions()) - @test sol1.info.solution ≈ param1 + @test sol1.info.solution ≈ param1 param2 = [10, 20, 30] sc = ScalarObjective(; f = obji, g = g!, h = h!, param = param2) op = OptimizationProblem(sc) sol2 = solve(op, [3.0, 4.0, 4.0], BFGS(), OptimizationOptions()) - @test sol2.info.solution ≈ param2 -end \ No newline at end of file + @test sol2.info.solution ≈ param2 +end