【发布时间】:2019-04-10 03:09:38
【问题描述】:
我正在尝试创建一个最佳的轮班时间表,将员工分配到轮班时间。输出的目标应该是花费最少的钱。棘手的部分是我需要考虑特定的约束。这些是:
1) At any given time period, you must meet the minimum staffing requirements
2) A person has a minimum and maximum amount of hours they can do
3) An employee can only be scheduled to work within their available hours
4) A person can only work one shift per day
staff_availability df 包含可以从['Person'] 中选择的员工、他们可以工作的最小-最大小时数['MinHours']-['MaxHours']、他们获得多少报酬['HourlyWage'],以及可用性,以小时数表示@ 987654327@ 和 15 分钟段 ['Availability_15min_Seg']。
注意:如果不需要,不必为可用员工分配轮班。他们只是可以这样做。
staffing_requirements df 包含一天中的时间['Time'] 以及这些时间段内所需的员工['People']。
脚本返回一个df'availability_per_member',显示每个时间点有多少员工可用。所以1 表示可以调度,0 表示不可用。然后,它旨在分配班次时间,同时使用pulp 考虑约束。
我得到了输出,但轮班时间没有连续应用于员工。
我没有满足第四个限制条件,即员工每天只能工作一班
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
import pulp
staffing_requirements = pd.DataFrame({
'Time' : ['0/1/1900 8:00:00','0/1/1900 9:59:00','0/1/1900 10:00:00','0/1/1900 12:29:00','0/1/1900 12:30:00','0/1/1900 13:00:00','0/1/1900 13:02:00','0/1/1900 13:15:00','0/1/1900 13:20:00','0/1/1900 18:10:00','0/1/1900 18:15:00','0/1/1900 18:20:00','0/1/1900 18:25:00','0/1/1900 18:45:00','0/1/1900 18:50:00','0/1/1900 19:05:00','0/1/1900 19:07:00','0/1/1900 21:57:00','0/1/1900 22:00:00','0/1/1900 22:30:00','0/1/1900 22:35:00','1/1/1900 3:00:00','1/1/1900 3:05:00','1/1/1900 3:20:00','1/1/1900 3:25:00'],
'People' : [1,1,2,2,3,3,2,2,3,3,4,4,3,3,2,2,3,3,4,4,3,3,2,2,1],
})
staff_availability = pd.DataFrame({
'Person' : ['C1','C2','C3','C4','C5','C6','C7','C8','C9','C10','C11'],
'MinHours' : [3,3,3,3,3,3,3,3,3,3,3],
'MaxHours' : [10,10,10,10,10,10,10,10,10,10,10],
'HourlyWage' : [26,26,26,26,26,26,26,26,26,26,26],
'Availability_Hr' : ['8-18','8-18','8-18','9-18','9-18','9-18','12-1','12-1','17-3','17-3','17-3'],
'Availability_15min_Seg' : ['1-41','1-41','1-41','5-41','5-41','5-41','17-69','17-79','37-79','37-79','37-79'],
})
staffing_requirements['Time'] = ['/'.join([str(int(x.split('/')[0])+1)] + x.split('/')[1:]) for x in staffing_requirements['Time']]
staffing_requirements['Time'] = pd.to_datetime(staffing_requirements['Time'], format='%d/%m/%Y %H:%M:%S')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
# 15 Min
staffing_requirements = staffing_requirements.groupby(pd.Grouper(freq='15T',key='Time'))['People'].max().ffill()
staffing_requirements = staffing_requirements.reset_index(level=['Time'])
staffing_requirements.index = range(1, len(staffing_requirements) + 1)
staff_availability.set_index('Person')
staff_costs = staff_availability.set_index('Person')[['MinHours', 'MaxHours', 'HourlyWage']]
availability = staff_availability.set_index('Person')[['Availability_15min_Seg']]
availability[['first_15min', 'last_15min']] = availability['Availability_15min_Seg'].str.split('-', expand=True).astype(int)
availability_per_member = [pd.DataFrame(1, columns=[idx], index=range(row['first_15min'], row['last_15min']+1))
for idx, row in availability.iterrows()]
availability_per_member = pd.concat(availability_per_member, axis='columns').fillna(0).astype(int).stack()
availability_per_member.index.names = ['Timeslot', 'Person']
availability_per_member = (availability_per_member.to_frame()
.join(staff_costs[['HourlyWage']])
.rename(columns={0: 'Available'}))
''' Generate shift times based off availability '''
prob = pulp.LpProblem('CreateStaffing', pulp.LpMinimize) # Minimize costs
timeslots = staffing_requirements.index
persons = availability_per_member.index.levels[1]
# A member is either staffed or is not at a certain timeslot
staffed = pulp.LpVariable.dicts("staffed",
((timeslot, staffmember) for timeslot, staffmember
in availability_per_member.index),
lowBound=0,
cat='Binary')
# Objective = cost (= sum of hourly wages)
prob += pulp.lpSum(
[staffed[timeslot, staffmember] * availability_per_member.loc[(timeslot, staffmember), 'HourlyWage']
for timeslot, staffmember in availability_per_member.index]
)
# Staff the right number of people
for timeslot in timeslots:
prob += (sum([staffed[(timeslot, person)] for person in persons])
== staffing_requirements.loc[timeslot, 'People'])
# Do not staff unavailable persons
for timeslot in timeslots:
for person in persons:
if availability_per_member.loc[(timeslot, person), 'Available'] == 0:
prob += staffed[timeslot, person] == 0
# Do not underemploy people
for person in persons:
prob += (sum([staffed[(timeslot, person)] for timeslot in timeslots])
>= staff_costs.loc[person, 'MinHours']*4) # timeslot is 15 minutes => 4 timeslots = hour
# Do not overemploy people
for person in persons:
prob += (sum([staffed[(timeslot, person)] for timeslot in timeslots])
<= staff_costs.loc[person, 'MaxHours']*4) # timeslot is 15 minutes => 4 timeslots = hour
prob.solve()
print(pulp.LpStatus[prob.status])
output = []
for timeslot, staffmember in staffed:
var_output = {
'Timeslot': timeslot,
'Staffmember': staffmember,
'Staffed': staffed[(timeslot, staffmember)].varValue,
}
output.append(var_output)
output_df = pd.DataFrame.from_records(output)#.sort_values(['timeslot', 'staffmember'])
output_df.set_index(['Timeslot', 'Staffmember'], inplace=True)
if pulp.LpStatus[prob.status] == 'Optimal':
print(output_df)
以下是前两个小时的输出(8 个 15 分钟时隙)。问题是这些转变不是连续的。安排在第一个8 时间段的员工主要是不同的。我会在前 2 小时内有 5 人开始。员工每天只能工作一班。
Timeslot C
0 1 C2
1 2 C2
2 3 C1
3 4 C3
4 5 C6
5 6 C1
6 7 C5
7 8 C2
【问题讨论】:
-
如果您有一个软约束(例如 MinHours 应该是 5,但并非必须如此),我建议您关联一个成本并将其添加到您的目标函数中。
-
谢谢@ThomasBoeck。因此,我是否应该在
objective中包含所有约束? -
非常酷的问题,我在类似的领域工作,但更多的是预测时间和填补技能空白。我想这是给联络中心的?你有 github 吗?希望看到更多你的代码。
-
不是联络中心。这是在热情好客。不幸的是,我没有 GitHub。在与其他用户共享之前,我已尝试确定一个可行的选项。
-
我还没有。分配合适的人数是一个简单的解决方法。但是将轮班时间连续应用于员工让我感到悲痛。我没有有效地满足我的第四个约束。
标签: python pandas linear-programming pulp integer-programming