1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| import random
# 服务时间概率相关变量 numbers = [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60] probabilities = [1/36, 2/36, 3/36, 4/36, 5/36, 6/36, 5/36, 4/36, 3/36, 2/36, 1/36]
# 依据probabilities输出每次的服务时间 def service_time(): random_number = random.random()
cumulative_probabilities = [sum(probabilities[:i+1]) for i in range(len(probabilities))] index = next(i for i, p in enumerate(cumulative_probabilities) if p > random_number) return numbers[index]
# 1/3的概率有电话打来 def sendcar(): random_number = random.random() if random_number < 1/3: return True else: return False # 救护车数量和时间长度(分钟) carsNumber=4 timeLength=24
# listTime为每分钟有多少救护车的列表 listTime=[carsNumber]*timeLength
currentTime = 0 #当前时间 validResponse=0 #有效发车次数 invalidResponse=0 #无效发车次数
# 主函数部分 while currentTime < timeLength: # 在5t时刻 if(currentTime%5==0): # 判断是否有电话打来 if sendcar(): # 在时间线上找 for waitTime in range(currentTime+5, timeLength): if listTime[waitTime]>0 and waitTime-currentTime<10:# 打完电话(currentTime+5)后 等待时间小于5分钟 validResponse+=1 service_time_value = str(service_time()) beginTime = waitTime finishTime = beginTime + eval(service_time_value) print(f"有效发车 电话到达时间{currentTime} | 实际发车时间{beginTime} | 服务持续时间{service_time_value} | 服务完成时间{finishTime}", end='\n')
# 根据题目情景,打电话开始时刻5分钟后派遣出可用车辆 for j in range(waitTime , waitTime + eval(service_time_value)): if j>=timeLength: break listTime[j] -= 1 currentTime+=1 break
elif listTime[waitTime]>0 and waitTime-currentTime>=10:# 打完电话(currentTime+5)后 等待时间大于5分钟 invalidResponse+=1 service_time_value = str(service_time()) beginTime = waitTime finishTime = beginTime + eval(service_time_value) print(f"无效发车 电话到达时间{currentTime} | 实际发车时间{beginTime} | 服务持续时间{service_time_value} | 服务完成时间{finishTime}", end='\n')
# 根据题目情景,打电话开始时刻5分钟后派遣出可用车辆 for j in range(waitTime , waitTime + eval(service_time_value)): if j>=timeLength: break listTime[j] -= 1 currentTime+=1 break # 在限定时间内找不到可用车辆 else: print(f"找不到可用无效发车 电话到达时间{currentTime} ", end='\n') invalidResponse+=1 currentTime+=1
# 在5t该时刻无电话打来 else: currentTime+=1 # 不在5t时刻 else: currentTime += 1
print("有效发车:",validResponse) print("无效发车:",invalidResponse)
# 有效发车/无效发车+有效发车 rate = validResponse/(validResponse+invalidResponse)
print(f"当救护车辆为{carsNumber}时运行{timeLength}分钟,有效发车率为{rate:.3f}", end='\n')
|