use black on examples/python

This commit is contained in:
Laurent Perron
2023-07-01 06:06:53 +02:00
parent d65333dab0
commit 84ec414e61
41 changed files with 18801 additions and 4140 deletions

244
examples/python/shift_scheduling_sat.py Executable file → Normal file
View File

@@ -11,6 +11,7 @@
# 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.
"""Creates a shift scheduling problem and solves it."""
from absl import app
@@ -20,28 +21,30 @@ from ortools.sat.python import cp_model
from google.protobuf import text_format
_OUTPUT_PROTO = flags.DEFINE_string(
'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.')
"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."
)
def negated_bounded_span(works, start, length):
"""Filters an isolated sub-sequence of variables assined to True.
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.
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.
Args:
works: a list of variables to extract the span from.
start: the start to the span.
length: the length of the span.
Args:
works: a list of variables to extract the span from.
start: the start to the span.
length: the length of the span.
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.
"""
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.
"""
sequence = []
# Left border (start of works, or works[start - 1])
if start > 0:
@@ -54,35 +57,36 @@ def negated_bounded_span(works, start, length):
return sequence
def add_soft_sequence_constraint(model, works, hard_min, soft_min, min_cost,
soft_max, hard_max, max_cost, prefix):
def add_soft_sequence_constraint(
model, works, hard_min, soft_min, min_cost, soft_max, hard_max, max_cost, prefix
):
"""Sequence constraint on true variables with soft and hard bounds.
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.
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.
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.
"""
Returns:
a tuple (variables_list, coefficient_list) containing the different
penalties created by the sequence constraint.
"""
cost_literals = []
cost_coefficients = []
@@ -96,7 +100,7 @@ def add_soft_sequence_constraint(model, works, hard_min, soft_min, min_cost,
for length in range(hard_min, soft_min):
for start in range(len(works) - length + 1):
span = negated_bounded_span(works, start, length)
name = ': under_span(start=%i, length=%i)' % (start, length)
name = ": under_span(start=%i, length=%i)" % (start, length)
lit = model.NewBoolVar(prefix + name)
span.append(lit)
model.AddBoolOr(span)
@@ -110,7 +114,7 @@ def add_soft_sequence_constraint(model, works, hard_min, soft_min, min_cost,
for length in range(soft_max + 1, hard_max + 1):
for start in range(len(works) - length + 1):
span = negated_bounded_span(works, start, length)
name = ': over_span(start=%i, length=%i)' % (start, length)
name = ": over_span(start=%i, length=%i)" % (start, length)
lit = model.NewBoolVar(prefix + name)
span.append(lit)
model.AddBoolOr(span)
@@ -120,61 +124,61 @@ def add_soft_sequence_constraint(model, works, hard_min, soft_min, min_cost,
# Just forbid any sequence of true variables with length hard_max + 1
for start in range(len(works) - hard_max):
model.AddBoolOr(
[works[i].Not() for i in range(start, start + hard_max + 1)])
model.AddBoolOr([works[i].Not() for i in range(start, start + hard_max + 1)])
return cost_literals, cost_coefficients
def add_soft_sum_constraint(model, works, hard_min, soft_min, min_cost,
soft_max, hard_max, max_cost, prefix):
def add_soft_sum_constraint(
model, works, hard_min, soft_min, min_cost, soft_max, hard_max, max_cost, prefix
):
"""Sum constraint with soft and hard bounds.
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.
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.
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.
"""
Returns:
a tuple (variables_list, coefficient_list) containing the different
penalties created by the sequence constraint.
"""
cost_variables = []
cost_coefficients = []
sum_var = model.NewIntVar(hard_min, hard_max, '')
sum_var = model.NewIntVar(hard_min, hard_max, "")
# This adds the hard constraints on the sum.
model.Add(sum_var == sum(works))
# Penalize sums below the soft_min target.
if soft_min > hard_min and min_cost > 0:
delta = model.NewIntVar(-len(works), len(works), '')
delta = model.NewIntVar(-len(works), len(works), "")
model.Add(delta == soft_min - sum_var)
# TODO(user): Compare efficiency with only excess >= soft_min - sum_var.
excess = model.NewIntVar(0, 7, prefix + ': under_sum')
excess = model.NewIntVar(0, 7, prefix + ": under_sum")
model.AddMaxEquality(excess, [delta, 0])
cost_variables.append(excess)
cost_coefficients.append(min_cost)
# Penalize sums above the soft_max target.
if soft_max < hard_max and max_cost > 0:
delta = model.NewIntVar(-7, 7, '')
delta = model.NewIntVar(-7, 7, "")
model.Add(delta == sum_var - soft_max)
excess = model.NewIntVar(0, 7, prefix + ': over_sum')
excess = model.NewIntVar(0, 7, prefix + ": over_sum")
model.AddMaxEquality(excess, [delta, 0])
cost_variables.append(excess)
cost_coefficients.append(max_cost)
@@ -187,7 +191,7 @@ def solve_shift_scheduling(params, output_proto):
# Data
num_employees = 8
num_weeks = 3
shifts = ['O', 'M', 'A', 'N']
shifts = ["O", "M", "A", "N"]
# Fixed assignment: (employee, shift, day).
# This fixes the first 2 days of the schedule.
@@ -220,7 +224,7 @@ def solve_shift_scheduling(params, output_proto):
(5, 3, 10, -2),
# Employee 2 does not want a night shift on the first Friday (positive
# weight).
(2, 3, 4, 4)
(2, 3, 4, 4),
]
# Shift constraints on continuous sequence :
@@ -277,7 +281,7 @@ def solve_shift_scheduling(params, output_proto):
for e in range(num_employees):
for s in range(num_shifts):
for d in range(num_days):
work[e, s, d] = model.NewBoolVar('work%i_%i_%i' % (e, s, d))
work[e, s, d] = model.NewBoolVar("work%i_%i_%i" % (e, s, d))
# Linear terms of the objective in a minimization context.
obj_int_vars = []
@@ -305,9 +309,16 @@ def solve_shift_scheduling(params, output_proto):
for e in range(num_employees):
works = [work[e, shift, d] for d in range(num_days)]
variables, coeffs = add_soft_sequence_constraint(
model, works, hard_min, soft_min, min_cost, soft_max, hard_max,
model,
works,
hard_min,
soft_min,
min_cost,
soft_max,
hard_max,
max_cost,
'shift_constraint(employee %i, shift %i)' % (e, shift))
"shift_constraint(employee %i, shift %i)" % (e, shift),
)
obj_bool_vars.extend(variables)
obj_bool_coeffs.extend(coeffs)
@@ -318,10 +329,17 @@ def solve_shift_scheduling(params, output_proto):
for w in range(num_weeks):
works = [work[e, shift, d + w * 7] for d in range(7)]
variables, coeffs = add_soft_sum_constraint(
model, works, hard_min, soft_min, min_cost, soft_max,
hard_max, max_cost,
'weekly_sum_constraint(employee %i, shift %i, week %i)' %
(e, shift, w))
model,
works,
hard_min,
soft_min,
min_cost,
soft_max,
hard_max,
max_cost,
"weekly_sum_constraint(employee %i, shift %i, week %i)"
% (e, shift, w),
)
obj_int_vars.extend(variables)
obj_int_coeffs.extend(coeffs)
@@ -330,14 +348,15 @@ def solve_shift_scheduling(params, output_proto):
for e in range(num_employees):
for d in range(num_days - 1):
transition = [
work[e, previous_shift, d].Not(), work[e, next_shift,
d + 1].Not()
work[e, previous_shift, d].Not(),
work[e, next_shift, d + 1].Not(),
]
if cost == 0:
model.AddBoolOr(transition)
else:
trans_var = model.NewBoolVar(
'transition (employee=%i, day=%i)' % (e, d))
"transition (employee=%i, day=%i)" % (e, d)
)
transition.append(trans_var)
model.AddBoolOr(transition)
obj_bool_vars.append(trans_var)
@@ -350,28 +369,25 @@ def solve_shift_scheduling(params, output_proto):
works = [work[e, s, w * 7 + d] for e in range(num_employees)]
# Ignore Off shift.
min_demand = weekly_cover_demands[d][s - 1]
worked = model.NewIntVar(min_demand, num_employees, '')
worked = model.NewIntVar(min_demand, num_employees, "")
model.Add(worked == sum(works))
over_penalty = excess_cover_penalties[s - 1]
if over_penalty > 0:
name = 'excess_demand(shift=%i, week=%i, day=%i)' % (s, w,
d)
excess = model.NewIntVar(0, num_employees - min_demand,
name)
name = "excess_demand(shift=%i, week=%i, day=%i)" % (s, w, d)
excess = model.NewIntVar(0, num_employees - min_demand, name)
model.Add(excess == worked - min_demand)
obj_int_vars.append(excess)
obj_int_coeffs.append(over_penalty)
# Objective
model.Minimize(
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))))
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)))
)
if output_proto:
print('Writing proto to %s' % output_proto)
with open(output_proto, 'w') as text_file:
print("Writing proto to %s" % output_proto)
with open(output_proto, "w") as text_file:
text_file.write(str(model))
# Solve the model.
@@ -384,43 +400,45 @@ def solve_shift_scheduling(params, output_proto):
# Print solution.
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
print()
header = ' '
header = " "
for w in range(num_weeks):
header += 'M T W T F S S '
header += "M T W T F S S "
print(header)
for e in range(num_employees):
schedule = ''
schedule = ""
for d in range(num_days):
for s in range(num_shifts):
if solver.BooleanValue(work[e, s, d]):
schedule += shifts[s] + ' '
print('worker %i: %s' % (e, schedule))
schedule += shifts[s] + " "
print("worker %i: %s" % (e, schedule))
print()
print('Penalties:')
print("Penalties:")
for i, var in enumerate(obj_bool_vars):
if solver.BooleanValue(var):
penalty = obj_bool_coeffs[i]
if penalty > 0:
print(' %s violated, penalty=%i' % (var.Name(), penalty))
print(" %s violated, penalty=%i" % (var.Name(), penalty))
else:
print(' %s fulfilled, gain=%i' % (var.Name(), -penalty))
print(" %s fulfilled, gain=%i" % (var.Name(), -penalty))
for i, var in enumerate(obj_int_vars):
if solver.Value(var) > 0:
print(' %s violated by %i, linear penalty=%i' %
(var.Name(), solver.Value(var), obj_int_coeffs[i]))
print(
" %s violated by %i, linear penalty=%i"
% (var.Name(), solver.Value(var), obj_int_coeffs[i])
)
print()
print('Statistics')
print(' - status : %s' % solver.StatusName(status))
print(' - conflicts : %i' % solver.NumConflicts())
print(' - branches : %i' % solver.NumBranches())
print(' - wall time : %f s' % solver.WallTime())
print("Statistics")
print(" - status : %s" % solver.StatusName(status))
print(" - conflicts : %i" % solver.NumConflicts())
print(" - branches : %i" % solver.NumBranches())
print(" - wall time : %f s" % solver.WallTime())
def main(_):
solve_shift_scheduling(_PARAMS.value, _OUTPUT_PROTO.value)
if __name__ == '__main__':
if __name__ == "__main__":
app.run(main)