实例14 ATM机等待时间

 

 

  需求:对于特定客户流,客户的平均等待时间是多少?

  • ATM:由于不同客户使用的功能不同,机器的处理时间也不同。这里假设处理时间服从随机分布
  • 客户流:假设客户到达ATM机的时间是随机的
  • 平均时间:每个客户平均的等待时间

思路:

(1)以ATM每次处理结束的时间为时间驱动事件;

(2)需要一个等待队列,维护客户的到达时间;

(3)时间变化时,驱动等待队列变化

import random


class ATM:
    def __init__(self, maxtime=5):
        self.t_max = maxtime
    def getServCompleteTime(self, start=0):
        """模拟返回ATM机的一次任务的处理时间"""
        return start + random.randint(1, self.t_max)

class Customers:
    def __init__(self, n):
        self.count = n  # 所有来的客户数
        self.left = n   # 还剩几个客户没有服务完

    def getNextArrvTime(self, start=0, arrvtime=10):
        """模拟客户的到达时间"""
        if self.left != 0:
            self.left -= 1
            return start + random.randint(1, arrvtime)
        else:
            return 0

    def isOver(self):
        return True if self.left == 0 else False

a = ATM()
c = Customers(100)


wait_list = []  # 等待队列
wait_time = 0   # 所有客户的总共等待时间
cur_time = 0    # 当前时刻
cur_time += c.getNextArrvTime()
wait_list.append(cur_time)

while len(wait_list) != 0 or not c.isOver():

    if wait_list[0] <= cur_time:
        next_time = a.getServCompleteTime(cur_time)
        # ATM机的下次可用窗口时刻
        del wait_list[0]
    else:
        next_time = cur_time + 1
        # 如果没有客户在等待服务,时间进1

    if not c.isOver() and len(wait_list) == 0:
        next_arrv = c.getNextArrvTime(cur_time)
        wait_list.append(next_arrv)  # 模拟客户到达

    if not c.isOver() and wait_list[-1] < next_time:
        next_arrv = c.getNextArrvTime(wait_list[-1])
        wait_list.append(next_arrv)  # 模拟客户到达
        while next_arrv < next_time and not c.isOver():
            next_arrv = c.getNextArrvTime(next_arrv)
            wait_list.append(next_arrv)  # 模拟客户到达

    for i in wait_list:  # 计算客户的等待时间
        if i <= cur_time:
            wait_time += next_time - cur_time
        elif cur_time < i < next_time:
            wait_time += next_time - i
        else:
            pass
    cur_time = next_time  # 以ATM机的每次可用窗口时刻为时间驱动点

print(wait_time/c.count)
View Code

 

posted @ 2021-09-03 23:55  seaidler  阅读(12)  评论(0)    收藏  举报