尝试使用线性规划优化流程。获取有关以下内容的错误:IndexError:索引 1 超出尺寸为 1

问题描述 投票:0回答:0

我正在尝试根据以下数据框优化工人的日程安排。

    Time Windows Shift 1 Shift 2 Shift 3 Shift 4  Workers Required
0    6:00 - 9:00       1       0       0       1              55.0
1   9:00 - 12:00       1       0       0       0              46.0
2  12:00 - 15:00       1       1       0       0              59.0
3  15:00 - 18:00       0       1       0       0              23.0
4  18:00 - 21:00       0       1       1       0              60.0
5  21:00 - 24:00       0       0       1       0              38.0
6   24:00 - 3:00       0       0       1       1              20.0
7    3:00 - 6:00       0       0       0       1              30.0
8      Wage_Rate     135     140     190     188               0.0

首先(创建数据框):

import pandas as pd
df = pd.read_clipboard(sep='\\s+')
df = pd.DataFrame(df)

这是我正在测试的代码。

import pandas as pd
import pulp
from pulp import LpMaximize, LpMinimize, LpProblem, LpStatus, lpSum, LpVariable
import numpy as np


df = df.fillna(0).applymap(lambda x: 1 if x == "X" else x)

df.set_index('Time Windows')
a = df.drop(columns=["Workers Required"]).values
a


df.drop(df.tail(1).index,inplace=True)
print(df.shape)


df = df.fillna(0).applymap(lambda x: 1 if x == "X" else x)
print(df.shape)


a = df.to_numpy()
a


# number of shifts
n = a.shape[0]


# number of time windows
T = a.shape[0]


# number of workers required per time window
d = df["Workers Required"].values


# wage rate per shift
#Get last row of dataframe
last_row = df.iloc[-1:,1:]
#Get last row of dataframe as numpy array
w = last_row.to_numpy()
w


# Decision variables
y = LpVariable.dicts("num_workers", list(range(n)), lowBound=0, cat="Integer")
y


# Create problem
prob = LpProblem("scheduling_workers", LpMinimize)



prob += lpSum([w[j] * y[j] for j in range(n)])


for t in range(T):
    prob += lpSum([a[t, j] * y[j] for j in range(n)]) >= d[t]


prob.solve()
print("Status:", LpStatus[prob.status])


for shift in range(n):
    print(f"The number of workers needed for shift {shift} is {int(y[shift].value())} workers")

当我到达这条线时:

prob += lpSum([w[j] * y[j] for j in range(n)])

我收到这个错误。

Traceback (most recent call last):

  Cell In[197], line 1
    prob += lpSum([w[j] * y[j] for j in range(n)])

  Cell In[197], line 1 in <listcomp>
    prob += lpSum([w[j] * y[j] for j in range(n)])

IndexError: index 1 is out of bounds for axis 0 with size 1

我试图遵循的例子来自下面的链接。

https://towardsdatascience.com/how-to-solve-a-staff-scheduling-problem-with-python-63ae50435ba4

python python-3.x optimization linear-programming
© www.soinside.com 2019 - 2024. All rights reserved.