在给定到达和离开时间矩阵的情况下,想出给定时间排队的人数?

Coming up with the number of people in a queue at a given time, given matrices of arrival and departure times?

几个小时以来我一直在思考如何做到这一点,但我卡住了。

我有一个包含客户到达时间的矩阵 A 和一个包含客户离开时间的矩阵 D。例如。到达矩阵中的时间表示该时间一位顾客到达,离开矩阵中的时间表示一位顾客离开。

我正在尝试绘制商店中顾客数量的时间序列,从 t=1..800 开始,间隔为 1。但是,顾客的到达和离开时间由随机变量决定,并且这是一个随着时间步长随机增加而运行的模拟,所以我发现很难在模拟本身中存储给定时间间隔的客户数量。

我认为一定有一种方法可以在给定到达和离开时间矩阵的情况下,以均匀间隔的时间间隔用客户数量填充矩阵 N,但我想不出它是什么是。有人可以给我指出正确的方向吗?

到达和离开时间为 "events",您的数组包含这些事件的时间。基本逻辑是找到下一个事件的时间并进行与该事件相关的更新。对于到达,队列长度增加。对于出发,队列长度递减。以下是该想法的一个相当蛮力的实现(Python3 因为你没有指定)它会在它发生变化时打印出时间和队列长度:

a = [1.1, 2.9, 5.1, 6.5]
d = [3.5, 5.2, 7.2, 8.0]

queue = 0
a_index = 0
d_index = 0
print(0, ':', queue)

while a_index < len(a) and d_index < len(d):
    if a[a_index] < d[d_index]:  # did an arrival come first?
        queue += 1
        print(a[a_index], ':', queue)
        a_index += 1
    else:
        queue -= 1
        print(d[d_index], ':', queue)
        d_index += 1

# ran out of elements in one of the arrays,
# iterate through remainder of the other

while a_index < len(a):
    queue += 1
    print(a[a_index], ':', queue)
    a_index += 1

while d_index < len(d):
    queue -= 1
    print(d[d_index], ':', queue)
    d_index += 1

如果您只想在整数时间打印,请将它们也设置为事件:

a = [1.1, 2.9, 5.1, 6.5]
d = [3.5, 5.2, 7.2, 8.0]

queue = 0
a_index = 0
d_index = 0
print_time = 1
max_time = 10
print(0, ':', queue)

while a_index < len(a) and d_index < len(d):
    if a[a_index] < d[d_index] and a[a_index] <= print_time:
        queue += 1
        a_index += 1
    elif d[d_index] <= print_time:
        queue -= 1
        d_index += 1
    else:
        print(print_time, ':', queue)
        print_time += 1

while a_index < len(a):
    if a[a_index] <= print_time:
        queue += 1
        a_index += 1
    else:
        print(print_time, ':', queue)
        print_time += 1

while d_index < len(d):
    if d[d_index] <= print_time:
        queue -= 1
        d_index += 1
    else:
        print(print_time, ':', queue)
        print_time += 1

while print_time <= max_time:
    print(print_time, ':', queue)
    print_time += 1

这些无疑可以收紧,但它们传达了方法。

如果您的事件不止这个数量,更好的组织原则是将事件放入优先级队列中,按发生时间排序,然后将它们一个接一个地分派给基于发生的事件类型的适当的状态转换逻辑。您可以在 this paper. The paper implements the ideas in Java, but a Python3 implementation and a demo queueing model are available here.

中找到这种方法的逻辑