-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathshells.py
102 lines (80 loc) · 3.21 KB
/
shells.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
#
# Copyright (c) Tobias Pfandzelter. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#
import numpy as np
import os
import typing
import matplotlib.pyplot as plt
import seaborn as sns
import placement
import config
def save_placement(name: str, p: typing.List[typing.Tuple[int, int]]) -> None:
with open(os.path.join(config.PLACEMENTS_DIR, "{}.csv".format(name)), "w") as f:
f.write("x,y\n")
for x, y in p:
f.write("{},{}\n".format(x, y))
def save_placement_picture(name: str, k1: int, k2: int, d1: typing.Union[int, float], d2: typing.Union[int, float], d: typing.Union[int, float], p: typing.List[typing.Tuple[int, int]]) -> None:
fig = plt.figure(figsize=(15, 15))
ax = fig.gca()
ax.set_xticks(np.arange(0, k1, 1))
ax.set_xlim(0, k1-1)
ax.set_yticks(np.arange(0, k2, 1))
ax.set_ylim(0, k2-1)
ax.grid()
pal = sns.color_palette(n_colors=len(p))
n = np.array(p)
covered = set()
for i in range(0, len(n)):
rn = n[i]
color = pal[i]
inrangex = []
inrangey = []
for bx in range(0, k1):
for by in range(0, k2):
if placement.weighted_lee(rn, (bx, by), k1, k2, d1, d2) <= d:
inrangex.append(bx)
inrangey.append(by)
covered.add((bx, by))
ax.scatter(inrangex, inrangey, color=color, marker="s", s=100, zorder=10)
uncovered = set([ (bx, by) for bx in range(0, k1) for by in range(0, k2) ])
uncovered = uncovered.difference(covered)
# print(uncovered)
uncovered = list(uncovered)
# print(uncovered)
if len(uncovered) > 0:
uncovered = np.array(uncovered)
ax.scatter(uncovered[:,0], uncovered[:,1], color="red", marker="s", s=100, zorder=10)
ax.scatter(n[:,0], n[:,1], axes=ax, s=200, color="black", zorder=12)
fig.savefig(os.path.join(config.PLACEMENTS_DIR, "{}.png".format(name)), format='png')
plt.close(fig)
# for each shell and slo, generate a placement, save it to a file, save it as a picture
for s in config.SHELLS:
for slo in config.SLO:
print("=" * config.TERM_SIZE)
print("Generating {}-{} for {}".format(slo["d"], slo["type"], s["name"]))
print("")
# generate the placement
k1 = s["planes"]
k2 = s["sats"]
if slo["type"] == "hops":
d1 = 1
d2 = 1
elif slo["type"] == "mean":
d1 = s["D_N_mean"]
d2 = s["D_M"]
elif slo["type"] == "max":
d1 = s["D_N_max"]
d2 = s["D_M"]
else:
raise ValueError("SLO type not supported")
p, eps = placement.placement(k1, k2, d1, d2, slo["d"], debug=config.DEBUG)
print("k1: {}\nk2: {}\nd1: {}\nd2: {}\nt: {}\n".format(k1, k2, d1, d2, slo["d"], eps))
print("Number of resource nodes: {}".format(len(p)))
print("Epsilon: {}".format(eps))
# save the placement to a file
name = "{}_{}_{}".format(s["name"], slo["type"], slo["d"])
save_placement(name, p)
# save the placement as a picture
save_placement_picture(name, k1, k2, d1, d2, slo["d"], p)
print("=" * config.TERM_SIZE)