Advertisement
makispaiktis

Queue Theory

Jul 22nd, 2024
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. def factorial(n):
  2.     fact = 1
  3.     for i in range(1, n + 1):
  4.         fact *= i
  5.     return fact
  6.  
  7.  
  8. # Explanation of Kendal Models
  9. print("******** Kendal Models 1/2/3/4/5 ********\n")
  10. print("1 = {M, G}  ------> Packets Arrival Distribution")
  11. print("2 = {M, G, D}  ---> Service Time Distribution")
  12. print("3 = {1 .... N} ---> Number of serving people (like waiters)")
  13. print("4 = {1 ... +oo} --> Number of clients/sources from where the load comes from")
  14. print("5 = {1 ... +oo} --> Number of clients in the system (being served + waiting to be served)")
  15.  
  16.  
  17.  
  18. # 1. M/M/1 Model - Example with: lamda=2, mu=5
  19. print("M/M/1 = 1 server, infinite external sources, infinite queue")
  20. lamda = float(input("Give me arrival rate (lambda): "))
  21. mu = float(input("Give me service rate (mu): "))
  22. rho = lamda / mu
  23. P0 = 1 - rho
  24. print()
  25.  
  26. print(f"Traffic load = rho = lamda / mu = {rho:.2f}")
  27. print(f"Probability of 0 clients in the system = P(0) = 1 - rho = {P0:.2f}")
  28. print(f"Probability of n clients in the system = P(n) = P(0)*rho^n = {P0:.2f}*{rho:.2f}^n\n")
  29.  
  30. N = lamda / (mu - lamda)
  31. Nq = rho**2 / (1 - rho)
  32. Ns = N - Nq
  33. print(f"Average number of clients (total)  in the system = N  = {N:.2f}")
  34. print(f"Average number of clients (queue)  in the system = Nq = {Nq:.2f}")
  35. print(f"Average number of clients (served) in the system = Ns = {Ns:.2f}\n")
  36.  
  37. T = 1 / (mu - lamda)
  38. Ts = 1 / mu
  39. Tq = T - Ts
  40. print(f"Average total delay time in the system   = T = {T:.2f}")
  41. print(f"Average time of service in the system    = Ts = {Ts:.2f}")
  42. print(f"Average waiting time in the system queue = Tq = {Tq:.2f}\n")
  43.  
  44.  
  45.  
  46. # 2. M/M/1/oo/N - Example with: lamda=2, mu=5, N=3
  47. def p(n, P0, rho):
  48.     return P0 * rho**n
  49.  
  50. print("M/M/1/oo/N = 1 server, infinite external sources, finite queue")
  51. lamda = float(input("Give me arrival rate (lambda): "))
  52. mu = float(input("Give me service rate (mu): "))
  53. N = int(input("Give me the number of total seats available (N): "))
  54. rho = lamda / mu
  55. P0 = (1 - rho) / 1 - rho**(N+1)
  56. P_blockage = p(N, P0, rho)
  57. print()
  58.  
  59. print(f"Traffic load = rho = lamda / mu = {rho:.2f}")
  60. print(f"Probability of blockage = P(blockage) = P(N) = P({N}) = {100*P_blockage:.2f}%\n")
  61. print(f"For 1 server and {N} total seats:\n")
  62. for i in range(0, N + 1):
  63.     print(f"P({i}) = {100*p(i, P0, rho):.2f}%")
  64.  
  65.  
  66.  
  67.  
  68. # 3. M/M/N/oo/N (Erlang B) - Example with lamda=2, mu=1, N=5
  69. def prob_erlang_b(n, N, rho):
  70.     a = rho**n / factorial(n)
  71.     b = sum([rho**i / factorial(i) for i in range(0, N + 1)])
  72.     return a / b
  73.  
  74. print("M/M/N/oo/N = N servers, infinite external sources, N seats ---> no queue, non-zero blockage probability")
  75. lamda = float(input("Give me arrival rate (lambda): "))
  76. mu = float(input("Give me service rate (mu): "))
  77. N = int(input("Give me the number of servers = number of total seats (N): "))
  78. rho = lamda / mu
  79. P_blockage = prob_erlang_b(N, N, rho)
  80. print()
  81.  
  82. print(f"Traffic load = rho = lamda / mu = {rho:.2f}")
  83. print(f"Probability of blockage = P(blockage) = P(N) = P({N}) = {100*P_blockage:.2f}%\n")
  84. print(f"For rho={rho} and N={N} servers-total seats, we have: ")
  85. for i in range(0, N + 1):
  86.     print(f"P({i}) = {100*prob_erlang_b(i, N, rho):.2f}%")
  87. print(f"P({N+1}) = P({N+2}) = .... = 0.00% (there is no queue)\n")
  88.  
  89. T_avg = 1 / mu
  90. N_avg = rho * (1 - P_blockage)
  91. print(f"Average waiting time in the system queue = T_avg = {T_avg:.2f}")
  92. print(f"Average number of clients in the system  = N_avg = {N_avg:.2f}\n")
  93. print("\n\n\n")
  94.  
  95.  
  96.  
  97.  
  98.  
  99. import matplotlib.pyplot as plt
  100. print(f"Blockage probability: Searching for different values of N (not only for N={N})")
  101. P_blockage_list = list()
  102. N_list = list(range(1, 21))
  103. for N in N_list:
  104.     P_blockage_list.append(prob_erlang_b(N, N, rho))
  105.  
  106. d = {N_list[i] : f"{100*P_blockage_list[i]:.2f}%" for i in range(len(N_list))}
  107. print(d, "\n\n")
  108. plt.plot(N_list, P_blockage_list)
  109. plt.plot(N_list, P_blockage_list, 'ro')
  110. plt.grid(True)
  111. plt.title(f"Blockage probability (rho={rho:.2f})")
  112. plt.xlabel("Number of servers [N]")
  113. plt.ylabel("Blockage probability [%]")
  114. plt.show()
  115.  
  116.  
  117.  
  118. # 4. M/M/N/oo/oo (Erlang C) - Example with: lamda=6, mu=3, N=5
  119. print("M/M/N/oo/oo = N servers, infinite external sources, infinite queue")
  120. print("Rule: rho < 1 <----> lamda / mu*N < 1")
  121. lamda = float(input("Give me arrival rate (lambda): "))
  122. mu = float(input("Give me service rate (mu): "))
  123. N = int(input("Give me the number of servers (N): "))
  124. rho = lamda / mu
  125. density_traffic_load = rho / N
  126. print()
  127.  
  128. print(f"Traffic load = rho = lamda / mu = {rho:.2f}")
  129. print(f"Density of traffic load = rho = lamda / mu = {density_traffic_load:.2f}")
  130.  
  131. P_blockage_erlang_B = prob_erlang_b(N, N, rho)
  132. P_T = (N*P_blockage_erlang_B) / (N - rho*(1-P_blockage_erlang_B))
  133. print(f"Probability of request delay = P_T = {100*P_T:.2f}%\n")
  134.  
  135. Nq = rho * P_T / (N - rho)
  136. Ns = rho * N
  137. N_sys = Nq + Ns
  138. T_avg = 1 / mu + Nq / lamda
  139. print(f"Average number of clients in the system =       N_sys = {N_sys:.2f}")
  140. print(f"Average number of clients (queue)  in the system = Nq = {Nq:.2f}")
  141. print(f"Average number of clients (served) in the system = Ns = {Ns:.2f}\n")
  142. print(f"Average delay time in system = T_avg = {T_avg:.2f}")
  143.  
  144.  
  145.  
  146. # 5. M/M/N/M/N (Engset) - Example with: lamda=6, mu=3, N=5, M=10
  147. def c(n, k):
  148.     return factorial(n) / (factorial(k) * factorial(n - k))
  149.  
  150. print("M/M/N/M/N = N servers, M external sources, no queue")
  151. lamda = float(input("Give me arrival rate (lambda): "))
  152. mu = float(input("Give me service rate (mu): "))
  153. N = int(input("Give me the number of servers-total seats (N): "))
  154. M = int(input("Give me the number of external sources (M): "))
  155. rho = lamda / mu
  156. print()
  157.  
  158. print(f"Traffic load = rho = lamda / mu = {rho:.2f}")
  159. print(f"Density of traffic load = rho = lamda / mu = {density_traffic_load:.2f}\n")
  160.  
  161. P_N = rho**N * c(M, N) / sum([rho**i * c(M, i) for i in range(0, N+1)])
  162. P_L = rho**N * c(M-1, N) / sum([rho**i * c(M-1, i) for i in range(0, N+1)])
  163. print(f"Probability of system congestion:   P_N = {100*P_N:.2f}%")
  164. print(f"Probability of requests congestion: P_L = {100*P_L:.2f}%")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement