Advertisement
Guest User

Untitled

a guest
Oct 17th, 2014
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. import random
  2.  
  3. '''
  4. Parameters
  5. - number of nodes in network
  6. - number of nodes each node is subscribed to
  7.  
  8. Variants
  9. - change network topology (power law, preferential attachment)
  10. - change rules that individual nodes use to update state
  11. -- each node might only update each cycle with probability 20%
  12. - change information in state, such as adding surveys
  13. '''
  14.  
  15. class Node:
  16.  
  17. def __init__(self, node_id):
  18. self.node_id = node_id
  19. self.state = random.randint(1,2)
  20. self.subscribed = []
  21. pass
  22.  
  23. def ConnectToRandomNodes(self, node_max, connection_count):
  24. for idx in range(connection_count):
  25. i = -1
  26. while 1:
  27. i = random.randint(0,node_max-1)
  28. if i not in self.subscribed and i != self.node_id:
  29. #do not allow self connections and double connections
  30. break
  31. self.subscribed.append(i) #add i to list of nodes subscribed
  32.  
  33. def Update(self, nodes): # one round of consensus
  34. s1 = 0 #count of nodes in state 1, who are subscribed to
  35. s2 = 0 #count of nodes in state 2, who are subscribed to
  36. for i in self.subscribed:
  37. if nodes[i].state == 1:
  38. s1 += 1
  39. if nodes[i].state == 2:
  40. s2 += 1
  41.  
  42. #choose update rule
  43.  
  44. #look at subscribed peers, choose state based upon peer state
  45. if random.randint(1,s1+s2) <= s1:
  46. self.state = 1
  47. else:
  48. self.state = 2
  49.  
  50. #if more than 80% of peers are in agreement, snap to agreement
  51. if float(s1) / float(s1+s2) > 0.8:
  52. self.state = 1
  53. if float(s2) / float(s1+s2) > 0.8:
  54. self.state = 2
  55.  
  56. class Graph:
  57.  
  58.  
  59. def init(self, node_count):
  60. self.nodes = {}
  61. self.round_count = 0
  62. self.converged = False
  63.  
  64. #generate n nodes, connect them to each other
  65. for i in range(node_count):
  66. node_id = len(self.nodes)
  67. x = Node(node_id)
  68. #connect each node to 8 other nodes
  69. x.ConnectToRandomNodes(node_count, 12) #set node connections
  70. self.nodes[node_id] = x
  71.  
  72.  
  73. def round(self): #one round of consensus
  74. index = range(len(self.nodes))
  75. random.shuffle(index) #update nodes in random order
  76. for i in index:
  77. self.nodes[i].Update(self.nodes) #apply one round of consensus
  78. self.round_count += 1
  79.  
  80. def printReport(self):
  81. s1 = 0
  82. s2 = 0
  83. for i in range(len(self.nodes)):
  84. if self.nodes[i].state == 1:
  85. s1 += 1
  86. if self.nodes[i].state == 2:
  87. s2 += 1
  88. print("Round %d: node_count= %d, state1= %d, state2= %d" % (self.round_count, len(self.nodes), s1,s2))
  89. if (s1 == 0) or (s2 == 0):
  90. self.converged = True
  91.  
  92. node_count = 1280
  93. g = Graph()
  94. g.init(node_count)
  95.  
  96. g.printReport()
  97. while g.converged == False:
  98. g.round()
  99. g.printReport()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement