-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstrong_convexity_and_sharpness.jl
216 lines (179 loc) · 4.86 KB
/
strong_convexity_and_sharpness.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using Boscia
using Test
using Random
using LinearAlgebra
using FrankWolfe
## Log barrier
# min_x - ∑ log(xi + ϵ) - log(N - ∑ xi + ϵ)
# s.t. x ∈ {0,1}^n
# Strong convexity: μ = 1 / (1 + ϵ)^2n
# Sharpness: M = sqrt(2/μ), θ = 1/2
## General convex quadratic
# min_x 1/2 x' * Q * x + b' * x
# s.t. ∑ x = N
# x ∈ {0,1}^n
# Strong convexity: μ = minimum(eigvals(Q))
# Sharpness: M = sqrt(2/μ), θ = 1/2
seed = 0x5526f8e0e9a68f36
Random.seed!(seed)
@testset "Strong convexity" begin
@testset "Log barrier" begin
n = 50
N = Int(floor(3/4 * n))
ϵ = 1e-3
function f(x)
return - sum(log(xi + ϵ) for xi in x) - log(N - sum(x) + ϵ)
end
function grad!(storage, x)
storage .= - 1 ./ (x .+ ϵ) .- 1/sum(N - sum(x) + ϵ)
return storage
end
int_vars = collect(1:n)
sblmo = Boscia.UnitSimplexSimpleBLMO(N)
line_search = FrankWolfe.Adaptive()
x, _, result = Boscia.solve(
f,
grad!,
sblmo,
fill(0.0, n),
fill(floor(N/2), n),
int_vars,
n,
verbose=true,
line_search=line_search,
time_limit=120,
print_iter=1000,
)
μ = 1/(1 + ϵ)^(2*n)
x_sc, _, result_sc = Boscia.solve(
f,
grad!,
sblmo,
fill(0.0, n),
fill(floor(N/2), n),
int_vars,
n,
verbose=true,
line_search=line_search,
strong_convexity=μ,
time_limit=120,
print_iter=1000,
)
@test f(x_sc) <= f(x) + 1e-6
@test result_sc[:dual_bound] > result[:dual_bound]
end
@testset "General convex quadratic" begin
n = 20
N = Int(floor(n/2))
Q = rand(n, n)
Q = Q' * Q
@assert isposdef(Q)
b = rand(n)
function f(x)
return 1/2 * x' * Q * x - b' * x
end
function grad!(storage, x)
storage .= Q * x - b
return storage
end
val, sol = Boscia.min_via_enum_prob_simplex(f, n, N)
blmo = Boscia.ProbabilitySimplexSimpleBLMO(N)
μ = minimum(eigvals(Q))
x, _, _ = Boscia.solve(
f,
grad!,
blmo,
fill(0.0, n),
fill(1.0, n),
collect(1:n),
n,
strong_convexity=μ,
verbose=true,
fw_epsilon=1e-3,
)
@test isapprox(f(x), f(sol), atol=1e-5, rtol=1e-2)
end
end
@testset "Sharpness" begin
@testset "Log barrier" begin
n = 50
N = Int(floor(3/4 * n))
ϵ = 1e-3
function f(x)
return - sum(log(xi + ϵ) for xi in x) - log(N - sum(x) + ϵ)
end
function grad!(storage, x)
storage .= - 1 ./ (x .+ ϵ) .- 1/sum(N - sum(x) + ϵ)
return storage
end
int_vars = collect(1:n)
sblmo = Boscia.UnitSimplexSimpleBLMO(N)
line_search = FrankWolfe.Adaptive()
x, _, result = Boscia.solve(
f,
grad!,
sblmo,
fill(0.0, n),
fill(floor(N/2), n),
int_vars,
n,
verbose=true,
line_search=line_search,
time_limit=120,
print_iter=1000,
)
μ = 1/(1 + ϵ)^(2*n)
θ = 1/2
M = sqrt(2/μ)
x_sc, _, result_sc = Boscia.solve(
f,
grad!,
sblmo,
fill(0.0, n),
fill(floor(N/2), n),
int_vars,
n,
verbose=true,
line_search=line_search,
sharpness_constant=M,
sharpness_exponent=θ,
time_limit=120,
print_iter=1000,
)
@test f(x_sc) <= f(x) + 1e-6
@test result_sc[:dual_bound] >= result[:dual_bound]
end
@testset "General convex quadratic" begin
n = 20
N = Int(floor(n/2))
Q = rand(n, n)
Q = Q' * Q
@assert isposdef(Q)
b = rand(n)
function f(x)
return 1/2 * x' * Q * x - b' * x
end
function grad!(storage, x)
storage .= Q * x - b
return storage
end
val, sol = Boscia.min_via_enum_prob_simplex(f, n, N)
blmo = Boscia.ProbabilitySimplexSimpleBLMO(N)
μ = minimum(eigvals(Q))
θ = 1/2
M = sqrt(2/μ)
x, _, _ = Boscia.solve(
f,
grad!,
blmo,
fill(0.0, n),
fill(1.0, n),
collect(1:n),
n,
sharpness_constant=M,
sharpness_exponent=θ,
verbose=true,
)
@test isapprox(f(x), f(sol), atol=1e-5, rtol=1e-2)
end
end