######################################################################## ######################################################################## # # Queue.py # Simulate a queue where people arrive randomly with an average time of # X seconds between people and are dealt with in Y seconds on average # Use a loop with a 1 second step in the simulation # # Print the maximum wait that any one of 1000 simulated people had # Note this solution uses numpy to generate distrubibutions, you can # also "math" functions and "random" functions (import math,random) # ######################################################################## ######################################################################## X = 10 # starting value, can be adjusted Y = 7 # starting value, can be adjusted nPerson = 1000 # number of people to simulate, stop when all dealt with import numpy as np def timeToNext(beta): """Return time to next person, input is beta = 1/(average arrival rate)""" # functional form of time to next person if ac is the average arrival time # is p(t) = 1/beta * exp(- t / beta ) # where beta = beta # np.random.exponential uses scale = beta # https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.random.exponential.html ranExp = np.random.exponential(scale = beta, size=1) return int(ranExp) # note forcing integer times here to simplify def addAPerson(t,q): """ Function to add a person to the queue with average time between people = X""" # t = time added # q = your queue pass # write your function here def removeAPerson(t,q): """Function to remove a person how has been dealt with, return time they waited""" # t = time removed # q = your queue return 0 #your function here, return time waited def arriveTimesArray(n,dt): """Return a numpy array of integers, which are the times each person will arrive""" return 0 # return the array here def timerLoop(X,Y): """Run a loop to add and remove people as required""" arriveTimes = arriveTimesArray(nPerson,X) # queue q = [] # waied times wait = [] ## loop over the times from 0 -> ... in steps of 1s ## until there are no people to arrive and noone in the queue ## store the time each person waited in the "wait" array as an integer # # np.max is the maximum of an array, np.mean is the average print("Max time waited was {}s average {}s".format(np.max(wait),np.mean(wait))) timerLoop(X,Y) print("\nOnce this works try rerunning with different arrival times and processing times\ntimerLoop(10,7) # default values")