Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python纸浆警告“;“覆盖以前设置的目标”;向问题添加约束时_Python_Constraints_Linear Programming_Pulp - Fatal编程技术网

Python纸浆警告“;“覆盖以前设置的目标”;向问题添加约束时

Python纸浆警告“;“覆盖以前设置的目标”;向问题添加约束时,python,constraints,linear-programming,pulp,Python,Constraints,Linear Programming,Pulp,我正在尝试使用纸浆库为一家诊所构建一个员工调度系统。但是,每当我尝试添加约束时,我总是会收到标题中提到的警告,如下面的代码所示。我使用纸浆文档中的代码作为构建LP问题的参考 问题的简要总结:诊所有3个地点,我正在构建一个系统,可以构建一个最优解决方案,在这3个地点安排员工名单。我们没有安排/计算时间,而是按天安排(例如,Jim在周一、周二和周五工作)。每个诊所对每天所需的特定专业(我在下面的代码中称之为角色)的员工数量都有要求。目前,我正试图添加一个限制条件,限制特定角色的员工数量,这些员工可以

我正在尝试使用纸浆库为一家诊所构建一个员工调度系统。但是,每当我尝试添加约束时,我总是会收到标题中提到的警告,如下面的代码所示。我使用纸浆文档中的代码作为构建LP问题的参考

问题的简要总结:诊所有3个地点,我正在构建一个系统,可以构建一个最优解决方案,在这3个地点安排员工名单。我们没有安排/计算时间,而是按天安排(例如,Jim在周一、周二和周五工作)。每个诊所对每天所需的特定专业(我在下面的代码中称之为角色)的员工数量都有要求。目前,我正试图添加一个限制条件,限制特定角色的员工数量,这些员工可以安排在某一天的特定位置

现在,函数maxnum_(day、location、role)只返回3(用于测试约束)(即,可以安排在任何位置的最大员工数为3)。当我使用Good day设置约束时

您不应该在pulp中使用python的标准
sum
函数tu sum表达式或变量。您应该使用软件包提供的
lpSum
函数。它不仅效率更高,而且在这种情况下,它解决了您的问题。不过,我无法解释原因

因此,在约束的代码中,您应该具有:

shift\u model+=lpSum([系数(shift\u实例)*x[shift\u实例]用于shift\u实例\
在变量列表中])
#添加限制特定角色的可能员工数量的约束,以安排在给定地点的给定日期
日班制:#工资期内的每一天
对于所有位置中的位置。键():#对于每个诊所
对于角色中的角色:#对于每个角色
shift_model+=lpSum([x[shift_instance]表示变量列表中的shift_实例,如果shift_实例中的日期和位置在shift_实例中\
和shift_实例中的角色]==maxnum_(日期、位置、角色),“最大{}{}{}员工数”。格式(日期、位置、角色)
此外,这是关于性能的一般建议,在迭代之前预过滤变量字典更有效。它还使代码更清晰。以下是已编辑的模型零件:


x=LpVariable.dicts('shift',变量列表,下限=0,上限=1,cat=LpInteger)
班次调度模型=LPP问题(“员工调度模型”,LPU)
#目标函数
shift_模型+=lpSum([系数(shift_实例)*x[shift_实例]用于shift_实例)\
在变量列表中])
#对于每一天,位置和角色:变量列表
x_dlr={}
对于(e,d,l,r),_xin x.items():
_tup=d,l,r
如果安装程序不在x dlr中:
x_dlr[\u tup]=[]
x_dlr[\u tup].append(\u x)
#添加限制特定角色的可能员工数量的约束,以安排在给定地点的给定日期
日班制:#工资期内的每一天
对于所有位置中的位置。键():#对于每个诊所
对于角色中的角色:#对于每个角色
_tup=日期、位置、角色
shift_model+=lpSum(x_dlr.get(_tup,[])==maxnum_(*\u tup),“为{{}{}{}{}提供的最大员工数”。格式(日期、位置、角色)
shift_model.solve()
打印(“最佳员工计划:”)
对于listOfVariables中的shift_实例:
如果x[shift_instance].value()==1.0:
打印(x[shift\u实例])
你好

您不应该在pulp中使用python的标准
sum
函数tu sum表达式或变量。您应该使用软件包提供的
lpSum
函数。它不仅效率更高,而且在这种情况下,它解决了您的问题。不过,我无法解释原因

因此,在约束的代码中,您应该具有:

shift\u model+=lpSum([系数(shift\u实例)*x[shift\u实例]用于shift\u实例\
在变量列表中])
#添加限制特定角色的可能员工数量的约束,以安排在给定地点的给定日期
日班制:#工资期内的每一天
对于所有位置中的位置。键():#对于每个诊所
对于角色中的角色:#对于每个角色
shift_model+=lpSum([x[shift_instance]表示变量列表中的shift_实例,如果shift_实例中的日期和位置在shift_实例中\
和shift_实例中的角色]==maxnum_(日期、位置、角色),“最大{}{}{}员工数”。格式(日期、位置、角色)
此外,这是关于性能的一般建议,在迭代之前预过滤变量字典更有效。它还使代码更清晰。以下是已编辑的模型零件:


x=LpVariable.dicts('shift',变量列表,下限=0,上限=1,cat=LpInteger)
班次调度模型=LPP问题(“员工调度模型”,LPU)
#目标函数
shift_模型+=lpSum([系数(shift_实例)*x[shift_实例]用于shift_实例)\
在变量列表中])
#对于每一天,位置和角色:变量列表
x_dlr={}
对于(e,d,l,r),_xin x.items():
_tup=d,l,r
如果安装程序不在x dlr中:
x_dlr[\u tup]=[]
x_dlr[\u tup].append(\u x)
#添加限制特定角色的可能员工数量的约束,以安排在给定地点的给定日期
日班制:#工资期内的每一天
对于所有位置中的位置。键():#对于每个诊所
对于角色中的角色:#对于每个角色
_tup=日期、位置、角色
shift_model+=lpSum(x_dlr.get(_tup,[])==maxnum_(*\u tup),“为{{}{}{}{}提供的最大员工数”。格式(日期、位置、角色)
shift_model.solve()
打印(“最佳员工计划:”)
对于listOfVariables中的shift_实例:
如果x[shift_instance].value()=1。
from pulp import *
def maxnum_(d,l,r):
    return 3
def coefficients(instance):

    #Given a shift instance, returns the weight of the preference
    #based on the parameters of the instance.
    #Chosen values are subject to change.

    weight = 0
    employee = instance[0]
    day = instance[1]
    location = instance[2]
    role = instance[3]
    global allEmployees
    if day not in allEmployees[employee]['Availability']:
        weight -= 5
    else:
        weight += 1
    if location not in allEmployees[employee]['PreferredLocationOfWork']:
        weight -= 2
    else:
        weight+=1
    return weight


shifts = ['M1','M2','T1','T2','W1','W2','R1','R2','F1','F2']
allEmployees = {'Billy Bob': {'License': 'Nurse Practitioner', 'Specialty': 'Urgent', 'Age': 'Geriatric', 'Availability': ['M1', 'T1', 'F1', 'M2', 'R2', 'F2'], 'PreferredLocationOfWork': 'PPHC', 'Requested_dates_off': ['2020-05-09', '2021-01-31', 'YYYY-MM-DD']}, 'Jimmy John': {'License': 'Physician', 'Specialty': 'Emergency', 'Age': 'Pediatric', 'Availability': ['T1', 'F1', 'W2', 'R2'], 'PreferredLocationOfWork': 'CHCF', 'Requested_dates_off': ['2020-05-09', '2021-01-31', 'YYYY-MM-DD']}}
# Ignoring specialty/age/license required and min number employees required for now, will implement later
allLocations = {'CHCF': {'MinNumberEmployeesRequiredPresent': 1, 'SpecialtyRequired': ['Emergency', 'Urgent', 'Urgent'], 'AgeRequired': ['Pediatric', 'Geriatric', 'Pediatric'], 'LicenseRequired': ['Physician', 'NurseMidwife', 'NursePracticioner']}, 'THS': {'MinNumberEmployeesRequiredPresent': 1, 'SpecialtyRequired': ['Emergency', 'Urgent', 'Primary', 'Obstetrics'], 'AgeRequired': ['Pediatric', 'Geriatric', 'Family', 'Adult'], 'LicenseRequired': ['Physician', 'NurseMidwife', 'NursePracticioner', 'Physician']}, 'PPHC': {'MinNumberEmployeesRequiredPresent': 1, 'SpecialtyRequired': ['Urgent', 'Urgent', 'Urgent'], 'AgeRequired': ['Geriatric', 'Geriatric', 'Pediatric'], 'LicenseRequired': ['Physician', 'NurseMidwife', 'NursePracticioner']}}
age_ = ['Pediatric', 'Adult','Geriatric', 'Family']
specialty_ = ['Emergency', 'Urgent Care', 'Primary Care', 'Obstetrics']
license_ = ['Physician','Nurse Midwife', 'Nurse Practitioner']
roles = [','.join([a,s,l]) for a in age_ for s in specialty_ for l in license_ ]
listOfVariables = []
# Start creating the tuples of shift instances, these will be the variables of our LP problem
for employee in allEmployees.keys():
    specialty = []
    specialty.append(allEmployees[employee]['Age'])
    specialty.append(allEmployees[employee]['Specialty'])
    specialty.append(allEmployees[employee]['License'])
    specialtyString = ','.join(specialty)
    #TODO: Implement weighted alternates...

    for day in shifts:
        # Include all locations, set preferred location coefficient to 10 and non preferred to 1?
        for location in allLocations.keys():
            # In coefficients, set days not in preference to 1, all preferred dates to 10
            listOfVariables.append((employee, day, location, specialtyString))

x = LpVariable.dicts('shift', listOfVariables, lowBound = 0, upBound = 1, cat = LpInteger)

shift_model = LpProblem("Employee_Scheduling_Model" , LpMaximize)
# The objective function
shift_model += sum([coefficients(shift_instance) * x[shift_instance] for shift_instance \
            in listOfVariables])

# Add Constraint limiting the number of possible employees of a specific role to schedule on a given day at a given location
for day in shifts: # for each day in pay period
    for location in allLocations.keys(): # for each clinic
        for role in roles: # for each role
            shift_model += sum([x[shift_instance] for shift_instance in listOfVariables if day in shift_instance and location in shift_instance\
                    and role in shift_instance]) == maxnum_(day, location, role), "Max employees for {} {} {}".format(day,location,role)

shift_model.solve()
print("Optimal employee schedule: ")
for shift_instance in listOfVariables:
    if x[shift_instance].value() == 1.0:
        print(x[shift_instance])