-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.py
More file actions
475 lines (400 loc) · 17.8 KB
/
optimizer.py
File metadata and controls
475 lines (400 loc) · 17.8 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# optimizer.py
"""
Match optimization for badminton sessions.
This module uses PuLP with Gurobi solver to generate optimal match assignments
balancing skill levels, power balance, and pairing history.
"""
import logging
from collections import defaultdict
from itertools import combinations
import random
import pulp
from constants import (
OPTIMIZER_RANK_MIN,
OPTIMIZER_RANK_MAX,
OPTIMIZER_BIG_M,
PARTNER_HISTORY_MULTIPLIER,
COURT_HISTORY_NORMALIZATION,
OPTIMIZER_TIME_LIMIT,
)
from logger import log_optimizer_debug
from app_types import (
MatchList,
OptimizerResult,
CourtHistory,
PlayerGenders,
PlayerName,
PlayerPair,
RealSkills,
RequiredPartners,
SinglesMatch,
DoublesMatch,
TierRatings,
)
logger = logging.getLogger("app.optimizer")
def get_partnership_penalty(
pair: PlayerPair, court_history: CourtHistory
) -> float:
"""Calculate the penalty for a pair being partners (teammates).
Only considers previous times they were partners."""
partner_count, _ = court_history.get(tuple(sorted(pair)), (0, 0))
return float(partner_count * PARTNER_HISTORY_MULTIPLIER)
def get_same_court_penalty(
pair: PlayerPair, court_history: CourtHistory
) -> float:
"""Calculate the base penalty for a pair sharing a court in any role."""
partner_count, opponent_count = court_history.get(tuple(sorted(pair)), (0, 0))
return float(partner_count + opponent_count)
# ============================================================================
# Singles mode matching logic
# ============================================================================
def generate_singles_round(
available_players: list[PlayerName],
num_courts: int,
real_skills: RealSkills,
court_history: CourtHistory,
weights: dict[str, float] | None = None,
time_limit: float = OPTIMIZER_TIME_LIMIT,
) -> OptimizerResult:
"""
Generates a singles round with 1v1 matches.
Uses simpler optimization focusing on skill balance and opponent history.
Matches players with similar real skills for competitive 1v1 games.
Args:
available_players: List of player names available to play
num_courts: Number of courts to fill. If more courts are requested than
players can fill, the optimizer will auto-reduce to the maximum
feasible number of courts.
real_skills: Dict mapping player names to their real skill ratings (0-5 scale)
court_history: Dict tracking how often player pairs have shared a court
weights: Dict with 'skill', 'power', 'pairing' weight values
Returns:
OptimizerResult with matches and updated court history
"""
if weights is None:
weights = {"skill": 1, "power": 1, "pairing": 1}
# Auto-reduce courts if not enough players
max_courts = len(available_players) // 2
num_courts = min(num_courts, max_courts)
if num_courts == 0:
return OptimizerResult(matches=[], court_history=court_history)
prob = pulp.LpProblem("Badminton_Singles_Optimizer", pulp.LpMinimize)
# x: Binary variable indicating if a player is on a court
x = pulp.LpVariable.dicts(
"OnCourt", (available_players, range(num_courts)), cat="Binary"
)
# For singles, we track opponents (not partners)
player_pairs = list(combinations(sorted(available_players), 2))
o = pulp.LpVariable.dicts(
"Opponents", (player_pairs, range(num_courts)), cat="Binary"
)
# Skill balance variables (using normalized values based on the 0-5 contract)
max_rating_on_court = pulp.LpVariable.dicts("MaxRatingOnCourt", range(num_courts))
min_rating_on_court = pulp.LpVariable.dicts("MinRatingOnCourt", range(num_courts))
total_skill_objective = pulp.lpSum(
max_rating_on_court[c] - min_rating_on_court[c] for c in range(num_courts)
)
# Court history objective (minimize sharing court with same players)
# In singles, all pairs are opponents (no partners), so we just use same_court_penalty
total_court_history_objective = pulp.lpSum(
o[pair][c] * get_same_court_penalty(pair, court_history)
for pair in player_pairs
for c in range(num_courts)
)
prob += (
weights["skill"] * total_skill_objective
+ weights["pairing"] * total_court_history_objective
), "Minimize_Weighted_Objectives"
# Constraints: exactly 2 players per court
for c in range(num_courts):
prob += pulp.lpSum(x[p][c] for p in available_players) == 2
# Each player plays at most once
for p in available_players:
prob += pulp.lpSum(x[p][c] for c in range(num_courts)) <= 1
# Total players on all courts
prob += (
pulp.lpSum(x[p][c] for p in available_players for c in range(num_courts))
== num_courts * 2
)
# Link opponent variables
for p1, p2 in player_pairs:
for c in range(num_courts):
prob += o[(p1, p2)][c] <= x[p1][c]
prob += o[(p1, p2)][c] <= x[p2][c]
prob += o[(p1, p2)][c] >= x[p1][c] + x[p2][c] - 1
# Skill balance constraints (use real_skills for competitive matchups)
for c in range(num_courts):
for p in available_players:
prob += max_rating_on_court[c] >= real_skills[p] - OPTIMIZER_BIG_M * (
1 - x[p][c]
)
prob += min_rating_on_court[c] <= real_skills[p] + OPTIMIZER_BIG_M * (
1 - x[p][c]
)
# Solve
solver = pulp.GUROBI(msg=False, timeLimit=time_limit)
prob.solve(solver)
if prob.status == pulp.LpStatusInfeasible:
logger.error(
"No optimal solution found for singles. Status: %s",
pulp.LpStatus[prob.status],
)
return OptimizerResult(matches=None, court_history=court_history)
# Log debug info
log_optimizer_debug(
logger=logger,
num_courts=num_courts,
max_rating_on_court=max_rating_on_court,
min_rating_on_court=min_rating_on_court,
total_skill_objective=pulp.value(total_skill_objective),
total_court_history_objective=pulp.value(total_court_history_objective),
objective_value=pulp.value(prob.objective),
)
# Build matches
matches = []
updated_court_history = court_history.copy()
for c in range(num_courts):
court_players = [p for p in available_players if x[p][c].value() > 0.5]
if len(court_players) == 2:
p1, p2 = sorted(court_players)
pair_key = tuple(sorted((p1, p2)))
# In singles, all pairs are opponents (no partners)
partner_count, opponent_count = updated_court_history.get(pair_key, (0, 0))
updated_court_history[pair_key] = (partner_count, opponent_count + 1)
matches.append(
SinglesMatch(
court=c + 1,
player_1=p1,
player_2=p2,
)
)
return OptimizerResult(matches=matches, court_history=updated_court_history)
# ============================================================================
# Doubles mode and main entry point
# ============================================================================
def generate_one_round(
tier_ratings: TierRatings,
real_skills: RealSkills,
player_genders: PlayerGenders,
players_to_rest: set[PlayerName],
num_courts: int,
court_history: CourtHistory,
players_per_court: int = 4,
weights: dict[str, float] | None = None,
is_doubles: bool = True,
required_partners: RequiredPartners | None = None,
time_limit: float = OPTIMIZER_TIME_LIMIT,
) -> OptimizerResult:
"""
Generates a single, optimized round of badminton matches.
Supports both Doubles (4 players per court) and Singles (2 players per court).
Uses decoupled inputs for different objectives:
- tier_ratings: Z-score normalized ratings for court grouping (social hierarchy)
- real_skills: Direct normalized ratings for team fairness (win probability)
Args:
tier_ratings: Dict mapping player names to tier ratings (Z-score normalized, 0-5 scale)
real_skills: Dict mapping player names to real skill ratings (direct normalized, 0-5 scale)
player_genders: Dict mapping player names to 'M' or 'F'
players_to_rest: Set of player names who should rest this round
num_courts: Number of courts to fill. If more courts are requested than
available players can fill, the optimizer will auto-reduce to the
maximum feasible number. Check len(result.matches) to see actual
courts used.
court_history: Dict tracking how often player pairs have shared a court
players_per_court: Number of players per court (2 for singles, 4 for doubles)
weights: Dict with 'skill', 'power', 'pairing' weight values
is_doubles: True for doubles mode, False for singles mode
required_partners: Optional dict mapping players to their required partners (doubles only)
Returns:
OptimizerResult with matches and updated court history
"""
logger.debug("Tier ratings: %s", tier_ratings)
logger.debug("Real skills: %s", real_skills)
if weights is None:
weights = {"skill": 1, "power": 1, "pairing": 1}
if required_partners is None:
required_partners = {}
all_players = list(tier_ratings.keys())
available_players = [p for p in all_players if p not in players_to_rest]
random.shuffle(available_players)
num_available = len(available_players)
# Auto-reduce courts if not enough players
max_courts = num_available // players_per_court
num_courts = min(num_courts, max_courts)
if num_courts == 0:
return OptimizerResult(matches=[], court_history=court_history)
players_needed = num_courts * players_per_court
# For singles mode, use simpler matching logic
if not is_doubles:
return generate_singles_round(
available_players,
num_courts,
real_skills,
court_history,
weights,
time_limit=time_limit,
)
prob = pulp.LpProblem("Badminton_Full_Optimizer", pulp.LpMinimize)
# x: Binary variable indicating if a player is on a court
x = pulp.LpVariable.dicts(
"OnCourt", (available_players, range(num_courts)), cat="Binary"
)
# t: Binary variable indicating if a pair of players are partners on a court
player_pairs = list(combinations(sorted(available_players), 2))
t = pulp.LpVariable.dicts(
"Partners", (player_pairs, range(num_courts)), cat="Binary"
)
# s: Binary variable indicating if a pair of players share the same court
s = pulp.LpVariable.dicts(
"SameCourt", (player_pairs, range(num_courts)), cat="Binary"
)
max_rating_on_court = pulp.LpVariable.dicts("MaxRatingOnCourt", range(num_courts))
min_rating_on_court = pulp.LpVariable.dicts("MinRatingOnCourt", range(num_courts))
total_skill_objective = pulp.lpSum(
max_rating_on_court[c] - min_rating_on_court[c] for c in range(num_courts)
)
max_team_power = pulp.LpVariable.dicts("MaxTeamPower", range(num_courts))
min_team_power = pulp.LpVariable.dicts("MinTeamPower", range(num_courts))
total_power_objective = pulp.lpSum(
max_team_power[c] - min_team_power[c] for c in range(num_courts)
)
# Court history objective: penalize sharing the court (s) and repeating partnerships (t)
total_court_history_objective = (
pulp.lpSum(
s[pair][c] * get_same_court_penalty(pair, court_history)
+ t[pair][c] * get_partnership_penalty(pair, court_history)
for pair in player_pairs
for c in range(num_courts)
)
/ COURT_HISTORY_NORMALIZATION
)
prob += (
weights["skill"] * total_skill_objective
+ weights["power"] * total_power_objective
+ weights["pairing"] * total_court_history_objective
), "Minimize_Weighted_Objectives"
for c in range(num_courts):
prob += pulp.lpSum(x[p][c] for p in available_players) == players_per_court
for p in available_players:
prob += pulp.lpSum(x[p][c] for c in range(num_courts)) <= 1
prob += (
pulp.lpSum(x[p][c] for p in available_players for c in range(num_courts))
== players_needed
)
for p1, p2 in player_pairs:
for c in range(num_courts):
prob += t[(p1, p2)][c] <= x[p1][c]
prob += t[(p1, p2)][c] <= x[p2][c]
for p in available_players:
for c in range(num_courts):
prob += (
pulp.lpSum(t[pair][c] for pair in player_pairs if p in pair) == x[p][c]
)
# Link same-court variables: s[pair][c] = 1 iff both players are on court c
for p1, p2 in player_pairs:
for c in range(num_courts):
prob += s[(p1, p2)][c] <= x[p1][c]
prob += s[(p1, p2)][c] <= x[p2][c]
prob += s[(p1, p2)][c] >= x[p1][c] + x[p2][c] - 1
# Hard constraints for required partners:
# If a player plays, they must be with a required partner,
# OR their required partners must be "validly occupied" with another mutual teammate.
for player, partners in required_partners.items():
if player not in available_players:
continue
# Filter to only available partners
active_partners = [p for p in partners if p in available_players]
if active_partners:
for c in range(num_courts):
satisfaction_terms = []
for p in active_partners:
# 1. Direct partnership: Player is with Partner p
satisfaction_terms.append(t[tuple(sorted((player, p)))][c])
# 2. Indirect excuse: Partner p is with another Teammate k
# If my partner is playing with another valid teammate, I am excused.
p_partners = required_partners.get(p, set())
for k in p_partners:
if k != player and k in available_players:
# Note: This may add duplicate terms if the friendship graph is dense
# (e.g. triangle), but for boolean LP constraints (sum >= 1),
# redundancy is harmless and ensures correctness.
satisfaction_terms.append(t[tuple(sorted((p, k)))][c])
prob += pulp.lpSum(satisfaction_terms) >= x[player][c]
for c in range(num_courts):
for p in available_players:
# Skill balance constraints use TIER RATINGS for court grouping (social hierarchy)
# If x=1: max >= tier. If x=0: max >= tier - BIG_M (trivially true)
prob += max_rating_on_court[c] >= tier_ratings[p] - OPTIMIZER_BIG_M * (
1 - x[p][c]
)
# If x=1: min <= tier. If x=0: min <= tier + BIG_M (trivially true)
prob += min_rating_on_court[c] <= tier_ratings[p] + OPTIMIZER_BIG_M * (
1 - x[p][c]
)
for p1, p2 in player_pairs:
# Team power uses REAL SKILLS for fairness (win probability)
# Averaged so power imbalance is on same 0-5 scale as skill spread
pair_power = (real_skills[p1] + real_skills[p2]) / 2
# Robust Big-M constraints for team power
prob += max_team_power[c] >= pair_power - OPTIMIZER_BIG_M * (
1 - t[(p1, p2)][c]
)
prob += min_team_power[c] <= pair_power + OPTIMIZER_BIG_M * (
1 - t[(p1, p2)][c]
)
# Use the Gurobi solver with the specified time limit.
solver = pulp.GUROBI(
msg=False,
timeLimit=time_limit,
)
prob.solve(solver)
if prob.status == pulp.LpStatusInfeasible:
logger.error(
"No optimal solution found for doubles. Status: %s",
pulp.LpStatus[prob.status],
)
return OptimizerResult(matches=None, court_history=court_history)
# Log debug info
log_optimizer_debug(
logger=logger,
num_courts=num_courts,
max_rating_on_court=max_rating_on_court,
min_rating_on_court=min_rating_on_court,
total_skill_objective=pulp.value(total_skill_objective),
total_court_history_objective=pulp.value(total_court_history_objective),
objective_value=pulp.value(prob.objective),
max_team_power=max_team_power,
min_team_power=min_team_power,
total_power_objective=pulp.value(total_power_objective),
)
matches = []
updated_court_history = court_history.copy()
for c in range(num_courts):
court_players = [p for p in available_players if x[p][c].value() > 0.5]
# Identify partner pairs (2 pairs per court in doubles)
partner_pairs: set[PlayerPair] = set()
for p1, p2 in combinations(court_players, 2):
pair_key = tuple(sorted((p1, p2)))
if t[pair_key][c].value() > 0.5:
partner_pairs.add(pair_key)
# Update court history for ALL pairs that shared this court (6 pairs for doubles)
# Partners increment partner_count, opponents increment opponent_count
for p1, p2 in combinations(sorted(court_players), 2):
pair_key = tuple(sorted((p1, p2)))
partner_count, opponent_count = updated_court_history.get(pair_key, (0, 0))
if pair_key in partner_pairs:
updated_court_history[pair_key] = (partner_count + 1, opponent_count)
else:
updated_court_history[pair_key] = (partner_count, opponent_count + 1)
if len(partner_pairs) == players_per_court / 2:
partnerships = list(partner_pairs)
team1 = partnerships[0]
team2 = partnerships[1]
matches.append(
DoublesMatch(
court=c + 1,
team_1=team1,
team_2=team2,
)
)
return OptimizerResult(matches=matches, court_history=updated_court_history)