-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathtest_QuadtoSOCBridge.jl
More file actions
419 lines (396 loc) · 14.1 KB
/
test_QuadtoSOCBridge.jl
File metadata and controls
419 lines (396 loc) · 14.1 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
module TestConstraintQuadToSOC
import LinearAlgebra
import SparseArrays
using Test
import LDLFactorizations
import MathOptInterface as MOI
function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
return
end
include("../utilities.jl")
function test_error_for_nonconvex_quadratic_constraints()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
bridged_mock = MOI.Bridges.Constraint.QuadtoSOC{Float64}(mock)
x = MOI.add_variable(bridged_mock)
@test_throws(
MOI.UnsupportedConstraint,
MOI.add_constraint(
bridged_mock,
MOI.ScalarQuadraticFunction(
[MOI.ScalarQuadraticTerm(1.0, x, x)],
MOI.ScalarAffineTerm{Float64}[],
0.0,
),
MOI.GreaterThan(0.0),
)
)
@test_throws(
MOI.UnsupportedConstraint,
MOI.add_constraint(
bridged_mock,
MOI.ScalarQuadraticFunction(
[MOI.ScalarQuadraticTerm(-1.0, x, x)],
MOI.ScalarAffineTerm{Float64}[],
0.0,
),
MOI.LessThan(0.0),
)
)
return
end
function test_quadratic_constraints_with_2_variables()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
config = MOI.Test.Config()
bridged_mock = MOI.Bridges.Constraint.QuadtoSOC{Float64}(mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
MOI.OPTIMAL,
(MOI.FEASIBLE_POINT, [0.5, 0.5]),
),
)
MOI.Test.test_constraint_qcp_duplicate_diagonal(bridged_mock, config)
MOI.empty!(bridged_mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
MOI.OPTIMAL,
(MOI.FEASIBLE_POINT, [0.5, (√13 - 1) / 4]),
),
)
MOI.Test.test_constraint_qcp_duplicate_off_diagonal(bridged_mock, config)
ci = first(
MOI.get(
mock,
MOI.ListOfConstraintIndices{
MOI.VectorAffineFunction{Float64},
MOI.RotatedSecondOrderCone,
}(),
),
)
x, y = MOI.get(mock, MOI.ListOfVariableIndices())
# The matrix is
# 2 1
# 1 2
# for which the Cholesky factorization is U' * U with U =
# √2 √2/2
# . √3/√2
expected = MOI.VectorAffineFunction{Float64}(
MOI.VectorAffineTerm.(
[3, 3, 4],
MOI.ScalarAffineTerm.([√2, √2 / 2, √3 / √2], [x, y, y]),
),
[1.0, 1.0, 0.0, 0.0],
)
@test MOI.get(mock, MOI.ConstraintFunction(), ci) ≈ expected
end
function test_qcp()
model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
mock = MOI.Utilities.MockOptimizer(model)
config = MOI.Test.Config()
bridged_mock = MOI.Bridges.Constraint.QuadtoSOC{Float64}(mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
[1 / 2, 7 / 4],
(MOI.VectorAffineFunction{Float64}, MOI.Nonnegatives) =>
[zeros(2)],
(MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone) => [[0.25, 1.0, -1 / √2]],
),
)
MOI.Test.test_quadratic_constraint_integration(bridged_mock, config)
MOI.empty!(bridged_mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
[√2],
(MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone) => [[1 / √2, 1 / (2 * √2), -1 / √2]],
),
)
MOI.Test.test_quadratic_constraint_basic(bridged_mock, config)
MOI.empty!(bridged_mock)
MOI.Test.test_quadratic_constraint_minimize(bridged_mock, config)
ci = first(
MOI.get(
bridged_mock,
MOI.ListOfConstraintIndices{
MOI.ScalarQuadraticFunction{Float64},
MOI.LessThan{Float64},
}(),
),
)
_test_delete_bridge(
bridged_mock,
ci,
1,
((MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone, 0),),
)
MOI.empty!(bridged_mock)
MOI.Utilities.set_mock_optimize!(
mock,
(mock::MOI.Utilities.MockOptimizer) -> MOI.Utilities.mock_optimize!(
mock,
[1.0, 1.0],
(MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone) => [[1.0, 1 / 3, -1 / √2, -1 / √6]],
),
)
MOI.Test.test_quadratic_constraint_LessThan(bridged_mock, config)
MOI.empty!(bridged_mock)
MOI.Test.test_quadratic_constraint_GreaterThan(bridged_mock, config)
F = MOI.ScalarQuadraticFunction{Float64}
S = MOI.GreaterThan{Float64}
ci = first(MOI.get(bridged_mock, MOI.ListOfConstraintIndices{F,S}()))
for attr in [MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart()]
msg = "In order to set the `$attr`, the `MOI.Bridges.Constraint.QuadtoSOCBridge` needs to get the `MathOptInterface.VariablePrimalStart()` but it is not set. Set the `MathOptInterface.VariablePrimalStart()` first before setting the `$attr` in order to fix this."
err = MOI.SetAttributeNotAllowed(attr, msg)
@test_throws err MOI.set(bridged_mock, attr, ci, 0.0)
end
return
end
function test_fill_reducing_permutation()
model = MOI.Utilities.Model{Float64}()
bridge = MOI.Bridges.Constraint.QuadtoSOC{Float64}(model)
x = MOI.add_variables(bridge, 3)
Q = Float64[2 1 1; 1 2 0; 1 0 2]
f = 0.5 * x' * Q * x
MOI.add_constraint(bridge, f, MOI.LessThan(2.0))
indices = MOI.get(
model,
MOI.ListOfConstraintIndices{
MOI.VectorAffineFunction{Float64},
MOI.RotatedSecondOrderCone,
}(),
)
F = LinearAlgebra.cholesky(Q)
# Test that the sparse Cholesky pivot is permuted
SF = LinearAlgebra.cholesky(SparseArrays.sparse(Q))
@test SF.p == [3, 2, 1]
U = Matrix(F.U)
@test MOI.get(model, MOI.ConstraintFunction(), indices[1]) ≈
MOI.VectorAffineFunction(
[
MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(U[1, 1], x[1])),
MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(U[1, 2], x[2])),
MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(U[1, 3], x[3])),
MOI.VectorAffineTerm(4, MOI.ScalarAffineTerm(U[2, 2], x[2])),
MOI.VectorAffineTerm(4, MOI.ScalarAffineTerm(U[2, 3], x[3])),
MOI.VectorAffineTerm(5, MOI.ScalarAffineTerm(U[3, 3], x[3])),
],
[1.0, 2.0, 0.0, 0.0, 0.0],
)
return
end
function test_runtests()
MOI.Bridges.runtests(
MOI.Bridges.Constraint.QuadtoSOCBridge,
"""
variables: x, y
2.0 * x * x + 2.0 * y * y <= 0.0
""",
"""
variables: x, y
[1.0, 0.0, 2.0 * x, 2.0 * y] in RotatedSecondOrderCone(4)
""",
variable_start = 0.0,
)
MOI.Bridges.runtests(
MOI.Bridges.Constraint.QuadtoSOCBridge,
"""
variables: x, y
-2.0 * x * x + -2.0 * y * y >= 0.0
""",
"""
variables: x, y
[1.0, 0.0, 2.0 * x, 2.0 * y] in RotatedSecondOrderCone(4)
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Constraint.QuadtoSOCBridge,
"""
variables: x, y
2.0 * x * x + 0.5 * y * y + 2.0 * x <= 3.0
""",
"""
variables: x, y
[1.0, -2.0 * x + 3.0, 2.0 * x, 1.0 * y] in RotatedSecondOrderCone(4)
""",
)
MOI.Bridges.runtests(
MOI.Bridges.Constraint.QuadtoSOCBridge,
"""
variables: x, y, z
-2.0 * x * x + -1.0 * y * y + -2.0 * x * y + 3.0 * z >= 4.0
""",
"""
variables: x, y, z
[1.0, 3.0 * z + -4.0, 2.0 * x + 1.0 * y, 1.0 * y] in RotatedSecondOrderCone(4)
""",
)
return
end
"""
test_copy_to_start()
Tests that `copy_to` copies the variable starting values first and the
constraint primal and dual starting values second as is needed by the
[`MOI.Bridges.Constraint.QuadtoSOCBridge`](@ref).
"""
function test_copy_to_start()
src = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
x = MOI.add_variable(src)
c = MOI.add_constraint(src, 1.0x * x + 2.0x, MOI.LessThan(-1.0))
MOI.set(src, MOI.ConstraintPrimalStart(), c, 1.0)
MOI.set(src, MOI.ConstraintDualStart(), c, -1.0)
MOI.set(src, MOI.VariablePrimalStart(), x, -1.0)
model = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
dest = MOI.Bridges.Constraint.QuadtoSOC{Float64}(model)
index_map = MOI.copy_to(dest, src)
@test MOI.get(dest, MOI.ConstraintPrimalStart(), index_map[c]) == 1.0
@test MOI.get(dest, MOI.ConstraintDualStart(), index_map[c]) == -1.0
@test MOI.get(dest, MOI.VariablePrimalStart(), index_map[x]) == -1.0
end
function test_deletion_of_variable_in_bridged_constraint()
inner = MOI.Utilities.Model{Float64}()
model = MOI.Bridges.Constraint.QuadtoSOC{Float64}(inner)
x = MOI.add_variables(model, 2)
MOI.set(model, MOI.VariableName(), x, ["x", "y"])
f = 1.0 * x[1] * x[1] + 1.0 * x[2] * x[2]
c = MOI.add_constraint(model, f, MOI.LessThan(1.0))
MOI.delete(model, x[1])
@test MOI.get(model, MOI.ConstraintFunction(), c) ≈ 1.0 * x[2] * x[2]
return
end
MOI.Utilities.@model(
Model2153,
(),
(MOI.EqualTo,),
(MOI.RotatedSecondOrderCone,),
(),
(),
(MOI.ScalarAffineFunction,),
(MOI.VectorOfVariables,),
(),
)
function MOI.supports(
::Model2153{T},
::MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{T}},
) where {T}
return false
end
function test_deletion_of_variable_in_bridged_slacked_objective()
model = MOI.Bridges.full_bridge_optimizer(Model2153{Float64}(), Float64)
MOI.Bridges.add_bridge(
model,
MOI.Bridges.Constraint.QuadtoSOCBridge{Float64},
)
x = MOI.add_variables(model, 2)
MOI.set(model, MOI.VariableName(), x, ["x", "y"])
f = 1.0 * x[1] * x[1] + 1.0 * x[2] * x[2]
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.delete(model, x[1])
@test MOI.get(model, MOI.ObjectiveFunction{typeof(f)}()) ≈ 1.0 * x[2] * x[2]
return
end
function test_constraint_primal_no_quad_terms()
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Constraint.QuadtoSOC{Float64}(inner)
x = MOI.add_variable(model)
f = MOI.ScalarQuadraticFunction(
MOI.ScalarQuadraticTerm{Float64}[],
[MOI.ScalarAffineTerm(1.0, x)],
0.0,
)
c = MOI.add_constraint(model, f, MOI.LessThan(1.0))
MOI.set(model, MOI.ConstraintPrimalStart(), c, 1.0)
@test MOI.get(model, MOI.ConstraintPrimalStart(), c) == 1.0
return
end
function test_semidefinite_cholesky_fail()
inner = MOI.Utilities.Model{Float64}()
model = MOI.Bridges.Constraint.QuadtoSOC{Float64}(inner)
x = MOI.add_variables(model, 2)
f = 0.5 * x[1] * x[1] + 1.0 * x[1] * x[2] + 0.5 * x[2] * x[2]
c = MOI.add_constraint(model, f, MOI.LessThan(1.0))
F, S = MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone
ci = only(MOI.get(inner, MOI.ListOfConstraintIndices{F,S}()))
g = MOI.get(inner, MOI.ConstraintFunction(), ci)
y = MOI.get(inner, MOI.ListOfVariableIndices())
sum_y = 1.0 * y[1] + 1.0 * y[2]
@test isapprox(g, MOI.Utilities.vectorize([1.0, 1.0, sum_y, 0.0]))
return
end
function test_compute_sparse_sqrt_edge_cases()
for A in Any[
# Trivial Cholesky
[1.0 0.0; 0.0 2.0],
# Cholesky works, with pivoting
[1.0 0.0 1.0; 0.0 1.0 1.0; 1.0 1.0 3.0],
# Cholesky fails due to 0 eigen value. LDL works
[1.0 1.0; 1.0 1.0],
# Cholesky succeeds, even though 0 eigen value
[2.0 2.0; 2.0 2.0],
# Cholesky fails because of 0 column/row. LDL works
[2.0 0.0; 0.0 0.0],
]
B = SparseArrays.sparse(A)
f = zero(MOI.ScalarQuadraticFunction{eltype(A)})
s = MOI.GreaterThan(zero(eltype(A)))
I, J, V = MOI.Bridges.Constraint.compute_sparse_sqrt(B, f, s)
U = zeros(eltype(A), size(A))
for (i, j, v) in zip(I, J, V)
U[i, j] += v
end
@test isapprox(A, U' * U; atol = 1e-10)
end
# Test failures
for A in Any[
[-1.0 0.0; 0.0 1.0],
# Found from test_quadratic_nonconvex_constraint_basic
[0.0 -1.0; -1.0 0.0],
# Different element type. We could potentially make this work in future,
# but it first requires https://github.com/JuliaSmoothOptimizers/LDLFactorizations.jl/pull/142
BigFloat[-1.0 0.0; 0.0 1.0],
BigFloat[1.0 0.0; 0.0 2.0],
BigFloat[1.0 1.0; 1.0 1.0],
]
B = SparseArrays.sparse(A)
f = zero(MOI.ScalarQuadraticFunction{eltype(A)})
s = MOI.GreaterThan(zero(eltype(A)))
@test_throws(
MOI.UnsupportedConstraint{typeof(f),typeof(s)},
MOI.Bridges.Constraint.compute_sparse_sqrt(B, f, s),
)
end
return
end
function test_compute_sparse_sqrt_fallback()
# Test the default fallback that is hit when LDLFactorizations isn't loaded.
# We could put the test somewhere else so it runs before this file is
# loaded, but that's pretty flakey for a long-term solution. Instead, we're
# going to abuse the lack of a strong type signature to hit it:
f = zero(MOI.ScalarAffineFunction{Float64})
A = SparseArrays.sparse([-1.0 0.0; 0.0 1.0])
@test_throws(
MOI.AddConstraintNotAllowed{typeof(f),MOI.GreaterThan{Float64}},
MOI.Bridges.Constraint.compute_sparse_sqrt(A, f, MOI.GreaterThan(0.0)),
)
return
end
end # module
TestConstraintQuadToSOC.runtests()