# Sum of squares optimization

**URL:** <https://discourse.julialang.org/t/sum-of-squares-optimization/15075>\
**Category:** Optimization (Mathematical)\
**Created:** [September 17, 2018, 3:54pm UTC](https://discourse.julialang.org/t/sum-of-squares-optimization/15075 "2018-09-17T15:54:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 17, 2018, 3:54pm UTC](https://discourse.julialang.org/t/sum-of-squares-optimization/15075/1 "2018-09-17T15:54:49Z")

</div>

I have the following optimization problem that I want to solve by the sum of squares approach. It has a linear objective function and I am looking for the best set of (a, b, and c) that satisfies a specific polynomial.

min (1/3_a_x\_max^3+1/2_b_x\_max^2+c_x\_max+sin(x\_max)- 1/3_a_x\_min^3+1/2_b_x\_min^2+c_x\_min+sin(x\_min)); (x\_min=-pi/2, x\_max=pi/2)

subject to:  
x\<=pi/2;  
x\>=-pi/2;  
I also have the following constraint that enforces (a_x^2+b_x+c) to lay above the Taylor expansion of cosine function which is non-negative if we enforce x to vary within [-pi/2, pi/2].  
a_x^2+b_x+c \> = 1-x^2/2! +x^4/4! -x^6/6! +x^8/8! -x^10/10! + epsilon;  
(epsilon: a very small number)  
I run the following code to solve the problem.

using MultivariatePolynomials  
using JuMP  
using PolyJuMP  
using SumOfSquares  
using DynamicPolynomials  
using Mosek

@polyvar x  
m = SOSModel(solver = MosekSolver())  
@variable m a  
@variable m b  
@variable m c  
P= a_x^2+b_x+c-1+x^2/factorial(2) -x^4/factorial(4) +x^6/factorial(6) -x^8/factorial(8) +x^10/factorial(10) – epsilon;  
S = @set x \>= -pi/2 && x \<= pi/2  
x\_max=pi/2;  
x\_min=-pi/2;  
@objective m Min (1/3_a_x\_max^3+1/2_b_x\_max^2+c_x\_max+sin(x\_max)- 1/3_a_x\_min^3+1/2_b_x\_min^2+c_x\_min+sin(x\_min));  
@constraint( m, p \>= 0, domain = S)

I got following error when I executed the code:  
LoadError: UndefVarError: @set not defined.  
Can you please let me know how I can debug this. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [September 17, 2018, 4:22pm UTC](https://discourse.julialang.org/t/sum-of-squares-optimization/15075/2 "2018-09-17T16:22:17Z")

</div>

You need to do `using SemialgebraicSets`

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 17, 2018, 7:12pm UTC](https://discourse.julialang.org/t/sum-of-squares-optimization/15075/3 "2018-09-17T19:12:35Z")

</div>

As a tip, you can use triple backticks (```) to enclose code to format it.

This:

> ```  
> println(“example”)  
> ```

becomes this:

```julia
println("example")

```

It makes your code easier to read 🙂

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [September 19, 2018, 1:10am UTC](https://discourse.julialang.org/t/sum-of-squares-optimization/15075/4 "2018-09-19T01:10:16Z")

</div>

I run the following piece if code to obtain an understimator for cosine function. I have replace cosine function with its corresponding Taylor series.

```julia
using MultivariatePolynomials
using JuMP
using PolyJuMP
using SumOfSquares
using DynamicPolynomials
using Mosek
using SemialgebraicSets

@polyvar x
m = SOSModel(solver = MosekSolver())
@variable m a
@variable m b
@variable m c

epsilon = 1.387895246507398e-07
x_min = -3*pi/2
x_max = -pi/2

p = -1 + (x+pi)^2/factorial(2) - (x+pi)^4/factorial(4) + (x+pi)^6/factorial(6) - (x+pi)^8/factorial(8) + (x+pi)^10/factorial(10) - (x+pi)^12/factorial(12) + (x+pi)^14/factorial(14) - (x+pi)^16/factorial(16) + (x+pi)^18/factorial(18) - (x+pi)^20/factorial(20) + (x+pi)^20/factorial(20) - epsilon - a*x^2 - b*x - c
#p = 1 - x^2/factorial(2) + x^4/factorial(4) - x^6/factorial(6) + x^8/factorial(8) - x^10/factorial(10) + x^12/factorial(12) - x^14/factorial(14) + x^16/factorial(16) - x^18/factorial(18) + x^20/factorial(20) - epsilon -a*x^2 - b*x - c
s = @set x >= x_min && x <= x_max

@objective m Min (sin(x_max) - 1/3*a*x_max^3 - 1/2*b*x_max^2 - c*x_max - sin(x_min) + 1/3*a*x_min^3 + 1/2*b*x_min^2 + c*x_min )
@constraint( m, p >= 0, domain = s)

solve(m)

println("a =", getvalue(a))
println("b =", getvalue(b))
println("c =", getvalue(c))

```

After running the code I got following “WARNING: Not solved to optimality, status: Stall”  
and the result do not provide a reasonable understimator for the cosine function within [-3pi/2, -pi/2] span. I will appreciate any help.

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [October 10, 2018, 8:01am UTC](https://discourse.julialang.org/t/sum-of-squares-optimization/15075/5 "2018-10-10T08:01:01Z")

</div>

@Rasoul You might try to [change the polynomial basis](http://www.juliaopt.org/SumOfSquares.jl/latest/constraints.html#Changing-the-polynomial-basis-1) to have better numerical behavior. Note that this is currently only available on SumOfSquares master branch.

---

<div class="post-metadata">

**Author:** ![Rasoul](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rasoul](https://discourse.julialang.org/u/Rasoul)\
**Post date:** [October 10, 2018, 12:34pm UTC](https://discourse.julialang.org/t/sum-of-squares-optimization/15075/6 "2018-10-10T12:34:42Z")

</div>

> [@blegat](#):
>
> might try to [change the polynomial basis](http://www.juliaopt.org/SumOfSquares.jl/latest/constraints.html#Changing-the-polynomial-basis-1) to have better numerical behavior. Note that this is currently only available on Su

Thank you so much!
