SIGN IN SIGN UP
google / or-tools UNCLAIMED

Google's Operations Research tools:

0 0 0 C++
#!/usr/bin/env python3
2025-01-10 11:35:44 +01:00
# Copyright 2010-2025 Google LLC
2018-11-18 09:04:16 -08:00
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
2018-11-16 16:54:37 -08:00
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2023-07-01 06:06:53 +02:00
2018-11-16 16:54:37 -08:00
"""Creates a shift scheduling problem and solves it."""
from absl import app
from absl import flags
2023-11-16 19:46:56 +01:00
from ortools.sat.python import cp_model
2020-11-30 11:59:56 +01:00
2022-10-07 18:21:23 +02:00
_OUTPUT_PROTO = flags.DEFINE_string(
2023-07-01 06:06:53 +02:00
"output_proto", "", "Output file to write the cp_model proto to."
)
_PARAMS = flags.DEFINE_string(
"params", "max_time_in_seconds:10.0", "Sat solver parameters."
)
2018-11-18 09:04:16 -08:00
2018-11-16 16:54:37 -08:00
2023-11-16 19:46:56 +01:00
def negated_bounded_span(
works: list[cp_model.BoolVarT], start: int, length: int
) -> list[cp_model.BoolVarT]:
2025-03-07 10:33:36 +01:00
"""Filters an isolated sub-sequence of variables assigned to True.
2018-11-16 16:54:37 -08:00
2023-07-01 06:06:53 +02:00
Extract the span of Boolean variables [start, start + length), negate them,
and if there is variables to the left/right of this span, surround the span by
them in non negated form.
2018-11-18 09:04:16 -08:00
2023-07-01 06:06:53 +02:00
Args:
works: a list of variables to extract the span from.
start: the start to the span.
length: the length of the span.
2018-11-18 09:04:16 -08:00
2023-07-01 06:06:53 +02:00
Returns:
a list of variables which conjunction will be false if the sub-list is
assigned to True, and correctly bounded by variables assigned to False,
or by the start or end of works.
"""
2018-11-16 16:54:37 -08:00
sequence = []
2023-11-16 19:46:56 +01:00
# left border (start of works, or works[start - 1])
2018-11-18 09:04:16 -08:00
if start > 0:
sequence.append(works[start - 1])
for i in range(length):
sequence.append(~works[start + i])
2023-11-16 19:46:56 +01:00
# right border (end of works or works[start + length])
2018-11-21 11:00:26 -08:00
if start + length < len(works):
2018-11-18 09:04:16 -08:00
sequence.append(works[start + length])
2018-11-16 16:54:37 -08:00
return sequence
2023-07-01 06:06:53 +02:00
def add_soft_sequence_constraint(
2023-11-16 19:46:56 +01:00
model: cp_model.CpModel,
works: list[cp_model.BoolVarT],
hard_min: int,
soft_min: int,
min_cost: int,
soft_max: int,
hard_max: int,
max_cost: int,
prefix: str,
) -> tuple[list[cp_model.BoolVarT], list[int]]:
2018-11-18 09:04:16 -08:00
"""Sequence constraint on true variables with soft and hard bounds.
2023-07-01 06:06:53 +02:00
This constraint look at every maximal contiguous sequence of variables
assigned to true. If forbids sequence of length < hard_min or > hard_max.
Then it creates penalty terms if the length is < soft_min or > soft_max.
Args:
model: the sequence constraint is built on this model.
works: a list of Boolean variables.
hard_min: any sequence of true variables must have a length of at least
hard_min.
soft_min: any sequence should have a length of at least soft_min, or a
linear penalty on the delta will be added to the objective.
min_cost: the coefficient of the linear penalty if the length is less than
soft_min.
soft_max: any sequence should have a length of at most soft_max, or a linear
penalty on the delta will be added to the objective.
hard_max: any sequence of true variables must have a length of at most
hard_max.
max_cost: the coefficient of the linear penalty if the length is more than
soft_max.
prefix: a base name for penalty literals.
Returns:
a tuple (variables_list, coefficient_list) containing the different
penalties created by the sequence constraint.
"""
2018-11-16 16:54:37 -08:00
cost_literals = []
cost_coefficients = []
2018-11-18 09:04:16 -08:00
# Forbid sequences that are too short.
for length in range(1, hard_min):
for start in range(len(works) - length + 1):
2023-11-16 19:46:56 +01:00
model.add_bool_or(negated_bounded_span(works, start, length))
2018-11-18 09:04:16 -08:00
# Penalize sequences that are below the soft limit.
if min_cost > 0:
for length in range(hard_min, soft_min):
for start in range(len(works) - length + 1):
2018-11-21 11:00:26 -08:00
span = negated_bounded_span(works, start, length)
name = f": under_span(start={start}, length={length})"
2023-11-16 19:46:56 +01:00
lit = model.new_bool_var(prefix + name)
2018-11-18 09:04:16 -08:00
span.append(lit)
2023-11-16 19:46:56 +01:00
model.add_bool_or(span)
2018-11-18 09:04:16 -08:00
cost_literals.append(lit)
# We filter exactly the sequence with a short length.
# The penalty is proportional to the delta with soft_min.
cost_coefficients.append(min_cost * (soft_min - length))
# Penalize sequences that are above the soft limit.
if max_cost > 0:
for length in range(soft_max + 1, hard_max + 1):
for start in range(len(works) - length + 1):
2018-11-21 11:00:26 -08:00
span = negated_bounded_span(works, start, length)
name = f": over_span(start={start}, length={length})"
2023-11-16 19:46:56 +01:00
lit = model.new_bool_var(prefix + name)
2018-11-18 09:04:16 -08:00
span.append(lit)
2023-11-16 19:46:56 +01:00
model.add_bool_or(span)
2018-11-18 09:04:16 -08:00
cost_literals.append(lit)
2018-11-21 11:00:26 -08:00
# Cost paid is max_cost * excess length.
cost_coefficients.append(max_cost * (length - soft_max))
2018-11-16 16:54:37 -08:00
# Just forbid any sequence of true variables with length hard_max + 1
2021-02-15 16:04:27 +01:00
for start in range(len(works) - hard_max):
model.add_bool_or([~works[i] for i in range(start, start + hard_max + 1)])
2018-11-16 16:54:37 -08:00
return cost_literals, cost_coefficients
2023-07-01 06:06:53 +02:00
def add_soft_sum_constraint(
2023-11-16 19:46:56 +01:00
model: cp_model.CpModel,
works: list[cp_model.BoolVarT],
hard_min: int,
soft_min: int,
min_cost: int,
soft_max: int,
hard_max: int,
max_cost: int,
prefix: str,
) -> tuple[list[cp_model.IntVar], list[int]]:
"""sum constraint with soft and hard bounds.
2018-11-16 16:54:37 -08:00
2023-07-01 06:06:53 +02:00
This constraint counts the variables assigned to true from works.
If forbids sum < hard_min or > hard_max.
Then it creates penalty terms if the sum is < soft_min or > soft_max.
Args:
model: the sequence constraint is built on this model.
works: a list of Boolean variables.
hard_min: any sequence of true variables must have a sum of at least
hard_min.
soft_min: any sequence should have a sum of at least soft_min, or a linear
penalty on the delta will be added to the objective.
min_cost: the coefficient of the linear penalty if the sum is less than
soft_min.
soft_max: any sequence should have a sum of at most soft_max, or a linear
penalty on the delta will be added to the objective.
hard_max: any sequence of true variables must have a sum of at most
hard_max.
max_cost: the coefficient of the linear penalty if the sum is more than
soft_max.
prefix: a base name for penalty variables.
Returns:
a tuple (variables_list, coefficient_list) containing the different
penalties created by the sequence constraint.
"""
2018-11-16 16:54:37 -08:00
cost_variables = []
cost_coefficients = []
2023-11-16 19:46:56 +01:00
sum_var = model.new_int_var(hard_min, hard_max, "")
2018-11-18 09:04:16 -08:00
# This adds the hard constraints on the sum.
2023-11-16 19:46:56 +01:00
model.add(sum_var == sum(works))
2018-11-16 16:54:37 -08:00
2018-11-18 09:04:16 -08:00
# Penalize sums below the soft_min target.
2018-11-16 16:54:37 -08:00
if soft_min > hard_min and min_cost > 0:
2023-11-16 19:46:56 +01:00
delta = model.new_int_var(-len(works), len(works), "")
model.add(delta == soft_min - sum_var)
2018-11-21 11:00:26 -08:00
# TODO(user): Compare efficiency with only excess >= soft_min - sum_var.
2023-11-16 19:46:56 +01:00
excess = model.new_int_var(0, 7, prefix + ": under_sum")
model.add_max_equality(excess, [delta, 0])
2018-11-16 16:54:37 -08:00
cost_variables.append(excess)
cost_coefficients.append(min_cost)
2018-11-18 09:04:16 -08:00
# Penalize sums above the soft_max target.
2018-11-16 16:54:37 -08:00
if soft_max < hard_max and max_cost > 0:
2023-11-16 19:46:56 +01:00
delta = model.new_int_var(-7, 7, "")
model.add(delta == sum_var - soft_max)
excess = model.new_int_var(0, 7, prefix + ": over_sum")
model.add_max_equality(excess, [delta, 0])
2018-11-16 16:54:37 -08:00
cost_variables.append(excess)
cost_coefficients.append(max_cost)
2018-11-18 09:04:16 -08:00
2018-11-16 16:54:37 -08:00
return cost_variables, cost_coefficients
2023-11-16 19:46:56 +01:00
def solve_shift_scheduling(params: str, output_proto: str):
2018-11-16 16:54:37 -08:00
"""Solves the shift scheduling problem."""
# Data
num_employees = 8
num_weeks = 3
2023-07-01 06:06:53 +02:00
shifts = ["O", "M", "A", "N"]
2018-11-16 16:54:37 -08:00
2018-11-21 11:00:26 -08:00
# Fixed assignment: (employee, shift, day).
2018-11-16 16:54:37 -08:00
# This fixes the first 2 days of the schedule.
fixed_assignments = [
(0, 0, 0),
(1, 0, 0),
2018-11-21 11:00:26 -08:00
(2, 1, 0),
(3, 1, 0),
(4, 2, 0),
(5, 2, 0),
(6, 2, 3),
(7, 3, 0),
2018-11-16 16:54:37 -08:00
(0, 1, 1),
(1, 1, 1),
2018-11-21 11:00:26 -08:00
(2, 2, 1),
(3, 2, 1),
(4, 2, 1),
(5, 0, 1),
(6, 0, 1),
(7, 3, 1),
2018-11-16 16:54:37 -08:00
]
2018-11-21 11:00:26 -08:00
# Request: (employee, shift, day, weight)
# A negative weight indicates that the employee desire this assignment.
requests = [
2022-03-28 16:42:17 +02:00
# Employee 3 does not want to work on the first Saturday (negative weight
# for the Off shift).
2018-11-21 11:00:26 -08:00
(3, 0, 5, -2),
2021-12-29 16:47:49 +01:00
# Employee 5 wants a night shift on the second Thursday (negative weight).
2018-11-21 11:00:26 -08:00
(5, 3, 10, -2),
2022-03-28 16:42:17 +02:00
# Employee 2 does not want a night shift on the first Friday (positive
# weight).
2023-07-01 06:06:53 +02:00
(2, 3, 4, 4),
2018-11-16 16:54:37 -08:00
]
# Shift constraints on continuous sequence :
2018-11-18 09:04:16 -08:00
# (shift, hard_min, soft_min, min_penalty,
# soft_max, hard_max, max_penalty)
2018-11-16 16:54:37 -08:00
shift_constraints = [
# One or two consecutive days of rest, this is a hard constraint.
(0, 1, 1, 0, 2, 2, 0),
2022-03-28 16:42:17 +02:00
# between 2 and 3 consecutive days of night shifts, 1 and 4 are
2018-11-16 16:54:37 -08:00
# possible but penalized.
(3, 1, 2, 20, 3, 4, 5),
]
# Weekly sum constraints on shifts days:
# (shift, hard_min, soft_min, min_penalty,
# soft_max, hard_max, max_penalty)
weekly_sum_constraints = [
# Constraints on rests per week.
(0, 1, 2, 7, 2, 3, 4),
# At least 1 night shift per week (penalized). At most 4 (hard).
(3, 0, 1, 3, 4, 4, 0),
]
# Penalized transitions:
# (previous_shift, next_shift, penalty (0 means forbidden))
penalized_transitions = [
# Afternoon to night has a penalty of 4.
(2, 3, 4),
# Night to morning is forbidden.
(3, 1, 0),
]
2025-03-07 10:33:36 +01:00
# daily demands for work shifts (morning, afternoon, night) for each day
2018-11-16 16:54:37 -08:00
# of the week starting on Monday.
weekly_cover_demands = [
(2, 3, 1), # Monday
(2, 3, 1), # Tuesday
(2, 2, 2), # Wednesday
(2, 3, 1), # Thursday
(2, 2, 2), # Friday
(1, 2, 3), # Saturday
(1, 3, 1), # Sunday
]
# Penalty for exceeding the cover constraint per shift type.
excess_cover_penalties = (2, 2, 5)
num_days = num_weeks * 7
num_shifts = len(shifts)
model = cp_model.CpModel()
work = {}
for e in range(num_employees):
for s in range(num_shifts):
for d in range(num_days):
work[e, s, d] = model.new_bool_var(f"work{e}_{s}_{d}")
2018-11-16 16:54:37 -08:00
# Linear terms of the objective in a minimization context.
2023-11-16 19:46:56 +01:00
obj_int_vars: list[cp_model.IntVar] = []
obj_int_coeffs: list[int] = []
obj_bool_vars: list[cp_model.BoolVarT] = []
obj_bool_coeffs: list[int] = []
2018-11-16 16:54:37 -08:00
# Exactly one shift per day.
for e in range(num_employees):
for d in range(num_days):
2023-11-16 19:46:56 +01:00
model.add_exactly_one(work[e, s, d] for s in range(num_shifts))
2018-11-16 16:54:37 -08:00
# Fixed assignments.
2018-11-21 11:00:26 -08:00
for e, s, d in fixed_assignments:
2023-11-16 19:46:56 +01:00
model.add(work[e, s, d] == 1)
2018-11-16 16:54:37 -08:00
# Employee requests
2018-11-21 11:00:26 -08:00
for e, s, d, w in requests:
2018-11-18 09:04:16 -08:00
obj_bool_vars.append(work[e, s, d])
2018-11-21 11:00:26 -08:00
obj_bool_coeffs.append(w)
2018-11-16 16:54:37 -08:00
# Shift constraints
for ct in shift_constraints:
shift, hard_min, soft_min, min_cost, soft_max, hard_max, max_cost = ct
for e in range(num_employees):
works = [work[e, shift, d] for d in range(num_days)]
2018-11-21 11:00:26 -08:00
variables, coeffs = add_soft_sequence_constraint(
2023-07-01 06:06:53 +02:00
model,
works,
hard_min,
soft_min,
min_cost,
soft_max,
hard_max,
max_cost,
f"shift_constraint(employee {e}, shift {shift})",
2023-07-01 06:06:53 +02:00
)
2018-11-18 09:04:16 -08:00
obj_bool_vars.extend(variables)
obj_bool_coeffs.extend(coeffs)
2018-11-16 16:54:37 -08:00
# Weekly sum constraints
for ct in weekly_sum_constraints:
shift, hard_min, soft_min, min_cost, soft_max, hard_max, max_cost = ct
for e in range(num_employees):
for w in range(num_weeks):
works = [work[e, shift, d + w * 7] for d in range(7)]
2018-11-21 11:00:26 -08:00
variables, coeffs = add_soft_sum_constraint(
2023-07-01 06:06:53 +02:00
model,
works,
hard_min,
soft_min,
min_cost,
soft_max,
hard_max,
max_cost,
f"weekly_sum_constraint(employee {e}, shift {shift}, week {w})",
2023-07-01 06:06:53 +02:00
)
2018-11-18 09:04:16 -08:00
obj_int_vars.extend(variables)
obj_int_coeffs.extend(coeffs)
2018-11-16 16:54:37 -08:00
# Penalized transitions
for previous_shift, next_shift, cost in penalized_transitions:
for e in range(num_employees):
for d in range(num_days - 1):
transition = [
~work[e, previous_shift, d],
~work[e, next_shift, d + 1],
2018-11-16 16:54:37 -08:00
]
if cost == 0:
2023-11-16 19:46:56 +01:00
model.add_bool_or(transition)
2018-11-16 16:54:37 -08:00
else:
2023-11-16 19:46:56 +01:00
trans_var = model.new_bool_var(
f"transition (employee={e}, day={d})"
2023-07-01 06:06:53 +02:00
)
2018-11-16 16:54:37 -08:00
transition.append(trans_var)
2023-11-16 19:46:56 +01:00
model.add_bool_or(transition)
2018-11-18 09:04:16 -08:00
obj_bool_vars.append(trans_var)
obj_bool_coeffs.append(cost)
2018-11-16 16:54:37 -08:00
# Cover constraints
for s in range(1, num_shifts):
for w in range(num_weeks):
for d in range(7):
works = [work[e, s, w * 7 + d] for e in range(num_employees)]
2018-11-18 09:04:16 -08:00
# Ignore Off shift.
2018-11-16 16:54:37 -08:00
min_demand = weekly_cover_demands[d][s - 1]
2023-11-16 19:46:56 +01:00
worked = model.new_int_var(min_demand, num_employees, "")
model.add(worked == sum(works))
2018-11-16 16:54:37 -08:00
over_penalty = excess_cover_penalties[s - 1]
if over_penalty > 0:
name = f"excess_demand(shift={s}, week={w}, day={d})"
2023-11-16 19:46:56 +01:00
excess = model.new_int_var(0, num_employees - min_demand, name)
model.add(excess == worked - min_demand)
2018-11-18 09:04:16 -08:00
obj_int_vars.append(excess)
obj_int_coeffs.append(over_penalty)
2018-11-16 16:54:37 -08:00
# Objective
2023-11-16 19:46:56 +01:00
model.minimize(
2023-07-01 06:06:53 +02:00
sum(obj_bool_vars[i] * obj_bool_coeffs[i] for i in range(len(obj_bool_vars)))
+ sum(obj_int_vars[i] * obj_int_coeffs[i] for i in range(len(obj_int_vars)))
)
2018-11-18 09:04:16 -08:00
2021-09-03 15:21:25 +02:00
if output_proto:
print(f"Writing proto to {output_proto}")
2023-07-01 06:06:53 +02:00
with open(output_proto, "w") as text_file:
2018-11-18 09:04:16 -08:00
text_file.write(str(model))
2018-11-16 16:54:37 -08:00
# Solve the model.
solver = cp_model.CpSolver()
2018-11-18 09:04:16 -08:00
if params:
2025-07-23 17:38:49 +02:00
solver.parameters.parse_text_format(params)
2018-11-21 11:00:26 -08:00
solution_printer = cp_model.ObjectiveSolutionPrinter()
2023-11-16 19:46:56 +01:00
status = solver.solve(model, solution_printer)
2018-11-16 16:54:37 -08:00
# Print solution.
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
print()
2023-07-01 06:06:53 +02:00
header = " "
2018-11-16 16:54:37 -08:00
for w in range(num_weeks):
2023-07-01 06:06:53 +02:00
header += "M T W T F S S "
2018-11-16 16:54:37 -08:00
print(header)
for e in range(num_employees):
2023-07-01 06:06:53 +02:00
schedule = ""
2018-11-16 16:54:37 -08:00
for d in range(num_days):
for s in range(num_shifts):
2023-11-16 19:46:56 +01:00
if solver.boolean_value(work[e, s, d]):
2023-07-01 06:06:53 +02:00
schedule += shifts[s] + " "
print(f"worker {e}: {schedule}")
2018-11-18 09:04:16 -08:00
print()
2023-07-01 06:06:53 +02:00
print("Penalties:")
2018-11-18 09:04:16 -08:00
for i, var in enumerate(obj_bool_vars):
2023-11-16 19:46:56 +01:00
if solver.boolean_value(var):
2018-11-18 09:04:16 -08:00
penalty = obj_bool_coeffs[i]
if penalty > 0:
2023-11-16 19:46:56 +01:00
print(f" {var.name} violated, penalty={penalty}")
2018-11-18 09:04:16 -08:00
else:
2023-11-16 19:46:56 +01:00
print(f" {var.name} fulfilled, gain={-penalty}")
2018-11-18 09:04:16 -08:00
for i, var in enumerate(obj_int_vars):
2023-11-16 19:46:56 +01:00
if solver.value(var) > 0:
2023-07-01 06:06:53 +02:00
print(
f" {var.name} violated by {solver.value(var)}, linear"
f" penalty={obj_int_coeffs[i]}"
2023-07-01 06:06:53 +02:00
)
2018-11-16 16:54:37 -08:00
print()
print(solver.response_stats())
2018-11-16 16:54:37 -08:00
def main(_):
2022-10-07 18:21:23 +02:00
solve_shift_scheduling(_PARAMS.value, _OUTPUT_PROTO.value)
2018-11-18 09:04:16 -08:00
2023-07-01 06:06:53 +02:00
if __name__ == "__main__":
app.run(main)