Solving a Realistic Sports Scheduling Problem with Python
Have you ever wondered how professional sports leagues create their game schedules?
It’s a fascinating optimization problem that combines mathematics and computer science.
Today, I’ll walk you through solving a realistic sports scheduling problem using $Python$.
The Problem: Regional Basketball League Scheduling
Let’s tackle scheduling for a regional basketball league with $8$ teams. The constraints are:
- Each team must play against every other team twice (home and away)
- Games occur on weekends with $4$ matches per weekend
- No team should play more than one game per weekend
- Travel distances should be minimized (teams located far from each other should have games consolidated)
This is a classic combinatorial optimization problem that appears simple but quickly becomes complex.
Let’s solve this using $Python$ with the $PuLP$ library for linear programming:
1 | import numpy as np |
Code Explanation
Let’s break down the solution step by step:
1. Problem Setup
First, I define our $8$ teams with their geographic locations using x-y coordinates.
This is realistic because in actual sports scheduling, the locations of teams are critical for minimizing travel distances.
1 | teams = { |
Then I calculate the Euclidean distances between each pair of teams.
In a real-world scenario, these would be actual road distances or travel times.
2. Mathematical Model
This is where the optimization begins.
The problem is formulated as a linear programming model using PuLP:
1 | model = LpProblem("Sports_Scheduling", LpMinimize) |
The key decision variables are binary variables indicating whether team i plays against team j on weekend w:
1 | game_vars[(i, j, w)] = LpVariable(f"Game_{i}_{j}_{w}", 0, 1, LpBinary) |
3. Objective Function
Our goal is to minimize the total travel distance:
1 | objective = lpSum([game_vars[(i, j, w)] * distances[(i, j)] for i in teams for j in teams for w in range(n_weekends) if i != j]) |
This can be represented mathematically as:
$$\min \sum_{i \in Teams} \sum_{j \in Teams, j \neq i} \sum_{w \in Weekends} game_{i,j,w} \times distance_{i,j}$$
4. Constraints
Several constraints ensure a valid schedule:
- Each team plays against every other team exactly once at home and once away:
$$\sum_{w \in Weekends} game_{i,j,w} = 1 \quad \forall i,j \in Teams, i \neq j$$
- Each team plays at most one game per weekend:
$$\sum_{j \in Teams, j \neq i} (game_{i,j,w} + game_{j,i,w}) \leq 1 \quad \forall i \in Teams, \forall w \in Weekends$$
- Exactly 4 games per weekend:
$$\sum_{i \in Teams} \sum_{j \in Teams, j \neq i} game_{i,j,w} = 4 \quad \forall w \in Weekends$$
5. Solution Extraction
After solving the model, I extract the schedule into a more readable format:
1 | schedule = {w: [] for w in range(n_weekends)} |
Output
Status: Optimal
Optimized Schedule:
Game 1 Game 2 Game 3 \
0 Sharks vs Lions Tigers vs Hawks Bears vs Panthers
1 Sharks vs Wolves Tigers vs Panthers Lions vs Hawks
2 Sharks vs Panthers Tigers vs Bears Lions vs Eagles
3 Eagles vs Sharks Bears vs Tigers Wolves vs Lions
4 Eagles vs Wolves Sharks vs Bears Hawks vs Panthers
5 Tigers vs Wolves Hawks vs Eagles Bears vs Sharks
6 Eagles vs Bears Hawks vs Sharks Lions vs Wolves
7 Eagles vs Hawks Tigers vs Lions Wolves vs Sharks
8 Eagles vs Panthers Tigers vs Sharks Hawks vs Lions
9 Hawks vs Tigers Lions vs Sharks Bears vs Wolves
10 Sharks vs Eagles Lions vs Panthers Bears vs Hawks
11 Eagles vs Lions Sharks vs Tigers Hawks vs Bears
12 Tigers vs Eagles Hawks vs Wolves Lions vs Bears
13 Eagles vs Tigers Sharks vs Hawks Bears vs Lions
Game 4
0 Wolves vs Eagles
1 Bears vs Eagles
2 Wolves vs Hawks
3 Panthers vs Hawks
4 Lions vs Tigers
5 Panthers vs Lions
6 Panthers vs Tigers
7 Panthers vs Bears
8 Wolves vs Bears
9 Panthers vs Eagles
10 Wolves vs Tigers
11 Panthers vs Wolves
12 Panthers vs Sharks
13 Wolves vs Panthers
Visualization and Analysis
I’ve created several visualizations to help understand the optimized schedule:
1. Geographic Visualization
The first plot shows the geographic locations of teams with lines connecting teams that play on the same weekend.
Each color represents a different weekend:

This visualization is particularly useful for identifying whether games are geographically efficient - teams that are closer to each other should ideally play on the same weekends to minimize travel.
2. Schedule Heatmap
The heatmap provides a clear view of when each team plays, and whether they’re playing home (1) or away (-1):

This visualization helps identify whether the schedule is balanced.
Each team should have a mix of home and away games, and we can quickly see if any team has too many consecutive home or away games.
3. Travel Distance Analysis

This bar chart shows the total distance traveled by each team:
In an ideal schedule, these values should be relatively balanced to ensure fairness.
Wide variations might indicate scheduling inefficiencies.
4. Network Graph
Finally, the network graph shows the complete schedule with directed edges indicating who plays whom and when:

This graph helps identify the overall structure of the schedule and any potential patterns.
Conclusion
Sports scheduling is a complex optimization problem with numerous constraints.
The solution I’ve presented uses linear programming to find an optimal schedule that minimizes travel distances while satisfying all the logical constraints of a sports league.
In real-world applications, additional constraints might include:
- Arena availability
- TV broadcasting requirements
- Avoiding conflicts with other major events
- Balancing the distribution of strong opponents throughout the season
This approach can be extended to larger leagues with more complex constraints, though the computational complexity increases significantly with the number of teams and constraints.

















