Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. #JERZYMARTYNA
  2.  
  3. global Q_LIMIT mean_interarrival mean_service mean_delays_required
  4. global next_event_type num_custs_delayed num_events
  5. global area_server_status
  6.  
  7. % Input Parameters
  8. Q_LIMIT = 200;
  9. mean_interarrival = 1.0;
  10. mean_service = 0.5;
  11. mean_delays_required = 100;
  12. area_server_status=0; num_events = 2
  13. INIT; % calls initialization routine
  14.  
  15. % run the simulation while more delays are still required
  16. while(num_custs_delayed < mean_delays_required)
  17. TIMING;
  18. UPDATE;
  19. if(next_event_type==1)
  20. ARRIVE; % branch to arrival routine (function)
  21. elseif(next_event_type==2)
  22. DEPART; % branch to departure routine (function)
  23. end
  24. end
  25.  
  26. REPORT % call report generator function
  27. function INIT
  28. global mean_interarrival
  29. global num_custs_delayed num_in_q server_status
  30. global area_num_in_q time time_last_event time_next_event total_of_delays
  31. time=0.0;
  32. server_status=0;
  33. num_in_q = 0;
  34. time_last_event = 0.0;
  35. num_custs_delayed = 0;
  36. total_of_delays = 0.0;
  37. area_num_in_q = 0.0;
  38. time_next_event(1) = time+EXPON(mean_interarrival);
  39. time_next_event(2) = 1.0 * exp(30); %disp(['init'])
  40. end
  41.  
  42. function ARRIVE
  43. global Q_LIMIT mean_interarrival mean_service
  44. global num_custs_delayed num_in_q server_status
  45. global time time_arrival time_next_event total_of_delays
  46. time_next_event(1) = time + EXPON(mean_interarrival);
  47. % time
  48. % check to see whether server is busy
  49. if(server_status == 1) % server is busy
  50. num_in_q = num_in_q + 1; % increase the no. of customers in queue
  51. if (num_in_q) > Q_LIMIT
  52. disp(['num_in_q = ', num2str(num_in_q)]);
  53. disp(['Overflow of the array of time_arrival at ', num2str(time)]);
  54. pause
  55. end
  56. time_arrival(num_in_q) = time;
  57. else % server is idle
  58. delay = 0.0;
  59. total_of_delays = total_of_delays + delay;
  60. num_custs_delayed = num_custs_delayed + 1;
  61. server_status = 1;
  62. time_next_event(2) = time + EXPON(mean_service);
  63. end
  64. end
  65.  
  66. function DEPART
  67. global mean_service
  68. global num_custs_delayed num_in_q server_status
  69. global time time_arrival time_next_event total_of_delays
  70. if(num_in_q == 0) % queue is empty. Make the server idle and eliminate the departure event from consideration
  71. server_status = 0; % idle
  72. time_next_event(2) = 1.0 * exp(30);
  73. else % queue is not empty, so decrease the no. of customers in queue
  74. num_in_q = num_in_q - 1;
  75. delay = time - time_arrival(1);
  76. total_of_delays = total_of_delays + delay;
  77. num_custs_delayed = num_custs_delayed + 1; %???????
  78. time_next_event(2) = time + EXPON(mean_service);
  79. % move each customers's arrival time in queue up one place
  80. for i = 1:num_in_q
  81. time_arrival(i)=time_arrival(i+1);
  82. end
  83. end
  84. end
  85.  
  86. function e=EXPON(mean)
  87. u = rand(1);
  88. e = - mean * log(u);
  89. % uses the inverse transformation method to generate negativeexponential random numbers
  90. end
  91.  
  92. function TIMING
  93. global next_event_type num_events
  94. global time time_next_event
  95. min_time_next_event = 1.0*exp(29);
  96. next_event_type = 3; % determine the event type of the next event to occur
  97. for i=1:num_events
  98. if(time_next_event(i) < min_time_next_event)
  99. min_time_next_event = time_next_event(i);
  100. next_event_type = i;
  101. end;
  102. end
  103. if(next_event_type == 3)
  104. disp(['Event List Empty at Time ', num2str(time)]);
  105. end
  106. time = min_time_next_event;
  107. end
  108.  
  109. function UPDATE
  110. % Update area accumulated for time-average statistics
  111. global num_in_q server_status
  112. global area_num_in_q area_server_status time time_last_event
  113. time_since_last_event = time - time_last_event;
  114. time_last_event = time;
  115. area_num_in_q = area_num_in_q + num_in_q * time_since_last_event;
  116. area_server_status = area_server_status + server_status * time_since_last_event;
  117. end
  118.  
  119. function REPORT
  120. global num_custs_delayed
  121. global area_num_in_q area_server_status time total_of_delays
  122. disp(['Average Delay in Queue : ', num2str(total_of_delays/num_custs_delayed),' minutes']);
  123. disp(['Average number in Queue : ', num2str(area_num_in_q/time)])
  124. disp(['Service Utilization : ', num2str(area_server_status/time)]);
  125. disp(['Time Simulation Ended : ', num2str(time)]);
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement