-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathplacement.py
248 lines (188 loc) · 6.97 KB
/
placement.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#
# Copyright (c) Tobias Pfandzelter. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for details.
#
import typing
import numpy as np
import itertools
import tqdm
def weighted_lee(a: typing.Tuple[int, int], b: typing.Tuple[int, int], k1: int, k2: int, d1: float, d2: float) -> float:
return min((a[0] - b[0]) % k1, (b[0] - a[0]) % k1)*d1 + min((a[1] - b[1]) % k2, (b[1] - a[1]) % k2)*d2
def __placement(k1: int, k2: int, d1: float, d2: float, t: float, debug: bool = False) -> typing.Tuple[typing.List[typing.Tuple[int, int]], float]:
# calculate initial values
# t' is the hops on the modified torus
t_ = np.floor(t/d1)
if debug:
print("t':", t_)
# k2' is the vertical size of the modified torus
# k2'r is k2/k2'
k2_ = int(np.ceil( (d2 / d1) * k2))
k2_r = k2 / k2_
if debug:
print("k2':", k2_)
print("k2'r:", k2_r)
# now we have a torus of size k1*k2_ and perform a t_-hop placement on that.
# to convert y coordinates for resource nodes, transform ky_ back into y.
# build a torus of k*k so that k = 2(t_**2) + 2t_ + 1
k = int(2*(t_**2) + 2*t_ + 1)
if debug:
print("k:", k)
# construct a perfect t_-hop placement on the k*k torus
p = np.array(
[y for y in
[
[
int(i % k), int((2 * (t_**2) * i ) % k)
]
for i in np.arange(0, k)
]
]
)
# if debug:
# print("p:", p)
# use that to tile the x*ky_ torus
# we need np.ceil(x/k) times np.ceil(ky_/k) tiles, then we cut off some stuff
n = []
for i in range(int(np.ceil(k1/k))):
for j in range(int(np.ceil(k2_/k))):
for pl in p:
# convert ky_ back into y
# only if this is still in range, otherwise just discard
cx = (i * k) + pl[0]
cy = (j * k) + pl[1]
if cx < k1 and cy < k2_:
#print("add {} {} ({} {}) (i: {} j: {} k: {} pl:{})".format(cx, cy, cx, (j * k) + pl[1], i, j, k, pl))
n.append((cx, cy))
if debug:
print("n:", n)
# 6. because we cut off some resource nodes, we have some "uncovered" nodes at the edges.
# go through all the nodes
# one by one, add the nodes with the largest nodes until all are covered
# takes O(n^3) time (?) but should be fine
uncovered = set(itertools.product(range(k1), range(k2_)))
while len(uncovered) > 0:
max_dist = 0
max_node = None
for node in tqdm.tqdm(list(uncovered), disable=not debug):
min_dist = np.inf
for rn in n:
dist = weighted_lee(node, rn, k1, k2_, 1.0, 1.0)
if dist < min_dist:
min_dist = dist
if dist <= t_:
uncovered.remove(node)
break
if min_dist > t_ and min_dist > max_dist:
max_dist = min_dist
max_node = node
if max_node == None:
break
n.append(max_node)
uncovered.remove(max_node)
# convert the y coordinates back into the old k2
n = [(x, int(k2_r * y)) for x, y in n]
if debug:
print("n:", n)
#cy = int(np.floor(k2_r * ((j * k) + pl[1])))
# if debug:
# print("n:", n)
# figure out epsilon
eps = -np.inf
for node in itertools.product(range(k1), range(k2)):
# for node in itertools.product(range(k1), range(k2)):
min_dist = np.inf
for rn in n:
dist = weighted_lee(node, rn, k1, k2, d1, d2)
if dist < min_dist:
min_dist = dist
if min_dist-t > eps:
eps = min_dist-t
if min_dist > t:
if debug:
print("{}".format(min_dist - t))
if debug:
print("epsilon:", eps)
if debug:
print("n:", n)
if debug:
for node in itertools.product(range(k1), range(k2)):
# for node in itertools.product(range(k1), range(k2)):
min_dist = np.inf
for rn in n:
dist = weighted_lee(node, rn, k1, k2, d1, d2)
if dist < min_dist:
min_dist = dist
if min_dist > t + eps:
print("{}".format(min_dist - t))
raise ValueError("there is a min_dist > t!")
return n, eps
def placement(k1: int, k2: int, d1: float, d2: float, t: float, debug: bool = False) -> typing.Tuple[typing.List[typing.Tuple[int, int]], float]:
"""
Calculates a distance-t placement on a k1 x k2 2D torus with horizontal
weights d1 and vertical weights d2.
Parameters:
k1, k2: The dimensions of the torus.
d1, d2: The weights of thee torus.
t: The target distance limit
Returns:
A list of tuples, where each tuple contains the x and y coordinates of a
resource node.
"""
switch = d1 > d2
if switch:
k1, k2 = k2, k1
d1, d2 = d2, d1
if debug:
print("k1: {}, k2: {}, d1: {}, d2: {}, t: {}".format(k1, k2, d1, d2, t))
if d1 <= t and d2 <= t:
n, eps = __placement(k1, k2, d1, d2, t, debug)
if switch:
n = [(y, x) for x, y in n]
return n, eps
elif d1 <= t and d2 > t:
# tile with the k1 x 1 torus
dist = int(np.floor(t/d1)) * 2 + 1
n = [(x, 0) for x in range(0, k1, dist)]
if debug:
print("n:", n)
n_ = []
for i in range(k2):
for rn in n:
n_.append((rn[0], i))
n = n_
if switch:
n = [(y, x) for x, y in n]
return n, 0
else:
# every node is a resource node
n = [(x, y) for x, y in itertools.product(range(k1), range(k2))]
if switch:
n = [(y, x) for x, y in n]
return n, 0
if "__main__" == __name__:
np.random.seed(0)
for i in range(0, 20):
# make random x, y, dy, dx, t
x = np.random.randint(1, 50)
y = np.random.randint(1, 50)
dx = np.random.random() * 100 + 0.01
dy = np.random.random() * 100 + 0.01
t = np.random.random() * 100 + 0.01
# get a placement
n, eps = placement(x, y, dx, dy, t)
# make sure it is within t + epsilon
with tqdm.tqdm(total=x*y*len(n), desc="Checking error") as pbar:
for bx in range(0, x):
for by in range(0, y):
best_dist = np.inf
best_p = (np.inf, np.inf)
for i in range(0, len(n)):
p = n[i]
dist = weighted_lee(p, (bx, by), x, y, dx, dy)
if dist < best_dist:
best_dist = dist
best_p = p
pbar.update(1)
if best_dist > t + eps:
print(x, y, dx, dy, t, bx, by, best_p[0], best_p[1], best_dist)
exit(1)