-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.py
More file actions
213 lines (188 loc) · 7.34 KB
/
optimizer.py
File metadata and controls
213 lines (188 loc) · 7.34 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import random
from itertools import product
import cvxpy as cp
import numpy as np
from .helper import Helper
class Optimizer:
def __init__(self, algoName):
self.algo = algoName
self.helper = Helper()
self.delta_f_values = []
@staticmethod
def _activity_from_power(power, tol=1e-9):
return (np.asarray(power) > tol).astype(float)
def run(self, satellites, UEs, L, B, time, antSpacing):
K = len(UEs)
J = len(satellites)
T = 290
k0 = 1.38e-23
sigma = k0 * T * B[0]
if self.algo != "fractionalProgramming":
raise NameError("The algorithm is not implemented!")
r_combinations = np.array(list(product([0, 1], repeat=J)))
r_all = r_combinations.T
M = 5
max_iters = 20
sat_t_bound = np.tile(
np.sqrt(np.array([sat.maxPow for sat in satellites], dtype=float)), (K, 1)
)
ue_t_bound = np.repeat(
np.sqrt(np.array([ue.maxPow for ue in UEs], dtype=float))[:, None], J, axis=1
)
solutions = []
for rIdx in range(2**J):
r = r_all[:, rIdx]
d_val, u_val, p_dl_val, p_ul_val, chi_dl_val, chi_ul_val = self.helper.initializeValues(
K, J, r, satellites, UEs
)
if J == 1:
d = None
u = None
t_dl = None
t_ul = None
z_dl = cp.Variable((K, J), nonneg=True)
z_ul = cp.Variable((K, J), nonneg=True)
else:
d = cp.Variable((K, J), boolean=True)
u = cp.Variable((K, J), boolean=True)
t_dl = cp.Variable((K, J), nonneg=True)
t_ul = cp.Variable((K, J), nonneg=True)
z_dl = cp.Variable((K, J))
z_ul = cp.Variable((K, J))
const_param = cp.Parameter((K, J))
lin_dl_param = cp.Parameter((K, J))
lin_ul_param = cp.Parameter((K, J))
quad_dl_param = cp.Parameter((K, J), nonneg=True)
quad_ul_param = cp.Parameter((K, J), nonneg=True)
objective_expr = cp.sum(
const_param
+ cp.multiply(lin_dl_param, z_dl)
+ cp.multiply(lin_ul_param, z_ul)
- cp.multiply(quad_dl_param, cp.square(z_dl))
- cp.multiply(quad_ul_param, cp.square(z_ul))
)
if J == 1:
constraints = []
else:
constraints = [
cp.sum(d, axis=1) <= 1,
cp.sum(u, axis=1) <= 1,
cp.abs(d @ r - u @ r)
<= M * (2 - cp.sum(d, axis=1) - cp.sum(u, axis=1)),
t_dl <= sat_t_bound,
t_ul <= ue_t_bound,
z_dl >= 0,
z_ul >= 0,
z_dl <= t_dl,
z_ul <= t_ul,
z_dl >= t_dl - M * (1 - d),
z_dl <= M * d,
z_ul >= t_ul - M * (1 - u),
z_ul <= M * u,
]
constraints.extend(
cp.sum_squares(z_ul[k, :]) <= UEs[k].maxPow for k in range(K)
)
constraints.extend(
cp.sum_squares(z_dl[:, j]) <= satellites[j].maxPow for j in range(J)
)
prob = cp.Problem(cp.Maximize(objective_expr), constraints)
objectiveWithIterations = []
f0_withIterations = []
for _ in range(max_iters):
chi_dl_val, chi_ul_val = self.helper.updateChi(
d_val,
r,
u_val,
p_dl_val,
p_ul_val,
time,
sigma,
satellites,
UEs,
L,
antSpacing,
)
xi_dl, xi_ul = self.helper.updateXi(
d_val,
r,
u_val,
p_dl_val,
p_ul_val,
chi_dl_val,
chi_ul_val,
time,
sigma,
satellites,
UEs,
L,
antSpacing,
)
coeffs = self.helper.objective_coefficients(
r,
chi_dl_val,
chi_ul_val,
xi_dl,
xi_ul,
time,
sigma,
satellites,
UEs,
L,
antSpacing,
)
const_param.value = coeffs["constant"]
lin_dl_param.value = coeffs["linear_dl"]
lin_ul_param.value = coeffs["linear_ul"]
quad_dl_param.value = coeffs["quad_dl"]
quad_ul_param.value = coeffs["quad_ul"] + coeffs["quad_cross"]
prob.solve(solver=cp.MOSEK, verbose=False, warm_start=False)
if J == 1:
if z_dl.value is None or z_ul.value is None:
raise RuntimeError(
f"Solver returned no primal solution for numSat=1, spin={r.tolist()}, "
f"status={prob.status}"
)
p_dl_val = np.square(z_dl.value)
p_ul_val = np.square(z_ul.value)
d_val = self._activity_from_power(p_dl_val)
u_val = self._activity_from_power(p_ul_val)
else:
if (
d.value is None
or u.value is None
or t_dl.value is None
or t_ul.value is None
):
raise RuntimeError(
f"Solver returned no primal solution for spin={r.tolist()}, "
f"status={prob.status}"
)
d_val = d.value
u_val = u.value
p_dl_val = np.square(t_dl.value)
p_ul_val = np.square(t_ul.value)
f_0 = self.helper.objective_f0(
r, d_val, u_val, p_dl_val, p_ul_val, time, sigma, satellites, UEs, L, antSpacing
)
f0_withIterations.append(f_0)
objectiveWithIterations.append(prob.value)
with open("f2_J_4_K_10.txt", "w") as f:
for item in objectiveWithIterations:
f.write(str(item) + "\n")
with open("f0withIterations.txt", "w") as f:
for item in f0_withIterations:
f.write(str(item) + "\n")
solutions.append(f_0)
bestSolution_spin0 = float(solutions[0])
bestSolution_spin1 = float(solutions[-1])
bestSolution_randSpin = (
bestSolution_spin0 if random.random() < 0.5 else bestSolution_spin1
)
bestSolution_wspin = float(max(solutions))
return {
"bestSolution_wspin": bestSolution_wspin,
"bestSolution_spin0": bestSolution_spin0,
"bestSolution_spin1": bestSolution_spin1,
"bestSolution_randSpin": float(bestSolution_randSpin),
}