【问题标题】:Nurse scheduling CpSolver stuck in some solutions护士调度 CpSolver 卡在一些解决方案
【发布时间】:2019-05-23 06:52:33
【问题描述】:

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

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

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.CpSolver cp_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

这不是我想要的。

【问题讨论】:

  • 能否通过在on_solution_callback 中添加新约束来阻止当前解决方案?
  • @PatrickTrentin 对不起。什么意思?
  • @PatrickTrentin 我不知道如何解决我的问题。如果你能为此提供帮助。会很好的。

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


【解决方案1】:

github上给出了解决方案

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

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

【讨论】:

  • 感谢您的回答。但是,我想了解为什么 CpSolver 会这样做。如果你不介意。我在 github 问题中询问详细信息。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-17
  • 1970-01-01
  • 2022-01-05
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多