|
| 1 | +# # Mminimization of a univariate polynomial |
| 2 | + |
| 3 | +#md # [](@__BINDER_ROOT_URL__/generated/Getting started/univariate.ipynb) |
| 4 | +#md # [](@__NBVIEWER_ROOT_URL__/generated/Getting started/univariate.ipynb) |
| 5 | +# **Contributed by**: Benoît Legat |
| 6 | + |
| 7 | +using Test #src |
| 8 | +using DynamicPolynomials |
| 9 | +using SumOfSquares |
| 10 | +import CSDP |
| 11 | + |
| 12 | +# Consider the problem of finding both the minimum value of `p = x^4 - 4x^3 - 2x^2 + 12x + 3` as well as its minimizers. |
| 13 | + |
| 14 | +# We can use SumOfSquares.jl to find such these values as follows. |
| 15 | +# We first define the polynomial using DynamicPolynomials. |
| 16 | + |
| 17 | +@polyvar x |
| 18 | +p = x^4 - 4x^3 - 2x^2 + 12x + 3 |
| 19 | + |
| 20 | +# Secondly, we create a Sum-of-Squares program searching for the maximal lower bound `σ` of the polynomial. |
| 21 | + |
| 22 | +model = SOSModel(CSDP.Optimizer) |
| 23 | +@variable(model, σ) |
| 24 | +@constraint(model, cref, p >= σ) |
| 25 | +@objective(model, Max, σ) |
| 26 | + |
| 27 | +# Thirdly, solve the program and find `σ = -6` as lower bound: |
| 28 | + |
| 29 | +optimize!(model) |
| 30 | +solution_summary(model) |
| 31 | + |
| 32 | +# We can look at the certificate that `σ = -6` is a lower bound: |
| 33 | + |
| 34 | +sos_dec = sos_decomposition(cref, 1e-4) |
| 35 | + |
| 36 | +# Indeed, `p + 6 = (x^2 - 2x - 3)^2` so `p ≥ -6`. |
| 37 | +# We can now find the minimizers from the moment matrix: |
| 38 | + |
| 39 | +ν = moment_matrix(cref) |
| 40 | +ν.Q |
| 41 | + |
| 42 | +# This matrix is the convex combination of the moment matrices corresponding to two atomic measures at `-1` and `3` |
| 43 | +# which allows us to conclude that `-1` and `3` are global minimizers. |
| 44 | + |
| 45 | +η = extractatoms(ν, 1e-4) |
| 46 | +minimizers = [η.atoms[1].center; η.atoms[2].center] |
| 47 | + |
| 48 | +# Below are more details on what we mean by convex combination. |
| 49 | +# The moment matrix of the atomic measure at the first minimizer is: |
| 50 | + |
| 51 | +η1 = moment_matrix(dirac(monomials(x, 0:4), x => round(minimizers[1])), ν.basis.monomials) |
| 52 | +η1.Q |
| 53 | + |
| 54 | +# The moment matrix of the atomic measure at the second minimizer is: |
| 55 | + |
| 56 | +η2 = moment_matrix(dirac(monomials(x, 0:4), x => round(minimizers[2])), ν.basis.monomials) |
| 57 | +η2.Q |
| 58 | + |
| 59 | +# And the moment matrix is the convex combination of both: |
| 60 | + |
| 61 | +Q12 = η1.Q * η.atoms[1].weight + η2.Q * η.atoms[2].weight |
| 62 | +@test ν.Q ≈ Q12 rtol=1e-6 #src |
| 63 | + |
| 64 | +# Another way to see this (by linearity of the expectation) is that `ν` is the moment matrix |
| 65 | +# of the convex combination of the two atomic measures. |
0 commit comments