-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolveDirectly.py
178 lines (144 loc) · 4.45 KB
/
solveDirectly.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import cvxpy as cp
import numpy as np
import scipy.linalg as la
from scipy.fft import fft
from functions import randomized_rounding, min_eig_fisher
def solveTxDirectly(
sumArrayResponse,
rx,
numberOfTransmitters,
numberOfTransmitPositions,
):
sumArraySize = sumArrayResponse.shape[0]
F = la.dft(sumArraySize, scale="sqrtn")
lamb = cp.Variable()
tx = cp.Variable(numberOfTransmitPositions)
cost = cp.Maximize(lamb)
constraints = [
tx >= 0,
tx <= 1,
cp.sum(tx) == numberOfTransmitters,
cp.real(
sumArrayResponse.conj().T
@ cp.diag(
F.conj().T
@ cp.multiply(
fft(rx, sumArraySize, norm="ortho"),
(F[:, :numberOfTransmitPositions] @ tx),
)
)
@ sumArrayResponse
)
- lamb * np.eye(sumArrayResponse.shape[1])
>> 0,
]
problem = cp.Problem(cost, constraints)
problem.solve()
stats = {
"solverStats": problem.solver_stats,
"status": problem.status,
"value": problem.value,
}
return tx.value, stats
def solveRxDirectly(
sumArrayResponse,
tx,
numberOfReceivers,
numberOfReceivePositions,
):
sumArraySize = sumArrayResponse.shape[0]
F = la.dft(sumArraySize, scale="sqrtn")
lamb = cp.Variable()
rx = cp.Variable(numberOfReceivePositions)
cost = cp.Maximize(lamb)
constraints = [
rx >= 0,
rx <= 1,
cp.sum(rx) == numberOfReceivers,
cp.real(
sumArrayResponse.conj().T
@ cp.diag(
F.conj().T
@ cp.multiply(
fft(tx, sumArraySize, norm="ortho"),
(F[:, :numberOfReceivePositions] @ rx),
)
)
@ sumArrayResponse
)
- lamb * np.eye(sumArrayResponse.shape[1])
>> 0,
]
problem = cp.Problem(cost, constraints)
problem.solve()
stats = {
"solverStats": problem.solver_stats,
"status": problem.status,
"value": problem.value,
}
return rx.value, stats
def solveTxRxDirectly(
sumArrayResponse,
numberOfTransmitters,
numberOfReceivers,
numberOfTransmitPositions,
numberOfReceivePositions,
roundIntermediates=False,
initialization=None,
):
stats = {"total": {"alternatingDescentIterations": 0, "solveTime": 0}}
if initialization is None:
tx = (
np.ones(numberOfTransmitPositions)
* numberOfTransmitters
/ numberOfTransmitPositions
)
rx = (
np.ones(numberOfReceivePositions)
* numberOfReceivers
/ numberOfReceivePositions
)
else:
tx = initialization["tx"]
rx = initialization["rx"]
bestCrb = np.inf
currentCrb = 10 * min_eig_fisher(sumArrayResponse, np.convolve(tx, rx))
no_improvement_counter = 0
stats["alternatingDescent"] = []
while (
no_improvement_counter < 3
and stats["total"]["alternatingDescentIterations"] < 100
):
if roundIntermediates:
rx = randomized_rounding(rx, numberOfReceivers)
tx, tx_stats = solveTxDirectly(
sumArrayResponse,
rx,
numberOfTransmitters,
numberOfTransmitPositions,
)
stats["alternatingDescent"].append({"txStats": tx_stats})
stats["total"]["solveTime"] += tx_stats["solverStats"].solve_time
if roundIntermediates:
tx = randomized_rounding(tx, numberOfTransmitters)
rx, rx_stats = solveRxDirectly(
sumArrayResponse, tx, numberOfReceivers, numberOfReceivePositions
)
iteration = stats["total"]["alternatingDescentIterations"]
stats["alternatingDescent"][iteration]["rxStats"] = rx_stats
stats["total"]["solveTime"] += rx_stats["solverStats"].solve_time
currentCrb = min_eig_fisher(sumArrayResponse, np.convolve(tx, rx))
if currentCrb < bestCrb:
if bestCrb - currentCrb < 1e-3:
no_improvement_counter += 1
else:
no_improvement_counter = 0
bestCrb = currentCrb
else:
no_improvement_counter += 1
stats["total"]["alternatingDescentIterations"] += 1
return (
randomized_rounding(tx, numberOfTransmitters),
randomized_rounding(rx, numberOfReceivers),
stats,
)