| 1 |
""" |
| 2 |
A set partitioning model of a wedding seating problem |
| 3 |
|
| 4 |
Authors: Stuart Mitchell 2009 |
| 5 |
""" |
| 6 |
|
| 7 |
import pulp |
| 8 |
|
| 9 |
max_tables = 5 |
| 10 |
max_table_size = 4 |
| 11 |
guests = "A B C D E F G I J K L M N O P Q R".split() |
| 12 |
|
| 13 |
|
| 14 |
def happiness(table): |
| 15 |
""" |
| 16 |
Find the happiness of the table |
| 17 |
- by calculating the maximum distance between the letters |
| 18 |
""" |
| 19 |
return abs(ord(table[0]) - ord(table[-1])) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
possible_tables = [tuple(c) for c in pulp.allcombinations(guests, max_table_size)] |
| 24 |
|
| 25 |
|
| 26 |
x = pulp.LpVariable.dicts( |
| 27 |
"table", possible_tables, lowBound=0, upBound=1, cat=pulp.LpInteger |
| 28 |
) |
| 29 |
|
| 30 |
seating_model = pulp.LpProblem("Wedding Seating Model", pulp.LpMinimize) |
| 31 |
|
| 32 |
seating_model += sum([happiness(table) * x[table] for table in possible_tables]) |
| 33 |
|
| 34 |
|
| 35 |
seating_model += ( |
| 36 |
sum([x[table] for table in possible_tables]) <= max_tables, |
| 37 |
"Maximum_number_of_tables", |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
for guest in guests: |
| 42 |
seating_model += ( |
| 43 |
sum([x[table] for table in possible_tables if guest in table]) == 1, |
| 44 |
"Must_seat_%s" % guest, |
| 45 |
) |
| 46 |
|
| 47 |
seating_model.solve() |
| 48 |
|
| 49 |
print("The choosen tables are out of a total of %s:" % len(possible_tables)) |
| 50 |
for table in possible_tables: |
| 51 |
if x[table].value() == 1.0: |
| 52 |
print(table) |