[CP-SAT] convert to PEP8 convention

This commit is contained in:
Laurent Perron
2023-11-16 19:46:56 +01:00
parent 7f48cd9a9a
commit 5b6c803db3
103 changed files with 4648 additions and 3430 deletions

View File

@@ -29,13 +29,13 @@ class ObjectivePrinter(cp_model.CpSolverSolutionCallback):
def on_solution_callback(self):
print(
"Solution %i, time = %f s, objective = %i"
% (self.__solution_count, self.WallTime(), self.ObjectiveValue())
% (self.__solution_count, self.wall_time, self.objective_value)
)
self.__solution_count += 1
def tasks_and_workers_assignment_sat():
"""Solve the assignment problem."""
"""solve the assignment problem."""
model = cp_model.CpModel()
# CP-SAT solver is integer only.
@@ -53,71 +53,71 @@ def tasks_and_workers_assignment_sat():
x = {}
for i in all_workers:
for j in all_groups:
x[i, j] = model.NewBoolVar("x[%i,%i]" % (i, j))
x[i, j] = model.new_bool_var("x[%i,%i]" % (i, j))
## y_kj is 1 if task k is assigned to group j
y = {}
for k in all_tasks:
for j in all_groups:
y[k, j] = model.NewBoolVar("x[%i,%i]" % (k, j))
y[k, j] = model.new_bool_var("x[%i,%i]" % (k, j))
# Constraints
# Each task k is assigned to a group and only one.
for k in all_tasks:
model.Add(sum(y[k, j] for j in all_groups) == 1)
model.add(sum(y[k, j] for j in all_groups) == 1)
# Each worker i is assigned to a group and only one.
for i in all_workers:
model.Add(sum(x[i, j] for j in all_groups) == 1)
model.add(sum(x[i, j] for j in all_groups) == 1)
# cost per group
# Cost per group
sum_of_costs = sum(task_cost)
averages = []
num_workers_in_group = []
scaled_sum_of_costs_in_group = []
scaling = 1000 # We introduce scaling to deal with floating point average.
for j in all_groups:
n = model.NewIntVar(1, num_workers, "num_workers_in_group_%i" % j)
model.Add(n == sum(x[i, j] for i in all_workers))
c = model.NewIntVar(0, sum_of_costs * scaling, "sum_of_costs_of_group_%i" % j)
model.Add(c == sum(y[k, j] * task_cost[k] * scaling for k in all_tasks))
a = model.NewIntVar(0, sum_of_costs * scaling, "average_cost_of_group_%i" % j)
model.AddDivisionEquality(a, c, n)
n = model.new_int_var(1, num_workers, "num_workers_in_group_%i" % j)
model.add(n == sum(x[i, j] for i in all_workers))
c = model.new_int_var(0, sum_of_costs * scaling, "sum_of_costs_of_group_%i" % j)
model.add(c == sum(y[k, j] * task_cost[k] * scaling for k in all_tasks))
a = model.new_int_var(0, sum_of_costs * scaling, "average_cost_of_group_%i" % j)
model.add_division_equality(a, c, n)
averages.append(a)
num_workers_in_group.append(n)
scaled_sum_of_costs_in_group.append(c)
# All workers are assigned.
model.Add(sum(num_workers_in_group) == num_workers)
model.add(sum(num_workers_in_group) == num_workers)
# Objective.
obj = model.NewIntVar(0, sum_of_costs * scaling, "obj")
model.AddMaxEquality(obj, averages)
model.Minimize(obj)
obj = model.new_int_var(0, sum_of_costs * scaling, "obj")
model.add_max_equality(obj, averages)
model.minimize(obj)
# Solve and print out the solution.
solver = cp_model.CpSolver()
solver.parameters.max_time_in_seconds = 60 * 60 * 2
objective_printer = ObjectivePrinter()
status = solver.Solve(model, objective_printer)
print(solver.ResponseStats())
status = solver.solve(model, objective_printer)
print(solver.response_stats())
if status == cp_model.OPTIMAL:
for j in all_groups:
print("Group %i" % j)
for i in all_workers:
if solver.BooleanValue(x[i, j]):
if solver.boolean_value(x[i, j]):
print(" - worker %i" % i)
for k in all_tasks:
if solver.BooleanValue(y[k, j]):
if solver.boolean_value(y[k, j]):
print(" - task %i with cost %i" % (k, task_cost[k]))
print(
" - sum_of_costs = %i"
% (solver.Value(scaled_sum_of_costs_in_group[j]) // scaling)
% (solver.value(scaled_sum_of_costs_in_group[j]) // scaling)
)
print(" - average cost = %f" % (solver.Value(averages[j]) * 1.0 / scaling))
print(" - average cost = %f" % (solver.value(averages[j]) * 1.0 / scaling))
tasks_and_workers_assignment_sat()