/
BPT12e399.jl
169 lines (147 loc) · 5.85 KB
/
BPT12e399.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
# Adapted from Example 3.99 of [BPT12].
#
# [BPT12] Blekherman, G.; Parrilo, P. & Thomas, R.
# Semidefinite Optimization and Convex Algebraic Geometry
# Society for Industrial and Applied Mathematics, 2012
using Test
using SumOfSquares
using DynamicPolynomials
function BPT12e399_test(optimizer, config::MOI.Test.Config, remainder::Bool)
atol = config.atol
rtol = config.rtol
model = _model(optimizer)
@variable(model, α)
@polyvar x y
if remainder
cref = @constraint(
model,
10 - (x^2 + α * y) in SOSCone(),
newton_of_remainder = true,
maxdegree = nothing,
domain = @set x^2 + y^2 == 1
)
else
cref = @constraint(
model,
10 - (x^2 + α * y) in SOSCone(),
maxdegree = 2,
domain = @set x^2 + y^2 == 1
)
end
@objective(model, Max, α)
JuMP.optimize!(model)
α_value = remainder ? 6.0 : 10.0
@test termination_status(model) == MOI.OPTIMAL
@test objective_value(model) ≈ α_value atol = atol rtol = rtol
@test JuMP.primal_status(model) == MOI.FEASIBLE_POINT
@test JuMP.value(α) ≈ α_value atol = atol rtol = rtol
test_constraint_primal(cref, 10 - (x^2 + α_value * y))
p = gram_matrix(cref)
if remainder
@test value_matrix(p) ≈ [9 -3; -3 1] atol = atol rtol = rtol
@test p.basis.monomials == [1, y]
else
@test value_matrix(p) ≈ [
5 -5 0
-5 5 0
0 0 4
] atol = atol rtol = rtol
@test p.basis.monomials == [1, y, x]
end
@test dual_status(model) == MOI.FEASIBLE_POINT
μ = dual(cref)
@test μ isa AbstractMeasure{Float64}
@test length(moments(μ)) == 3
@test moment_value(moments(μ)[1]) ≈ (remainder ? 1 / 3 : 1.0) atol = atol rtol =
rtol
@test monomial(moments(μ)[1]) == 1
@test moment_value(moments(μ)[2]) ≈ 1 atol = atol rtol = rtol
@test monomial(moments(μ)[2]) == y
@test moment_value(moments(μ)[3]) ≈ (remainder ? -8 / 3 : 0.0) atol = atol rtol =
rtol
@test monomial(moments(μ)[3]) == x^2
μ = moments(cref)
@test μ isa AbstractMeasure{Float64}
if remainder
@test length(moments(μ)) == 3
@test moment_value(moments(μ)[1]) ≈ 1 / 3 atol = atol rtol = rtol
@test monomial(moments(μ)[1]) == 1
@test moment_value(moments(μ)[2]) ≈ 1 atol = atol rtol = rtol
@test monomial(moments(μ)[2]) == y
@test moment_value(moments(μ)[3]) ≈ 3 atol = atol rtol = rtol
@test monomial(moments(μ)[3]) == y^2
else
@test length(moments(μ)) == 5
@test moment_value(moments(μ)[1]) ≈ 1 atol = atol rtol = rtol
@test monomial(moments(μ)[1]) == 1
@test moment_value(moments(μ)[2]) ≈ 1 atol = atol rtol = rtol
@test monomial(moments(μ)[2]) == y
@test moment_value(moments(μ)[3]) ≈ 0 atol = atol rtol = rtol
@test monomial(moments(μ)[3]) == x
@test moment_value(moments(μ)[4]) ≈ 1 atol = atol rtol = rtol
@test monomial(moments(μ)[4]) == y^2
@test moment_value(moments(μ)[5]) ≈ 0 atol = atol rtol = rtol
@test monomial(moments(μ)[5]) == x * y
end
@objective(model, Min, α)
JuMP.optimize!(model)
@test termination_status(model) == MOI.OPTIMAL
@test objective_value(model) ≈ -α_value atol = atol rtol = rtol
@test JuMP.primal_status(model) == MOI.FEASIBLE_POINT
@test JuMP.value(α) ≈ -α_value atol = atol rtol = rtol
test_constraint_primal(cref, 10 - (x^2 - α_value * y))
p = gram_matrix(cref)
if remainder
@test value_matrix(p) ≈ [9 3; 3 1] atol = atol rtol = rtol
@test p.basis.monomials == [1, y]
else
@test value_matrix(p) ≈ [
5 5 0
5 5 0
0 0 4
] atol = atol rtol = rtol
@test p.basis.monomials == [1, y, x]
end
@test dual_status(model) == MOI.FEASIBLE_POINT
μ = dual(cref)
@test μ isa AbstractMeasure{Float64}
@test length(moments(μ)) == 3
@test moment_value(moments(μ)[1]) ≈ (remainder ? 1 / 3 : 1.0) atol = atol rtol =
rtol
@test monomial(moments(μ)[1]) == 1
@test moment_value(moments(μ)[2]) ≈ -1 atol = atol rtol = rtol
@test monomial(moments(μ)[2]) == y
@test moment_value(moments(μ)[3]) ≈ (remainder ? -8 / 3 : 0.0) atol = atol rtol =
rtol
@test monomial(moments(μ)[3]) == x^2
μ = moments(cref)
@test μ isa AbstractMeasure{Float64}
if remainder
@test length(moments(μ)) == 3
@test moment_value(moments(μ)[1]) ≈ 1 / 3 atol = atol rtol = rtol
@test monomial(moments(μ)[1]) == 1
@test moment_value(moments(μ)[2]) ≈ -1 atol = atol rtol = rtol
@test monomial(moments(μ)[2]) == y
@test moment_value(moments(μ)[3]) ≈ 3 atol = atol rtol = rtol
@test monomial(moments(μ)[3]) == y^2
else
@test length(moments(μ)) == 5
@test moment_value(moments(μ)[1]) ≈ 1 atol = atol rtol = rtol
@test monomial(moments(μ)[1]) == 1
@test moment_value(moments(μ)[2]) ≈ -1 atol = atol rtol = rtol
@test monomial(moments(μ)[2]) == y
@test moment_value(moments(μ)[3]) ≈ 0 atol = atol rtol = rtol
@test monomial(moments(μ)[3]) == x
@test moment_value(moments(μ)[4]) ≈ 1 atol = atol rtol = rtol
@test monomial(moments(μ)[4]) == y^2
@test moment_value(moments(μ)[5]) ≈ 0 atol = atol rtol = rtol
@test monomial(moments(μ)[5]) == x * y
end
return model
end
BPT12e399_rem_test(optimizer, config) = BPT12e399_test(optimizer, config, true)
sd_tests["BPT12e399_rem"] = BPT12e399_rem_test
function BPT12e399_maxdegree_test(optimizer, config)
return BPT12e399_test(optimizer, config, false)
end
sd_tests["BPT12e399_maxdegree"] = BPT12e399_maxdegree_test