|
| 1 | +# # Term sparsity |
| 2 | + |
| 3 | +#md # [](@__BINDER_ROOT_URL__/generated/Sparsity/sign_symmetry.ipynb) |
| 4 | +#md # [](@__NBVIEWER_ROOT_URL__/generated/Sparsity/sign_symmetry.ipynb) |
| 5 | +# **Adapted from**: Example 4 of [L09] |
| 6 | +# |
| 7 | +# [L09] Lofberg, Johan. |
| 8 | +# *Pre-and post-processing sum-of-squares programs in practice*. |
| 9 | +# IEEE transactions on automatic control 54, no. 5 (2009): 1007-1011. |
| 10 | + |
| 11 | +using Test #src |
| 12 | +using DynamicPolynomials |
| 13 | +@polyvar x[1:3] |
| 14 | + |
| 15 | +# We would like to determine whether the following polynomial is a sum-of-squares. |
| 16 | + |
| 17 | +poly = 1 + x[1]^4 + x[1] * x[2] + x[2]^4 + x[3]^2 |
| 18 | + |
| 19 | +# In order to do this, we can solve the following Sum-of-Squares program. |
| 20 | + |
| 21 | +import CSDP |
| 22 | +solver = CSDP.Optimizer |
| 23 | +using SumOfSquares |
| 24 | +function sos_check(sparsity) |
| 25 | + model = Model(solver) |
| 26 | + con_ref = @constraint(model, poly in SOSCone(), sparsity = sparsity) |
| 27 | + optimize!(model) |
| 28 | + @test termination_status(model) == MOI.OPTIMAL #src |
| 29 | + println(solution_summary(model)) |
| 30 | + return gram_matrix(con_ref) |
| 31 | +end |
| 32 | + |
| 33 | +g = sos_check(Sparsity.NoPattern()) |
| 34 | +@test g.basis.monomials == [x[1]^2, x[1] * x[2], x[2]^2, x[1], x[2], x[3], 1] #src |
| 35 | +g.basis.monomials |
| 36 | + |
| 37 | +# As detailed in the Example 4 of [L09], we can exploit the *sign symmetry* of |
| 38 | +# the polynomial to decompose the large positive semidefinite matrix into smaller ones. |
| 39 | + |
| 40 | +g = sos_check(Sparsity.SignSymmetry()) |
| 41 | +monos = [sub.basis.monomials for sub in g.sub_gram_matrices] |
| 42 | +@test length(monos) == 3 #src |
| 43 | +@test [x[1], x[2]] in monos #src |
| 44 | +@test [x[3]] in monos #src |
| 45 | +@test [x[1]^2, x[1] * x[2], x[2]^2, 1] in monos #src |
0 commit comments