# Polynomial minimization

This examples shows how to solve a constrained polynomial minimization problem. Given p(x) and g(x), consider the problem in `x`

    min  p(x)
    s.t. g(x) >= 0
    
We can lower bound the optimal value by solving the following problem in `gamma, sigma(x)`

    max  gamma
    s.t. p(x) - gamma - sigma(x) g(x) >= 0                (1)
         sigma(x) >= 0


```python
from sympy.abc import x, y
from posipoly import *

tot_deg = 4        # overall degree of problem
n = 2              # number of variables
pp_cone = 'psd'    # can be 'psd' or 'sdd'

p = Polynomial.from_sympy(-x**2 - y**2 + x, [x,y])
g = Polynomial.from_sympy(1 - x**2 - y**2, [x,y])

sigma_deg = tot_deg - g.d   # degree of sigma

prob = PPP()
prob.add_var('gamma', n, 0, 'coef')
prob.add_var('sigma', n, sigma_deg, 'pp')
prob.add_constraint({'gamma': -PTrans.eye(n, 0, n, tot_deg),
                     'sigma': -PTrans.mul_pol(n, sigma_deg, g)}, 
                     -p, 'pp')
prob.set_objective({'gamma': [-1]})

_, status = prob.solve(pp_cone) 
opt_gamma = prob.get_poly('gamma')(0,0)

print('a lower bound is {:.2f}'.format(opt_gamma))
```

    optimizing...
    solved in 0.02s
    solsta.optimal
    a lower bound is -2.00



```python
# plot polynomial for comparison
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

XX, YY = np.meshgrid(np.linspace(-2, 2, 100), np.linspace(-2, 2, 100))
XX = XX.flatten()
YY = YY.flatten()

VV = np.vectorize(p)(XX, YY)
VV_control = opt_gamma * np.ones(VV.shape)

mask = np.vectorize(g)(XX, YY) >= 0

ax = plt.figure().gca(projection='3d')
ax.plot_trisurf(XX[mask], YY[mask], VV[mask])
ax.plot_trisurf(XX[mask], YY[mask], VV_control[mask])

plt.show()
```


    <IPython.core.display.Javascript object>






6eaf72406133f759007281f2708a8c1134b198cf
ipynb
Jupyter Notebook
examples/polymin.ipynb
pettni/posipoly
a40afd093567f62979bba73eae61a6416009bedc
MIT
2021-07-09T02:07:44.000Z
2021-07-09T02:07:44.000Z
examples/polymin.ipynb
pettni/posipoly
a40afd093567f62979bba73eae61a6416009bedc
MIT
2018-07-24T18:21:44.000Z
2018-07-24T18:29:50.000Z
examples/polymin.ipynb
pettni/posipoly
a40afd093567f62979bba73eae61a6416009bedc
MIT
2020-04-28T17:18:59.000Z
2020-04-28T17:18:59.000Z
Qwen/Qwen-72B
1. YES
2. YES