Constrained optimization

$$ \arg \min_{\mathbf{x}} \;\; \frac{1}{2}\|\mathbf{x} - \mathbf{x}_0\|^2 \;\; \text{subject to}\;\; \mathbf{x}^T\mathbf{1} = 1, \;\; \mathbf{x} \ge 0. $$
In [ ]:
# Closed form Solution
# See Tutorial #4 on optimization

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import root_scalar
np.set_printoptions(precision=2, suppress=True)

# Define the function sum(max(b-gamma,0))-1
def my_func(gamma,x0):
  return np.sum(np.maximum(x0-gamma, 0))-1

# Solve for gamma in the equation sum(max(b-gamma,0))=1
x0 = np.random.rand(10)
gamma = root_scalar(my_func,args=(x0),bracket=[0, 3]).root

# Closed form solution
x = np.maximum(x0-gamma,0)
x
Out[ ]:
array([0.  , 0.07, 0.35, 0.  , 0.  , 0.  , 0.  , 0.28, 0.29, 0.  ])
In [ ]:
# Solve using CVX

import cvxpy as cvx
np.set_printoptions(precision=2, suppress=True)

c = np.ones(10)
# ===== Call CVX ==========
x = cvx.Variable(10)
objective   = cvx.Minimize( cvx.sum_squares(x-x0) )
constraints = [ c @ x == 1,
              x >= 0]
prob        = cvx.Problem(objective, constraints)
prob.solve()
sol = x.value
# =========================
sol
Out[ ]:
array([-0.  ,  0.07,  0.35, -0.  , -0.  , -0.  , -0.  ,  0.28,  0.29,
        0.  ])
In [ ]:
%%shell
jupyter nbconvert --to html /content/ECE595_lecture06_constrained.ipynb