确定值的分配 - Python

7

我正在尝试创建一个最佳的班次安排,让员工被分配到不同的班次。输出结果应旨在花费最少的费用。棘手之处在于,我需要考虑特定的限制条件。这些限制条件包括:

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'],以及可用性,以小时['Availability_Hr']和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,但不一定要严格遵守),我建议将其与成本相关联,并将其添加到您的目标函数中。 - Thomas Boeck
谢谢@ThomasBoeck。那我是否应该在“目标”中包含所有约束条件? - jonboy
非常酷的问题,我从事类似领域的工作,但更多地关注预测工时和填补技能缺口。我猜这是为了联系中心?你有一个GitHub账户吗?很想看看你的代码。 - Umar.H
1
不是联系中心。它属于酒店行业。很遗憾我没有GitHub。在与其他用户分享之前,我已经尝试确定一个可行的选项。 - jonboy
非常酷,你找到解决方案了吗?我曾经尝试制作类似的东西,但目前没有用例。 - Umar.H
1
我还没有。分配正确数量的人员是一个简单的修复。但是连续将班次应用于员工让我很苦恼。我没有有效地满足我的第四个约束条件。 - jonboy
2个回答

4

注意: 这是对早期版本问题的回答。


我认为求解器返回的解决方案是正确的;每个人都在工作他们的最小小时数,只是不是连续的。我运行了你的代码,然后说。
for person in persons:
    print("{}: {}".format(person, sum([staffed[(timeslot, person)].value() for timeslot in timeslots])))

并获得了:

C1: 12.0
C2: 12.0
C3: 12.0
C4: 20.0
C5: 23.0
C6: 18.0
C7: 22.0
C8: 29.0
C9: 22.0
C10: 27.0
C11: 32.0

所以每个人至少工作12个班次,即3小时。
如果你希望班次是连续的(即一个人不能在1号时段工作然后在3号时段工作),那么处理这个问题的典型方法是使用一个决策变量来说明每个员工开始工作的时间,而不是一个变量来指定他们工作的每个时间段。然后,引入一个参数,比如a[j][t],它等于1,如果在时隙j开始工作的员工正在时隙t工作。从那里,你可以计算谁在哪些时隙工作。
当你将MinHours设置为5时,问题无法解决的原因是它强制太多的人在某些小时工作。例如,在时间点41之前,必须有6个人完成他们的班次。这意味着在时间点41之前需要工作120个人时隙,但只需要在1号和41号之间工作97个人时隙。
这个问题可以通过将“聘请正确数量的员工”约束条件更改为>=而不是==来解决,假设该人员配备系统允许这样做。(如果不允许,则手头只有一个无法实现的实例。)
(顺便说一下 - 你可能会对运筹学和分析上提议的新的Stack Exchange网站感兴趣。我们会在那里回答类似这样的问题。 :-) )

谢谢 @grendelsdad。这非常有帮助。我会去看看那个网站。 - jonboy
好的,很高兴能帮到你。如果你认为我的回答足够恰当,请不要忘记接受它。 :) - LarrySnyder610
没问题。我先试试看。非常有帮助,但我也会在那里发布。谢谢。 - jonboy
我知道已经有一段时间了,这应该是一个单独的问题,但我有一个关于“聘用适当数量的人员”的查询。我不需要为每个员工分配班次。可用性数据框已确定以涵盖每种情况。我希望聘用最少量的人员。例如,如果只需要一个人,则只需聘用一个人。我不需要每个人都上班。这有意义吗? - jonboy

2
这里是对您修订后的问题的回答,即如何添加一个要求每个员工连续工作时间段的约束条件。
我建议您添加以下约束条件(在此以代数方式书写):
"最初的回答"
x[t+1,p] <= x[t,p] + (1 - (1/T) * sum_{s=1}^{t-1} x[s,p])    for all p, for all t < T

其中x是您的staffed变量(在这里写成x以节省空间),t是时间索引,T是时间段数,p是员工索引。
约束逻辑如下:如果x[t,p]=0(员工在期间t不工作)且x[s,p]=1对于任何s(员工在以前的任何时期都在工作),则x[t+1,p]必须=0(员工不能在期间t+1工作)。因此,一旦员工停止工作,他们就不能重新开始。请注意,如果x[t,p]=1x[s,p]=0对于每个s,则x[t+1,p]可以等于1
以下是我在pulp中实现此约束的方式:
# If an employee works and then stops, they can't start again
num_slots = max(timeslots)
for timeslot in timeslots:
    if timeslot < num_slots:
        for person in persons:
            prob += staffed[timeslot+1, person] <= staffed[timeslot, person] + \
                (1 - (1./num_slots) *
                 sum([staffed[(s, person)] for s in timeslots if s < timeslot]))

我运行了这个模型并得到了以下结果:

Optimal
                      Staffed
Timeslot Staffmember         
1        C2               1.0
2        C2               1.0
3        C2               1.0
4        C2               1.0
5        C2               1.0
6        C2               1.0
7        C2               1.0
8        C2               1.0
9        C2               1.0
         C6               1.0
10       C2               1.0
         C6               1.0
11       C2               1.0
         C6               1.0
12       C2               1.0
         C6               1.0
13       C3               1.0
         C6               1.0
14       C3               1.0
         C6               1.0

等等,员工是在连续的时间段工作。

请注意新的限制会使模型稍微慢一些。但如果您要解决更大的实例,可能需要重新考虑限制条件。Original Answer翻译成"最初的回答"。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接