# L-13 MCS 260 Wed 10 Feb 2010 : simuwait.py # simulate processing of jobs to compute waiting times def form(L): """ Given in L a list of floats, returns a string that contains the floats in %.2f format. """ s = "[ " + '%.2f' % L[0] for i in range(1,len(L)): s += ', ' + '%.2f' % L[i] return s + ' ]' def ask(): """ Prompts the user for the parameters of the simulation: n : the number of jobs, T : the length of time, mu : the average time per job, sg : the deviation in procesing times. Returns the tuple (n,T,mu,sg). """ n = input('number of jobs : ') T = input('length of time : ') mu = input(' mean time/job : ') sg = input(' deviation/job : ') return (n,T,mu,sg) def make_jobs(n,T,mu,sg): """ Given the parameters of the simulation, returns tuple of two lists with arrival and process times for each job. """ A = [] # arrival times of jobs P = [] # time to process each job import random for i in range(0,n): A.append(random.uniform(0,T)) P.append(random.gauss(mu,sg)) A.sort() return (A,P) def simulate(A,P): """ Given a list of arrivals and process times, returns a list of wait times for each job. """ W = [0] # no wait for first job b = P[0] # time busy for job for i in range(1,len(A)): t = A[i] - A[i-1] # elapsed time if t >= b: # idle printer b = 0 # no wait else: # busy printer b = b - t # wait W.append(b) # store wait b = b + P[i] # update busy return W def avgdev(W): """ Returns a tuple with the average and standard deviation of the numbers in W. """ import math avg = sum(W)/len(W) dev = 0 for i in range(0,len(W)): dev += (W[i] - avg)**2 dev = math.sqrt(dev) return (avg,dev) def main(): (n,T,mu,sg) = ask() (A,P) = make_jobs(n,T,mu,sg) print ' arrivals : ' + form(A) print 'job times : ' + form(P) W = simulate(A,P) print 'wait times : ' + form(W) (a,d) = avgdev(W) print 'average wait : %.2f' % a print ' deviation : %.2f' % d main()