基于python的M/M/m/∞/∞/FCFS的排队系统仿真
使用python模拟负指数分布的到达时间间隔以及服务时间
import math
import random
import numpy as np
import matplotlib.pyplot as plt
def expntl(L):
"""
negative exponential distribution
return a double random number, L is the mean value
"""
u = random.random()
return -L * math.log(u)
Total_time = 20
lam = 4
mu = 3
N = 1000000
arr_mean = 1/lam
ser_mean = 1/mu
arr_num = int(Total_time*lam*2)
events = np.zeros((5, arr_num))
services = 2
arr = np.array([expntl(arr_mean) for i in range(arr_num)])
events[0, :] = np.cumsum(arr)
events[1, :] = np.array([expntl(ser_mean) for i in range(arr_num)])
print(events.shape)
numbers = 2
member = [0, 1]
events[2, :2] = 0
events[3, 0] = events[0, 0] + events[1, 0]
events[3, 1] = events[0, 1] + events[1, 1]
events[4, :2] = 1
leave_time = np.array([events[3, 0], events[3, 1]])
ser_num = 0
for k in range(2, arr_num):
if events[0, k] >= Total_time:
ser_num = k
break
else:
if events[0, k] <= np.min(leave_time):
events[2, k] = np.min(leave_time) - events[0, k]
events[3, k] = sum(events[[0, 1, 2], k])
else:
events[2, k] = 0
events[3, k] = sum(events[[0, 1, 2], k])
leave_time = np.array([np.max(leave_time), events[3, k]])
numbers += 1
events[4, k] = 1
c = lam/mu
p = lam/(2*mu)
p0 = 1/(1+c+c**2/(2*(1-p)))
Lq = math.pow(services*p, services)*p*p0/(math.factorial(services)*math.pow(1-p, 2))
Ls = Lq + c
Ws = Ls/lam
Wq = Lq/lam
print("经公式计算所得的顾客的平均排队长为:", Lq)
print("经公式计算所得的顾客的平均队长为:", Ls)
print("经公式计算所得的顾客的平均逗留时间为:", Ws)
print("经公式计算所得的顾客的平均等待时间为:", Wq)
print("仿真所得的顾客的平均等待时间为:", np.mean(events[2, :ser_num]))
plt.figure(figsize=(14, 16))
plt.subplot(211)
plt.plot(np.arange(k), events[0, :k], label='arrive time')
plt.plot(np.arange(k), events[3, :k], label='leave time')
plt.xlabel('t![在这里插入图片描述](https://img-blog.csdnimg.cn/7ebe8d32b4034166ad85e77d95fcd2e2.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zuq5a-76Iqx,size_15,color_FFFFFF,t_70,g_se,x_16
he number of customers')
plt.ylabel('Times')
plt.legend()
ax1 = plt.gca()
ax1.spines['right'].set_color('none')
ax1.spines['top'].set_color('none')
plt.subplot(212)
plt.plot(np.arange(k), events[2, :k], label='wait time')
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
plt.show()
第一次写博客,研一某选修课的project,分享给大家,创作不易,点个赞哦
|