|
| 1 | +# Copyright (c) 2017: Miles Lubin and contributors |
| 2 | +# Copyright (c) 2017: Google Inc. |
| 3 | +# |
| 4 | +# Use of this source code is governed by an MIT-style license that can be found |
| 5 | +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. |
| 6 | + |
| 7 | +module TestConstraintQuadToSOCCliqueTrees |
| 8 | + |
| 9 | +import SparseArrays |
| 10 | +using Test |
| 11 | + |
| 12 | +import CliqueTrees |
| 13 | +import MathOptInterface as MOI |
| 14 | + |
| 15 | +function runtests() |
| 16 | + for name in names(@__MODULE__; all = true) |
| 17 | + if startswith("$(name)", "test_") |
| 18 | + @testset "$(name)" begin |
| 19 | + getfield(@__MODULE__, name)() |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + return |
| 24 | +end |
| 25 | + |
| 26 | +function test_compute_sparse_sqrt_edge_cases() |
| 27 | + for A in Any[ |
| 28 | + # Trivial Cholesky |
| 29 | + [1.0 0.0; 0.0 2.0], |
| 30 | + # Cholesky works, with pivoting |
| 31 | + [1.0 0.0 1.0; 0.0 1.0 1.0; 1.0 1.0 3.0], |
| 32 | + # Cholesky fails due to 0 eigenvalue. Pivoted Cholesky works. |
| 33 | + [1.0 1.0; 1.0 1.0], |
| 34 | + # Cholesky succeeds, even though 0 eigenvalue |
| 35 | + [2.0 2.0; 2.0 2.0], |
| 36 | + # Cholesky fails because of 0 column/row. Pivoted Cholesky works. |
| 37 | + [2.0 0.0; 0.0 0.0], |
| 38 | + # Early zero pivot - this case breaks LDLFactorizations but works |
| 39 | + # with CliqueTrees' pivoted Cholesky. |
| 40 | + [1.0 1.0 0.0; 1.0 1.0 0.0; 0.0 0.0 1.0], |
| 41 | + ] |
| 42 | + B = SparseArrays.sparse(A) |
| 43 | + f = zero(MOI.ScalarQuadraticFunction{eltype(A)}) |
| 44 | + s = MOI.GreaterThan(zero(eltype(A))) |
| 45 | + I, J, V = MOI.Bridges.Constraint.compute_sparse_sqrt(B, f, s) |
| 46 | + U = zeros(eltype(A), size(A)) |
| 47 | + for (i, j, v) in zip(I, J, V) |
| 48 | + U[i, j] += v |
| 49 | + end |
| 50 | + @test isapprox(A, U' * U; atol = 1e-10) |
| 51 | + end |
| 52 | + # Test failures |
| 53 | + for A in Any[ |
| 54 | + [-1.0 0.0; 0.0 1.0], |
| 55 | + # Indefinite matrix |
| 56 | + [0.0 -1.0; -1.0 0.0], |
| 57 | + # BigFloat not supported |
| 58 | + BigFloat[-1.0 0.0; 0.0 1.0], |
| 59 | + BigFloat[1.0 0.0; 0.0 2.0], |
| 60 | + BigFloat[1.0 1.0; 1.0 1.0], |
| 61 | + ] |
| 62 | + B = SparseArrays.sparse(A) |
| 63 | + f = zero(MOI.ScalarQuadraticFunction{eltype(A)}) |
| 64 | + s = MOI.GreaterThan(zero(eltype(A))) |
| 65 | + @test_throws( |
| 66 | + MOI.UnsupportedConstraint{typeof(f),typeof(s)}, |
| 67 | + MOI.Bridges.Constraint.compute_sparse_sqrt(B, f, s), |
| 68 | + ) |
| 69 | + end |
| 70 | + return |
| 71 | +end |
| 72 | + |
| 73 | +function test_semidefinite_cholesky_fail() |
| 74 | + inner = MOI.Utilities.Model{Float64}() |
| 75 | + model = MOI.Bridges.Constraint.QuadtoSOC{Float64}(inner) |
| 76 | + x = MOI.add_variables(model, 2) |
| 77 | + f = 0.5 * x[1] * x[1] + 1.0 * x[1] * x[2] + 0.5 * x[2] * x[2] |
| 78 | + c = MOI.add_constraint(model, f, MOI.LessThan(1.0)) |
| 79 | + F, S = MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone |
| 80 | + ci = only(MOI.get(inner, MOI.ListOfConstraintIndices{F,S}())) |
| 81 | + g = MOI.get(inner, MOI.ConstraintFunction(), ci) |
| 82 | + y = MOI.get(inner, MOI.ListOfVariableIndices()) |
| 83 | + sum_y = 1.0 * y[1] + 1.0 * y[2] |
| 84 | + @test isapprox(g, MOI.Utilities.vectorize([1.0, 1.0, sum_y, 0.0])) |
| 85 | + return |
| 86 | +end |
| 87 | + |
| 88 | +function test_early_zero_pivot() |
| 89 | + # This matrix has an early zero pivot that causes LDLFactorizations to |
| 90 | + # halt early, but CliqueTrees' pivoted Cholesky handles it correctly. |
| 91 | + inner = MOI.Utilities.Model{Float64}() |
| 92 | + model = MOI.Bridges.Constraint.QuadtoSOC{Float64}(inner) |
| 93 | + x = MOI.add_variables(model, 3) |
| 94 | + # (x[1] + x[2])^2 + x[3]^2 = x[1]^2 + 2*x[1]*x[2] + x[2]^2 + x[3]^2 |
| 95 | + # Q = [1 1 0; 1 1 0; 0 0 1] |
| 96 | + f = 0.5 * x[1] * x[1] + 1.0 * x[1] * x[2] + 0.5 * x[2] * x[2] + 0.5 * x[3] * x[3] |
| 97 | + c = MOI.add_constraint(model, f, MOI.LessThan(1.0)) |
| 98 | + F, S = MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone |
| 99 | + ci = only(MOI.get(inner, MOI.ListOfConstraintIndices{F,S}())) |
| 100 | + g = MOI.get(inner, MOI.ConstraintFunction(), ci) |
| 101 | + # Verify the constraint was created successfully |
| 102 | + @test MOI.output_dimension(g) == 5 # [1, rhs, Ux...] |
| 103 | + return |
| 104 | +end |
| 105 | + |
| 106 | +end # module |
| 107 | + |
| 108 | +TestConstraintQuadToSOCCliqueTrees.runtests() |
0 commit comments