-
Notifications
You must be signed in to change notification settings - Fork 0
/
decentral_search.py
56 lines (47 loc) · 1.48 KB
/
decentral_search.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
import dist_utils;
import math;
import hnetwork;
import random;
# returns the number of steps used in a decentralied search:
# S searcher (with next_hop function)
# N network (with dist function + graph G)
# s start node
# t goal node
# p probability at each step of dropped message
def search(S, N, s, t, p):
steps = 0;
while s != t:
s = S.next_hop(N, s, t);
steps = steps + 1;
if random.random() < p:
return float("inf");
return steps;
class SoftmaxDecentralizedSearch:
def __init__(self, alpha):
self.alpha = alpha;
# determines the next hop in a decentralied search:
# N network (with dist + graph)
# s start node
# t goal node
def next_hop(self, N, s, t):
D = dict();
for v in N.G.neighbors(s):
D[v] = -self.alpha * N.dist(v, t);
P = dist_utils.softmax_dist(D);
return dist_utils.random_item(P);
class PerfectDecentralizedSearch:
# determines the next hop in a decentralied search:
# N network (with dist + graph)
# s start node
# t goal node
def next_hop(self, N, s, t):
min_v = [];
min_d = float("inf");
for v in N.G.neighbors(s):
d = N.dist(v, t);
if d < min_d:
min_v = [v];
min_d = d;
elif d == min_d:
min_v.append(v);
return random.choice(min_v);