Programming Assignment 1

docx

School

New Jersey Institute Of Technology *

*We aren’t endorsed by this school

Course

610

Subject

Industrial Engineering

Date

Jan 9, 2024

Type

docx

Pages

4

Uploaded by MasterLemur1918

Report
Programming Assignment 1 The purpose of this homework is to run a simulation of different queuing policies, and observe their impact on average waiting time. Imagine that we have five interchangeable service stations (airline check-in counters; store check-out counters; DMV service windows; immigration posts in an international airport; etc). Arriving passengers (or clients or citizens or visitors) are lined up in FIFO queues, and you have to decide how to organize these queues and associated queuing policies: Option 1: all arriving passengers are placed in a single queue, and service stations take passengers from the front of that queue. Option 2: each service station has its own queue, and arriving passengers are dispatched to a queue according to one of many policies: 2.A: round robin (1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, ...). 2B: arriving passenger is placed in a shortest queue. 2.C: arriving passenger is placed in a random queue. Inputs to the simulation: The duration of the simulation measured in minutes (D: make it arbitrarily long, do not worry about it being or not being realistic). The average arrival rate measured in minutes (A: arrivals are random, but on average there is one new passenger every A minutes), The average service rate measured in minutes (S: service rates are random, but on average they need about S minutes of service). For the sake of this study, make sure to crowd the system, by choosing S >> 5*A, without causing an overflow of your queues. Also choose D to be long enough to get rid of any transitory effects. Outputs of the Simulation for each queuing policy: The duration of the simulation (which may be longer than the input parameter, as when check-in closes, there may be passengers in the waiting queues and service stations). The maximum length of the queue for each queue. The average and maximum waiting time for each queue. The rate of occupancy of each service station (percentage of time each station was busy). If you want: show the real-time evolution of the queues during the run-time simulation. Code( Python): import random import matplotlib.pyplot as plt
class Pro_Ass: Queue_init = [] Station_init = [] def __init__ ( self ) -> None : self .Station_init = [ 0 for i in range ( 5 )] def add_Service_job ( self , job_time): pass def get_service_job ( self , num_Station) -> int : pass def Ser_Time ( self ): for i in range ( len ( self .Station_init)): if self .Station_init[i]: self .Station_init[i] -= 1 # Pull from the queue when station is empty if self .Station_init[i] == 0 and not self .is_empty(): job = self .get_service_job(i) if job: self .Station_init[i] = job def is_empty ( self ) -> bool : for q in range ( len ( self .Queue_init)): if len ( self .Queue_init[q]): # Find if something in Queue_init return False return True #SingleQueue class SingleQueue(Pro_Ass): def __init__ ( self ): super (). __init__ () self .Queue_init = [[]] def add_Service_job ( self , job_time): self .Queue_init[ 0 ].insert( 0 , job_time) def get_service_job ( self , num_Station) -> int : return self .Queue_init[ 0 ].pop() #RoundRobinQueue class RoundRobinQueue(Pro_Ass): RR_num = 0 def __init__ ( self ): super (). __init__ () self .Queue_init = [[] for i in range ( 5 )] def add_Service_job ( self , job_time): self .Queue_init[ self .RR_num].insert( 0 , job_time) self .RR_num = ( self .RR_num + 1 ) % 5 def get_service_job ( self , num_Station) -> int : if len ( self .Queue_init[num_Station]): return self .Queue_init[num_Station].pop()
return 0 #ShortestQueue class ShortestQueue(Pro_Ass): def __init__ ( self ): super (). __init__ () self .Queue_init = [[] for i in range ( 5 )] def add_Service_job ( self , job_time): s_idx = self .Queue_init.index( min ( self .Queue_init)) self .Queue_init[s_idx].insert( 0 , job_time) def get_service_job ( self , num_Station) -> int : if len ( self .Queue_init[num_Station]): return self .Queue_init[num_Station].pop() return 0 # RandomQueue class RandomQueue(Pro_Ass): def __init__ ( self ): super (). __init__ () self .Queue_init = [[] for i in range ( 5 )] def add_Service_job ( self , job_time): s_idx = random.randrange( 0 , len ( self .Queue_init)) self .Queue_init[s_idx].insert( 0 , job_time) def get_service_job ( self , num_Station) -> int : if len ( self .Queue_init[num_Station]): return self .Queue_init[num_Station].pop() return 0 def simulation (A: int , S: int , D: int , test: Pro_Ass) -> ( list , Pro_Ass): stat = { 'size' : [[] for i in range ( len (test.Queue_init))] , 'Time_count' : 0 , 'busy' : [[] for i in range ( len (test.Station_init))] , 'Average_time' : None } while D or not test.is_empty(): if random.randint( 1 , A) == A and D: test.add_Service_job(random.randint( 1 , S)) test.Ser_Time() if D > 0 : D -= 1 stat[ 'Time_count' ] += 1 for q in range ( len (test.Queue_init)): stat[ 'size' ][q].append( len (test.Queue_init[q])) for s in range ( len (test.Station_init)): if test.Station_init[s] > 0 : stat[ 'busy' ][s].append( 1 )
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Totalitems_in_system = 0 for x in range ( len (stat[ 'size' ])): items = 0 for y in range ( len (stat[ 'size' ][x])): items += stat[ 'size' ][x][y] Totalitems_in_system += items stat[ 'Average_time' ] = Totalitems_in_system / stat[ 'Time_count' ] / A return stat , test if __name__ == "__main__" : # initialize parameters A = 4 S = 8 * A D = 10000 stats = { 'single_Queue' : {} , 'Round_robin' : {} , 'Short_Queue' : {} } single_Queue = SingleQueue() stat , _ = simulation(A , S , D , single_Queue) stats[ 'single_Queue' ] = stat for q in range ( len (stats[ 'single_Queue' ][ 'size' ])): ts = plt.plot(stats[ 'single_Queue' ][ 'size' ][q] , label = 'Queue %d' % q) print ( "Single Queue" , stat[ 'Average_time' ]) Round_robin = RoundRobinQueue() stat , _ = simulation(A , S , D , Round_robin) stats[ 'Round_robin' ] = stat for q in range ( len (stats[ 'Round_robin' ][ 'size' ])): ts = plt.plot(stats[ 'Round_robin' ][ 'size' ][q] , label = 'Queue %d' % q) print ( "RoundRobin Queue" , stat[ 'Average_time' ]) Short_Queue = ShortestQueue() stat , _ = simulation(A , S , D , Short_Queue) stats[ 'Short_Queue' ] = stat for q in range ( len (stats[ 'Short_Queue' ][ 'size' ])): ts = plt.plot(stats[ 'Short_Queue' ][ 'size' ][q] , label = 'Queue %d' % q) print ( "Shortest Queue" , stat[ 'Average_time' ]) Random_Queue = RandomQueue() stat , _ = simulation(A , S , D , Random_Queue) stats[ 'Random_Queue' ] = stat for q in range ( len (stats[ 'Random_Queue' ][ 'size' ])): ts = plt.plot(stats[ 'Random_Queue' ][ 'size' ][q] , label = 'Queue %d' % q) print ( "Random Queue" , stat[ 'Average_time' ])