Я реализую сложную задачу планирования медсестры.Я хочу, чтобы медсестры работали в непрерывную смену и соответствовали минимальным требованиям медсестер в каждой смене.
в частности, проблема меня поразила, я получил выполнимое, но застрявшее решение вроде:
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
Что не я хочу.