本文主要是介绍蒙特卡罗模拟之排队上厕所问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
蒙特卡罗模拟之排队上厕所问题
'''
电影结束后会有20人上厕所
20个人会在0-10分钟内全部到达厕所
每个人上厕所时间在1-3分钟
模拟只有一个厕所到达时间,等待时间,开始上厕所时间,结束时间
'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import Circlearrivingtime = np.random.uniform(0,20,size=20)
arrivingtime.sort()
workingtime = np.random.uniform(1,3,size=20)startingtime = [0 for i in range(20)]
finishtime = [0 for i in range(20)]
waitingtime = [0 for i in range(20)]
emptytime = [0 for i in range(20)]startingtime[0] = arrivingtime[0]
finishtime[0] = startingtime[0]+workingtime[0]
waitingtime[0] = startingtime[0]-arrivingtime[0]print(startingtime[0],workingtime[0],finishtime[0],waitingtime[0])for i in range(1,len(arrivingtime)):if finishtime[i-1] > arrivingtime[i]:startingtime[i] = finishtime[i-1]else:startingtime[i] = arrivingtime[i]emptytime[i]=arrivingtime[i]-finishtime[i-1]finishtime[i] = startingtime[i] + workingtime[i]waitingtime[i] = startingtime[i] - arrivingtime[i]print('第%d个人,到达时间 开始时间 工作时间 结束时间 等待时间\n' %i,arrivingtime[i],startingtime[i],workingtime[i],finishtime[i],waitingtime[i], '\n')#添加个排队人数计算
#plt.plot(arrivingtime,'--k')
#plt.plot(startingtime,'*')
#plt.fill_between(arrivingtime,startingtime,0)
#plt.grid()end=[]
y=[]
for i in np.arange(0,40.0,0.5):num=0for j in range(0,20):if i>arrivingtime[j] and i<startingtime[j]:num=num+1dic={}dic['source']=idic['targe']=numend.append(dic)y.append(num)x = [i for i in np.arange(0,40.0,0.5)]
y = y
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()#等待时间作图
fig = plt.figure(figsize = (6,4))
plt.plot(waitingtime,'go')
plt.grid(True,linestyle='--',color='gray',linewidth='0.8')
plt.title('排队上厕所等待时间')
plt.show()
这篇关于蒙特卡罗模拟之排队上厕所问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!