### Example Constraint and Moments Source: https://jump.dev/SumOfSquares.jl/stable/reference/constraints Demonstrates setting up a constraint with a domain and retrieving its moments. The dual of the constraint can be obtained using `dual(cref)`. ```julia @variable(model, α) @variable(model, β ≤ 1) using DynamicPolynomials @polyvar x y cref = @constraint(model, α * x - β * y == 0, domain = @set x == y) # The constraint is equivalent to # @constraint(model, (α - β) * y == 0) # for which the dual is the 1-element vector with the moment of `y` of value `-1`. # This is the result of `moments(cref)`. # However, the dual of `cref` obtained by `dual(cref)` is the 2-elements vector with both the moments of `x` and `y` of value `-1`. ``` -------------------------------- ### Copositive Constraint Example Source: https://jump.dev/SumOfSquares.jl/stable/reference/standard_form Demonstrates how to use the CopositiveInner cone for constraints. This example shows a quadratic form that must be copositive. ```julia @polyvar x y @constraint(model, x^2 - 2x*y + y^2 in CopositiveInner(SOSCone())) ``` -------------------------------- ### Initialize Sum-of-Squares model with a solver Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/goldstein_price Initialize an SOSModel using Clarabel as the dual optimizer. Ensure Clarabel is installed and available. ```julia import Clarabel using Dualization model = SOSModel(dual_optimizer(Clarabel.Optimizer)) ``` -------------------------------- ### Solution Summary Output for Degree 2 Source: https://jump.dev/SumOfSquares.jl/stable/generated/Systems%20and%20Control/julia_set Example output from `solution_summary` for a degree 2 Julia set, indicating optimal termination. ```text solution_summary(; result = 1, verbose = false) ├ solver_name : CSDP ├ Termination │ ├ termination_status : OPTIMAL │ ├ result_count : 1 │ └ raw_status : Problem solved to optimality. ├ Solution (result = 1) │ ├ primal_status : FEASIBLE_POINT │ ├ dual_status : FEASIBLE_POINT │ ├ objective_value : 6.57446e+00 │ └ dual_objective_value : 6.57446e+00 └ Work counters └ solve_time (sec) : 1.58851e-02 ``` -------------------------------- ### Example 2: Controller Calculation and Phase Plot Source: https://jump.dev/SumOfSquares.jl/stable/generated/Systems%20and%20Control/stabilization_of_nonlinear_systems Computes a stabilizing controller for a different nonlinear system and generates its phase plot. This example uses different system dynamics (f, g) and parameters (b, alpha) compared to Example 1. ```julia g = [0, 1] f = [2x[1]^3 + x[1]^2*x[2] - 6x[1]*x[2]^2 + 5x[2]^3, 0] b = x[1]^2 + x[2]^2 u = controller(f, g, b, 2.5, 0:3) ``` ```julia phase_plot(f + g * u, 2000, 5.0, [[-1.0, -5.0], [1.0, 5.0]]) ``` -------------------------------- ### Solution Summary Output Source: https://jump.dev/SumOfSquares.jl/stable/generated/Extension/certificate Example output from `solution_summary(model)` after optimizing a model with a Schmüdgen certificate. It shows solver details, termination status, and solution values. ```text solution_summary(; result = 1, verbose = false) ├ solver_name : Dual model with SCS attached ├ Termination │ ├ termination_status : OPTIMAL │ ├ result_count : 1 │ └ raw_status : solved ├ Solution (result = 1) │ ├ primal_status : FEASIBLE_POINT │ ├ dual_status : FEASIBLE_POINT │ ├ objective_value : 8.28444e-01 │ └ dual_objective_value : 8.28436e-01 └ Work counters └ solve_time (sec) : 3.94898e-03 ``` -------------------------------- ### Vectorized Equality Constraints for Matrices Source: https://jump.dev/SumOfSquares.jl/stable/constraints Demonstrates how to create vectorized equality constraints for matrices of polynomials. This example shows constraining the sum of rows and columns to match. ```julia @constraint(model, con[i=1:n], sum(P[i, :]) == sum(Q[:, i])) ``` -------------------------------- ### Run Outer Approximation and Get Summary Source: https://jump.dev/SumOfSquares.jl/stable/generated/Systems%20and%20Control/julia_set Sets a specific value for 'c', computes the outer approximation using the initialized solver and degree 2, and then prints a summary of the solution. ```julia c = -0.7 + 0.2im model = outer_approximation(solver, 2, c) solution_summary(model) ``` -------------------------------- ### Solution Summary Output for Degree 4 (recomputed) Source: https://jump.dev/SumOfSquares.jl/stable/generated/Systems%20and%20Control/julia_set Example output from `solution_summary` for a recomputed degree 4 Julia set, showing near-optimal termination. ```text solution_summary(; result = 1, verbose = false) ├ solver_name : CSDP ├ Termination │ ├ termination_status : ALMOST_OPTIMAL │ ├ result_count : 1 │ └ raw_status : Problem solved to near optimality. ├ Solution (result = 1) │ ├ primal_status : NEARLY_FEASIBLE_POINT │ ├ dual_status : NEARLY_FEASIBLE_POINT │ ├ objective_value : 5.04100e+00 │ └ dual_objective_value : 5.04104e+00 └ Work counters └ solve_time (sec) : 3.33478e-01 ``` -------------------------------- ### Quadratic Form Example Source: https://jump.dev/SumOfSquares.jl/stable/sumofsquares Demonstrates a nonnegative quadratic form and its relation to a positive semidefinite matrix. A certificate of nonnegativity can be extracted from the Cholesky decomposition of the matrix. ```mathematica x_1^2 + 2x_1x_2 + 5x_2^2 + 4x_2x_3 + x_3^2 = x^\top \begin{pmatrix}1 & 1 & 0\\1 & 5 & 2\\0 & 2 & 1\end{pmatrix} x ``` ```mathematica (x_1 + x_2)^2 + (2x_2 + x_3)^2 = x^\top \begin{pmatrix}1 & 1 & 0\\0 & 2 & 1\end{pmatrix}^\top \begin{pmatrix}1 & 1 & 0\\0 & 2 & 1\end{pmatrix} x ``` -------------------------------- ### Equality Constraint Between Polynomials Source: https://jump.dev/SumOfSquares.jl/stable/constraints Creates two quadratic polynomials `p` and `q` and constrains their sum to be equal to 1. This example requires the `DynamicPolynomials` and `SumOfSquares` packages. ```julia n = 3 using DynamicPolynomials @polyvar x[1:n] X = monomials(x, 0:2) using SumOfSquares model = Model() @variable(model, p, Poly(X)) @variable(model, q, Poly(X)) @constraint(model, p + q == 1) ``` -------------------------------- ### Example 3: Controller Calculation and Phase Plot with Invariant Hyperplane Source: https://jump.dev/SumOfSquares.jl/stable/generated/Systems%20and%20Control/stabilization_of_nonlinear_systems Demonstrates controller synthesis and phase plot generation for a system with an invariant hyperplane (x2 = 0). Special care is taken with initial conditions to illustrate behavior near this hyperplane. ```julia g = [0, x[2]] f = [-6x[1]*x[2]^2 - x[1]^2*x[2] + 2x[2]^3, 0] b = x[1]^2 + x[2]^2 u = controller(f, g, b, 3, 0:2) ``` ```julia X0 = [Float64[x1, x2] for x1 in -5:5:5, x2 in -5:5:5 if x2 != 0] ε = 1e-4 # We separate the starting point slightly from the hyperplane `x2 = 0` which is invariant. push!(X0, [-4, ε]) push!(X0, [-3, -ε]) push!(X0, [ 3, ε]) push!(X0, [ 4, -ε]) phase_plot(f + g * u, 2000, 10.0, X0) ``` -------------------------------- ### Example 1: Controller Calculation and Phase Plot Source: https://jump.dev/SumOfSquares.jl/stable/generated/Systems%20and%20Control/stabilization_of_nonlinear_systems Calculates a stabilizing controller for a specific nonlinear system and visualizes its phase plot. The system dynamics are defined by f and g, with b and alpha parameters for the controller synthesis. ```julia g = [0, 1] f = [x[2] - x[1]^3 + x[1]^2, 0] b = 3x[1]^2 + 2x[1]*x[2] + 2x[2]^2 u = controller(f, g, b, 4, 0:3) ``` ```julia phase_plot(f + g * u, 200, 10.0, [[x1, x2] for x1 in -5:5:5, x2 in -5:5:5 if x1 != 0 || x2 != 0]) ``` -------------------------------- ### Define Polynomial Optimization Problem and Constraints Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/ellipsoid Sets up the polynomial objective function `p` and the constraint set `K` which includes ellipsoid conditions. This is the initial setup for the optimization problem. ```julia using DynamicPolynomials @polyvar x[1:3] using SumOfSquares A = [ 0 0 1 0 -1 0 -2 1 -1 ] bz = [3, 0, -4] - [0, -1, -6] y = [1.5, -0.5, -5] p = -2x[1] + x[2] - x[3] e = A * x - y f = e'e - bz'bz / 4 K = @set sum(x) <= 4 && 3x[2] + x[3] <= 6 && f >= 0 && 0 <= x[1] && x[1] <= 2 && 0 <= x[2] && 0 <= x[3] && x[3] <= 3 ``` -------------------------------- ### Clarabel Solver Initialization Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/bilinear Imports the Clarabel solver and sets it as the default optimizer for the Sum of Squares model. Ensure Clarabel is installed and available in your Julia environment. ```julia import Clarabel solver = Clarabel.Optimizer ``` -------------------------------- ### Decompose a Univariate Polynomial into Squares Source: https://jump.dev/SumOfSquares.jl/stable/generated/Extension/univariate_solver This example demonstrates decomposing a specific univariate polynomial `p = x^4 + 4x^3 + 6x^2 + 4x + 5` into a sum of squares using the custom `MyUnivariateSolver.Optimizer`. The result is then printed. ```Julia using DynamicPolynomials @polyvar x p = x^4 + 4x^3 + 6x^2 + 4x + 5 dec = decompose(p, MyUnivariateSolver.Optimizer) ``` -------------------------------- ### Solve Polynomial Optimization Problem Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/ellipsoid Initiates the solution process for a polynomial optimization problem using the 'solve' function. This is the primary command to start the optimization. ```julia model8 = solve(8) ``` -------------------------------- ### Using TypedPolynomials in a Sum-of-Squares Constraint with Domain Source: https://jump.dev/SumOfSquares.jl/stable/generated/Extension/typed This example demonstrates how to define a Sum-of-Squares constraint using TypedPolynomials, specifying a domain defined by polynomial equalities. The model is then optimized using CSDP. ```julia TypedPolynomials.@polyvar x y S = @set x^3 * y + x == 2x^2 * y^2 && 3x^4 == y poly = -6x - 4y^3 + 2x*y^2 + 6x^3 - 3y^4 + 13x^2 * y^2 model = Model(CSDP.Optimizer) con_ref = @constraint(model, poly in SOSCone(), domain = S) optimize!(model) solution_summary(model) ``` -------------------------------- ### Import SOS and Set Up Solver Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/sum-of-squares_matrices Import the SumOfSquares library and configure an SDP solver (CSDP in this case) for optimization. Ensure the solver is set to silent to avoid excessive output. ```julia using SumOfSquares import CSDP solver = optimizer_with_attributes(CSDP.Optimizer, MOI.Silent() => true) ``` -------------------------------- ### Set up and Optimize SOS Model Source: https://jump.dev/SumOfSquares.jl/stable/generated/Extension/certificate Create an SOSModel, define a variable for the lower bound, set the objective to maximize this bound, and add a constraint that the polynomial is greater than or equal to the bound over the specified domain. Then, optimize the model. ```julia model = SOSModel(solver) @variable(model, α) @objective(model, Max, α) @constraint(model, c, p >= α, domain = S) optimize!(model) ``` -------------------------------- ### Motzkin Polynomial Example Source: https://jump.dev/SumOfSquares.jl/stable/sumofsquares Presents the Motzkin polynomial, which is a classic example of a polynomial that is always non-negative but not a sum of squares. It can still be certified using sum-of-squares programming. ```mathematica x_1^4x_2^2 + x_1^2x_2^4 + 1 - 3x_1^2x_2^2 \geq 0 \quad \forall x ``` -------------------------------- ### Get Neighbors in a Graph Source: https://jump.dev/SumOfSquares.jl/stable/reference/certificate Retrieves the neighbors of a specific node within a given graph. ```julia neighbors(G::Graph, node::Int) ``` -------------------------------- ### Initialize SOS Model and Solver Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/bound_on_global_extremum Sets up the Sum-of-Squares optimization model using the CSDP solver. The solver is configured to run silently. ```julia using SumOfSquares using CSDP solver = optimizer_with_attributes(CSDP.Optimizer, MOI.Silent() => true) model = SOSModel(solver); ``` -------------------------------- ### Get Moment Matrix of Polynomial Constraint Source: https://jump.dev/SumOfSquares.jl/stable/constraints Retrieves the moment matrix of a Sum-of-Squares constraint. This is used to check for atomic measures. ```julia ν = moment_matrix(cref) ``` -------------------------------- ### Set up SDP Model for Noncommutative Polynomial Source: https://jump.dev/SumOfSquares.jl/stable/generated/Noncommutative%20and%20Hermitian/noncommutative_variables Integrates with an SDP solver (CSDP) to create a model and constrain the noncommutative polynomial to be in the SOS cone. Requires importing SumOfSquares and a specific optimizer. ```julia using SumOfSquares import CSDP optimizer_constructor = optimizer_with_attributes(CSDP.Optimizer, MOI.Silent() => true) model = Model(optimizer_constructor) con_ref = @constraint(model, p in SOSCone()) optimize!(model) ``` -------------------------------- ### Initialize SOS Model and Constraint Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/getting_started Initialize an SOS model with a chosen SDP solver and add a non-negativity constraint for the polynomial. This requires importing SumOfSquares and a specific solver like CSDP. ```julia using SumOfSquares import CSDP solver = optimizer_with_attributes(CSDP.Optimizer, MOI.Silent() => true) model = SOSModel(solver) con_ref = @constraint(model, p >= 0) ``` -------------------------------- ### XORSpace Orthogonal Subspace Calculation Example Source: https://jump.dev/SumOfSquares.jl/stable/reference/certificate Illustrates the calculation of an orthogonal basis from a row echelon form within the XORSpace context. ```julia 1 a 0 c e b 1 d f ``` ```julia a 1 b 0 0 c 0 d 1 0 e 0 f 0 1 ``` -------------------------------- ### Importing necessary packages Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/dualization Imports the DynamicPolynomials and SumOfSquares packages for working with polynomial expressions and Sum-of-Squares constraints. ```julia using DynamicPolynomials using SumOfSquares ``` -------------------------------- ### Create SOS Model and Constraint Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/sum-of-squares_matrices Initialize an SOSModel with the chosen solver and add a constraint that the polynomial matrix P must be in the Positive Semidefinite Cone (PSDCone). Then, optimize the model. ```julia model = SOSModel(solver) mat_cref = @constraint(model, P in PSDCone()) optimize!(model) termination_status(model) ``` -------------------------------- ### Get Moments of Polynomial Constraint Source: https://jump.dev/SumOfSquares.jl/stable/constraints Retrieves the moments of a polynomial constraint, which is defined by the SOS decomposition. This may differ from the general dual. ```julia μ = moments(cref) ``` -------------------------------- ### Initialize SDP Solver and SOS Model Source: https://jump.dev/SumOfSquares.jl/stable/generated/Other%20Applications/bounds_in_probability Initializes a CSDP optimizer and an SOSModel for Sum-of-Squares programming. Ensures the solver is silent and uses the SOSModel for `>=` constraints. ```julia using CSDP solver = optimizer_with_attributes(CSDP.Optimizer, MOI.Silent() => true) model = SOSModel(solver); ``` -------------------------------- ### Get Dual of Polynomial Constraint Source: https://jump.dev/SumOfSquares.jl/stable/constraints Retrieves the dual of a polynomial constraint, which is a moment series. This is consistent with the general JuMP dual function. ```julia μ = dual(cref) ``` -------------------------------- ### Set up SOS Model and Constraint Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/circle Configures the Sum-of-Squares model with a chosen SDP solver (CSDP) and adds the nonnegativity constraint over the defined domain. ```julia import CSDP model = SOSModel(CSDP.Optimizer) set_silent(model) con_ref = @constraint(model, 1 - y^2 >= 0, domain = S) optimize!(model) ``` -------------------------------- ### Get Default Maxdegree for Certificate Source: https://jump.dev/SumOfSquares.jl/stable/reference/certificate Returns the default maxdegree to use for certifying a polynomial as Sum-of-Squares. It considers the maxdegree of monomials and domain multipliers. ```julia default_maxdegree(monos, domain) ``` -------------------------------- ### Get Gram Matrix Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/getting_started Retrieve the Gram matrix associated with a constraint reference. The Gram matrix is used to represent the polynomial as a sum of squares. ```julia q = gram_matrix(con_ref) ``` -------------------------------- ### Compute Atomic Measure with Fixed Rank Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/polynomial_optimization Computes an atomic measure with a fixed rank for polynomial optimization. This is a starting point for analyzing measures. ```julia atomic_measure(ν4, FixedRank(3)) ``` -------------------------------- ### Importing SCS and Dualization Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/polynomial_optimization Imports the SCS optimizer and the Dualization module for setting up the Sum-of-Squares model. ```python import SCS scs = SCS.Optimizer import Dualization dual_scs = Dualization.dual_optimizer(scs) ``` -------------------------------- ### Import necessary Julia packages Source: https://jump.dev/SumOfSquares.jl/stable/generated/Systems%20and%20Control/barrier_certificate Imports the DynamicPolynomials and SumOfSquares packages, along with an SDP solver (CSDP). This is required for defining and manipulating polynomials and setting up SOS models. ```julia using DynamicPolynomials @polyvar x[1:2] using SumOfSquares using CSDP ``` -------------------------------- ### Dualizing the SOS problem before solving Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/dualization Demonstrates how to dualize a Sum-of-Squares problem before passing it to the SCS solver using the Dualization package. This can lead to a smaller problem representation. ```julia using Dualization model_dual_scs = Model(dual_optimizer(SCS.Optimizer)) @objective(model_dual_scs, Max, 0.0) con_ref = @constraint(model_dual_scs, p in SOSCone()) optimize!(model_dual_scs) ``` -------------------------------- ### Define a New Polynomial for SOS Cone Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/getting_started Define a new polynomial that will be constrained to belong to the SOS cone. This polynomial has a different structure than the first example. ```julia p = 4*x^4*y^6 + x^2 - x*y^2 + y^2 ``` -------------------------------- ### Set up and solve the optimization model with symmetry Source: https://jump.dev/SumOfSquares.jl/stable/generated/Symmetry/permutation_symmetry Sets up a JuMP optimization model to find the maximum value of `t` such that `poly - t` is a Sum of Squares, exploiting the defined permutation symmetry. The `Symmetry.Pattern` is used to inform the solver about the symmetry. ```julia import CSDP solver = CSDP.Optimizer model = Model(solver) @variable(model, t) @objective(model, Max, t) pattern = Symmetry.Pattern(G, Symmetry.VariablePermutation()) con_ref = @constraint(model, poly - t in SOSCone(), symmetry = pattern) optimize!(model) value(t) ``` -------------------------------- ### Initialize SDP Solver Source: https://jump.dev/SumOfSquares.jl/stable/generated/Extension/hypercube Initializes an SDP solver with specific attributes. This solver will be used by the SOSModel for optimization. ```julia import CSDP solver = optimizer_with_attributes(CSDP.Optimizer, MOI.Silent() => true) ``` -------------------------------- ### Import SumOfSquares package Source: https://jump.dev/SumOfSquares.jl/stable/generated/Symmetry/permutation_symmetry Imports the SumOfSquares.jl package, which is essential for formulating and solving polynomial optimization problems with Sum of Squares (SOS) constraints. ```julia using SumOfSquares ``` -------------------------------- ### Display Solution Summary Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/polynomial_optimization Use `solution_summary(gmodel)` to display a detailed summary of the optimization solution found by Ipopt. This includes solver name, termination status, solution values, and work counters. ```julia solution_summary(gmodel) ``` -------------------------------- ### Polynomial Nonnegativity Example Source: https://jump.dev/SumOfSquares.jl/stable/sumofsquares Illustrates how a general polynomial can be represented using a matrix and a vector of monomials. The Cholesky factorization of the matrix provides a sum of squares certificate for nonnegativity. ```mathematica x_1^2 + 2x_1^2x_2 + 5x_1^2x_2^2 + 4x_1x_2^2 + x_2^2 = X^\top \begin{pmatrix}1 & 1 & 0\\1 & 5 & 2\\0 & 2 & 1\end{pmatrix} X ``` ```mathematica X = (x_1, x_1x_2, x_2) ``` ```mathematica (x_1 + x_1x_2)^2 + (2x_1x_2 + x_2)^2 = X^\top \begin{pmatrix}1 & 1 & 0\\0 & 2 & 1\end{pmatrix}^\top \begin{pmatrix}1 & 1 & 0\\0 & 2 & 1\end{pmatrix} X ``` -------------------------------- ### Local Optimization with Ipopt Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/polynomial_optimization Use Ipopt to find a local minimum for a polynomial objective function with inequality constraints. This demonstrates how to set up and run a local search solver. ```julia import Ipopt model = Model(Ipopt.Optimizer) @variable(model, a >= 0) @variable(model, b >= 0) @constraint(model, a + b >= 1) @NLobjective(model, Min, a^3 - a^2 + 2a*b - b^2 + b^3) optimize!(model) ``` -------------------------------- ### Select Dualized SDP Solver Source: https://jump.dev/SumOfSquares.jl/stable/generated/Extension/certificate Select an SDP solver and use Dualization.dual_optimizer to solve the dual problem, which can result in a smaller SDP. ```julia import SCS using Dualization solver = dual_optimizer(SCS.Optimizer) ``` -------------------------------- ### SOS Model with Maxdegree 4 Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/polynomial_optimization Defines an SOS model with a constraint on polynomial `p` and sets `maxdegree` to 4. This snippet demonstrates the basic setup for polynomial optimization with a specific degree constraint. ```julia model = SOSModel(dual_scs) @variable(model, α) @objective(model, Max, α) @constraint(model, c4, p >= α, domain = S, maxdegree = 4) optimize!(model) ``` -------------------------------- ### Set up the Sum-of-Squares program Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/univariate Create an SOSModel with a chosen optimizer, define a variable for the lower bound 'σ', and add a constraint that the polynomial 'p' must be greater than or equal to 'σ'. The objective is to maximize 'σ'. ```julia model = SOSModel(CSDP.Optimizer) @variable(model, σ) @constraint(model, cref, p >= σ) @objective(model, Max, σ) ``` -------------------------------- ### Display Solution Summary Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/circle Prints a summary of the optimization model's solution status, including solver information and objective values. ```julia solution_summary(model) ``` -------------------------------- ### Define Nonconvex QP Problem Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/qp Sets up the objective polynomial and the feasible region for the nonconvex QP. Requires LinearAlgebra, DynamicPolynomials, and SumOfSquares packages. ```julia using LinearAlgebra c = [42, 44, 45, 47, 47.5] Q = 100I using DynamicPolynomials @polyvar x[1:5] p = c'x - x' * Q * x / 2 using SumOfSquares K = @set x[1] >= 0 && x[1] <= 1 && x[2] >= 0 && x[2] <= 1 && x[3] >= 0 && x[3] <= 1 && x[4] >= 0 && x[4] <= 1 && x[5] >= 0 && x[5] <= 1 && 10x[1] + 12x[2] + 11x[3] + 7x[4] + 4x[5] <= 40 ``` -------------------------------- ### Equivalent Copositive Constraint (Domain Keyword) Source: https://jump.dev/SumOfSquares.jl/stable/reference/standard_form Shows the equivalent copositive constraint formulation using the `domain` keyword with an SOSCone. ```julia @polyvar x y @constraint(model, x^2 - 2x*y + y^2 in SOSCone(), domain = @set x*y ≥ 0) ``` -------------------------------- ### Initialize Model with SOSCone Constraint Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/getting_started Initialize a standard Model (instead of SOSModel) and add a constraint that the polynomial belongs to the SOS cone. This is an alternative way to enforce non-negativity for SOS. ```julia model = Model(solver) con_ref = @constraint(model, p in SOSCone()) ``` -------------------------------- ### Set up and Solve SOS Model with Symmetry Source: https://jump.dev/SumOfSquares.jl/stable/generated/Symmetry/even_reduction Sets up a Sum of Squares optimization model, incorporating the defined symmetry pattern and using the CSDP solver to find the maximum value of 't', which corresponds to the minimum of the polynomial. ```julia import CSDP solver = CSDP.Optimizer model = Model(solver) @variable(model, t) @objective(model, Max, t) pattern = Symmetry.Pattern(G, OnSign()) con_ref = @constraint(model, poly - t in SOSCone(), symmetry = pattern) optimize!(model) value(t) ``` -------------------------------- ### Solving a Problem with TypedPolynomials Source: https://jump.dev/SumOfSquares.jl/stable/generated/Extension/typed This snippet shows how to set up and solve a problem using TypedPolynomials with the SumOfSquares package and the CSDP optimizer. It defines variables, a constraint, and then optimizes the model. ```julia import TypedPolynomials TypedPolynomials.@polyvar x y using SumOfSquares import CSDP model = SOSModel(CSDP.Optimizer) con_ref = @constraint(model, 2x^4 + 5y^4 - x^2*y^2 >= -2(x^3*y + x + 1)) optimize!(model) solution_summary(model) ``` -------------------------------- ### CSDP Solver Output and Polynomial Basis Source: https://jump.dev/SumOfSquares.jl/stable/generated/Symmetry/dihedral This snippet displays the iterative output from the CSDP solver, indicating the progress and convergence of the SDP. It also shows the final primal and dual objective values, along with the basis polynomials derived from the symmetric constraint. ```text Iter: 11 Ap: 9.58e-01 Pobj: -1.6713293e-10 Ad: 9.58e-01 Dobj: -4.5284866e-09 Success: SDP solved Primal objective value: -1.6713293e-10 Dual objective value: -4.5284866e-09 Relative primal infeasibility: 1.08e-14 Relative dual infeasibility: 1.17e-09 Real Relative Gap: -4.36e-09 XZ Relative Gap: 1.21e-09 DIMACS error measures: 1.87e-14 0.00e+00 2.80e-09 0.00e+00 -4.36e-09 1.21e-09 CSDP 6.2.0 Iter: 0 Ap: 0.00e+00 Pobj: 0.0000000e+00 Ad: 0.00e+00 Dobj: 0.0000000e+00 Iter: 1 Ap: 7.31e-01 Pobj: -5.9540730e+00 Ad: 8.42e-01 Dobj: 3.0671681e-01 Iter: 2 Ap: 8.67e-01 Pobj: -1.6845198e+01 Ad: 8.68e-01 Dobj: 7.9208056e+00 Iter: 3 Ap: 8.41e-01 Pobj: -6.5167134e+00 Ad: 6.91e-01 Dobj: 4.6341001e+00 Iter: 4 Ap: 7.89e-01 Pobj: -3.0718716e+00 Ad: 8.67e-01 Dobj: 8.7844100e-01 Iter: 5 Ap: 8.59e-01 Pobj: -1.8420726e+00 Ad: 8.47e-01 Dobj: -3.3090925e-01 Iter: 6 Ap: 7.40e-01 Pobj: -1.2180545e+00 Ad: 8.00e-01 Dobj: -6.8726774e-01 Iter: 7 Ap: 8.82e-01 Pobj: -1.0523759e+00 Ad: 6.48e-01 Dobj: -8.1732017e-01 Iter: 8 Ap: 6.75e-01 Pobj: -9.8406183e-01 Ad: 7.70e-01 Dobj: -8.8525808e-01 Iter: 9 Ap: 8.70e-01 Pobj: -9.5552033e-01 Ad: 7.26e-01 Dobj: -9.1517422e-01 Iter: 10 Ap: 1.00e+00 Pobj: -9.2933412e-01 Ad: 8.92e-01 Dobj: -9.2629286e-01 Iter: 11 Ap: 8.61e-01 Pobj: -9.2914325e-01 Ad: 9.11e-01 Dobj: -9.3036068e-01 Iter: 12 Ap: 1.00e+00 Pobj: -9.2898054e-01 Ad: 1.00e+00 Dobj: -9.3115787e-01 Iter: 13 Ap: 7.19e-01 Pobj: -9.2961485e-01 Ad: 5.76e-01 Dobj: -9.3155213e-01 Iter: 14 Ap: 1.00e+00 Pobj: -9.2967165e-01 Ad: 3.54e-01 Dobj: -9.3180778e-01 Iter: 15 Ap: 4.27e-01 Pobj: -9.3066051e-01 Ad: 1.00e+00 Dobj: -9.3175037e-01 Iter: 16 Ap: 1.00e+00 Pobj: -9.3042315e-01 Ad: 1.00e+00 Dobj: -9.3211404e-01 Iter: 17 Ap: 5.07e-01 Pobj: -9.3105995e-01 Ad: 6.22e-01 Dobj: -9.3240464e-01 Iter: 18 Ap: 6.61e-01 Pobj: -9.3144565e-01 Ad: 4.35e-01 Dobj: -9.3257352e-01 Iter: 19 Ap: 1.00e+00 Pobj: -9.3153548e-01 Ad: 3.23e-01 Dobj: -9.3268808e-01 Iter: 20 Ap: 2.89e-01 Pobj: -9.3188125e-01 Ad: 1.00e+00 Dobj: -9.3271392e-01 Iter: 21 Ap: 9.38e-01 Pobj: -9.3191070e-01 Ad: 7.99e-01 Dobj: -9.3286395e-01 Iter: 22 Ap: 3.51e-01 Pobj: -9.3205000e-01 Ad: 2.86e-01 Dobj: -9.3293840e-01 Iter: 23 Ap: 5.13e-01 Pobj: -9.3215974e-01 Ad: 5.97e-01 Dobj: -9.3299570e-01 Iter: 24 Ap: 9.25e-01 Pobj: -9.3218115e-01 Ad: 9.83e-01 Dobj: -9.3300966e-01 Iter: 25 Ap: 7.15e-01 Pobj: -9.3218667e-01 Ad: 9.88e-01 Dobj: -9.3301310e-01 Iter: 26 Ap: 3.92e-01 Pobj: -9.3219317e-01 Ad: 7.81e-01 Dobj: -9.3301310e-01 Iter: 27 Ap: 1.00e+00 Pobj: -9.3218942e-01 Ad: 4.83e-01 Dobj: -9.3301314e-01 Iter: 28 Ap: 1.00e+00 Pobj: -9.3218987e-01 Ad: 1.00e+00 Dobj: -9.3301322e-01 value(t) = -0.9321899233673872 DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}, Float64}[1.0, -0.7071067811865472y² - 0.7071067811865475x²] DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}, Float64}[xy] DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}, Float64}[-0.7071067811865472y² + 0.7071067811865475x²] DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}, Float64}[y³, x²y, y] DynamicPolynomials.Polynomial{DynamicPolynomials.Commutative{DynamicPolynomials.CreationOrder}, MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}, Float64}[-x³, -xy², -x] ``` -------------------------------- ### Create SOSModel for Automatic Interpretation Source: https://jump.dev/SumOfSquares.jl/stable/constraints Initialize a JuMP model with `SOSModel` to automatically interpret polynomial nonnegativity constraints as sum-of-squares constraints. ```julia model = SOSModel(...) ``` -------------------------------- ### Clarabel.jl Solver Output and Summary Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/qp This output details the problem's characteristics, solver settings, and iterative progress for a polynomial optimization problem solved by Clarabel.jl. It includes convergence metrics and the final solution summary. ```text ------------------------------------------------------------- Clarabel.jl v0.11.0 - Clever Acronym (c) Paul Goulart University of Oxford, 2022 ------------------------------------------------------------- problem: variables = 253 constraints = 308 nnz(P) = 0 nnz(A) = 715 cones (total) = 13 : Zero = 1, numel = 56 : PSDTriangle = 12, numel = (21,21,21,21,...,21) settings: linear algebra: direct / qdldl, precision: 64 bit (1 thread) max iter = 200, time limit = Inf, max step = 0.990 tol_feas = 1.0e-08, tol_gap_abs = 1.0e-08, tol_gap_rel = 1.0e-08, static reg : on, ϵ1 = 1.0e-08, ϵ2 = 4.9e-32 dynamic reg: on, ϵ = 1.0e-13, δ = 2.0e-07 iter refine: on, reltol = 1.0e-13, abstol = 1.0e-12, max iter = 10, stop ratio = 5.0 equilibrate: on, min_scale = 1.0e-04, max_scale = 1.0e+04 max iter = 10 iter pcost dcost gap pres dres k/t μ step --------------------------------------------------------------------------------------------- 0 -1.3991e+01 -1.3991e+01 6.35e-16 5.29e-01 2.73e-01 1.00e+00 9.46e+00 ------ 1 2.4255e+02 2.4996e+02 3.06e-02 1.33e-01 4.20e-02 8.38e+00 2.67e+00 9.00e-01 2 7.3705e+01 7.9821e+01 8.30e-02 5.52e-02 2.55e-02 6.48e+00 1.52e+00 6.49e-01 3 3.6450e+01 3.8243e+01 4.92e-02 1.24e-02 5.79e-03 1.86e+00 4.41e-01 7.97e-01 4 3.1397e+01 3.2740e+01 4.28e-02 6.91e-03 3.30e-03 1.39e+00 2.70e-01 6.34e-01 5 2.1768e+01 2.2034e+01 1.22e-02 1.33e-03 6.52e-04 2.75e-01 5.65e-02 8.06e-01 6 2.2259e+01 2.2326e+01 2.99e-03 4.47e-04 2.25e-04 6.95e-02 1.91e-02 8.17e-01 7 2.1905e+01 2.1925e+01 9.14e-04 1.40e-04 7.07e-05 2.10e-02 6.08e-03 7.38e-01 8 2.1973e+01 2.1978e+01 1.91e-04 4.48e-05 2.26e-05 4.52e-03 1.97e-03 9.90e-01 9 2.2000e+01 2.2000e+01 2.57e-06 6.38e-07 3.20e-07 6.11e-05 2.82e-05 9.90e-01 10 2.2000e+01 2.2000e+01 2.82e-08 7.00e-09 3.51e-09 6.70e-07 3.09e-07 9.89e-01 11 2.2000e+01 2.2000e+01 3.49e-10 8.66e-11 4.34e-11 8.30e-09 3.83e-09 9.88e-01 --------------------------------------------------------------------------------------------- Terminated with status = solved solve time = 9.84ms solution_summary(; result = 1, verbose = false) ├ solver_name : Clarabel ├ Termination │ ├ termination_status : OPTIMAL │ └ raw_status : SOLVED ├ Solution (result = 1) │ ├ primal_status : FEASIBLE_POINT │ ├ dual_status : FEASIBLE_POINT │ ├ objective_value : -2.20000e+01 │ └ dual_objective_value : -2.20000e+01 └ Work counters ├ solve_time (sec) : 9.83550e-03 └ barrier_iterations : 11 ``` -------------------------------- ### Define Objective and Constraints with Symbolic Differentiation Source: https://jump.dev/SumOfSquares.jl/stable/generated/Polynomial%20Optimization/polynomial_optimization This snippet shows an equivalent problem formulation using registered functions for the objective and its derivatives, computed via symbolic differentiation. It sets up a JuMP model with Ipopt and registers the custom functions. ```julia f(a, b) = p(x => a, y => b) ∇p = differentiate(p, [x, y]) function ∇f(g, a, b) for i in eachindex(g) g[i] = ∇p[i](x => a, y => b) end end ∇²p = differentiate(∇p, [x, y]) function ∇²f(H, a, b) for j in axes(∇²p, 2) for i in j:size(∇²p, 1) H[i, j] = ∇²p[i, j](x => a, y => b) end end end using Ipopt gmodel = Model(Ipopt.Optimizer) @variable(gmodel, a >= 0) @variable(gmodel, b >= 0) @constraint(gmodel, a + b >= 1) register(gmodel, :f, 2, f, ∇f, ∇²f) @NLobjective(gmodel, Min, f(a, b)) optimize!(gmodel) ``` -------------------------------- ### Import necessary Julia packages Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/sos_decomposition Imports the required libraries for polynomial manipulation and Sum of Squares optimization. ```julia using DynamicPolynomials using SumOfSquares import CSDP ``` -------------------------------- ### Solution Summary Source: https://jump.dev/SumOfSquares.jl/stable/generated/Getting%20started/univariate Displays a summary of the optimization results, including solver name, termination status, objective value, and solve time. ```text solution_summary(; result = 1, verbose = false) ├ solver_name : CSDP ├ Termination │ ├ termination_status : OPTIMAL │ ├ result_count : 1 │ └ raw_status : Problem solved to optimality. ├ Solution (result = 1) │ ├ primal_status : FEASIBLE_POINT │ ├ dual_status : FEASIBLE_POINT │ ├ objective_value : -6.00000e+00 │ └ dual_objective_value : -6.00000e+00 └ Work counters └ solve_time (sec) : 1.46699e-03 ``` -------------------------------- ### Benchmarking Gröbner Basis Computation with DynamicPolynomials Source: https://jump.dev/SumOfSquares.jl/stable/generated/Extension/typed This snippet benchmarks the time taken by DynamicPolynomials to compute a Gröbner basis for the same system of polynomial equalities, allowing for comparison with TypedPolynomials. ```julia import DynamicPolynomials @btime let DynamicPolynomials.@polyvar x y S = @set x^3 * y + x == 2x^2 * y^2 && 3x^4 == y SemialgebraicSets.compute_gröbner_basis!(S.I) end ```