Package datk :: Package core :: Module networks
[hide private]
[frames] | no frames]

Source Code for Module datk.core.networks

 1  from distalgs import Network 
 2  import math 
 3  import random 
 4  import pdb 
 5   
6 -class Unidirectional_Ring(Network):
7 """A Network of n Processes arranged in a ring. Each edge is directed 8 from a Process to its clockwise neighbor, that is, messages can 9 only be sent in a clockwise direction."""
10 - def __init__(self, n, index_to_UID = None):
11 Network.__init__(self, n, index_to_UID) 12 for i in range(n): 13 self[i].link_to(self[(i+1)%n])
14
15 -class Bidirectional_Ring(Network):
16 """A Network of n Processes arranged in a ring. Each edge between a Process 17 and its neighbor is undirected, that is, messages can be sent in both 18 the clockwise and the counterclockwise directions."""
19 - def __init__(self, n, index_to_UID = None):
20 Network.__init__(self, n, index_to_UID) 21 for i in range(n): 22 self[i].link_to(self[(i+1)%n]) 23 self[i].link_to(self[(i-1)%n])
24
25 -class Unidirectional_Line(Network):
26 """A Network of n Processes arranged in a line. Each edge is directed 27 from a Process to its clockwise neighbor, that is, messages can 28 only be sent in a clockwise direction."""
29 - def __init__(self, n, index_to_UID = None):
30 Network.__init__(self, n, index_to_UID) 31 for i in range(n-1): 32 self[i].link_to(self[(i+1)])
33
34 -class Bidirectional_Line(Network):
35 """A Network of n Processes arranged in a line. Each edge between a Process 36 and its neighbor is undirected, that is, messages can be sent in both 37 the clockwise and the counterclockwise directions."""
38 - def __init__(self, n, index_to_UID = None):
39 Network.__init__(self, n, index_to_UID) 40 for i in range(n-1): 41 self[i].bi_link(self[(i+1)])
42
43 -class Complete_Graph(Network):
44 """A Network of n Processes arranged at the vertices of a Complete undirected 45 graph of size n."""
46 - def __init__(self, n, index_to_UID = None):
47 Network.__init__(self, n, index_to_UID) 48 for i in range(n-1): 49 for j in range(i+1,n): 50 self[i].bi_link(self[j])
51
52 -class Random_Line_Network(Network):
53 """A Network of n processes arranged randomly at the vertices of a connected 54 undirected line graph of size n. Additional pairs of vertices are connected 55 at random with a probability that is inversely proportional to the difference 56 in their positions on the line. 57 58 For example, the Process at index 3 is guaranteed to be connected to the Process 59 at index 4, and is more likely to be connected to the Process at index 5 than to 60 the Process at index 8. """
61 - def __init__(self, n, sparsity = 1):
62 """ 63 sparsity = 0 --> a Complete_Graph(n) 64 sparsity = infinity --> a Bidirectional_Line(n) 65 """ 66 Network.__init__(self, n) 67 def sigmoid(t): 68 if t > 100: return 1. 69 if t < -100: return 0. 70 return 1./(1.+math.exp(-t))
71 72 for i in range(n-1): 73 self[i].bi_link(self[(i+1)]) 74 for j in range(i+2, n): 75 if random.random() < sigmoid( -((i-j)**2)/(float(n)**0.5)*sparsity) *2.: 76 self[i].bi_link(self[j])
77