Skip to content

Commit 20c0573

Browse files
committed
added verbose_freq and cost in FB and DR
1 parent 9d84ac3 commit 20c0573

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

src/algorithms/DouglasRachford.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
# [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).
66
#
77

8-
struct DRSIterator{I <: Integer, R <: Real, T <: BlockArray{R}} <: ProximalAlgorithm{I, T}
8+
mutable struct DRSIterator{I <: Integer, R <: Real, T <: BlockArray{R}} <: ProximalAlgorithm{I, T}
99
x::T
1010
f
1111
g
12+
cost::R
1213
gamma::R
1314
maxit::I
1415
tol::R
1516
verbose::I
17+
verbose_freq::I
1618
y::T
1719
r::T
1820
z::T
@@ -22,13 +24,13 @@ end
2224
################################################################################
2325
# Constructor(s)
2426

25-
function DRSIterator(x0::BlockArray{R}; f=Zero(), g=Zero(), gamma::R=1.0, maxit::I=10000, tol::R=1e-4, verbose=2) where {I, R}
27+
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}
2628
y = blockcopy(x0)
2729
r = blockcopy(x0)
2830
z = blockcopy(x0)
2931
FPR_x = blockcopy(x0)
3032
FPR_x .= Inf
31-
return DRSIterator{I, R, typeof(x0)}(x0, f, g, gamma, maxit, tol, verbose, y, r, z, FPR_x)
33+
return DRSIterator{I, R, typeof(x0)}(x0, f, g, R(Inf), gamma, maxit, tol, verbose, verbose_freq, y, r, z, FPR_x)
3234
end
3335

3436
################################################################################
@@ -39,21 +41,22 @@ maxit(sol::DRSIterator) = sol.maxit
3941
converged(sol::DRSIterator, it) = blockmaxabs(sol.FPR_x)/sol.gamma <= sol.tol
4042

4143
verbose(sol::DRSIterator) = sol.verbose > 0
42-
verbose(sol::DRSIterator, it) = sol.verbose > 0 && (sol.verbose == 1 ? true : (it == 1 || it%100 == 0))
44+
verbose(sol::DRSIterator, it) = sol.verbose > 0 && (sol.verbose == 2 ? true : (it == 1 || it%sol.verbose_freq == 0))
4345

4446
function display(sol::DRSIterator)
45-
@printf("%6s | %10s | %10s |\n ", "it", "gamma", "fpr")
46-
@printf("------|------------|------------|\n")
47+
@printf("%6s | %10s | %10s | %10s |\n ", "it", "gamma", "fpr","cost")
48+
@printf("------|------------|------------|------------|\n")
4749
end
4850

4951
function display(sol::DRSIterator, it)
50-
@printf("%6d | %7.4e | %7.4e | \n", it, sol.gamma, blockmaxabs(sol.FPR_x)/sol.gamma)
52+
@printf("%6d | %7.4e | %7.4e | %7.4e | \n", it, sol.gamma, blockmaxabs(sol.FPR_x)/sol.gamma, sol.cost)
5153
end
5254

5355
function Base.show(io::IO, sol::DRSIterator)
5456
println(io, "Douglas-Rachford Splitting" )
5557
println(io, "fpr : $(blockmaxabs(sol.FPR_x))")
5658
println(io, "gamma : $(sol.gamma)")
59+
print(io, "cost : $(sol.cost)")
5760
end
5861

5962
################################################################################
@@ -67,9 +70,9 @@ end
6770
# Iteration
6871

6972
function iterate(sol::DRSIterator{I, T}, it::I) where {I, T}
70-
prox!(sol.y, sol.f, sol.x, sol.gamma)
73+
sol.cost = prox!(sol.y, sol.f, sol.x, sol.gamma)
7174
sol.r .= 2.*sol.y .- sol.x
72-
prox!(sol.z, sol.g, sol.r, sol.gamma)
75+
sol.cost += prox!(sol.z, sol.g, sol.r, sol.gamma)
7376
sol.FPR_x .= sol.y .- sol.z
7477
sol.x .-= sol.FPR_x
7578
return sol.z

src/algorithms/ForwardBackward.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ mutable struct FBSIterator{I <: Integer, R <: Real, T <: BlockArray{R}} <: Proxi
1515
fq
1616
Aq
1717
g
18+
cost::R
1819
gamma::R
1920
maxit::I
2021
tol::R
2122
adaptive::Bool
2223
fast::Bool
2324
verbose::I
25+
verbose_freq::I
2426
theta::R # extrapolation parameter
2527
y # gradient step
2628
z # proximal-gradient step
@@ -42,7 +44,7 @@ end
4244
################################################################################
4345
# Constructor
4446

45-
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=2) where {I, R, T}
47+
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}
4648
n = blocksize(x0)
4749
mq = size(Aq, 1)
4850
ms = size(As, 1)
@@ -59,7 +61,7 @@ function FBSIterator(x0::T; fs=Zero(), As=Identity(blocksize(x0)), fq=Zero(), Aq
5961
Aqz_prev = blockzeros(mq)
6062
Asz_prev = blockzeros(ms)
6163
gradfq_Aqz_prev = blockzeros(mq)
62-
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)
64+
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)
6365
end
6466

6567
################################################################################
@@ -70,21 +72,22 @@ maxit(sol::FBSIterator) = sol.maxit
7072
converged(sol::FBSIterator, it) = blockmaxabs(sol.FPR_x)/sol.gamma <= sol.tol
7173

7274
verbose(sol::FBSIterator) = sol.verbose > 0
73-
verbose(sol::FBSIterator, it) = sol.verbose > 0 && (sol.verbose == 1 ? true : (it == 1 || it%100 == 0))
75+
verbose(sol::FBSIterator, it) = sol.verbose > 0 && (sol.verbose == 2 ? true : (it == 1 || it%sol.verbose_freq == 0))
7476

7577
function display(sol::FBSIterator)
76-
@printf("%6s | %10s | %10s |\n ", "it", "gamma", "fpr")
77-
@printf("------|------------|------------|\n")
78+
@printf("%6s | %10s | %10s | %10s |\n ", "it", "gamma", "fpr", "cost")
79+
@printf("------|------------|------------|------------|\n")
7880
end
7981

8082
function display(sol::FBSIterator, it)
81-
@printf("%6d | %7.4e | %7.4e | \n", it, sol.gamma, blockmaxabs(sol.FPR_x)/sol.gamma)
83+
@printf("%6d | %7.4e | %7.4e | %7.4e | \n", it, sol.gamma, blockmaxabs(sol.FPR_x)/sol.gamma, sol.cost)
8284
end
8385

8486
function Base.show(io::IO, sol::FBSIterator)
8587
println(io, (sol.fast ? "Fast " : "")*"Forward-Backward Splitting" )
8688
println(io, "fpr : $(blockmaxabs(sol.FPR_x))")
8789
println(io, "gamma : $(sol.gamma)")
90+
print( io, "cost : $(sol.cost)")
8891
end
8992

9093
################################################################################
@@ -117,8 +120,9 @@ function initialize(sol::FBSIterator)
117120
sol.gamma = 1.0/L
118121
end
119122

123+
sol.cost = sol.fq_Aqx+sol.fs_Asx
120124
blockaxpy!(sol.y, sol.x, -sol.gamma, sol.At_gradf_Ax)
121-
prox!(sol.z, sol.g, sol.y, sol.gamma)
125+
sol.cost += prox!(sol.z, sol.g, sol.y, sol.gamma)
122126
blockaxpy!(sol.FPR_x, sol.x, -1.0, sol.z)
123127

124128
end
@@ -207,7 +211,8 @@ function iterate(sol::FBSIterator{I, R, T}, it) where {I, R, T}
207211
sol.f_Ax = sol.fs_Asx + sol.fq_Aqx
208212
end
209213
blockaxpy!(sol.y, sol.x, -sol.gamma, sol.At_gradf_Ax)
210-
prox!(sol.z, sol.g, sol.y, sol.gamma)
214+
sol.cost = sol.f_Ax
215+
sol.cost += prox!(sol.z, sol.g, sol.y, sol.gamma)
211216
blockaxpy!(sol.FPR_x, sol.x, -1.0, sol.z)
212217

213218
return sol.z

src/algorithms/ZeroFPR.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mutable struct ZeroFPRIterator{I <: Integer, R <: Real, T <: BlockArray} <: Prox
1313
tol::R
1414
adaptive::Bool
1515
verbose::I
16+
verbose_freq::I
1617
alpha::R
1718
sigma::R
1819
tau::R
@@ -38,7 +39,7 @@ end
3839
################################################################################
3940
# Constructor
4041

41-
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=2, alpha=0.95, sigma=0.5) where {I, R, T}
42+
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}
4243
n = blocksize(x0)
4344
mq = size(Aq, 1)
4445
ms = size(As, 1)
@@ -52,7 +53,7 @@ function ZeroFPRIterator(x0::T; fs=Zero(), As=Identity(blocksize(x0)), fq=Zero()
5253
gradfs_Asx = blockzeros(ms)
5354
At_gradf_Ax = blockzeros(n)
5455
d = blockzeros(x0)
55-
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)
56+
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)
5657
end
5758

5859
################################################################################
@@ -63,7 +64,7 @@ maxit(sol::ZeroFPRIterator) = sol.maxit
6364
converged(sol::ZeroFPRIterator, it) = blockmaxabs(sol.FPR_x)/sol.gamma <= sol.tol
6465

6566
verbose(sol::ZeroFPRIterator) = sol.verbose > 0
66-
verbose(sol::ZeroFPRIterator, it) = sol.verbose > 0 && (sol.verbose == 1 ? true : (it == 1 || it%100 == 0))
67+
verbose(sol::ZeroFPRIterator, it) = sol.verbose > 0 && (sol.verbose == 2 ? true : (it == 1 || it%sol.verbose_freq == 0))
6768

6869
function display(sol::ZeroFPRIterator)
6970
@printf("%6s | %10s | %10s | %10s | %10s |\n ", "it", "gamma", "fpr", "tau", "FBE")

0 commit comments

Comments
 (0)