Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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_Or Tools - Fatal编程技术网

Python 护士排班问题,使用或工具,在特定日期增加不同的轮班时间

Python 护士排班问题,使用或工具,在特定日期增加不同的轮班时间,python,or-tools,Python,Or Tools,我正在修改中的代码,我希望能够为某一天添加不同的轮班时间(例如,我希望星期五/第4天只有2班)。我的代码总是以错误代码结尾。我相信这是由于我设置的约束存在一些内部问题 我在类似的程序上看到了一些关于StackOverflow的帖子,但找不到一个能解决我具体问题的帖子 from ortools.sat.python import cp_model class employeePartialSolutionPrinter(cp_model.CpSolverSolutionCallback):

我正在修改中的代码,我希望能够为某一天添加不同的轮班时间(例如,我希望星期五/第4天只有2班)。我的代码总是以错误代码结尾。我相信这是由于我设置的约束存在一些内部问题

我在类似的程序上看到了一些关于StackOverflow的帖子,但找不到一个能解决我具体问题的帖子

from ortools.sat.python import cp_model

class employeePartialSolutionPrinter(cp_model.CpSolverSolutionCallback):
    """Print intermediate solutions."""
    def __init__(self, shifts, num_employee, num_days, num_shifts, sols):
        cp_model.CpSolverSolutionCallback.__init__(self)
        self._shifts = shifts
        self._num_employee = num_employee
        self._num_days = num_days
        self._num_shifts = num_shifts
        self._solutions = set(sols)
        self._solution_count = 0

    def on_solution_callback(self):
        self._solution_count += 1
        if self._solution_count in self._solutions:
            print('Solution %i' % self._solution_count)
            for d in range(self._num_days):
                print('Day %i' % d)
                for n in range(self._num_employee):
                    is_working = False
                    for s in range(self._num_shifts):
                        if self.Value(self._shifts[(n, d, s)]):
                            is_working = True
                            print('  Employee %i works shift %i' % (n, s))
                    if not is_working:
                        print('  Employee {} does not work'.format(n))

    def solution_count(self):
        return self._solution_count
model = cp_model.CpModel()
solver = cp_model.CpSolver()

num_employee = 5
num_shifts = 5
num_days = 5
all_employee = range(num_employee)
all_shifts = range(num_shifts)
all_days = range(num_days)

# Normal Hours
# Monday-Thursday Shift 0-4
# Friday Shift 0-1
friday_deduct = 3

shifts = {}
for n in all_employee:
    for d in all_days:
        if d == 4:
            for s in range(num_shifts-friday_deduct):
                shifts[(n, d, s)] = model.NewBoolVar('shift_n%id%is%i' % (n, d, s))
        else:
            for s in all_shifts:
                shifts[(n,d,s)] = model.NewBoolVar('shift_n%id%is%i' % (n,d,s))


"""
Constraints (Normal Time)
"""
# Each Shift is assigned to a single person per day
# Shift 2 need to be assigned to 3 person
# Shift 1 and 3 need to be assigned to 2 person
for d in all_days:
    if d == 4:
        for s in range(num_shifts-friday_deduct):
            if s == 1:
                model.Add(sum(shifts[(n, d, s)] for n in all_employee) == 2)
            else:
                model.Add(sum(shifts[(n, d, s)] for n in all_employee) == 1)
    else:
        for s in all_shifts:
            if s == 2 :
                model.Add(sum(shifts[(n,d,s)] for n in all_employee) == 3)
            elif s == 3 or s == 1:
                model.Add(sum(shifts[(n, d, s)] for n in all_employee) == 2)
            else:
                model.Add(sum(shifts[(n,d,s)] for n in all_employee) == 1)

#Each nurse works at most 10 shift per week, at least 4 shift per week
for n in range(num_employee):
    week = []
    for d in all_days:
        if d == 4:
            for s in range(num_shifts-friday_deduct):
                week.append(shifts[(n,d,s)])
            # week.append(sum(shifts[(n, d, s)] for s in range(num_shifts-friday_deduct)))
        else:
            for s in all_shifts:
                week.append(shifts[(n,d,s)])
            # week.append(sum(shifts[(n,d,s)] for s in all_shifts))
    model.Add(sum(week) >= 4)
    model.Add(sum(week) <=10)

solver.parameters.linearization_level = 0
a_few_solutions = range(5)
solution_printer = employeePartialSolutionPrinter(shifts, num_employee, 
num_days, num_shifts, a_few_solutions)
solver.SearchForAllSolutions(model, solution_printer)

您的回拨出现关键错误(第4天只有2班):

简单检查即可:

if (n, d, s) in self._shifts and self.Value(
    self._shifts[(n, d, s)]
):
但你肯定应该看看Laurent建议的另一个例子。

你能发布你的employeePartialSolutionPrinter类吗?这可能是一个关键错误,或者是您的回调中的某个错误。我已将该类添加到我的主代码中。此代码很脆弱,因为它需要num_=num_移位。请看一下这个例子:
for s in range(self._num_shifts):
    if self.Value(self._shifts[(n, d, s)]):
if (n, d, s) in self._shifts and self.Value(
    self._shifts[(n, d, s)]
):