1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from pulp import * import networkx as nx from itertools import combinations
teams = { 'Eagles': (10, 40), 'Sharks': (30, 10), 'Tigers': (90, 20), 'Hawks': (50, 50), 'Lions': (70, 80), 'Bears': (20, 70), 'Wolves': (60, 30), 'Panthers': (80, 60) }
distances = {} for team1, loc1 in teams.items(): for team2, loc2 in teams.items(): if team1 != team2: dist = np.sqrt((loc1[0] - loc2[0])**2 + (loc1[1] - loc2[1])**2) distances[(team1, team2)] = dist
n_teams = len(teams)
n_games = n_teams * (n_teams - 1)
n_weekends = n_games // 4
model = LpProblem("Sports_Scheduling", LpMinimize)
game_vars = {} for i in teams: for j in teams: if i != j: for w in range(n_weekends): game_vars[(i, j, w)] = LpVariable(f"Game_{i}_{j}_{w}", 0, 1, LpBinary)
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]) model += objective
for i in teams: for j in teams: if i != j: model += lpSum([game_vars[(i, j, w)] for w in range(n_weekends)]) == 1 model += lpSum([game_vars[(j, i, w)] for w in range(n_weekends)]) == 1
for w in range(n_weekends): for i in teams: model += lpSum([game_vars[(i, j, w)] + game_vars[(j, i, w)] for j in teams if i != j]) <= 1
for w in range(n_weekends): model += lpSum([game_vars[(i, j, w)] for i in teams for j in teams if i != j]) == 4
model.solve(PULP_CBC_CMD(msg=False))
print("Status:", LpStatus[model.status])
schedule = {w: [] for w in range(n_weekends)} for i in teams: for j in teams: if i != j: for w in range(n_weekends): if game_vars[(i, j, w)].value() == 1: schedule[w].append((i, j))
schedule_df = pd.DataFrame(index=range(n_weekends), columns=['Game 1', 'Game 2', 'Game 3', 'Game 4']) for w in range(n_weekends): for g in range(4): if g < len(schedule[w]): home, away = schedule[w][g] schedule_df.loc[w, f'Game {g+1}'] = f"{home} vs {away}"
print("\nOptimized Schedule:") print(schedule_df)
plt.figure(figsize=(10, 8)) for team, (x, y) in teams.items(): plt.scatter(x, y, s=100) plt.text(x+2, y+2, team, fontsize=12)
colors = ['red', 'blue', 'green', 'purple', 'orange', 'brown', 'pink', 'gray', 'olive', 'cyan', 'magenta', 'yellow'] for w in range(n_weekends): for home, away in schedule[w]: x1, y1 = teams[home] x2, y2 = teams[away] plt.plot([x1, x2], [y1, y2], color=colors[w % len(colors)], alpha=0.5, linewidth=2)
plt.title('Team Locations and Game Connections by Weekend') plt.xlabel('X Coordinate') plt.ylabel('Y Coordinate') plt.grid(True)
from matplotlib.lines import Line2D legend_elements = [Line2D([0], [0], color=colors[w % len(colors)], lw=2, label=f'Weekend {w+1}') for w in range(n_weekends)] plt.legend(handles=legend_elements, loc='upper right')
plt.savefig('team_locations.png') plt.show()
team_schedule = pd.DataFrame(0, index=teams.keys(), columns=range(1, n_weekends+1))
for w in range(n_weekends): for home, away in schedule[w]: team_schedule.loc[home, w+1] = 1 team_schedule.loc[away, w+1] = -1
plt.figure(figsize=(12, 8)) sns.heatmap(team_schedule, cmap="coolwarm", center=0, linewidths=.5, cbar_kws={'label': 'Game Type', 'ticks':[-1, 0, 1]}) plt.title('Team Schedule Heatmap (1: Home Game, -1: Away Game, 0: No Game)') plt.xlabel('Weekend Number') plt.ylabel('Team')
colorbar = plt.gcf().axes[1] colorbar.set_yticklabels(['Away', 'No Game', 'Home'])
plt.tight_layout() plt.savefig('schedule_heatmap.png') plt.show()
team_distances = {team: 0 for team in teams} for w in range(n_weekends): for home, away in schedule[w]: team_distances[away] += distances[(away, home)]
plt.figure(figsize=(10, 6)) plt.bar(team_distances.keys(), team_distances.values()) plt.title('Total Distance Traveled by Each Team') plt.xlabel('Team') plt.ylabel('Distance') plt.xticks(rotation=45) plt.tight_layout() plt.savefig('travel_distances.png') plt.show()
G = nx.DiGraph() for team in teams: G.add_node(team)
for w in range(n_weekends): for home, away in schedule[w]: G.add_edge(home, away, weekend=w+1)
plt.figure(figsize=(12, 10)) pos = {team: loc for team, loc in teams.items()} nx.draw_networkx_nodes(G, pos, node_size=700, node_color='lightblue') nx.draw_networkx_labels(G, pos, font_size=10)
for w in range(n_weekends): edge_list = [(home, away) for home, away in G.edges() if G[home][away]['weekend'] == w+1] nx.draw_networkx_edges(G, pos, edgelist=edge_list, edge_color=colors[w % len(colors)], arrows=True, width=1.5, alpha=0.7)
edge_labels = {(home, away): f"Week {G[home][away]['weekend']}" for home, away in G.edges()} nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=8)
plt.title('Game Schedule Network') plt.axis('off') plt.tight_layout() plt.savefig('schedule_network.png') plt.show()
|