-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgvg.py
More file actions
400 lines (309 loc) · 11.5 KB
/
gvg.py
File metadata and controls
400 lines (309 loc) · 11.5 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import numpy as np
import time
from tqdm import tqdm
import scipy.sparse as sp
from scipy.sparse.csgraph import dijkstra
import math
from scipy.stats import multivariate_normal
import multiprocessing as mp
class GVG:
def __init__(self, W, R, scalar=True, logging=True, mode="exists"): # mode exists or all
self.W = W
self.R = R
self.mode = mode
self.logging = logging
self.tol = 1e-9
self.f_vis = self.check_vis_scalar if scalar else self.check_vis_vectoral
self.unweighted = self.is_unweighted()
self.symmetric = self.is_symmetric()
self.tau = 0.5
if self.mode == "fraction" and (self.tau <= 0 or self.tau > 1):
raise ValueError("For fractional visibility, tau must satisfy 0 < tau <= 1.")
self.build_neighbors()
def is_symmetric(self):
if sp.issparse(self.W):
return (self.W != self.W.T).nnz == 0
else:
return np.all(self.W == self.W.T)
def is_unweighted(self):
if sp.issparse(self.W):
M = self.W.data
return np.all((M == 1) | (M == 0))
else:
return np.all((self.W == 1) | (self.W == 0))
def build_neighbors(self):
if sp.issparse(self.W):
W = self.W.tocsr()
self.neigh = []
self.wts = []
for u in range(W.shape[0]):
cols = W.indices[W.indptr[u]:W.indptr[u+1]]
vals = W.data[W.indptr[u]:W.indptr[u+1]]
self.neigh.append(cols)
self.wts.append(vals)
else:
W = np.asarray(self.W)
self.neigh = []
self.wts = []
for u in range(W.shape[0]):
cols = np.where(W[u] != 0)[0]
self.neigh.append(cols)
self.wts.append(W[u, cols])
def comp_dijkstra(self):
dist = dijkstra(
self.W,
unweighted=self.unweighted,
return_predecessors=False,
limit=self.R
)
return dist
def predec(self, dist_row):
n = len(dist_row)
preds = [[] for _ in range(n)]
d = dist_row
for u in range(n):
du = d[u]
if not np.isfinite(du):
continue
for idx, v in enumerate(self.neigh[u]):
dv = d[v]
if not np.isfinite(dv):
continue
w = 1.0 if self.unweighted else self.wts[u][idx]
if abs((du + w) - dv) <= self.tol:
preds[v].append(u)
return preds
def check_vis_scalar(self, x, path, d_path):
# d_path is accumulated distance along the path
# x is vector in R^n
if len(path) <= 2:
return True
X_s = x[path[0]]
X_t = x[path[-1]]
d_st = d_path[-1] - d_path[0]
for u in range(1, len(path) - 1):
X_u = x[path[u]]
d_su = d_path[u] - d_path[0] # dist up to node u from start s
a_su = d_su / d_st
L = (1 - a_su) * X_s + a_su * X_t
if X_u >= L:
return False
return True
def check_vis_vectoral(self, x, path, d_path, eps=1e-12):
if len(path) <= 2:
return True
s = path[0]
t = path[-1]
X_s = x[s]
X_t = x[t]
d_st = d_path[-1] - d_path[0]
if d_st <= 0:
return True
X_ss = np.linalg.norm(X_s)
if X_ss <= eps:
return False
P_s = X_ss
P_t = np.dot(X_s, X_t) / X_ss
for u in range(1, len(path) - 1):
node_u = path[u]
X_u = x[node_u]
P_u = np.dot(X_s, X_u) / X_ss
d_su = d_path[u] - d_path[0]
a_su = d_su / d_st
L = (1.0 - a_su) * P_s + a_su * P_t
if P_u >= L:
return False
return True
def soft_visibility(self, s, t, preds, x, dist_row):
# return True immediately if we find ONE shortest path that is visible
path_rev = [t]
def dfs(v):
if v == s:
path = list(reversed(path_rev))
d_path = [dist_row[w] for w in path]
return self.f_vis(x, path, d_path)
if len(preds[v]) == 0:
return False
for u in preds[v]:
path_rev.append(u)
ok = dfs(u)
path_rev.pop()
if ok:
return True
return False
return dfs(t)
def strict_visibility(self, s, t, preds, x, dist_row):
# return False immediately if we find ANY shortest path that is NOT visible
path_rev = [t]
def dfs(v):
if v == s:
path = list(reversed(path_rev))
d_path = [dist_row[w] for w in path]
return self.f_vis(x, path, d_path)
if len(preds[v]) == 0:
return False
for u in preds[v]:
path_rev.append(u)
ok = dfs(u)
path_rev.pop()
if not ok:
return False
return True
return dfs(t)
def frac_visibility(self, s, t, preds, x, dist_row):
# return True immediately if we accumulate at least ceil[tau * M_st] number of paths that are visible
M_st, _ = self.path_cnt(s, t, preds)
if M_st <= 0:
return False
thr = int(math.ceil(self.tau * M_st))
path_rev = [t]
acc_vis = 0
def dfs(v):
nonlocal acc_vis
if v == s:
path = list(reversed(path_rev))
d_path = [dist_row[w] for w in path]
ok = self.f_vis(x, path, d_path)
if ok:
acc_vis += 1
if acc_vis >= thr:
return True
return False
if len(preds[v]) == 0:
return False
for u in preds[v]:
path_rev.append(u)
hit = dfs(u)
path_rev.pop()
if hit:
return True
return False
return dfs(t)
def path_cnt(self, s, t, preds, cap=10**8):
n = len(preds)
cnt = -np.ones(n, dtype=np.int64)
hop = -np.ones(n, dtype=np.int64)
def dfs(v):
if v == s:
return 1, 0
if cnt[v] >= 0:
return cnt[v], hop[v]
if len(preds[v]) == 0:
c, h = 0, 10**8
else:
total = 0
best_h = 10**8
for u in preds[v]:
cu, hu = dfs(u)
total += cu
if total >= cap:
total = cap
if hu + 1 < best_h:
best_h = hu + 1
c, h = total, best_h
cnt[v] = c
hop[v] = h
return c, h
return dfs(t)
def build(self, x):
start_time = time.time()
N = len(x)
G_v = np.zeros((N, N), dtype=np.int8)
dist = self.comp_dijkstra()
for s in tqdm(range(N), desc="Visibility Computation", disable=not self.logging):
dist_row = dist[s, :]
preds = self.predec(dist_row)
targets = np.where(np.isfinite(dist_row) & (dist_row > 0) & (dist_row <= self.R))[0]
for t in targets:
if len(preds[t]) == 0:
continue
if self.mode == "exists":
ok = self.soft_visibility(s, t, preds, x, dist_row)
elif self.mode == "all":
ok = self.strict_visibility(s, t, preds, x, dist_row)
elif self.mode == "fraction":
ok = self.frac_visibility(s, t, preds, x, dist_row)
else:
raise ValueError("mode must be 'exists' or 'all'")
G_v[s, t] = 1 if ok else 0
if self.logging:
print("Unweighted:", self.unweighted, "| symmetric:", self.symmetric, "| mode:", self.mode)
print(f"Total time: {time.time() - start_time:.2f}s")
print("G_v is symmetric?:", np.all(G_v == G_v.T))
return G_v, dist
def build_parallel(self, x, n_jobs=16, chunk_size=50, return_dense=True):
start_time = time.time()
N = len(x)
dist = self.comp_dijkstra()
sources = np.arange(N, dtype=np.int32)
chunks = [sources[i:i+chunk_size] for i in range(0, N, chunk_size)]
try:
ctx = mp.get_context("fork")
except ValueError:
ctx = mp.get_context()
n_jobs = int(n_jobs)
if n_jobs <= 0:
n_jobs = 1
with ctx.Pool(
processes=n_jobs,
initializer=_init_worker,
initargs=(self, dist, x, N)
) as pool:
results = pool.map(_process_sources, chunks)
all_rows = []
all_cols = []
for r, c in results:
if r:
all_rows.append(np.asarray(r, dtype=np.int32))
all_cols.append(np.asarray(c, dtype=np.int32))
if len(all_rows) == 0:
G_sparse = sp.csr_matrix((N, N), dtype=np.int8)
else:
rr = np.concatenate(all_rows)
cc = np.concatenate(all_cols)
data = np.ones(rr.shape[0], dtype=np.int8)
G_sparse = sp.csr_matrix((data, (rr, cc)), shape=(N, N), dtype=np.int8)
if self.logging:
print(f"[build_parallel] jobs={n_jobs} chunk={chunk_size} time={time.time()-start_time:.2f}s")
diff = (G_sparse != G_sparse.T).nnz
print("[build_parallel] G_v symmetric?:", diff == 0)
if return_dense:
G_dense = G_sparse.toarray().astype(np.int8)
return G_dense, dist
else:
return G_sparse, dist
_GVG_obj = None
_DIST = None
_X = None
_N = None
def _init_worker(gvg_obj, dist, x, n):
global _GVG_obj, _DIST, _X, _N
_GVG_obj = gvg_obj
_DIST = dist
_X = x
_N = n
def _process_sources(s_list):
gvg = _GVG_obj
dist = _DIST
x = _X
rows = []
cols = []
for s in s_list:
dist_row = dist[s, :]
preds = gvg.predec(dist_row)
targets = np.where(np.isfinite(dist_row) & (dist_row > 0) & (dist_row <= gvg.R))[0]
for t in targets:
if len(preds[t]) == 0:
continue
if gvg.mode == "exists":
ok = gvg.soft_visibility(s, t, preds, x, dist_row)
elif gvg.mode == "all":
ok = gvg.strict_visibility(s, t, preds, x, dist_row)
elif gvg.mode == "fraction":
ok = gvg.frac_visibility(s, t, preds, x, dist_row)
else:
raise ValueError("mode must be 'exists', 'all', or 'fraction'")
if ok:
rows.append(s)
cols.append(t)
return rows, cols