Advertisement
Guest User

Untitled

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