-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.py
executable file
·144 lines (122 loc) · 4.45 KB
/
iterator.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/python
import cvxpy as cp
import numpy as np
class Iterator:
def __init__(self, a_num, s_num):
"""
H : a matrix, each column h_i is a random channel.
H.shape : (a_num, s_num).
a_num : means antennas number.
s_num : means sensors number.
init_a : initial solver.
"""
self.eps = 1e-5
self.max_ite_time = 20
self.a_num = a_num
self.s_num = s_num
self.H = None
self.H_real = None
self.H_imag = None
self.A = None
self.init_problem = None
self.a = None
self.C_OLD = None
self.problem = None
self.result = None
self.init_result = None
self.construct_init_problem()
self.construct_problem()
def load_channel(self, H):
if self.a_num != H.shape[0] or self.s_num != H.shape[1]:
print("H' shape don't match antennas X sensors")
return 1
else:
self.H = H
self.H_real.value = H.real
self.H_imag.value = H.imag
def construct_init_problem(self):
self.H_real = cp.Parameter((self.a_num, self.s_num))
self.H_imag = cp.Parameter((self.a_num, self.s_num))
self.A = cp.Variable((self.a_num, self.a_num), hermitian=True)
objective = cp.Minimize(cp.trace(self.A))
constraints = [self.A >> 0]
for k in range(self.s_num):
hk = self.H_real[:, k] + 1j * self.H_imag[:, k]
constraints.extend(
[cp.real(cp.quad_form(hk, self.A)) >= 1]
) # equal to cp.trace(A @ Hk) >= 1
self.init_problem = cp.Problem(objective, constraints)
def construct_problem(self):
self.C_OLD = cp.Parameter((2, self.s_num))
self.a = cp.Variable(self.a_num, complex=True)
objective = cp.Minimize(cp.sum_squares(self.a))
constraints = []
for k in range(self.s_num):
hk = self.H_real[:, k] + 1j * self.H_imag[:, k]
tmp = cp.conj(self.a) @ hk
ck = [cp.real(tmp), cp.imag(tmp)]
ck_old = self.C_OLD[:, k]
p2 = ck_old[0] * (ck[0] - ck_old[0]) + ck_old[1] * (ck[1] - ck_old[1])
constraints.extend([cp.sum_squares(ck_old) + 2 * p2 >= 1])
self.problem = cp.Problem(objective, constraints)
def cal_init_solver(self):
self.H_real.value = self.H.real
self.H_imag.value = self.H.imag
if self.a_num == 1:
self.init_result = self.init_problem.solve(solver="MOSEK", verbose=False)
else:
self.init_result = self.init_problem.solve(solver="SCS", verbose=False)
A_value = self.A.value
eigenvalues, eigenvectors = np.linalg.eig(A_value)
# try:
# eigenvalues, eigenvectors = np.linalg.eig(A_value)
# except:
# print(self.init_result)
# print(self.A.value)
# print(self.H)
eigenvalues_real = eigenvalues.real
max_index = np.argmax(eigenvalues_real)
self.init_a = np.sqrt(eigenvalues_real[max_index]) * eigenvectors[max_index]
if np.linalg.matrix_rank(A_value) == 1:
return 0
C_OLD = np.zeros((2, self.s_num))
for k in range(self.s_num):
hk = self.H[:, k]
tmp = np.conj(self.init_a) @ hk
C_OLD[:, k] = [tmp.real, tmp.imag]
self.C_OLD.value = C_OLD
return 1
def cal_solver(self):
self.result = self.problem.solve(solver="SCS", verbose=False)
C_NEW = np.zeros((2, self.s_num))
for k in range(self.s_num):
hk = self.H[:, k]
tmp = np.conj(self.a.value) @ hk
C_NEW[:, k] = [tmp.real, tmp.imag]
self.C_NEW = C_NEW
def loop(self):
if ~self.cal_init_solver():
self.a.value = self.init_a
self.result = self.init_result
return
count = 0
while True:
self.cal_solver()
count += 1
diff = np.linalg.norm(self.C_NEW - self.C_OLD.value)
if diff <= self.eps or count >= self.max_ite_time:
break
self.C_OLD.value = self.C_NEW
if __name__ == "__main__":
# seed = 1
# np.random.seed(seed)
a_num = 1
s_num = 15
H = (np.random.randn(a_num, s_num) + 1j * np.random.randn(a_num, s_num)) / np.sqrt(
2
)
ite = Iterator(H.shape[0], H.shape[1])
ite.load_channel(H)
ite.loop()
print(ite.result)
print(ite.a.value)