-
Notifications
You must be signed in to change notification settings - Fork 29
/
task_03.py
32 lines (28 loc) · 906 Bytes
/
task_03.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cvxpy as cp
import numpy as np
import scipy as sp
from scipy import sparse
# Generate problem data
sp.random.seed(1)
n = 15
m = 20
A = sparse.random(m, n, density=0.5)
x_true = np.multiply((np.random.rand(n) > 0.8).astype(float),
np.random.randn(n)) / np.sqrt(n)
b = A.dot(x_true) + 0.5*np.random.randn(m)
gammas = np.linspace(1, 10, 11)
# Define problem
x = cp.Variable(n)
gamma = cp.Parameter(nonneg=True)
objective = 0.5*cp.sum_squares(A@x - b) + gamma*cp.norm1(x)
prob = cp.Problem(cp.Minimize(objective))
# Solve problem for different values of gamma parameter
for gamma_val in gammas:
gamma.value = gamma_val
prob.solve()
# prob.solve(solver='OSQP', warm_start=True)
# Print result.
print("\nThe optimal value is", prob.value)
print("The optimal x is")
print(x.value)
print("The norm of the residual is ", cp.norm(A @ x - b, p=2).value)