forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
wedding_optimal_chart_sat.py
250 lines (212 loc) · 8.17 KB
/
wedding_optimal_chart_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
# Copyright 2010-2024 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.
"""Finding an optimal wedding seating chart.
From
Meghan L. Bellows and J. D. Luc Peterson
"Finding an optimal seating chart for a wedding"
http://www.improbable.com/news/2012/Optimal-seating-chart.pdf
http://www.improbable.com/2012/02/12/finding-an-optimal-seating-chart-for-a-wedding
Every year, millions of brides (not to mention their mothers, future
mothers-in-law, and occasionally grooms) struggle with one of the
most daunting tasks during the wedding-planning process: the
seating chart. The guest responses are in, banquet hall is booked,
menu choices have been made. You think the hard parts are over,
but you have yet to embark upon the biggest headache of them all.
In order to make this process easier, we present a mathematical
formulation that models the seating chart problem. This model can
be solved to find the optimal arrangement of guests at tables.
At the very least, it can provide a starting point and hopefully
minimize stress and arguments.
Adapted from
https://github.com/google/or-tools/blob/master/examples/csharp/wedding_optimal_chart.cs
"""
import time
from typing import Sequence
from absl import app
from ortools.sat.python import cp_model
class WeddingChartPrinter(cp_model.CpSolverSolutionCallback):
"""Print intermediate solutions."""
def __init__(self, seats, names, num_tables, num_guests):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__solution_count = 0
self.__start_time = time.time()
self.__seats = seats
self.__names = names
self.__num_tables = num_tables
self.__num_guests = num_guests
def on_solution_callback(self):
current_time = time.time()
objective = self.objective_value
print(
"Solution %i, time = %f s, objective = %i"
% (self.__solution_count, current_time - self.__start_time, objective)
)
self.__solution_count += 1
for t in range(self.__num_tables):
print("Table %d: " % t)
for g in range(self.__num_guests):
if self.value(self.__seats[(t, g)]):
print(" " + self.__names[g])
def num_solutions(self) -> int:
return self.__solution_count
def build_data():
"""Build the data model."""
# Easy problem (from the paper)
# num_tables = 2
# table_capacity = 10
# min_known_neighbors = 1
# Slightly harder problem (also from the paper)
num_tables = 5
table_capacity = 4
min_known_neighbors = 1
# Connection matrix: who knows who, and how strong
# is the relation
connections = [
[1, 50, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[50, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 50, 1, 1, 1, 1, 10, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 50, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 50, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 50, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 50, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 50, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 10, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 50, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
]
# Names of the guests. B: Bride side, G: Groom side
names = [
"Deb (B)",
"John (B)",
"Martha (B)",
"Travis (B)",
"Allan (B)",
"Lois (B)",
"Jayne (B)",
"Brad (B)",
"Abby (B)",
"Mary Helen (G)",
"Lee (G)",
"Annika (G)",
"Carl (G)",
"Colin (G)",
"Shirley (G)",
"DeAnn (G)",
"Lori (G)",
]
return num_tables, table_capacity, min_known_neighbors, connections, names
def solve_with_discrete_model() -> None:
"""Discrete approach."""
num_tables, table_capacity, min_known_neighbors, connections, names = build_data()
num_guests = len(connections)
all_tables = range(num_tables)
all_guests = range(num_guests)
# Create the cp model.
model = cp_model.CpModel()
#
# Decision variables
#
seats = {}
for t in all_tables:
for g in all_guests:
seats[(t, g)] = model.new_bool_var("guest %i seats on table %i" % (g, t))
colocated = {}
for g1 in range(num_guests - 1):
for g2 in range(g1 + 1, num_guests):
colocated[(g1, g2)] = model.new_bool_var(
"guest %i seats with guest %i" % (g1, g2)
)
same_table = {}
for g1 in range(num_guests - 1):
for g2 in range(g1 + 1, num_guests):
for t in all_tables:
same_table[(g1, g2, t)] = model.new_bool_var(
"guest %i seats with guest %i on table %i" % (g1, g2, t)
)
# Objective
model.maximize(
sum(
connections[g1][g2] * colocated[g1, g2]
for g1 in range(num_guests - 1)
for g2 in range(g1 + 1, num_guests)
if connections[g1][g2] > 0
)
)
#
# Constraints
#
# Everybody seats at one table.
for g in all_guests:
model.add(sum(seats[(t, g)] for t in all_tables) == 1)
# Tables have a max capacity.
for t in all_tables:
model.add(sum(seats[(t, g)] for g in all_guests) <= table_capacity)
# Link colocated with seats
for g1 in range(num_guests - 1):
for g2 in range(g1 + 1, num_guests):
for t in all_tables:
# Link same_table and seats.
model.add_bool_or(
[
~seats[(t, g1)],
~seats[(t, g2)],
same_table[(g1, g2, t)],
]
)
model.add_implication(same_table[(g1, g2, t)], seats[(t, g1)])
model.add_implication(same_table[(g1, g2, t)], seats[(t, g2)])
# Link colocated and same_table.
model.add(
sum(same_table[(g1, g2, t)] for t in all_tables) == colocated[(g1, g2)]
)
# Min known neighbors rule.
for g in all_guests:
model.add(
sum(
same_table[(g, g2, t)]
for g2 in range(g + 1, num_guests)
for t in all_tables
if connections[g][g2] > 0
)
+ sum(
same_table[(g1, g, t)]
for g1 in range(g)
for t in all_tables
if connections[g1][g] > 0
)
>= min_known_neighbors
)
# Symmetry breaking. First guest seats on the first table.
model.add(seats[(0, 0)] == 1)
### Solve model.
solver = cp_model.CpSolver()
solution_printer = WeddingChartPrinter(seats, names, num_tables, num_guests)
solver.solve(model, solution_printer)
print("Statistics")
print(" - conflicts : %i" % solver.num_conflicts)
print(" - branches : %i" % solver.num_branches)
print(" - wall time : %f s" % solver.wall_time)
print(" - num solutions: %i" % solution_printer.num_solutions())
def main(argv: Sequence[str]) -> None:
if len(argv) > 1:
raise app.UsageError("Too many command-line arguments.")
solve_with_discrete_model()
if __name__ == "__main__":
app.run(main)