Skip to content

Commit 8d42eb0

Browse files
dpoabelsiqueira
authored andcommitted
🤖 Format .jl files
1 parent 689d086 commit 8d42eb0

File tree

13 files changed

+475
-250
lines changed

13 files changed

+475
-250
lines changed

docs/make.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ makedocs(
55
doctest = true,
66
linkcheck = true,
77
strict = true,
8-
format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true",
9-
assets = ["assets/style.css"]),
8+
format = Documenter.HTML(
9+
prettyurls = get(ENV, "CI", nothing) == "true",
10+
assets = ["assets/style.css"],
11+
),
1012
sitename = "JSOSolvers.jl",
11-
pages = ["Home" => "index.md",
12-
"Solvers" => "solvers.md",
13-
"Internal" => "internal.md",
14-
"Reference" => "reference.md",
15-
]
13+
pages = [
14+
"Home" => "index.md",
15+
"Solvers" => "solvers.md",
16+
"Internal" => "internal.md",
17+
"Reference" => "reference.md",
18+
],
1619
)
1720

1821
deploydocs(repo = "github.com/JuliaSmoothOptimizers/JSOSolvers.jl.git")

src/lbfgs.jl

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ export lbfgs
66
An implementation of a limited memory BFGS line-search method for unconstrained
77
minimization.
88
"""
9-
function lbfgs(nlp :: AbstractNLPModel;
10-
x :: AbstractVector=copy(nlp.meta.x0),
11-
atol :: Real=eps(eltype(x)), rtol :: Real=eps(eltype(x)),
12-
max_eval :: Int=-1,
13-
max_time :: Float64=30.0,
14-
verbose :: Bool=true,
15-
mem :: Int=5)
16-
9+
function lbfgs(
10+
nlp::AbstractNLPModel;
11+
x::AbstractVector = copy(nlp.meta.x0),
12+
atol::Real = eps(eltype(x)),
13+
rtol::Real = eps(eltype(x)),
14+
max_eval::Int = -1,
15+
max_time::Float64 = 30.0,
16+
verbose::Bool = true,
17+
mem::Int = 5,
18+
)
1719
if !unconstrained(nlp)
1820
error("lbfgs should only be called for unconstrained problems. Try tron instead")
1921
end
@@ -29,14 +31,17 @@ function lbfgs(nlp :: AbstractNLPModel;
2931

3032
f = obj(nlp, x)
3133
∇f = grad(nlp, x)
32-
H = InverseLBFGSOperator(T, n, mem, scaling=true)
34+
H = InverseLBFGSOperator(T, n, mem, scaling = true)
3335

3436
∇fNorm = nrm2(n, ∇f)
3537
ϵ = atol + rtol * ∇fNorm
3638
iter = 0
3739

38-
@info log_header([:iter, :f, :dual, :slope, :bk], [Int, T, T, T, Int],
39-
hdr_override=Dict(:f=>"f(x)", :dual=>"‖∇f‖", :slope=>"∇fᵀd"))
40+
@info log_header(
41+
[:iter, :f, :dual, :slope, :bk],
42+
[Int, T, T, T, Int],
43+
hdr_override = Dict(:f => "f(x)", :dual => "‖∇f‖", :slope => "∇fᵀd"),
44+
)
4045

4146
optimal = ∇fNorm ϵ
4247
tired = neval_obj(nlp) > max_eval 0 || elapsed_time > max_time
@@ -46,7 +51,7 @@ function lbfgs(nlp :: AbstractNLPModel;
4651
h = LineModel(nlp, x, ∇f)
4752

4853
while !(optimal || tired || stalled)
49-
d = - H * ∇f
54+
d = -H * ∇f
5055
slope = dot(n, d, ∇f)
5156
if slope 0
5257
@error "not a descent direction" slope
@@ -57,7 +62,8 @@ function lbfgs(nlp :: AbstractNLPModel;
5762

5863
redirect!(h, x, d)
5964
# Perform improved Armijo linesearch.
60-
t, good_grad, ft, nbk, nbW = armijo_wolfe(h, f, slope, ∇ft, τ₁=T(0.9999), bk_max=25, verbose=false)
65+
t, good_grad, ft, nbk, nbW =
66+
armijo_wolfe(h, f, slope, ∇ft, τ₁ = T(0.9999), bk_max = 25, verbose = false)
6167

6268
@info log_row(Any[iter, f, ∇fNorm, slope, nbk])
6369

@@ -91,6 +97,13 @@ function lbfgs(nlp :: AbstractNLPModel;
9197
end
9298
end
9399

94-
return GenericExecutionStats(status, nlp, solution=x, objective=f, dual_feas=∇fNorm,
95-
iter=iter, elapsed_time=elapsed_time)
100+
return GenericExecutionStats(
101+
status,
102+
nlp,
103+
solution = x,
104+
objective = f,
105+
dual_feas = ∇fNorm,
106+
iter = iter,
107+
elapsed_time = elapsed_time,
108+
)
96109
end

src/tron.jl

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
export tron
55

6-
tron(nlp :: AbstractNLPModel; variant=:Newton, kwargs...) = tron(Val(variant), nlp; kwargs...)
6+
tron(nlp::AbstractNLPModel; variant = :Newton, kwargs...) = tron(Val(variant), nlp; kwargs...)
77

88
"""
99
tron(nlp)
@@ -18,24 +18,24 @@ TRON is described in
1818
Chih-Jen Lin and Jorge J. Moré, *Newton's Method for Large Bound-Constrained
1919
Optimization Problems*, SIAM J. Optim., 9(4), 1100–1127, 1999.
2020
"""
21-
function tron(::Val{:Newton},
22-
nlp :: AbstractNLPModel;
23-
subsolver_logger :: AbstractLogger=NullLogger(),
24-
x :: AbstractVector=copy(nlp.meta.x0),
25-
μ₀ :: Real=eltype(x)(1e-2),
26-
μ₁ :: Real=one(eltype(x)),
27-
σ :: Real=eltype(x)(10),
28-
max_eval :: Int=-1,
29-
max_time :: Real=30.0,
30-
max_cgiter :: Int=50,
31-
use_only_objgrad :: Bool=false,
32-
cgtol :: Real=eltype(x)(0.1),
33-
atol :: Real=eps(eltype(x)),
34-
rtol :: Real=eps(eltype(x)),
35-
fatol :: Real=zero(eltype(x)),
36-
frtol :: Real=eps(eltype(x))^eltype(x)(2/3)
37-
)
38-
21+
function tron(
22+
::Val{:Newton},
23+
nlp::AbstractNLPModel;
24+
subsolver_logger::AbstractLogger = NullLogger(),
25+
x::AbstractVector = copy(nlp.meta.x0),
26+
μ₀::Real = eltype(x)(1e-2),
27+
μ₁::Real = one(eltype(x)),
28+
σ::Real = eltype(x)(10),
29+
max_eval::Int = -1,
30+
max_time::Real = 30.0,
31+
max_cgiter::Int = 50,
32+
use_only_objgrad::Bool = false,
33+
cgtol::Real = eltype(x)(0.1),
34+
atol::Real = eps(eltype(x)),
35+
rtol::Real = eps(eltype(x)),
36+
fatol::Real = zero(eltype(x)),
37+
frtol::Real = eps(eltype(x))^eltype(x)(2 / 3),
38+
)
3939
if !(unconstrained(nlp) || bound_constrained(nlp))
4040
error("tron should only be called for unconstrained or bound-constrained problems")
4141
end
@@ -79,16 +79,19 @@ function tron(::Val{:Newton},
7979

8080
αC = one(T)
8181
tr = TRONTrustRegion(min(max(one(T), πx / 10), 100))
82-
@info log_header([:iter, :f, :dual, :radius, :ratio, :cgstatus], [Int, T, T, T, T, String],
83-
hdr_override=Dict(:f=>"f(x)", :dual=>"π", :radius=>"Δ"))
82+
@info log_header(
83+
[:iter, :f, :dual, :radius, :ratio, :cgstatus],
84+
[Int, T, T, T, T, String],
85+
hdr_override = Dict(:f => "f(x)", :dual => "π", :radius => "Δ"),
86+
)
8487
while !(optimal || tired || stalled || unbounded)
8588
# Current iteration
8689
xc .= x
8790
fc = fx
8891
Δ = get_property(tr, :radius)
8992
H = hess_op!(nlp, xc, temp)
9093

91-
αC, s, cauchy_status = cauchy(x, H, gx, Δ, αC, ℓ, u, μ₀=μ₀, μ₁=μ₁, σ=σ)
94+
αC, s, cauchy_status = cauchy(x, H, gx, Δ, αC, ℓ, u, μ₀ = μ₀, μ₁ = μ₁, σ = σ)
9295

9396
if cauchy_status != :success
9497
@error "Cauchy step returned: $cauchy_status"
@@ -97,7 +100,7 @@ function tron(::Val{:Newton},
97100
continue
98101
end
99102
s, Hs, cgits, cginfo = with_logger(subsolver_logger) do
100-
projected_newton!(x, H, gx, Δ, cgtol, s, ℓ, u, max_cgiter=max_cgiter)
103+
projected_newton!(x, H, gx, Δ, cgtol, s, ℓ, u, max_cgiter = max_cgiter)
101104
end
102105
slope = dot(n, gx, s)
103106
qs = dot(n, s, Hs) / 2 + slope
@@ -153,7 +156,6 @@ function tron(::Val{:Newton},
153156
tired = el_time > max_time || neval_obj(nlp) > max_eval 0
154157
optimal = πx <= ϵ
155158
unbounded = fx < fmin
156-
157159
end
158160
@info log_row(Any[iter, fx, πx, get_property(tr, :radius)])
159161

@@ -169,8 +171,16 @@ function tron(::Val{:Newton},
169171
status = :unbounded
170172
end
171173

172-
return GenericExecutionStats(status, nlp, solution=x, objective=fx, dual_feas=πx,
173-
primal_feas=zero(eltype(x)), iter=iter, elapsed_time=el_time)
174+
return GenericExecutionStats(
175+
status,
176+
nlp,
177+
solution = x,
178+
objective = fx,
179+
dual_feas = πx,
180+
primal_feas = zero(eltype(x)),
181+
iter = iter,
182+
elapsed_time = el_time,
183+
)
174184
end
175185

176186
"""`s = projected_line_search!(x, H, g, d, ℓ, u; μ₀ = 1e-2)`
@@ -182,12 +192,15 @@ Performs a projected line search, searching for a step size `t` such that
182192
where `s = P(x + t * d) - x`, while remaining on the same face as `x + d`.
183193
Backtracking is performed from t = 1.0. `x` is updated in place.
184194
"""
185-
function projected_line_search!(x::AbstractVector{T},
186-
H::Union{AbstractMatrix,AbstractLinearOperator},
187-
g::AbstractVector{T},
188-
d::AbstractVector{T},
189-
::AbstractVector{T},
190-
u::AbstractVector{T}; μ₀::Real = T(1e-2)) where T <: Real
195+
function projected_line_search!(
196+
x::AbstractVector{T},
197+
H::Union{AbstractMatrix, AbstractLinearOperator},
198+
g::AbstractVector{T},
199+
d::AbstractVector{T},
200+
::AbstractVector{T},
201+
u::AbstractVector{T};
202+
μ₀::Real = T(1e-2),
203+
) where {T <: Real}
191204
α = one(T)
192205
_, brkmin, _ = breakpoints(x, d, ℓ, u)
193206
nsteps = 0
@@ -229,11 +242,18 @@ with the sufficient decrease condition
229242
230243
q(s) ≦ μ₀sᵀg.
231244
"""
232-
function cauchy(x::AbstractVector{T},
233-
H::Union{AbstractMatrix,AbstractLinearOperator},
234-
g::AbstractVector{T},
235-
Δ::Real, α::Real, ℓ::AbstractVector{T}, u::AbstractVector{T};
236-
μ₀::Real = T(1e-2), μ₁::Real = one(T), σ::Real = T(10)) where T <: Real
245+
function cauchy(
246+
x::AbstractVector{T},
247+
H::Union{AbstractMatrix, AbstractLinearOperator},
248+
g::AbstractVector{T},
249+
Δ::Real,
250+
α::Real,
251+
::AbstractVector{T},
252+
u::AbstractVector{T};
253+
μ₀::Real = T(1e-2),
254+
μ₁::Real = one(T),
255+
σ::Real = T(10),
256+
) where {T <: Real}
237257
# TODO: Use brkmin to care for g direction
238258
_, _, brkmax = breakpoints(x, -g, ℓ, u)
239259
n = length(x)
@@ -302,10 +322,17 @@ min q(d) = ¹/₂dᵀHs + dᵀg s.t. ℓ ≦ x + d ≦ u, ‖d‖ ≦ Δ
302322
starting from `s`. The steps are computed using the conjugate gradient method
303323
projected on the active bounds.
304324
"""
305-
function projected_newton!(x::AbstractVector{T}, H::Union{AbstractMatrix,AbstractLinearOperator},
306-
g::AbstractVector{T}, Δ::Real, cgtol::Real, s::AbstractVector{T},
307-
::AbstractVector{T}, u::AbstractVector{T};
308-
max_cgiter::Int = 50) where T <: Real
325+
function projected_newton!(
326+
x::AbstractVector{T},
327+
H::Union{AbstractMatrix, AbstractLinearOperator},
328+
g::AbstractVector{T},
329+
Δ::Real,
330+
cgtol::Real,
331+
s::AbstractVector{T},
332+
::AbstractVector{T},
333+
u::AbstractVector{T};
334+
max_cgiter::Int = 50,
335+
) where {T <: Real}
309336
n = length(x)
310337
status = ""
311338

@@ -330,7 +357,7 @@ function projected_newton!(x::AbstractVector{T}, H::Union{AbstractMatrix,Abstrac
330357
gfnorm = norm(wa)
331358

332359
ZHZ = Z' * H * Z
333-
st, stats = Krylov.cg(ZHZ, -gfree, radius=Δ, rtol=cgtol, atol=zero(T))
360+
st, stats = Krylov.cg(ZHZ, -gfree, radius = Δ, rtol = cgtol, atol = zero(T))
334361
iters += 1
335362
status = stats.status
336363

0 commit comments

Comments
 (0)