A classic problem of constrained resource allocation with preferences! This is a variant of the Assignment Problem with additional preference constraints. Here's a step-by-step approach to help you solve it:
Step 1: Collect Preferences (as you've mentioned)
- Create a table with:
- People (1-10) as rows
- Gifts (1-10) as columns
- Each cell contains the Preference Rank (1 = most wanted, 10 = least wanted) for each person-gift pair
Step 2: Normalize Preferences (Optional but Recommended)
- To simplify comparisons, convert the ranks into scores (higher is better):
- One simple method: Inverse Ranking: Score = 11 - Rank (so, 1st choice becomes 10 points, 2nd choice becomes 9 points, ..., 10th choice becomes 1 point)
- Alternatively, use a more nuanced scoring system if you prefer (e.g., exponential decay)
Step 3: Choose an Allocation Method
Select one of the following methods to find a suitable allocation. Each method has its trade-offs between optimality, fairness, and computational simplicity:
A. Greedy Algorithm (Simple, Fast, but Suboptimal)
- Sort people by the strength of their top preference (highest score).
- Iterate through the sorted list:
- Assign the top preferred gift to the current person, if available.
- If not, assign their next highest preferred available gift.
- Pros: Easy to understand and implement. Cons: Might lead to suboptimal overall satisfaction.
Pseudocode Example (Greedy Algorithm):
def greedy_algorithm(preferences):
# Sort people by their top preference score in descending order
sorted_people = sorted(preferences, key=lambda x: x[0][0], reverse=True)
# Initialize assignment dictionary and set of assigned gifts
assignments = {}
assigned_gifts = set()
# Iterate through sorted people
for person, prefs in sorted_people.items():
# Find the first preferred gift that hasn't been assigned
for gift, score in prefs.items():
if gift not in assigned_gifts:
assignments[person] = gift
assigned_gifts.add(gift)
break # Move to the next person
return assignments
B. Hungarian Algorithm with Preference Matrix (Optimal, but Computationally Intensive)
- Construct a Cost Matrix where each cell [i, j] represents the "cost" of assigning gift j to person i (e.g., the inverse of the score from Step 2).
- Apply the Hungarian Algorithm to find the optimal assignment minimizing total cost.
- Pros: Globally optimal solution. Cons: Computationally complex, might be overkill for small groups.
Pseudocode Example (Hungarian Algorithm):
import numpy as np
from scipy.optimize import linear_sum_assignment
def hungarian_algorithm(preferences):
# Convert preferences to a cost matrix (lower values are better)
cost_matrix = np.array([[11 - score for score in person_prefs] for person_prefs in preferences])
# Run the Hungarian Algorithm to find the optimal assignment
row_ind, col_ind = linear_sum_assignment(cost_matrix)
# Create a dictionary mapping people to gifts based on the assignment
assignments = {f"Person {i+1}": f"Gift {j+1}" for i, j in zip(row_ind, col_ind)}
return assignments
C. Constraint Programming or Integer Linear Programming (ILP) (Balances Optimality and Complexity)
- Model the problem using constraints (e.g., each person gets exactly one gift, each gift is assigned to at most one person) and an objective function (maximize total satisfaction score).
- Solve using a Constraint Programming solver or an ILP solver.
- Pros: Balances optimality with manageable complexity. Cons: Requires familiarity with these programming paradigms.
Pseudocode Example (Constraint Programming):
from pulp import LpMaximize, LpProblem, lpSum, LpVariable
def constraint_programming(preferences):
# Initialize the problem
model = LpProblem(name="gift-assignment", sense=LpMaximize)
# Define binary variables: x[i][j] = 1 if person i gets gift j
assignment_vars = LpVariable.dicts("Assignment", (range(len(preferences)), range(len(preferences[0]))), 0, 1, cat="Integer")
# Objective: Maximize total satisfaction
model += lpSum([preferences[i][j] * assignment_vars[i][j] for i in range(len(preferences)) for j in range(len(preferences[0]))])
# Constraints: Each person gets exactly one gift, each gift is assigned to at most one person
for i in range(len(preferences)):
model += lpSum([assignment_vars[i][j] for j in range(len(preferences[0]))]) == 1, f"Person_{i}_gets_one_gift"
for j in range(len(preferences[0])):
model += lpSum([assignment_vars[i][j] for i in range(len(preferences))]) == 1, f"Gift_{j}_assigned_once"
# Solve the problem
status = model.solve()
# Extract and return the assignments
assignments = {}
for i in range(len(preferences)):
for j in range(len(preferences[0])):
if assignment_vars[i][j].varValue:
assignments[f"Person {i+1}"] = f"Gift {j+1}"
return assignments
D. Randomized or Heuristic Methods (for Very Large Groups or Unique Requirements)
- Genetic Algorithms, Simulated Annealing, or other metaheuristics can be employed for extremely large groups or if the above methods are infeasible.
- Pros: Scalable. Cons: Solution quality can vary; requires tuning.
Example Walkthrough (Greedy Algorithm for simplicity)
Suppose we have the following preference scores (Inverse Ranking) for three people and three gifts:
| Gift A | Gift B | Gift C |
|---|
| Person 1 | 10 | 6 | 4 |
| Person 2 | 10 | 5 | 8 |
| Person 3 | 8 | 9 | 3 |
- Sorted by Top Preference Strength: Person 1 & Person 2 (tied, then arbitrary order), Person 3
- Assignment:
- Person 1: Gift A (top choice)
- Person 2: Gift A is taken, so Gift B (next best available)
- Person 3: Gift B is taken, so Gift C (only one left, but not their worst choice due to the process)
Final Assignment (Greedy Example)
- Person 1: Gift A
- Person 2: Gift B
- Person 3: Gift C
Scaling to 10 People and 10 Gifts
- The process remains the same, regardless of the method chosen. Simply apply the selected algorithm to the full preference matrix.
Which Method to Choose?
- For a group of 10, Constraint Programming/ILP (C) or the Hungarian Algorithm (B) might offer the best balance between optimality and computational feasibility.
- If simplicity is paramount and some suboptimality is acceptable, the Greedy Algorithm (A) is straightforward to implement.