护士调度CpSolver卡在某些解决方案中

时间:2019-05-23 06:52:34

标签: python-3.x constraint-programming or-tools

我正在实施一个复杂的护士调度问题。 我希望护士连续轮班工作,并与每个班次所需的最少护士数保持一致。

具体来说,我遇到的问题是我可行但解决方案卡住了:

Solution 0
 Day 0

  Nurse 0 works from shift 0 to 3
  Nurse 1 works from shift 4 to 7

Solution 1
 Day 0

  Nurse 0 works from shift 0 to 3
  Nurse 1 works from shift 4 to 7

cp_model.CpSolver刚给了我200次相同的解决方案。

我希望我的解决方案是这样的:

Solution 0
 Day 0

  Nurse 0 works from shift 0 to 3
  Nurse 1 works from shift 4 to 7

Solution 1
 Day 0

  Nurse 0 works from shift 0 to 2
  Nurse 1 works from shift 2 to 7

Solution 2
 Day 0

  Nurse 0 works from shift 5 to 7
  Nurse 1 works from shift 0 to 5

以及更多其他可行的解决方案。

我已经看过有关cp_model和cp_model的文档。CpSolvercp_model.py reference.md

但在使用CpSolver时,上述方法均未提及。 我想知道使用CpSolver时可以选择一些方法吗? 还是我错过了什么?

这是我的代码:

from ortools.sat.python import cp_model
num_nurses = 2    
num_shifts = 8    
num_days = 1      
all_nurses = list(range(num_nurses))
all_shifts = list(range(num_shifts))
all_days = list(range(num_days))
start_shift = list(range(num_shifts))
end_shift = list(range(num_shifts))
RESEARCH_model = cp_model.CpModel()


# Creates shift variables.
RESEARCH_shifts = {}
for n in all_nurses:
    for d in all_days:
        for start in start_shift:
            for end in end_shift:
                RESEARCH_shifts[(n, d, start, end)] =\
                RESEARCH_model.NewBoolVar('shift_n{}d{}start{}end{}'.format(n, 
                d, start, end))

# constraint with minimum required nurse equal to 1
for d in all_days:
    for s in all_shifts:
        RESEARCH_model.Add(sum(RESEARCH_shifts[(n, d, start, end)] 
        for n in all_nurses
        for start in start_shift  for end in range(start , num_shifts ) 
        if start <= s <= end) == 1)

# constraint with continuous shifts
for d in all_days:
    for n in all_nurses:
        RESEARCH_model.Add(sum(RESEARCH_shifts[(n, d, start, end)] 
        for start in start_shift
        for end in range(start, num_shifts )
        if 0 <= (end - start + 1) <= 8 ) <= 1)

# the callback

class RESEARCH_NursesPartialSolutionPrinter(cp_model.CpSolverSolutionCallback):
    """Print intermediate solutions."""

    def __init__(self, shifts, num_nurses, num_days, num_shifts ,start_shift, end_shift, sols):
        cp_model.CpSolverSolutionCallback.__init__(self)
        self._shifts = shifts
        self._num_nurses = num_nurses
        self._num_days = num_days
        self._num_shifts = num_shifts
        self._solutions = set(sols)
        self._solution_count = 0
        self._start_shift = start_shift
        self._end_shift = end_shift

    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 {}'.format(d))
                for n in range(self._num_nurses):
                    for start in self._start_shift:
                        for end in range(start, self._num_shifts):
                            if self.Value(self._shifts[(n, d, start, end)]) == True:
                                print('  Nurse {} works from shift {} to {}'.format(n, start, end))

# create solver and solve it.
solver = cp_model.CpSolver()

# Display the first n solutions.
a_few_solutions = range(200)
RESEARCH_solution_printer = RESEARCH_NursesPartialSolutionPrinter(RESEARCH_shifts, 
                                                                  num_nurses, 
                                                                  num_days, 
                                                                  num_shifts, 
                                                                  start_shift, 
                                                                  end_shift,  
                                                                  a_few_solutions)


solver.parameters.max_time_in_seconds = 2.0
solver.SearchForAllSolutions(RESEARCH_model, RESEARCH_solution_printer)


我希望输出如下:

Solution 0

 Day 0

  Nurse 0 works from shift 0 to 3
  Nurse 1 works from shift 4 to 7

Solution 1

 Day 0

  Nurse 0 works from shift 0 to 2
  Nurse 1 works from shift 3 to 7

Solution 2

 Day 0

  Nurse 0 works from shift 6 to 7
  Nurse 1 works from shift 0 to 5

但是我得到的输出如下:

Solution 0
 Day 0

  Nurse 0 works from shift 0 to 3
  Nurse 1 works from shift 4 to 7

Solution 1
 Day 0

  Nurse 0 works from shift 0 to 3
  Nurse 1 works from shift 4 to 7

Solution 2
 Day 0

  Nurse 0 works from shift 0 to 3
  Nurse 1 works from shift 4 to 7

我不想要哪个。

1 个答案:

答案 0 :(得分:1)

解决方案在github上给出

https://github.com/google/or-tools/issues/1300

问题在于此代码创建了许多不受约束的布尔变量。 列举所有解决方案时,您正在探索这些变量的所有2 ^ n组合。