Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- seeds = []
- a_matrix = []
- final_matrix = []
- infection = []
- runSize = []
- def receive_input():
- global numNodes
- global firstNode
- input_file = open("icm_test2.txt")
- numNodes = int(input_file.readline().strip())
- firstNode = int(input_file.readline().strip())
- seededNodes = input_file.readline().strip()
- lastComma = -1
- while(not seededNodes.find(",", lastComma + 1) == -1):
- seeds.append(int(seededNodes[lastComma+1: seededNodes.index(",", lastComma+1)].strip())-firstNode)
- lastComma = seededNodes.index(",", lastComma + 1)
- seeds.append(int(seededNodes[lastComma+1:].strip())-firstNode)
- rounds = input_file.readline().strip()
- lastComma = -1
- while(not rounds.find(",", lastComma + 1) == -1):
- runSize.append(int(rounds[lastComma+1: rounds.index(",", lastComma+1)].strip()))
- lastComma = rounds.index(",", lastComma + 1)
- runSize.append(int(rounds[lastComma+1:].strip()))
- for i in range(numNodes):
- if i in seeds:
- infection.append(1)
- else:
- infection.append(0)
- rowArray = []
- for i in range(numNodes):
- rowArray.append(0)
- for j in range(numNodes):
- a_matrix.append(rowArray.copy())
- final_matrix.append(rowArray.copy())
- for line in input_file:
- edges = line.split(",")
- first = int(edges[0].strip())-firstNode
- second = int(edges[1].strip())-firstNode
- prob = float(edges[2].strip())
- a_matrix[first][second] = prob
- a_matrix[second][first] = prob
- final_matrix[first][second] = prob
- final_matrix[second][first] = prob
- def infect(node):
- column_counter = 0
- for weight in a_matrix[node]:
- if infection[node] == 1 and weight != 0:
- if flip_coin(weight):
- infection[column_counter] = 1
- a_matrix[node][column_counter] = 0
- column_counter += 1
- def flip_coin(weight):
- if random.random() < weight:
- return True
- return False
- def main():
- print("\n\n")
- receive_input()
- for rounds in range(len(runSize)):
- infection_sum = 0
- for run in range(runSize[rounds]):
- new_infection = []
- while new_infection != infection:
- new_infection = infection.copy()
- for i in range(len(a_matrix)):
- infect(i)
- for j in range(len(infection)):
- if infection[j] == 1:
- infection_sum += 1
- if not j in seeds:
- infection[j] = 0
- for k in final_matrix:
- a_matrix.pop(0)
- a_matrix.append(k.copy())
- avgInfected = infection_sum/runSize[rounds]
- print(str(runSize[rounds]) + " run average: " + str(format(avgInfected, '.3f')) + " nodes infection_sum, ", end = '')
- print("proportion = " + str(format((avgInfected/numNodes), '.3f')))
- print("\n\n")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement