Skip to content

Commit

Permalink
🤖 Format .jl files
Browse files Browse the repository at this point in the history
  • Loading branch information
dpo committed Jan 19, 2025
1 parent 11e2a62 commit 60cfd2b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
74 changes: 37 additions & 37 deletions src/R2DH.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,39 @@ The objective and gradient of `nlp` will be accessed.
The value returned is a `GenericExecutionStats`, see `SolverCore.jl`.
"""
function R2DH(
nlp::AbstractDiagonalQNModel{R, S},
nlp::AbstractDiagonalQNModel{R, S},
h,
options::ROSolverOptions{R};
kwargs...,
) where {R <: Real, S}
kwargs_dict = Dict(kwargs...)
x0 = pop!(kwargs_dict, :x0, nlp.meta.x0)
xk, k, outdict = R2DH(
x -> obj(nlp, x),
(g, x) -> grad!(nlp, x, g),
h,
options::ROSolverOptions{R};
hess_op(nlp, x0),
options,
x0;
l_bound = nlp.meta.lvar,
u_bound = nlp.meta.uvar,
kwargs...,
) where {R <: Real, S}
kwargs_dict = Dict(kwargs...)
x0 = pop!(kwargs_dict, :x0, nlp.meta.x0)
xk, k, outdict = R2DH(
x -> obj(nlp, x),
(g, x) -> grad!(nlp, x, g),
h,
hess_op(nlp, x0),
options,
x0;
l_bound = nlp.meta.lvar,
u_bound = nlp.meta.uvar,
kwargs...,
)
sqrt_ξ_νInv = outdict[:sqrt_ξ_νInv]
stats = GenericExecutionStats(nlp)
set_status!(stats, outdict[:status])
set_solution!(stats, xk)
set_objective!(stats, outdict[:fk] + outdict[:hk])
set_residuals!(stats, zero(eltype(xk)), sqrt_ξ_νInv)
set_iter!(stats, k)
set_time!(stats, outdict[:elapsed_time])
set_solver_specific!(stats, :Fhist, outdict[:Fhist])
set_solver_specific!(stats, :Hhist, outdict[:Hhist])
set_solver_specific!(stats, :Time_hist, outdict[:Time_hist])
set_solver_specific!(stats, :NonSmooth, outdict[:NonSmooth])
set_solver_specific!(stats, :SubsolverCounter, outdict[:Chist])
return stats
end
)
sqrt_ξ_νInv = outdict[:sqrt_ξ_νInv]
stats = GenericExecutionStats(nlp)
set_status!(stats, outdict[:status])
set_solution!(stats, xk)
set_objective!(stats, outdict[:fk] + outdict[:hk])
set_residuals!(stats, zero(eltype(xk)), sqrt_ξ_νInv)
set_iter!(stats, k)
set_time!(stats, outdict[:elapsed_time])
set_solver_specific!(stats, :Fhist, outdict[:Fhist])
set_solver_specific!(stats, :Hhist, outdict[:Hhist])
set_solver_specific!(stats, :Time_hist, outdict[:Time_hist])
set_solver_specific!(stats, :NonSmooth, outdict[:NonSmooth])
set_solver_specific!(stats, :SubsolverCounter, outdict[:Chist])
return stats
end

"""
R2DH(f, ∇f!, h, options, x0)
Expand Down Expand Up @@ -169,11 +169,11 @@ function R2DH(
s = zero(xk)
ψ = has_bnds ? shifted(h, xk, l_bound - xk, u_bound - xk, selected) : shifted(h, xk)

Fobj_hist = zeros(maxIter+1)
Hobj_hist = zeros(maxIter+1)
time_hist = zeros(maxIter+1)
Fobj_hist = zeros(maxIter + 1)
Hobj_hist = zeros(maxIter + 1)
time_hist = zeros(maxIter + 1)
FHobj_hist = fill!(Vector{R}(undef, Mmonotone - 1), R(-Inf))
Complex_hist = zeros(Int, maxIter+1)
Complex_hist = zeros(Int, maxIter + 1)
if verbose > 0
#! format: off
@info @sprintf "%6s %8s %8s %7s %8s %7s %7s %7s %1s" "iter" "f(x)" "h(x)" "√(ξ/ν)" "ρ" "σ" "‖x‖" "‖s‖" ""
Expand Down Expand Up @@ -265,7 +265,7 @@ function R2DH(
shift!(ψ, xk)
∇f!(∇fk, xk)
push!(D, s, ∇fk - ∇fk⁻) # update QN operator
DNorm = norm(D.d, Inf)
DNorm = norm(D.d, Inf)
∇fk⁻ .= ∇fk
end

Expand All @@ -275,7 +275,7 @@ function R2DH(

Dkσk .= D.d .+ σk
ν = 1 / ((DNorm + σk) * (1 + θ))

tired = maxIter > 0 && k maxIter
if !tired
@. mν∇fk = -ν * ∇fk
Expand Down
10 changes: 5 additions & 5 deletions src/R2N.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function R2N(
subsolver_logger::Logging.AbstractLogger = Logging.NullLogger(),
subsolver = R2,
subsolver_options = ROSolverOptions(ϵa = options.ϵa),
Mmonotone::Int = 1,
Mmonotone::Int = 1,
selected::AbstractVector{<:Integer} = 1:(f.meta.nvar),
) where {H, R}
start_time = time()
Expand Down Expand Up @@ -144,14 +144,14 @@ function R2N(
elapsed_time = time() - start_time
Fobj_hist[k] = fk
Hobj_hist[k] = hk
Mmonotone > 1 && (FHobj_hist[mod(k-1, Mmonotone - 1) + 1] = fk + hk)
Mmonotone > 1 && (FHobj_hist[mod(k - 1, Mmonotone - 1) + 1] = fk + hk)

# model for first prox-gradient step and ξ1
φ1(d) = ∇fk' * d
mk1(d) = φ1(d) + ψ(d)

# model for subsequent prox-gradient steps and ξ
φ(d) = ∇fk' * d + dot(d, Bk * d) / 2 + σk * dot(d, d) / 2
φ(d) = ∇fk' * d + dot(d, Bk * d) / 2 + σk * dot(d, d) / 2

∇φ!(g, d) = begin
mul!(g, Bk, d)
Expand Down Expand Up @@ -184,7 +184,7 @@ function R2N(
end
s1 = copy(s)

subsolver_options.ϵa = k == 1 ? 1.0e-3 : min(sqrt_ξ1_νInv ^ (1.5) , sqrt_ξ1_νInv * 1e-3)
subsolver_options.ϵa = k == 1 ? 1.0e-3 : min(sqrt_ξ1_νInv^(1.5), sqrt_ξ1_νInv * 1e-3)
verbose > 0 && @debug "setting inner stopping tolerance to" subsolver_options.optTol
subsolver_options.σk = σk
subsolver_args = subsolver == R2DH ? (SpectralGradient(νInv, f.meta.nvar),) : ()
Expand Down Expand Up @@ -291,4 +291,4 @@ function R2N(
set_solver_specific!(stats, :NonSmooth, h)
set_solver_specific!(stats, :SubsolverCounter, Complex_hist[1:k])
return stats
end
end
11 changes: 8 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,17 @@ for (h, h_name) ∈ ((NormL1(λ), "l1"),)
end

R2N_R2DH(args...; kwargs...) = R2N(args...; subsolver = R2DH, kwargs...)
for (mod, mod_name) ((SpectralGradientModel, "spg"), (DiagonalPSBModel, "psb"), (LSR1Model, "lsr1"), (LBFGSModel, "lbfgs"))
for (mod, mod_name) (
(SpectralGradientModel, "spg"),
(DiagonalPSBModel, "psb"),
(LSR1Model, "lsr1"),
(LBFGSModel, "lbfgs"),
)
for (h, h_name) ((NormL0(λ), "l0"), (NormL1(λ), "l1"))
for solver_sym (:R2DH, :R2N, :R2N_R2DH)
solver_sym (:R2N,:R2N_R2DH) && mod_name ("spg", "psb") && continue
solver_sym (:R2N, :R2N_R2DH) && mod_name ("spg", "psb") && continue
solver_sym == :R2DH && mod_name != "spg" && continue
solver_sym == :R2N_R2DH && h_name == "l1" && continue # this test seems to fail because s seems to be equal to zeros within the subsolver
solver_sym == :R2N_R2DH && h_name == "l1" && continue # this test seems to fail because s seems to be equal to zeros within the subsolver
solver_name = string(solver_sym)
solver = eval(solver_sym)
@testset "bpdn-$(mod_name)-$(solver_name)-$(h_name)" begin
Expand Down

0 comments on commit 60cfd2b

Please sign in to comment.