IndexError: index 2 is out of bounds for axis 0 with size 2 (List Scheduling )

IndexError: index 2 is out of bounds for axis 0 with size 2 (List Scheduling )

我在尝试安排以下内容时遇到问题。 for 循环中似乎有错误。特别是在这部分:mM[iRow,j] = p[k]。 但是我不明白哪里出了问题。

m=2  # machines
n= 4  # number of jobs
p= np.array([1,2,3,4])  # processing times
iTimemax = np.sum(p)

# Initialisation
iTime = 0
k= 0                    
iRow = 0  # the iRowth job of the machine
mM=np.zeros((n,m))

for i in range (iTimemax):
    for j in range (m):
        if np.sum(mM[:,j])  <= iTime:
            mM[iRow,j] = p[k]
            k = k + 1  # next job to be assigned
    iRow = iRow + 1
    iTime = iTime +1

你的 p 数组的长度是 4,每次它进入 if 条件时你都递增 k。您需要在 if 条件中添加检查或在外循环中重置 k。

例如:

import numpy as np
m=2  # machines
n= 4  # number of jobs
p= np.array([1,2,3,4])  # processing times
iTimemax = np.sum(p)

# Initialisation
iTime = 0
k= 0
iRow = 0  # the iRowth job of the machine
mM=np.zeros((n,m))
for i in range (iTimemax):
    for j in range (m):
        if np.sum(mM[:,j])  <= iTime and k < len(p):
            mM[iRow,j] = p[k]
            k = k + 1  # next job to be assigned
    iRow = iRow + 1
    iTime = iTime +1