如何追加数组以避免索引越界?

2024-05-19 05:22:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试模拟一个调度问题。每天都有很多人打电话到医院要求预约。在每一天结束时,到达者被分配一个时间段。最初,我构建了一个数组,其中包含每天的到达量和一个表示每天时隙数的数组。循环浏览每天的到达时间,到达时间被分配到最近的时间段。但是,当到达数相对较高时,代码将尝试将到达分配到服务器数组末尾以外的时间段,即它引用的索引大于服务器数组。当模型试图将到达时间分配给服务器阵列当前包含的时间段时,是否有任何方法可以自动附加数组?你知道吗

到目前为止,我首先生成了一个到达数组(a),并将服务器数组设置为到达率大小的两倍。虽然只要不选择极端值(高labda或低mu),这个方法就行,但是我想创建一个更健壮的s。我个人认为最简单的方法是附加s

def simple_model_test():

    labda, mu, Days = 10, 4, 10
    a = poisson(labda).rvs(Days)            # Generate array with number of arrivals each day
    s = np.ones_like(a) * mu                # Generate equal sizes array with time slots
    s = np.append(s, np.ones_like(a) * mu)  # Add some additional days at the end of time horizon

    for i in range(0, len(a)):              # Cycle through all days of time horizon
        j = i + 1                           # Patients cannot be served at day of arrival
        # if s[j] is empty:                   # I was trying to do something like this, but this does not work
           # s = np.append(s, mu)
        while a[i] > 0:                     # Assign all patients that arrived to a time slot
            if s[j] > 0:                    # Check if time slots are available at day j
                a[i] -= 1                   # To indicate that a patient is assigned
                s[j] -= 1                   # To indicate that a time slot is filled at day j
            else:
                j += 1                      # Check for free time slots next day
    print(s)

simple_model_test()

所以现在它给出了一个错误:“IndexError:index 20超出了大小为20的轴0的界限”。所以当s[j]还不存在时,我想附加s。你知道吗


Tags: of方法服务器iftimenp时间数组

热门问题