Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions src/ProximalAlgorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ function run(solver::ProximalAlgorithm{I, T})::Tuple{I, T} where {I, T}
# [...]
# end
#
if verbose(solver) display(solver) end
for (it, point) in enumerate(solver)
if verbose(solver, it) display(solver, it) end
end
if verbose(solver) display(solver, it) end
return (it, point)
end

Expand Down
31 changes: 24 additions & 7 deletions src/algorithms/DouglasRachford.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
# [1] Eckstein, Bertsekas "On the Douglas-Rachford Splitting Method and the Proximal Point Algorithm for Maximal Monotone Operators*", Mathematical Programming, vol. 55, no. 1, pp. 293-318 (1989).
#

struct DRSIterator{I <: Integer, R <: Real, T <: BlockArray{R}} <: ProximalAlgorithm{I, T}
mutable struct DRSIterator{I <: Integer, R <: Real, T <: BlockArray{R}} <: ProximalAlgorithm{I, T}
x::T
f
g
cost::R
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget about the cost for now, this we will add later. Let's not modify anything in the initialize and iterate methods in this PR, only visualization stuff.

gamma::R
maxit::I
tol::R
verbose::I
verbose_freq::I
y::T
r::T
z::T
Expand All @@ -22,13 +24,13 @@ end
################################################################################
# Constructor(s)

function DRSIterator(x0::BlockArray{R}; f=Zero(), g=Zero(), gamma::R=1.0, maxit::I=10000, tol::R=1e-4, verbose=1) where {I, R}
function DRSIterator(x0::BlockArray{R}; f=Zero(), g=Zero(), gamma::R=1.0, maxit::I=10000, tol::R=1e-4, verbose=1, verbose_freq = 100) where {I, R}
y = blockcopy(x0)
r = blockcopy(x0)
z = blockcopy(x0)
FPR_x = blockcopy(x0)
FPR_x .= Inf
return DRSIterator{I, R, typeof(x0)}(x0, f, g, gamma, maxit, tol, verbose, y, r, z, FPR_x)
return DRSIterator{I, R, typeof(x0)}(x0, f, g, R(Inf), gamma, maxit, tol, verbose, verbose_freq, y, r, z, FPR_x)
end

################################################################################
Expand All @@ -38,9 +40,24 @@ maxit(sol::DRSIterator) = sol.maxit

converged(sol::DRSIterator, it) = blockmaxabs(sol.FPR_x)/sol.gamma <= sol.tol

verbose(sol::DRSIterator, it) = sol.verbose > 0
verbose(sol::DRSIterator) = sol.verbose > 0
verbose(sol::DRSIterator, it) = sol.verbose > 0 && (sol.verbose == 2 ? true : (it == 1 || it%sol.verbose_freq == 0))

display(sol::DRSIterator, it) = println("$(it) $(blockmaxabs(sol.FPR_x)/sol.gamma)")
function display(sol::DRSIterator)
@printf("%6s | %10s | %10s | %10s |\n ", "it", "gamma", "fpr","cost")
@printf("------|------------|------------|------------|\n")
end

function display(sol::DRSIterator, it)
@printf("%6d | %7.4e | %7.4e | %7.4e | \n", it, sol.gamma, blockmaxabs(sol.FPR_x)/sol.gamma, sol.cost)
end

function Base.show(io::IO, sol::DRSIterator)
println(io, "Douglas-Rachford Splitting" )
println(io, "fpr : $(blockmaxabs(sol.FPR_x))")
println(io, "gamma : $(sol.gamma)")
print(io, "cost : $(sol.cost)")
end

################################################################################
# Initialization
Expand All @@ -53,9 +70,9 @@ end
# Iteration

function iterate(sol::DRSIterator{I, T}, it::I) where {I, T}
prox!(sol.y, sol.f, sol.x, sol.gamma)
sol.cost = prox!(sol.y, sol.f, sol.x, sol.gamma)
sol.r .= 2.*sol.y .- sol.x
prox!(sol.z, sol.g, sol.r, sol.gamma)
sol.cost += prox!(sol.z, sol.g, sol.r, sol.gamma)
sol.FPR_x .= sol.y .- sol.z
sol.x .-= sol.FPR_x
return sol.z
Expand Down
29 changes: 23 additions & 6 deletions src/algorithms/ForwardBackward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ mutable struct FBSIterator{I <: Integer, R <: Real, T <: BlockArray{R}} <: Proxi
fq
Aq
g
cost::R
gamma::R
maxit::I
tol::R
adaptive::Bool
fast::Bool
verbose::I
verbose_freq::I
theta::R # extrapolation parameter
y # gradient step
z # proximal-gradient step
Expand All @@ -42,7 +44,7 @@ end
################################################################################
# Constructor

function FBSIterator(x0::T; fs=Zero(), As=Identity(blocksize(x0)), fq=Zero(), Aq=Identity(blocksize(x0)), g=Zero(), gamma::R=-1.0, maxit::I=10000, tol::R=1e-4, adaptive=false, fast=false, verbose=1) where {I, R, T}
function FBSIterator(x0::T; fs=Zero(), As=Identity(blocksize(x0)), fq=Zero(), Aq=Identity(blocksize(x0)), g=Zero(), gamma::R=-1.0, maxit::I=10000, tol::R=1e-4, adaptive=false, fast=false, verbose=1, verbose_freq = 100) where {I, R, T}
n = blocksize(x0)
mq = size(Aq, 1)
ms = size(As, 1)
Expand All @@ -59,7 +61,7 @@ function FBSIterator(x0::T; fs=Zero(), As=Identity(blocksize(x0)), fq=Zero(), Aq
Aqz_prev = blockzeros(mq)
Asz_prev = blockzeros(ms)
gradfq_Aqz_prev = blockzeros(mq)
FBSIterator{I, R, T}(x, fs, As, fq, Aq, g, gamma, maxit, tol, adaptive, fast, verbose, 1.0, y, z, z_prev, FPR_x, Aqx, Asx, gradfq_Aqx, gradfs_Asx, 0.0, 0.0, 0.0, At_gradf_Ax, Aqz_prev, Asz_prev, gradfq_Aqz_prev)
FBSIterator{I, R, T}(x, fs, As, fq, Aq, g, R(Inf), gamma, maxit, tol, adaptive, fast, verbose, verbose_freq, 1.0, y, z, z_prev, FPR_x, Aqx, Asx, gradfq_Aqx, gradfs_Asx, 0.0, 0.0, 0.0, At_gradf_Ax, Aqz_prev, Asz_prev, gradfq_Aqz_prev)
end

################################################################################
Expand All @@ -69,10 +71,23 @@ maxit(sol::FBSIterator) = sol.maxit

converged(sol::FBSIterator, it) = blockmaxabs(sol.FPR_x)/sol.gamma <= sol.tol

verbose(sol::FBSIterator, it) = sol.verbose > 0
verbose(sol::FBSIterator) = sol.verbose > 0
verbose(sol::FBSIterator, it) = sol.verbose > 0 && (sol.verbose == 2 ? true : (it == 1 || it%sol.verbose_freq == 0))

function display(sol::FBSIterator)
@printf("%6s | %10s | %10s | %10s |\n ", "it", "gamma", "fpr", "cost")
@printf("------|------------|------------|------------|\n")
end

function display(sol::FBSIterator, it)
println("$(it) $(sol.gamma) $(blockmaxabs(sol.FPR_x)/sol.gamma)")
@printf("%6d | %7.4e | %7.4e | %7.4e | \n", it, sol.gamma, blockmaxabs(sol.FPR_x)/sol.gamma, sol.cost)
end

function Base.show(io::IO, sol::FBSIterator)
println(io, (sol.fast ? "Fast " : "")*"Forward-Backward Splitting" )
println(io, "fpr : $(blockmaxabs(sol.FPR_x))")
println(io, "gamma : $(sol.gamma)")
print( io, "cost : $(sol.cost)")
end

################################################################################
Expand Down Expand Up @@ -105,8 +120,9 @@ function initialize(sol::FBSIterator)
sol.gamma = 1.0/L
end

sol.cost = sol.fq_Aqx+sol.fs_Asx
blockaxpy!(sol.y, sol.x, -sol.gamma, sol.At_gradf_Ax)
prox!(sol.z, sol.g, sol.y, sol.gamma)
sol.cost += prox!(sol.z, sol.g, sol.y, sol.gamma)
blockaxpy!(sol.FPR_x, sol.x, -1.0, sol.z)

end
Expand Down Expand Up @@ -195,7 +211,8 @@ function iterate(sol::FBSIterator{I, R, T}, it) where {I, R, T}
sol.f_Ax = sol.fs_Asx + sol.fq_Aqx
end
blockaxpy!(sol.y, sol.x, -sol.gamma, sol.At_gradf_Ax)
prox!(sol.z, sol.g, sol.y, sol.gamma)
sol.cost = sol.f_Ax
sol.cost += prox!(sol.z, sol.g, sol.y, sol.gamma)
blockaxpy!(sol.FPR_x, sol.x, -1.0, sol.z)

return sol.z
Expand Down
6 changes: 6 additions & 0 deletions src/algorithms/Template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ maxit(sol::TemplateIterator) = sol.maxit

converged(sol::TemplateIterator, it) = false

verbose(sol::TemplateIterator) = true
verbose(sol::TemplateIterator, it) = true

display(sol::TemplateIterator) = println("its ")
display(sol::TemplateIterator, it) = println("$(it) iterations performed")

function Base.show(io::IO, sol::TemplateIterator)
print(io, "Template Solver" )
end

################################################################################
# Initialization

Expand Down
22 changes: 18 additions & 4 deletions src/algorithms/ZeroFPR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mutable struct ZeroFPRIterator{I <: Integer, R <: Real, T <: BlockArray} <: Prox
tol::R
adaptive::Bool
verbose::I
verbose_freq::I
alpha::R
sigma::R
tau::R
Expand All @@ -38,7 +39,7 @@ end
################################################################################
# Constructor

function ZeroFPRIterator(x0::T; fs=Zero(), As=Identity(blocksize(x0)), fq=Zero(), Aq=Identity(blocksize(x0)), g=Zero(), gamma::R=-1.0, maxit::I=10000, tol::R=1e-4, adaptive=false, memory=10, verbose=1, alpha=0.95, sigma=0.5) where {I, R, T}
function ZeroFPRIterator(x0::T; fs=Zero(), As=Identity(blocksize(x0)), fq=Zero(), Aq=Identity(blocksize(x0)), g=Zero(), gamma::R=-1.0, maxit::I=10000, tol::R=1e-4, adaptive=false, memory=10, verbose=1, verbose_freq=100, alpha=0.95, sigma=0.5) where {I, R, T}
n = blocksize(x0)
mq = size(Aq, 1)
ms = size(As, 1)
Expand All @@ -52,7 +53,7 @@ function ZeroFPRIterator(x0::T; fs=Zero(), As=Identity(blocksize(x0)), fq=Zero()
gradfs_Asx = blockzeros(ms)
At_gradf_Ax = blockzeros(n)
d = blockzeros(x0)
ZeroFPRIterator{I, R, T}(x, fs, As, fq, Aq, g, gamma, maxit, tol, adaptive, verbose, alpha, sigma, 0.0, y, xbar, LBFGS(x, memory), FPR_x, Aqx, Asx, gradfq_Aqx, gradfs_Asx, 0.0, 0.0, 0.0, At_gradf_Ax, 0.0, 0.0, [], [], d)
ZeroFPRIterator{I, R, T}(x, fs, As, fq, Aq, g, gamma, maxit, tol, adaptive, verbose, verbose_freq, alpha, sigma, 0.0, y, xbar, LBFGS(x, memory), FPR_x, Aqx, Asx, gradfq_Aqx, gradfs_Asx, 0.0, 0.0, 0.0, At_gradf_Ax, 0.0, 0.0, [], [], d)
end

################################################################################
Expand All @@ -62,10 +63,23 @@ maxit(sol::ZeroFPRIterator) = sol.maxit

converged(sol::ZeroFPRIterator, it) = blockmaxabs(sol.FPR_x)/sol.gamma <= sol.tol

verbose(sol::ZeroFPRIterator, it) = sol.verbose > 0
verbose(sol::ZeroFPRIterator) = sol.verbose > 0
verbose(sol::ZeroFPRIterator, it) = sol.verbose > 0 && (sol.verbose == 2 ? true : (it == 1 || it%sol.verbose_freq == 0))

function display(sol::ZeroFPRIterator)
@printf("%6s | %10s | %10s | %10s | %10s |\n ", "it", "gamma", "fpr", "tau", "FBE")
@printf("------|------------|------------|------------|------------|\n")
end
function display(sol::ZeroFPRIterator, it)
println("$(it) $(sol.gamma) $(blockmaxabs(sol.FPR_x)/sol.gamma) $(blockmaxabs(sol.d)) $(sol.tau) $(sol.FBE_x)")
@printf("%6d | %7.4e | %7.4e | %7.4e | %7.4e | \n", it, sol.gamma, blockmaxabs(sol.FPR_x)/sol.gamma, sol.tau, sol.FBE_x)
end

function Base.show(io::IO, sol::ZeroFPRIterator)
println(io, "ZeroFPR" )
println(io, "fpr : $(blockmaxabs(sol.FPR_x))")
println(io, "gamma : $(sol.gamma)")
println(io, "tau : $(sol.tau)")
print( io, "FBE : $(sol.FBE_x)")
end

################################################################################
Expand Down
3 changes: 3 additions & 0 deletions test/test_l1logreg_small.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fs=f, As=A, g=g, tol=1e-6, adaptive=true)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 1658
println(sol)

# Fast/Adaptive

x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fs=f, As=A, g=g, tol=1e-6, adaptive=true, fast=true)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 473
println(sol)

# ZeroFPR/Adaptive

x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.ZeroFPR(x0; fs=f, As=A, g=g, tol=1e-6, adaptive=true)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 19
println(sol)
7 changes: 7 additions & 0 deletions test/test_lasso_small.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,51 @@ x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=A, g=g, gamma=1.0/norm(A)^2)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 140
println(sol)

# Nonfast/Adaptive

x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=A, g=g, adaptive=true)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 247
println(sol)

# Fast/Nonadaptive

x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=A, g=g, gamma=1.0/norm(A)^2, fast=true)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 94
println(sol)

# Fast/Adaptive

x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=A, g=g, adaptive=true, fast=true)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 156
println(sol)

# ZeroFPR/Nonadaptive

x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.ZeroFPR(x0; fq=f, Aq=A, g=g, gamma=1.0/norm(A)^2)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 8
println(sol)

# ZeroFPR/Adaptive

x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.ZeroFPR(x0; fq=f, Aq=A, g=g, adaptive=true)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 10
println(sol)

# Douglas-Rachford

x0 = zeros(n)
@time it, x, sol = ProximalAlgorithms.DRS(x0; f=f2, g=g, gamma=10.0/norm(A)^2)
@test vecnorm(x - x_star, Inf) <= 1e-4
println(sol)
6 changes: 6 additions & 0 deletions test/test_lasso_small_split_f.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,44 @@ x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=opA, g=g, gamma=1.0/norm(A)^2)
@test vecnorm(x .- x_star, Inf) <= 1e-4
@test it == 140
println(sol)

# Nonfast/Adaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=opA, g=g, adaptive=true)
@test vecnorm(x .- x_star, Inf) <= 1e-4
@test it == 247
println(sol)

# Fast/Nonadaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=opA, g=g, gamma=1.0/norm(A)^2, fast=true)
@test vecnorm(x .- x_star, Inf) <= 1e-4
@test it == 94
println(sol)

# Fast/Adaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=opA, g=g, adaptive=true, fast=true)
@test vecnorm(x .- x_star, Inf) <= 1e-4
@test it == 156
println(sol)

# ZeroFPR/Nonadaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.ZeroFPR(x0; fq=f, Aq=opA, g=g, gamma=1.0/norm(A)^2)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 8
println(sol)

# ZeroFPR/Adaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.ZeroFPR(x0; fq=f, Aq=opA, g=g, adaptive=true)
@test vecnorm(x - x_star, Inf) <= 1e-4
@test it == 10
println(sol)
6 changes: 6 additions & 0 deletions test/test_lasso_small_split_x.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,37 @@ x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=opA, g=g, gamma=1.0/norm(A)^2)
@test ProximalAlgorithms.blockmaxabs(x .- x_star) <= 1e-4
@test it == 140
println(sol)

# Nonfast/Adaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=opA, g=g, adaptive=true)
@test ProximalAlgorithms.blockmaxabs(x .- x_star) <= 1e-4
@test it == 247
println(sol)

# Fast/Nonadaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=opA, g=g, gamma=1.0/norm(A)^2, fast=true)
@test ProximalAlgorithms.blockmaxabs(x .- x_star) <= 1e-4
@test it == 94
println(sol)

# Fast/Adaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.FBS(x0; fq=f, Aq=opA, g=g, adaptive=true, fast=true)
@test ProximalAlgorithms.blockmaxabs(x .- x_star) <= 1e-4
@test it == 156
println(sol)

# ZeroFPR/Adaptive

x0 = ProximalAlgorithms.blockzeros(x_star)
@time it, x, sol = ProximalAlgorithms.ZeroFPR(x0; fq=f, Aq=opA, g=g, adaptive=true)
@test ProximalAlgorithms.blockmaxabs(x .- x_star) <= 1e-4
@test it == 10
println(sol)

1 change: 1 addition & 0 deletions test/test_template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ x0 = randn(10)
it, x, sol = ProximalAlgorithms.Template(x0)
@test it == 10
@test norm(x - x0) == 0.0
println(sol)