Last active
June 27, 2018 22:39
-
-
Save mgraczyk/09e683827db2b8e1452fd14e30a2c521 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import sys | |
from random import randint | |
from math import ceil | |
def run_simulation(N, sims): | |
threshold = int(0.5 + 2. * N / 3) | |
print('{} / {}'.format(threshold, N)) | |
results = [] | |
for i in range(sims): | |
infections = [False] * N | |
infections[0] = True | |
n = 0 | |
while True: | |
num_infected = sum(infections) | |
if num_infected >= threshold: | |
results.append(n) | |
break | |
next_infections = infections.copy() | |
for node in range(N): | |
if infections[node]: | |
peer = randint(0, N - 2) | |
if peer >= node: | |
peer += 1 | |
next_infections[peer] = True | |
infections = next_infections | |
n += 1 | |
results = np.array(results) | |
print('Results from {} sims'.format(sims)) | |
print('Mean: {}'.format(results.mean())) | |
print('Min: {}'.format(results.min())) | |
print('Max: {}'.format(results.max())) | |
return results.mean() | |
if __name__ == '__main__': | |
N = int(sys.argv[1]) if len(sys.argv) > 1 else 50 | |
sims = int(sys.argv[2]) if len(sys.argv) > 1 else 100 | |
print('Running {} sims with N = {}'.format(sims, N)) | |
run_simulation(N, sims) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment