Skip to content

Commit

Permalink
update algorithm verbosity and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Jan 17, 2025
1 parent 541a435 commit dd0df83
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 73 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "yas"
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ jobs:
fail-fast: false
matrix:
version:
- '1.4'
- '1.6'
- 'lts'
- '1' # automatically expands to the latest stable 1.x release of Julia
os:
- ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name = "OptimKit"
uuid = "77e91f04-9b3b-57a6-a776-40b61faaebe0"
authors = ["Jutho Haegeman"]
version = "0.3.2"
version = "0.4"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[compat]
julia = "1"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
31 changes: 14 additions & 17 deletions src/cg.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
abstract type CGFlavor end

struct ConjugateGradient{F<:CGFlavor,T<:Real,L<:AbstractLineSearch} <: OptimizationAlgorithm
flavor::F
maxiter::Int
gradtol::T
linesearch::L
restart::Int
verbosity::Int
end
function ConjugateGradient(; flavor=HagerZhang(), maxiter=typemax(Int), gradtol::Real=1e-8,
restart=typemax(Int), verbosity::Int=0,
linesearch::AbstractLineSearch=HagerZhangLineSearch())
return ConjugateGradient(flavor, maxiter, gradtol, linesearch, restart, verbosity)
@kwdef struct ConjugateGradient{F<:CGFlavor,T<:Real,L<:AbstractLineSearch} <:
OptimizationAlgorithm
flavor::F = HagerZhang()
maxiter::Int = typemax(Int)
gradtol::T = 1e-8
linesearch::L = HagerZhangLineSearch()
restart::Int = typemax(Int)
verbosity::Int = 1
ls_verbosity::Int = 10
end

function optimize(fg, x, alg::ConjugateGradient;
Expand Down Expand Up @@ -85,7 +82,7 @@ function optimize(fg, x, alg::ConjugateGradient;
if normgrad <= alg.gradtol || numiter >= alg.maxiter
break
end
verbosity >= 2 &&
verbosity >= 3 &&
@info @sprintf("CG: iter %4d: f = %.12f, ‖∇f‖ = %.4e, α = %.2e, β = %.2e, nfg = %d",
numiter, f, normgrad, α, β, nfg)

Expand All @@ -101,14 +98,14 @@ function optimize(fg, x, alg::ConjugateGradient;
# increase α for next step
α = 2 * α
end
if verbosity > 0
if normgrad <= alg.gradtol
if normgrad <= alg.gradtol
verbosity >= 2 &&
@info @sprintf("CG: converged after %d iterations: f = %.12f, ‖∇f‖ = %.4e",
numiter, f, normgrad)
else
else
verbosity >= 1 &&
@warn @sprintf("CG: not converged to requested tol: f = %.12f, ‖∇f‖ = %.4e",
f, normgrad)
end
end
history = [fhistory normgradhistory]
return x, f, g, numfg, history
Expand Down
27 changes: 11 additions & 16 deletions src/gd.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
struct GradientDescent{T<:Real,L<:AbstractLineSearch} <: OptimizationAlgorithm
maxiter::Int
gradtol::T
linesearch::L
verbosity::Int
end
function GradientDescent(; maxiter=typemax(Int),
gradtol::Real=1e-8,
verbosity::Int=0,
linesearch::AbstractLineSearch=HagerZhangLineSearch())
return GradientDescent(maxiter, gradtol, linesearch, verbosity)
@kwdef struct GradientDescent{T<:Real,L<:AbstractLineSearch} <: OptimizationAlgorithm
maxiter::Int = typemax(Int)
gradtol::T = 1e-8
linesearch::L = HagerZhangLineSearch()
verbosity::Int = 1
ls_verbosity::Int = 1
end

function optimize(fg, x, alg::GradientDescent;
Expand Down Expand Up @@ -56,21 +51,21 @@ function optimize(fg, x, alg::GradientDescent;
if normgrad <= alg.gradtol || numiter >= alg.maxiter
break
end
verbosity >= 2 &&
verbosity >= 3 &&
@info @sprintf("GD: iter %4d: f = %.12f, ‖∇f‖ = %.4e, α = %.2e, nfg = %d",
numiter, f, normgrad, α, nfg)

# increase α for next step
α = 2 * α
end
if verbosity > 0
if normgrad <= alg.gradtol
if normgrad <= alg.gradtol
verbosity >= 2 &&
@info @sprintf("GD: converged after %d iterations: f = %.12f, ‖∇f‖ = %.4e",
numiter, f, normgrad)
else
else
verbosity >= 1 &&
@warn @sprintf("GD: not converged to requested tol: f = %.12f, ‖∇f‖ = %.4e",
f, normgrad)
end
end
history = [fhistory normgradhistory]
return x, f, g, numfg, history
Expand Down
50 changes: 13 additions & 37 deletions src/lbfgs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,16 @@ LBFGS optimization algorithm.
linesearch::AbstractLineSearch = HagerZhangLineSearch())`: Construct an LBFGS object with the specified parameters.
"""
struct LBFGS{T<:Real,L<:AbstractLineSearch} <: OptimizationAlgorithm
m::Int
maxiter::Int
gradtol::T
acceptfirst::Bool
linesearch::L
verbosity::Int
@kwdef struct LBFGS{T<:Real,L<:AbstractLineSearch} <: OptimizationAlgorithm
m::Int = 8
maxiter::Int = typemax(Int)
gradtol::T = 1e-8
acceptfirst::Bool = true
linesearch::L = HagerZhangLineSearch()
verbosity::Int = 1
ls_verbosity::Int = 1
end

"""
LBFGS(m::Int = 8; maxiter = typemax(Int), gradtol::Real = 1e-8, acceptfirst::Bool = true,
verbosity::Int = 0,
linesearch::AbstractLineSearch = HagerZhangLineSearch())
Construct an LBFGS object with the specified parameters.
## Arguments
- `m::Int = 8`: The number of previous iterations to store for the limited memory BFGS approximation.
- `maxiter::Int = typemax(Int)`: The maximum number of iterations.
- `gradtol::Real = 1e-8`: The tolerance for the norm of the gradient.
- `acceptfirst::Bool = true`: Whether to accept the first step of the line search.
- `verbosity::Int = 0`: The verbosity level.
- `linesearch::AbstractLineSearch = HagerZhangLineSearch()`: The line search algorithm to use.
## Returns
- `LBFGS`: The LBFGS object.
"""
LBFGS(m::Int=8; maxiter=typemax(Int), gradtol::Real=1e-8, acceptfirst::Bool=true,
verbosity::Int=0,
linesearch::AbstractLineSearch=HagerZhangLineSearch()) = LBFGS(m, maxiter, gradtol,
acceptfirst, linesearch,
verbosity)

function optimize(fg, x, alg::LBFGS;
precondition=_precondition, (finalize!)=_finalize!,
retract=_retract, inner=_inner, (transport!)=_transport!,
Expand Down Expand Up @@ -119,7 +95,7 @@ function optimize(fg, x, alg::LBFGS;
if normgrad <= alg.gradtol || numiter >= alg.maxiter
break
end
verbosity >= 2 &&
verbosity >= 3 &&
@info @sprintf("LBFGS: iter %4d: f = %.12f, ‖∇f‖ = %.4e, α = %.2e, m = %d, nfg = %d",
numiter, f, normgrad, α, length(H), nfg)

Expand Down Expand Up @@ -183,14 +159,14 @@ function optimize(fg, x, alg::LBFGS;
push!(H, (scale!(s, 1 / norms), scale!(y, 1 / norms), ρ))
end
end
if verbosity > 0
if normgrad <= alg.gradtol
if normgrad <= alg.gradtol
verbosity >= 2 &&
@info @sprintf("LBFGS: converged after %d iterations: f = %.12f, ‖∇f‖ = %.4e",
numiter, f, normgrad)
else
else
verbosity >= 1 &&
@warn @sprintf("LBFGS: not converged to requested tol: f = %.12f, ‖∇f‖ = %.4e",
f, normgrad)
end
end
history = [fhistory normgradhistory]
return x, f, g, numfg, history
Expand Down

0 comments on commit dd0df83

Please sign in to comment.