forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vendor_scheduling_sat.py
144 lines (120 loc) · 4.93 KB
/
vendor_scheduling_sat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2010-2018 Google LLC
# 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
#
# 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.
"""Solves a simple shift scheduling problem."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ortools.sat.python import cp_model
class SolutionPrinter(cp_model.CpSolverSolutionCallback):
"""Print intermediate solutions."""
def __init__(self, num_vendors, num_hours, possible_schedules,
selected_schedules, hours_stat, min_vendors):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__solution_count = 0
self.__num_vendors = num_vendors
self.__num_hours = num_hours
self.__possible_schedules = possible_schedules
self.__selected_schedules = selected_schedules
self.__hours_stat = hours_stat
self.__min_vendors = min_vendors
def on_solution_callback(self):
"""Called at each new solution."""
self.__solution_count += 1
print('Solution %i: ', self.__solution_count)
print(' min vendors:', self.__min_vendors)
for i in range(self.__num_vendors):
print(' - vendor %i: ' % i, self.__possible_schedules[self.Value(
self.__selected_schedules[i])])
print()
for j in range(self.__num_hours):
print(' - # workers on day%2i: ' % j, end=' ')
print(self.Value(self.__hours_stat[j]), end=' ')
print()
print()
def solution_count(self):
"""Returns the number of solution found."""
return self.__solution_count
def main():
"""Create the shift scheduling model and solve it."""
# Create the model.
model = cp_model.CpModel()
#
# data
#
num_vendors = 9
num_hours = 10
num_work_types = 1
traffic = [100, 500, 100, 200, 320, 300, 200, 220, 300, 120]
max_traffic_per_vendor = 100
# Last columns are :
# index_of_the_schedule, sum of worked hours (per work type).
# The index is useful for branching.
possible_schedules = [[1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0,
8], [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1,
4], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 2,
5], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 4],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 4,
3], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0]]
num_possible_schedules = len(possible_schedules)
selected_schedules = []
vendors_stat = []
hours_stat = []
# Auxiliary data
min_vendors = [t // max_traffic_per_vendor for t in traffic]
all_vendors = range(num_vendors)
all_hours = range(num_hours)
#
# declare variables
#
x = {}
for v in all_vendors:
tmp = []
for h in all_hours:
x[v, h] = model.NewIntVar(0, num_work_types, 'x[%i,%i]' % (v, h))
tmp.append(x[v, h])
selected_schedule = model.NewIntVar(0, num_possible_schedules - 1,
's[%i]' % v)
hours = model.NewIntVar(0, num_hours, 'h[%i]' % v)
selected_schedules.append(selected_schedule)
vendors_stat.append(hours)
tmp.append(selected_schedule)
tmp.append(hours)
model.AddAllowedAssignments(tmp, possible_schedules)
#
# Statistics and constraints for each hour
#
for h in all_hours:
workers = model.NewIntVar(0, 1000, 'workers[%i]' % h)
model.Add(workers == sum(x[v, h] for v in all_vendors))
hours_stat.append(workers)
model.Add(workers * max_traffic_per_vendor >= traffic[h])
#
# Redundant constraint: sort selected_schedules
#
for v in range(num_vendors - 1):
model.Add(selected_schedules[v] <= selected_schedules[v + 1])
# Solve model.
solver = cp_model.CpSolver()
solution_printer = SolutionPrinter(num_vendors, num_hours,
possible_schedules, selected_schedules,
hours_stat, min_vendors)
status = solver.SearchForAllSolutions(model, solution_printer)
print('Status = %s' % solver.StatusName(status))
print('Statistics')
print(' - conflicts : %i' % solver.NumConflicts())
print(' - branches : %i' % solver.NumBranches())
print(' - wall time : %f s' % solver.WallTime())
print(
' - number of solutions found: %i' % solution_printer.solution_count())
if __name__ == '__main__':
main()