-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_cone_problem.py
68 lines (52 loc) · 1.55 KB
/
random_cone_problem.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import scipy.sparse as sp
# size of each block
block_m, block_n = 100, 20
# number of blocks
N = 100
# sparsity density of coupling blocks
p = 0.005 / N
# add blocks to the diagonal
diags = []
for i in xrange(N):
delta_m, delta_n = np.random.randint(
-0.1 * block_m, 0.1 * block_m), np.random.randint(-0.1 * block_n, 0.1 * block_n)
diags.append(np.random.randn(block_m + delta_m, block_n + delta_n))
# create the sparse diagonal matrix
A = sp.block_diag(diags).tocsr()
# create sparse coupling
m, n = A.shape
nnz = m * n * p
ij = (np.random.randint(m, size=(nnz,)), np.random.randint(n, size=(nnz,)))
data = np.random.randn(nnz)
Acouple = sp.coo_matrix((data, ij), (m, n))
# add the two
A = A + Acouple
# randomly generate an "x" (free)
x = np.random.randn((n))
# generate a vector which will be used to create the complementary "s" and "y"
tmp = np.random.randn((m))
y = np.maximum(tmp, 0)
s = np.maximum(-tmp, 0)
# v = tmp[1:]
# t = tmp[0]
# if np.linalg.norm(v) <= -t:
# y = np.zeros((m))
# elif np.linalg.norm(v) <= t:
# y = tmp
# else:
# alpha = (0.5)*(np.linalg.norm(v) + t)
# y = np.hstack((alpha, alpha*v/np.linalg.norm(v)))
# s = y - tmp
# now, s and y are complementary primal-dual pair
c = -A.T * y
b = A * x + s
dims = {'l': m, 'q': [], 's': []}
# A = A.tocoo()
# solvers.conelp(cvxopt.matrix(c), cvxopt.spmatrix(A.data, A.row, A.col), cvxopt.matrix(b), dims)
#
import ecos
ecos.solve(c, A, b, dims)
objval = c.T.dot(x)
socp_vars = {'c': c, 'G': A, 'h': b, 'A': None, 'b': None, 'dims': dims}
# print b.T.dot(y)