Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import random
  2.  
  3. seeds = []
  4. a_matrix = []
  5. final_matrix = []
  6. infection = []
  7. runSize = []
  8.  
  9. def receive_input():
  10. global numNodes
  11. global firstNode
  12.  
  13. input_file = open("icm_test2.txt")
  14. numNodes = int(input_file.readline().strip())
  15. firstNode = int(input_file.readline().strip())
  16.  
  17. seededNodes = input_file.readline().strip()
  18. lastComma = -1
  19. while(not seededNodes.find(",", lastComma + 1) == -1):
  20. seeds.append(int(seededNodes[lastComma+1: seededNodes.index(",", lastComma+1)].strip())-firstNode)
  21. lastComma = seededNodes.index(",", lastComma + 1)
  22. seeds.append(int(seededNodes[lastComma+1:].strip())-firstNode)
  23.  
  24. rounds = input_file.readline().strip()
  25. lastComma = -1
  26. while(not rounds.find(",", lastComma + 1) == -1):
  27. runSize.append(int(rounds[lastComma+1: rounds.index(",", lastComma+1)].strip()))
  28. lastComma = rounds.index(",", lastComma + 1)
  29. runSize.append(int(rounds[lastComma+1:].strip()))
  30.  
  31. for i in range(numNodes):
  32. if i in seeds:
  33. infection.append(1)
  34. else:
  35. infection.append(0)
  36.  
  37. rowArray = []
  38. for i in range(numNodes):
  39. rowArray.append(0)
  40. for j in range(numNodes):
  41. a_matrix.append(rowArray.copy())
  42. final_matrix.append(rowArray.copy())
  43.  
  44. for line in input_file:
  45. edges = line.split(",")
  46. first = int(edges[0].strip())-firstNode
  47. second = int(edges[1].strip())-firstNode
  48. prob = float(edges[2].strip())
  49.  
  50. a_matrix[first][second] = prob
  51. a_matrix[second][first] = prob
  52.  
  53. final_matrix[first][second] = prob
  54. final_matrix[second][first] = prob
  55.  
  56. def infect(node):
  57. column_counter = 0
  58. for weight in a_matrix[node]:
  59. if infection[node] == 1 and weight != 0:
  60. if flip_coin(weight):
  61. infection[column_counter] = 1
  62. a_matrix[node][column_counter] = 0
  63. column_counter += 1
  64.  
  65.  
  66. def flip_coin(weight):
  67. if random.random() < weight:
  68. return True
  69. return False
  70.  
  71. def main():
  72. print("\n\n")
  73.  
  74. receive_input()
  75. for rounds in range(len(runSize)):
  76. infection_sum = 0
  77. for run in range(runSize[rounds]):
  78. new_infection = []
  79. while new_infection != infection:
  80. new_infection = infection.copy()
  81. for i in range(len(a_matrix)):
  82. infect(i)
  83.  
  84. for j in range(len(infection)):
  85. if infection[j] == 1:
  86. infection_sum += 1
  87. if not j in seeds:
  88. infection[j] = 0
  89.  
  90. for k in final_matrix:
  91. a_matrix.pop(0)
  92. a_matrix.append(k.copy())
  93.  
  94. avgInfected = infection_sum/runSize[rounds]
  95. print(str(runSize[rounds]) + " run average: " + str(format(avgInfected, '.3f')) + " nodes infection_sum, ", end = '')
  96. print("proportion = " + str(format((avgInfected/numNodes), '.3f')))
  97.  
  98. print("\n\n")
  99.  
  100. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement