/
test_socp.jl
281 lines (250 loc) · 10.5 KB
/
test_socp.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@testset "SOCP Atoms: $solver" for solver in solvers
if can_solve_socp(solver)
@testset "norm 2 atom" begin
x = Variable(2, 1)
A = [1 2; 2 1; 3 4]
b = [2; 3; 4]
p = minimize(norm2(A * x + b))
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 0.64888 atol=TOL
@test evaluate(norm2(A * x + b)) ≈ 0.64888 atol=TOL
x = Variable(2, 1)
A = [1 2; 2 1; 3 4]
b = [2; 3; 4]
lambda = 1
p = minimize(norm2(A * x + b) + lambda * norm2(x), x >= 1)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 14.9049 atol=TOL
@test evaluate(norm2(A * x + b) + lambda * norm2(x)) ≈ 14.9049 atol=TOL
x = Variable(2)
p = minimize(norm2([x[1] + 2x[2] + 2; 2x[1] + x[2] + 3; 3x[1]+4x[2] + 4]) + lambda * norm2(x), x >= 1)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 14.9049 atol=TOL
@test evaluate(norm2(A * x + b) + lambda * norm2(x)) ≈ 14.9049 atol=TOL
x = Variable(2, 1)
A = [1 2; 2 1; 3 4]
b = [2; 3; 4]
lambda = 1
p = minimize(norm2(A * x + b) + lambda * norm_1(x), x >= 1)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 15.4907 atol=TOL
@test evaluate(norm2(A * x + b) + lambda * norm_1(x)) ≈ 15.4907 atol=TOL
end
@testset "frobenius norm atom" begin
m = Variable(4, 5)
c = [m[3, 3] == 4, m >= 1]
p = minimize(norm(vec(m), 2), c)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ sqrt(35) atol=TOL
@test evaluate(norm(vec(m), 2)) ≈ sqrt(35) atol=TOL
end
@testset "quad over lin atom" begin
x = Variable(3, 1)
A = [2 -3 5; -2 9 -3; 5 -8 3]
b = [-3; 9; 5]
c = [3 2 4]
d = -3
p = minimize(quadoverlin(A*x + b, c*x + d))
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 17.7831 atol=TOL
@test (evaluate(quadoverlin(A * x + b, c * x + d)))[1] ≈ 17.7831 atol=TOL
end
@testset "sum squares atom" begin
x = Variable(2, 1)
A = [1 2; 2 1; 3 4]
b = [2; 3; 4]
p = minimize(sumsquares(A*x + b))
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 0.42105 atol=TOL
@test (evaluate(sumsquares(A * x + b)))[1] ≈ 0.42105 atol=TOL
end
@testset "square atom" begin
x = Variable(2, 1)
A = [1 2; 2 1; 3 4]
b = [2; 3; 4]
p = minimize(sum(square(A*x + b)))
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 0.42105 atol=TOL
@test evaluate(sum(square(A * x + b))) ≈ 0.42105 atol=TOL
x = Variable(2, 1)
A = [1 2; 2 1; 3 4]
b = [2; 3; 4]
expr = A * x + b
p = minimize(sum(dot(^)(expr,2))) # elementwise ^
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 0.42105 atol=TOL
@test evaluate(sum(broadcast(^, expr, 2))) ≈ 0.42105 atol=TOL
p = minimize(sum(dot(*)(expr, expr))) # elementwise *
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 0.42105 atol=TOL
@test evaluate(sum((dot(*))(expr, expr))) ≈ 0.42105 atol=TOL
end
@testset "inv pos atom" begin
x = Variable(4)
p = minimize(sum(invpos(x)), invpos(x) < 2, x > 1, x == 2, 2 == x)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 2 atol=TOL
@test evaluate(sum(invpos(x))) ≈ 2 atol=TOL
x = Variable(3)
p = minimize(sum(dot(/)([3,6,9], x)), x<=3)
solve!(p, solver)
@test x.value ≈ fill(3.0, (3, 1)) atol=TOL
@test p.optval ≈ 6 atol=TOL
@test evaluate(sum((dot(/))([3, 6, 9], x))) ≈ 6 atol=TOL
x = Variable()
p = minimize(sum([3,6,9]/x), x<=3)
solve!(p, solver)
@test x.value ≈ 3 atol=TOL
@test p.optval ≈ 6 atol=TOL
@test evaluate(sum([3, 6, 9] / x)) ≈ 6 atol=TOL
end
@testset "geo mean atom" begin
x = Variable(2)
y = Variable(2)
p = minimize(geomean(x, y), x >= 1, y >= 2)
# not DCP compliant
@test vexity(p) == ConcaveVexity()
p = maximize(geomean(x, y), 1 < x, x < 2, y < 2)
# Just gave it a vector as an objective, not okay
@test_throws Exception solve!(p, solver)
p = maximize(sum(geomean(x, y)), 1 < x, x < 2, y < 2)
solve!(p, solver)
@test p.optval ≈ 4 atol=TOL
@test evaluate(sum(geomean(x, y))) ≈ 4 atol=TOL
end
@testset "sqrt atom" begin
x = Variable()
p = maximize(sqrt(x), 1 >= x)
end
@testset "quad form atom" begin
x = Variable(3, 1)
A = [0.8608 0.3131 0.5458; 0.3131 0.8584 0.5836; 0.5458 0.5836 1.5422]
p = minimize(quadform(x, A), [x >= 1])
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 6.1464 atol=TOL
@test (evaluate(quadform(x, A)))[1] ≈ 6.1464 atol=TOL
x = Variable(3, 1)
A = -1.0*[0.8608 0.3131 0.5458; 0.3131 0.8584 0.5836; 0.5458 0.5836 1.5422]
c = [3 2 4]
p = maximize(c*x , [quadform(x, A) >= -1])
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 3.7713 atol=TOL
@test (evaluate(quadform(x, A)))[1] ≈ -1 atol=TOL
end
@testset "huber atom" begin
x = Variable(3)
p = minimize(sum(huber(x, 1)), x >= 2)
@test vexity(p) == ConvexVexity()
solve!(p, solver)
@test p.optval ≈ 9 atol=TOL
@test evaluate(sum(huber(x, 1))) ≈ 9 atol=TOL
end
@testset "rational norm atom" begin
A = [1 2 3; -1 2 3]
b = A * ones(3)
x = Variable(3)
p = minimize(norm(x, 4.5), [A * x == b])
@test vexity(p) == ConvexVexity()
# Solution is approximately x = [1, .93138, 1.04575]
solve!(p, solver)
@test p.optval ≈ 1.2717 atol=TOL
@test evaluate(norm(x, 4.5)) ≈ 1.2717 atol=TOL
end
@testset "rational norm dual norm" begin
v = [0.463339, 0.0216084, -2.07914, 0.99581, 0.889391]
x = Variable(5)
q = 1.379; # q norm constraint that generates many inequalities
qs = q / (q - 1); # Conjugate to q
p = minimize(x' * v)
p.constraints += (norm(x, q) <= 1)
@test vexity(p) == ConvexVexity()
solve!(p, solver) # Solution is -norm(v, q / (q - 1))
@test p.optval ≈ -2.144087 atol=TOL
@test sum(evaluate(x' * v)) ≈ -2.144087 atol=TOL
@test evaluate(norm(x, q)) ≈ 1 atol=TOL
@test sum(evaluate(x' * v)) ≈ -(sum(abs.(v) .^ qs) ^ (1 / qs)) atol=TOL
end
@testset "rational norm atom sum" begin
A = [-0.719255 -0.229089
-1.33632 -1.37121
0.703447 -1.4482]
b = [-1.82041, -1.67516, -0.866884]
q = 1.5
xvar = Variable(2)
p = minimize(.5 * sumsquares(xvar) + norm(A * xvar - b, q))
@test vexity(p) == ConvexVexity()
solve!(p, solver)
# Compute gradient, check it is zero(ish)
x_opt = xvar.value
margins = A * x_opt - b
qs = q / (q - 1); # Conjugate
denom = sum(abs.(margins).^q)^(1/qs)
g = x_opt + A' * (abs.(margins).^(q-1) .* sign.(margins)) / denom
@test p.optval ≈ 1.7227 atol=TOL
@test norm(g, 2) ^ 2 ≈ 0 atol=TOL
end
@testset "norm consistent with Base for matrix variables" begin
A = randn(4, 4)
x = Variable(4, 4)
x.value = A
# Matrix norm
@test evaluate(opnorm(x)) ≈ opnorm(A) atol=TOL
@test evaluate(opnorm(x, 1)) ≈ opnorm(A, 1) atol=TOL
@test evaluate(opnorm(x, 2)) ≈ opnorm(A, 2) atol=TOL
@test evaluate(opnorm(x, Inf)) ≈ opnorm(A, Inf) atol=TOL
# Vector norm
# TODO: Once the deprecation for norm on matrices is removed, remove the `vec` calls
@test evaluate(norm(vec(x), 1)) ≈ norm(vec(A), 1) atol=TOL
@test evaluate(norm(vec(x), 2)) ≈ norm(vec(A), 2) atol=TOL
@test evaluate(norm(vec(x), 7)) ≈ norm(vec(A), 7) atol=TOL
@test evaluate(norm(vec(x), Inf)) ≈ norm(vec(A), Inf) atol=TOL
end
@testset "Fixed and freed variables" begin
@testset "fix and free addition" begin
x = Variable()
y = Variable()
p = minimize(x+y, x>=0, y>=0)
solve!(p, solver)
@test p.optval ≈ 0 atol=TOL
y.value = 4
fix!(y)
solve!(p, solver)
@test p.optval ≈ 4 atol=TOL
free!(y)
solve!(p, solver)
@test p.optval ≈ 0 atol=TOL
end
@testset "fix multiplication" begin
a = [1,2,3,2,1]
x = Variable(length(a))
gamma = Variable(Positive())
fix!(gamma, 0.7)
p = minimize(norm(x-a) + gamma*norm(x[1:end-1] - x[2:end]))
solve!(p, solver)
o1 = p.optval
# x should be very close to a
@test o1 ≈ 0.7 * norm(a[1:end - 1] - a[2:end]) atol=TOL
# increase regularization
fix!(gamma, 1.0)
solve!(p, solver)
o2 = p.optval
# x should be very close to mean(a)
@test o2 ≈ norm(a .- mean(a)) atol=TOL
@test o1 <= o2
end
end
end
end