"""
A set partitioning model of a wedding seating problem

Authors: Stuart Mitchell 2009
"""

import pulp

max_tables = 5
max_table_size = 4
guests = "A B C D E F G I J K L M N O P Q R".split()


def happiness(
    table: tuple[str, str]
    | tuple[str, str, str, str]
    | tuple[str]
    | tuple[str, str, str],
) -> int:
    """
    Find the happiness of the table
    - by calculating the maximum distance between the letters
    """
    return abs(ord(table[0]) - ord(table[-1]))


# BEGIN possible_tables
# create list of all possible tables
possible_tables = [tuple(c) for c in pulp.allcombinations(guests, max_table_size)]
# END possible_tables

# BEGIN class_and_obj_fn
seating_model = pulp.LpProblem("Wedding Seating Model", pulp.LpMinimize)

# BEGIN define_x
# create a binary variable to state that a table setting is used
_table_keys = ["_".join(t) for t in possible_tables]
vars_by_key = seating_model.add_variable_dict(
    "table_%s", (_table_keys,), 0, 1, pulp.LpInteger
)
x = {t: vars_by_key["_".join(t)] for t in possible_tables}
# END define_x

seating_model += pulp.lpSum([happiness(table) * x[table] for table in possible_tables])
# END class_and_obj_fn

# BEGIN total_table_constraint
# specify the maximum number of tables
seating_model += (
    pulp.lpSum([x[table] for table in possible_tables]) <= max_tables,
    "Maximum_number_of_tables",
)
# END total_table_constraint

# BEGIN exactly_one_table_constraint
# A guest must seated at one and only one table
for guest in guests:
    seating_model += (
        pulp.lpSum([x[table] for table in possible_tables if guest in table]) == 1,
        f"Must_seat_{guest}",
    )
# END exactly_one_table_constraint

seating_model.solve()

print(f"The chosen tables are out of a total of {len(possible_tables)}:")
for table in possible_tables:
    if x[table].value() == 1.0:
        print(table)
