Skip to content

Commit eca66f6

Browse files
authored
Add polynomial optimization example (#271)
* Add polynomial optimization example * Add missing #src * Fix * Fix
1 parent 8cd8f96 commit eca66f6

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# # Extracting minimizers
2+
3+
#md # [![](https://mybinder.org/badge_logo.svg)](@__BINDER_ROOT_URL__/generated/Polynomial Optimization/extracting_minimizers.ipynb)
4+
#md # [![](https://img.shields.io/badge/show-nbviewer-579ACA.svg)](@__NBVIEWER_ROOT_URL__/generated/Polynomial Optimization/extracting_minimizers.ipynb)
5+
# **Adapted from**: Example 6.23 of [L09]
6+
#
7+
# [L09] Laurent, Monique.
8+
# *Sums of squares, moment matrices and optimization over polynomials.*
9+
# Emerging applications of algebraic geometry (2009): 157-270.
10+
11+
# ## Introduction
12+
13+
# Consider the polynomial optimization problem [L09, Example 6.23] of
14+
# minimizing the linear function $-x_1 - x_2$
15+
# over the basic semialgebraic set defined by the inequalities
16+
# $x_2 \le 2x_1^4 - 8x_1^3 + 8x_1^2 + 2$,
17+
# $x_2 \le 4x_1^4 - 32x_1^3 + 88x_1^2 - 96x_1 + 36$ and the box constraints
18+
# $0 \le x_1 \le 3$ and $0 \le x_2 \le 4$,
19+
# World Scientific, **2009**.
20+
21+
using Test #src
22+
using DynamicPolynomials
23+
@polyvar x[1:2]
24+
p = -sum(x)
25+
using SumOfSquares
26+
K = @set x[1] >= 0 && x[1] <= 3 && x[2] >= 0 && x[2] <= 4 && x[2] <= 2x[1]^4 - 8x[1]^3 + 8x[1]^2 + 2 && x[2] <= 4x[1]^4 - 32x[1]^3 + 88x[1]^2 - 96x[1] + 36
27+
28+
# We will now see how to find the optimal solution using Sum of Squares Programming.
29+
# We first need to pick an SDP solver, see [here](https://jump.dev/JuMP.jl/v0.21.6/installation/#Supported-solvers) for a list of the available choices.
30+
31+
import CSDP
32+
solver = optimizer_with_attributes(CSDP.Optimizer, MOI.Silent() => true)
33+
34+
# A Sum-of-Squares certificate that $p \ge \alpha$ over the domain `S`, ensures that $\alpha$ is a lower bound to the polynomial optimization problem.
35+
# The following function searches for the largest lower bound and finds zero using the `d`th level of the hierarchy`.
36+
37+
function solve(d)
38+
model = SOSModel(solver)
39+
@variable(model, α)
40+
@objective(model, Max, α)
41+
@constraint(model, c, p >= α, domain = K, maxdegree = d)
42+
optimize!(model)
43+
println(solution_summary(model))
44+
return model
45+
end
46+
47+
# The first level of the hierarchy gives a lower bound of `-7``
48+
49+
model4 = solve(4)
50+
@test objective_value(model4) -7 rtol=1e-4 #src
51+
@test termination_status(model4) == MOI.OPTIMAL #src
52+
53+
# The second level improves the lower bound
54+
55+
model5 = solve(5)
56+
@test objective_value(model5) -20/3 rtol=1e-4 #src
57+
@test termination_status(model5) == MOI.OPTIMAL #src
58+
59+
# The third level finds the optimal objective value as lower bound...
60+
61+
model7 = solve(7)
62+
@test objective_value(model7) -5.5080 rtol=1e-4 #src
63+
@test termination_status(model7) == MOI.OPTIMAL #src
64+
65+
# ...and proves it by exhibiting the minimizer.
66+
67+
ν7 = moment_matrix(model7[:c])
68+
η = extractatoms(ν7, 1e-3) # Returns nothing as the dual is not atomic
69+
@test length.atoms) == 1 #src
70+
@test η.atoms[1].center [2.3295, 3.1785] rtol=1e-4 #src
71+
72+
# We can indeed verify that the objective value at `x_opt` is equal to the lower bound.
73+
74+
x_opt = η.atoms[1].center
75+
@test x_opt [2.3295, 3.1785] rtol=1e-4 #src
76+
p(x_opt)

0 commit comments

Comments
 (0)