using Pkg
Pkg.activate(".")
# Pkg.instantiate()
Activating project at `~/Mathematics/Teaching/2022/Computational Group Theory/2022 WS Karlsruhe/Symbolic SOS`
using LinearAlgebra
using DynamicPolynomials
using SumOfSquares
using SCS
@polyvar x y
motzkin = x^4*y^2 + y^4*x^2 - 3*x^2*y^2 + 1
m = SOSModel()
sos_constraint = @constraint m motzkin in SOSCone()
print(m)
set_optimizer(m, SCS.Optimizer)
optimize!(m)
@info termination_status(m)
------------------------------------------------------------------ SCS v3.2.1 - Splitting Conic Solver (c) Brendan O'Donoghue, Stanford University, 2012 ------------------------------------------------------------------ problem: variables n: 10, constraints m: 20 cones: z: primal zero / dual free vars: 10 s: psd vars: 10, ssize: 1 settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07 alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1 max_iters: 100000, normalize: 1, rho_x: 1.00e-06 acceleration_lookback: 10, acceleration_interval: 10 lin-sys: sparse-direct-amd-qdldl nnz(A): 20, nnz(P): 0 ------------------------------------------------------------------ iter | pri res | dua res | gap | obj | scale | time (s) ------------------------------------------------------------------ 0| 5.99e+00 2.00e-01 2.40e+00 1.20e+00 1.00e-01 3.76e-04 25| 2.80e+13 5.69e+11 3.53e+19 1.76e+19 1.00e-01 5.95e-04 ------------------------------------------------------------------ status: infeasible timings: total: 5.98e-04s = setup: 1.09e-04s + solve: 4.89e-04s lin-sys: 4.04e-05s, cones: 2.73e-04s, accel: 3.15e-05s ------------------------------------------------------------------ objective = inf ------------------------------------------------------------------
[ Info: INFEASIBLE
m = Model()
@constraint m sos_constraint (x^2 + y^2 + 1)*motzkin in SOSCone()
print(m)
set_optimizer(m, SCS.Optimizer)
optimize!(m)
@info termination_status(m)
------------------------------------------------------------------ SCS v3.2.1 - Splitting Conic Solver (c) Brendan O'Donoghue, Stanford University, 2012 ------------------------------------------------------------------ problem: variables n: 45, constraints m: 72 cones: z: primal zero / dual free vars: 27 s: psd vars: 45, ssize: 1 settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07 alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1 max_iters: 100000, normalize: 1, rho_x: 1.00e-06 acceleration_lookback: 10, acceleration_interval: 10 lin-sys: sparse-direct-amd-qdldl nnz(A): 90, nnz(P): 0 ------------------------------------------------------------------ iter | pri res | dua res | gap | obj | scale | time (s) ------------------------------------------------------------------ 0| 3.00e+00 2.61e-01 1.87e+00 9.35e-01 1.00e-01 3.17e-04 50| 8.10e-07 2.31e-07 1.58e-06 7.88e-07 1.00e-01 1.39e-03 ------------------------------------------------------------------ status: solved timings: total: 1.39e-03s = setup: 1.53e-04s + solve: 1.24e-03s lin-sys: 1.12e-04s, cones: 8.32e-04s, accel: 5.73e-05s ------------------------------------------------------------------ objective = 0.000001 ------------------------------------------------------------------
[ Info: OPTIMAL
sos_dec = sos_decomposition(m[:sos_constraint], 1e-6);
res = polynomial(sos_dec) - (x^2 + y^2 + 1)*motzkin
norm(res, 1)
1.1325427921717647e-6
@info "" rounded_solution=round(polynomial(sos_dec), digits=6) original_motzkin=(x^2 + y^2 + 1.0)*motzkin
┌ Info: │ rounded_solution = x⁶y² + 2.0x⁴y⁴ + x²y⁶ - 2.0x⁴y² - 2.0x²y⁴ - 3.0x²y² + x² + y² + 1.0 └ original_motzkin = x⁶y² + 2.0x⁴y⁴ + x²y⁶ - 2.0x⁴y² - 2.0x²y⁴ - 3.0x²y² + x² + y² + 1.0
G = gram_matrix(m[:sos_constraint]);
# @show norm(G.Q - round.(G.Q, digits = 2), 1)
round.(G.Q, digits=10)
9×9 Matrix{Float64}: 1.0 0.0 0.499997 -0.0 … 0.0 0.0 -0.0 0.0 1.00001 -0.0 -0.0 -0.0 0.0 -1.0 0.499997 -0.0 1.0 -0.0 0.0 -0.0 -0.0 -0.0 -0.0 -0.0 0.999988 0.0 -0.999994 0.0 0.0 0.0 0.0 0.0 -0.999994 0.0 -0.0 -1.49999 -0.0 -1.49999 0.0 … -0.0 0.0 0.0 0.0 -0.0 0.0 0.0 1.0 -0.0 -0.0 0.0 0.0 -0.0 -0.999994 -0.0 1.0 0.0 -0.0 -1.0 -0.0 0.0 -0.0 0.0 1.0
g = G.basis.monomials' * round.(Int, 2G.Q) * G.basis.monomials
g
g - 2(x^2 + y^2 + 1)*motzkin
round.(Int, 2G.Q)
9×9 Matrix{Int64}: 2 0 1 0 0 -3 0 0 0 0 2 0 0 0 0 0 0 -2 1 0 2 0 0 -3 0 0 0 0 0 0 2 0 0 0 -2 0 0 0 0 0 2 0 -2 0 0 -3 0 -3 0 0 6 0 0 0 0 0 0 0 -2 0 2 0 0 0 0 0 -2 0 0 0 2 0 0 -2 0 0 0 0 0 0 2
m = SOSModel()
# X = DynamicPolynomials.monomials([x,y], 2:2)
X = [x^2, y^2, 1]
3-element Vector{Term{true, Int64}}: x² y² 1
@variable m denom Poly(X)
@constraint m denom*motzkin ∈ SOSCone()
@constraint m sum(coefficients(denom)) >= 1
print(m)
set_optimizer(m, SCS.Optimizer)
optimize!(m)
@info termination_status(m)
------------------------------------------------------------------ SCS v3.2.1 - Splitting Conic Solver (c) Brendan O'Donoghue, Stanford University, 2012 ------------------------------------------------------------------ problem: variables n: 48, constraints m: 73 cones: z: primal zero / dual free vars: 27 l: linear vars: 1 s: psd vars: 45, ssize: 1 settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07 alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1 max_iters: 100000, normalize: 1, rho_x: 1.00e-06 acceleration_lookback: 10, acceleration_interval: 10 lin-sys: sparse-direct-amd-qdldl nnz(A): 105, nnz(P): 0 ------------------------------------------------------------------ iter | pri res | dua res | gap | obj | scale | time (s) ------------------------------------------------------------------ 0| 3.41e-01 5.94e-02 1.14e-01 5.71e-02 1.00e-01 2.36e-04 25| 2.18e-06 2.97e-07 0.00e+00 0.00e+00 1.00e-01 8.06e-04 ------------------------------------------------------------------ status: solved timings: total: 8.09e-04s = setup: 1.32e-04s + solve: 6.77e-04s lin-sys: 6.06e-05s, cones: 4.37e-04s, accel: 3.27e-05s ------------------------------------------------------------------ objective = 0.000000 ------------------------------------------------------------------
[ Info: OPTIMAL
denominator = round.(value(m[:denom]), digits=5)