import pulp
from pulp import LpProblem, LpMinimize, LpVariable, LpBinary, lpSum, value, LpStatus
# Problem Description:
# We aim to schedule courses into time slots while respecting:
# - Each course is assigned to exactly one slot.
# - No teacher has overlapping courses in the same slot.
# - Each slot has limited capacity (max courses per slot).
# - FAIRNESS: Minimize the *maximum burden* across teachers, where burden_t = sum over assigned slots s: hardness[s].
# Hardness reflects "undesirability" of slots (e.g., late evenings). This ensures equitable distribution
# of inconvenient slots across teachers.
# Data
num_courses = 12
teachers_per_course = [1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5] # Teacher for each course 0-11
num_slots = 12
max_per_slot = 3 # Max parallel courses per slot (room availability proxy)
hardness = [1, 1, 2, # Good morning slots (low hardness)
2, 2, 3, # Afternoon
3, 3, 4, # Late afternoon/evening
4, 5, 5] # Bad late slots (high hardness)
# Named slots for readability
slot_names = [f"Slot_{i+1} (h={hardness[i]})" for i in range(num_slots)]
# Sets
C = list(range(num_courses)) # Courses
S = list(range(num_slots)) # Slots
T = list(set(teachers_per_course)) # Teachers
teach = {c: teachers_per_course[c] for c in C}
num_teach_per_teacher = {t: sum(1 for c in C if teach[c] == t) for t in T}
print("=== Course Scheduling Optimization with Fairness ===")
print(f"• {num_courses} courses, {len(T)} teachers, {num_slots} slots")
print(f"• Teachers' course loads: {num_teach_per_teacher}")
print(f"• Max courses/slot: {max_per_slot}")
print("• Slot hardness (higher = worse):", hardness)
print()
# Model
prob = LpProblem("Fair_Scheduling", LpMinimize)
# Variables
x = LpVariable.dicts("assign_course_to_slot", (C, S), cat=LpBinary)
# Auxiliary: burden per teacher
burden = {t: lpSum(hardness[s] * x[c][s] for c in C if teach[c] == t for s in S) for t in T}
# Fairness variable: max burden
Z = LpVariable("max_burden", lowBound=0)
# Objective: Minimize the maximum burden (fairness)
prob += Z
# Constraints
# 1. Each course assigned to exactly one slot
for c in C:
prob += lpSum(x[c][s] for s in S) == 1, f"assign_course_{c}"
# 2. Slot capacity
for s in S:
prob += lpSum(x[c][s] for c in C) <= max_per_slot, f"slot_capacity_{s}"
# 3. No teacher overlap in same slot
for t in T:
for s in S:
prob += lpSum(x[c][s] for c in C if teach[c] == t) <= 1, f"teacher_{t}_no_overlap_{s}"
# 4. Fairness: every teacher's burden <= Z
for t in T:
prob += burden[t] <= Z, f"fairness_teacher_{t}"
print("Solving...")
prob.solve(pulp.PULP_CBC_CMD(msg=0)) # Use CBC solver (default in PuLP)
print(f"Status: {LpStatus[prob.status]}")
print(f"Optimal max burden (fairness metric): {value(Z):.1f}")
print()
# Output schedule
schedule = {s: [] for s in S}
for c in C:
for s in S:
if value(x[c][s]) == 1:
schedule[s].append(c)
print("=== Schedule ===")
for s in S:
if schedule[s]:
courses = [f"C{c} (T{teach[c]})" for c in schedule[s]]
print(f"{slot_names[s]}: {', '.join(courses)}")
print()
print("=== Teacher Burdens ===")
for t in T:
b = value(burden[t])
print(f"Teacher {t}: burden = {b:.1f}")
print("\nModel solved successfully! Copy-paste to run.")
Explanation
Mathematical Model
Sets:
- ( C ): Courses (( |C| = 12 ))
- ( S ): Time slots (( |S| = 12 ))
- ( T ): Teachers (( |T| = 5 ))
Parameters:
teach[c]: Teacher assigned to course ( c )
hardness[s]: Undesirability of slot ( s ) (1=ideal morning, 5=late night)
max_per_slot: Max courses per slot
Decision Variables:
- ( x_{c,s} \in {0,1} ): 1 if course ( c ) in slot ( s )
Burden:
- ( burden_t = \sum_{c: teach[c]=t} \sum_s hardness_s \cdot x_{c,s} )
Objective:
[
\min Z \quad \text{s.t.} \quad burden_t \leq Z \quad \forall t \in T
]
This minimax fairness equalizes the worst teacher's burden.
Constraints:
- Each course assigned once: ( \sum_s x_{c,s} = 1 \ \forall c )
- Slot capacity: ( \sum_c x_{c,s} \leq K \ \forall s )
- Teacher no-overlap: ( \sum_{c: teach[c]=t} x_{c,s} \leq 1 \ \forall t,s )
Key Features
- Scalable: Increase
num_courses, num_slots for larger instances (solver handles ~100s fine).
- Fairness Guarantee: No teacher gets disproportionately bad slots.
- Realism: Customize
hardness, add rooms/students by extending constraints.
- Extensions Ideas:
- Add student groups for fairness: similar min-max on their burdens.
- Preferences: Soft costs in objective.
- Rooms: New set ( R ), assign ( x_{c,s,r} ).
Run the code—it solves in seconds! Adjust data for your scenario. 🕵️