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

Format project #71

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)

Expand Down
2 changes: 1 addition & 1 deletion src/nlsolve/problem_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions src/nlsolve/spectral/dfsane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,22 @@ 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
nexp::N
γ::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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/nlsolve/trustregions/newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
21 changes: 11 additions & 10 deletions src/objectives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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???
Expand Down
25 changes: 11 additions & 14 deletions test/nlsolve/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
@test alloced == 0
2 changes: 1 addition & 1 deletion test/nlsolve/problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/optimize/param.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
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)
G .= 2 * (x .- data)
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
@test sol2.info.solution ≈ param2
end
Loading