-
Notifications
You must be signed in to change notification settings - Fork 5
/
sos_problem.jl
164 lines (135 loc) · 4.29 KB
/
sos_problem.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
using JuMP
function DP.coefficients(p, b::SA.FixedBasis)
return DP.coefficients(p, b.elts)
end
function invariant_constraint!(
M_orb::AbstractMatrix{<:AbstractFloat},
M::Matrix{<:Integer},
invariant_vec::SparseVector,
)
M_orb .= zero(eltype(M_orb))
for i in eachindex(M)
if M[i] ∈ SparseArrays.nonzeroinds(invariant_vec)
M_orb[i] += invariant_vec[M[i]]
end
end
return M_orb
end
function sos_problem(poly::AbstractPolynomial)
vars = DP.variables(poly)
basis_psd = DP.monomials(vars, 0:DP.maxdegree(poly)÷2)
basis_constraints = SA.FixedBasis(
DP.monomials(vars, 0:DP.maxdegree(poly)),
SA.DiracMStructure(*),
)
M = [basis_constraints[x*y] for x in basis_psd, y in basis_psd]
sos_model = JuMP.Model()
JuMP.@variable sos_model t
JuMP.@objective sos_model Max t
n = length(basis_psd)
P = JuMP.@variable sos_model P[1:n, 1:n] Symmetric
JuMP.@constraint sos_model P in PSDCone()
objective = poly - t
for (idx, b) in enumerate(basis_constraints)
c = DP.coefficient(objective, b)
JuMP.@constraint sos_model LinearAlgebra.dot(P, M .== idx) == c
end
return sos_model
end
function sos_problem(
poly::AbstractPolynomial,
invariant_vs::AbstractVector,
basis_constraints::SA.AbstractBasis,
basis_psd,
T=Float64,
)
M = [basis_constraints[x*y] for x in basis_psd, y in basis_psd]
sos_model = JuMP.Model()
JuMP.@variable sos_model t
JuMP.@objective sos_model Max t
n = length(basis_psd)
P = JuMP.@variable sos_model P[1:n, 1:n] Symmetric
JuMP.@constraint sos_model P in PSDCone()
# preallocating
M_orb = similar(M, T)
C = DP.coefficients(poly - t, basis_constraints)
for iv in invariant_vs
c = dot(C, iv)
# average Ms into M_orb with weights given by iv
M_orb = invariant_constraint!(M_orb, M, iv)
JuMP.@constraint sos_model dot(M_orb, P) == c
end
return sos_model
end
function sos_problem(
poly::AbstractPolynomial,
wedderburn::SW.WedderburnDecomposition,
basis_psd;
)
m = JuMP.Model()
M = let basis_constraints = SA.basis(wedderburn)
[basis_constraints[x*y] for x in basis_psd, y in basis_psd]
end
JuMP.@variable m t
JuMP.@objective m Max t
psds = map(SW.direct_summands(wedderburn)) do ds
dim = size(ds, 1)
P = JuMP.@variable m [1:dim, 1:dim] Symmetric
JuMP.@constraint m P in PSDCone()
return P
end
# preallocating
# Mπs = zeros.(eltype(wedderburn), size.(psds))
M_orb = similar(M, eltype(wedderburn))
C = DP.coefficients(poly - t, SW.basis(wedderburn))
for iv in SW.invariant_vectors(wedderburn)
c = dot(C, iv)
M_orb = invariant_constraint!(M_orb, M, iv)
# Mπs = SW.diagonalize!(Mπs, M_orb, wedderburn)
Mπs = SW.diagonalize(M_orb, wedderburn)
JuMP.@constraint m sum(
dot(Mπ, Pπ) for (Mπ, Pπ) in zip(Mπs, psds) if !iszero(Mπ)
) == c
end
return m
end
function sos_problem(
poly::AbstractPolynomial,
G::Group,
action::SW.Action,
T=Float64;
decompose_psd=true,
semisimple=false
)
max_deg = DP.maxdegree(poly)
vars = DP.variables(poly)
basis_psd = DP.monomials(vars, 0:max_deg÷2)
basis_constraints = DP.monomials(vars, 0:max_deg)
if decompose_psd == true
wedderburn, symmetry_adaptation_time =
@timed SW.WedderburnDecomposition(
T,
G,
action,
basis_constraints,
basis_psd,
semisimple=semisimple,
)
model, model_creation_time =
@timed sos_problem(poly, wedderburn, basis_psd)
else
(invariant_vs, basis_cnstr), symmetry_adaptation_time = @timed let G = G
basis = SA.FixedBasis(basis_constraints, SA.DiracMStructure(*))
tblG = SW.Characters.CharacterTable(Rational{Int}, G)
iv = SW.invariant_vectors(tblG, action, basis)
iv, basis
end
model, model_creation_time =
@timed sos_problem(poly, invariant_vs, basis_cnstr, basis_psd, T)
end
stats = Dict(
"symmetry_adaptation" => symmetry_adaptation_time,
"model_creation" => model_creation_time,
)
return model, stats
end