Created
December 17, 2022 18:08
-
-
Save jpasquier/52e561625da9697067968804f13da49d 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
#!/bin/python3 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from multiprocessing import Pool, cpu_count | |
np.random.seed(666) | |
# Function that calculates (by simulation) the overflow probability of the | |
# system | |
def prob( | |
n = 80, # Number of employees likely to clock out during the rush period | |
p = 300, # Duration of rush period (seconds) | |
d = 5, # Time required for clocking out (seconds) | |
k = 16, # Number of time clocks | |
s = 10**5 # Number of simulations | |
): | |
# Each employee arrives randomly in the rush period according to a uniform | |
# distribution. The clocking out times are stored in a list of arrays. | |
t = [np.random.uniform(low=0, high=p, size=n) for i in range(s)] | |
# Sort each array of the clocking out time list (sorting by time for each | |
# simulation) | |
t = [np.sort(x) for x in t] | |
# For each employee sorted in the order of arrival, we calculate the time | |
# difference with the employee with k more ranks in the order of arrival. | |
# If one of these time differences is less than the time needed to clocking | |
# out, then the system is overflowed. | |
o = [[t[i][j + k] - t[i][j] <= d for j in range(n - k)] for i in range(s)] | |
# Proportion of overflowding | |
return np.mean([any(x) for x in o]) | |
# Probability of overflowding according to the number of devices. The other | |
# parameters are fixed. Default values are defined in the function. | |
def prob_k(k): | |
return prob(k=k) | |
devices = range(2, 17) | |
with Pool(processes=cpu_count()) as P: | |
probs = P.map(prob_k, devices) | |
# Figure | |
plt.plot(devices, probs) | |
plt.xlabel('Number of devices') | |
plt.ylabel('Probability of overflowding') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment