forked from koyeb/example-fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
1025 lines (867 loc) · 40.3 KB
/
solver.py
File metadata and controls
1025 lines (867 loc) · 40.3 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from collections import defaultdict
from dataclasses import dataclass, fields
from enum import Enum
from functools import lru_cache
from ortools.sat.python import cp_model
import os
from typing import NamedTuple, Optional
class Position(Enum):
BACK_COURT = 1
FRONT_COURT = 2
@classmethod
@lru_cache(maxsize=None) # Cache since we have limited positions
def from_element_type(cls, element_type: int) -> "Position":
"""Convert API element_type to Position enum."""
return cls.BACK_COURT if element_type == 1 else cls.FRONT_COURT
def __str__(self) -> str:
return self.name
@dataclass(frozen=True)
class Player:
id: int
name: str
position: Position
team: int
form: float
points_per_game: float
now_cost: int
selling_price: Optional[int] = None
def get_score(self, metric: str) -> float:
return self.form if metric == "form" else self.points_per_game
def get_cost(self) -> int:
return self.selling_price if self.selling_price is not None else self.now_cost
@dataclass(frozen=True)
class Fixture:
event: int
team_h: int
team_a: int
@dataclass(frozen=True)
class Phase:
id: int
start_event: int
stop_event: int
class OptimizationConfig(NamedTuple):
"""Configuration parameters for optimization"""
SQUAD_SIZE: int = 10
FRONT_COURT_COUNT: int = 5
BACK_COURT_COUNT: int = 5
STARTING_PLAYERS: int = 5
MAX_PLAYERS_PER_TEAM: int = 2
FREE_TRANSFERS: int = 2
TRANSFER_PENALTY: int = 1000
SOLVER_TIMEOUT: int = 300 # seconds
@dataclass
class Transfers:
bank: int
cost: int
limit: int
made: int
status: str
value: int
class TeamOptimizer:
def __init__(
self,
players: list[dict],
fixtures: list[dict],
phases: list[dict],
teams: list[dict],
scoring_metric: str = "form",
current_squad: Optional[list[dict]] = None,
config: Optional[OptimizationConfig] = None,
transfers: Optional[Transfers] = None,
):
print(f"transfers :>> {transfers}")
self.config = config or OptimizationConfig()
self.scoring_metric = scoring_metric
# Calculate free transfers from transfers info if available
self.free_transfers = (
transfers.limit - transfers.made
if transfers is not None
else self.config.FREE_TRANSFERS
)
# self.free_transfers = self.config.FREE_TRANSFERS
# Pre-compute team lookups before processing players
self.team_lookup = {t["id"]: t for t in teams}
self.team_names = {t["id"]: t["name"] for t in teams}
self.team_short_names = {t["id"]: t["short_name"] for t in teams}
# Calculate budget once
self.budget = (
sum(p["selling_price"] for p in current_squad) + transfers.bank
if current_squad and transfers
else 1000
)
# Process data in optimal order
self.players = self._process_players(players, current_squad)
# Create a player_map for fast ID lookups
self.player_map = {p.id: p for p in self.players}
self.fixtures = self._process_fixtures(fixtures)
self.phases = self._process_phases(phases)
# Pre-compute player lookups for optimization
self.players_by_team = defaultdict(list)
self.players_by_position = defaultdict(list)
for player in self.players:
self.players_by_team[player.team].append(player)
self.players_by_position[player.position].append(player)
# Cache for expensive computations
self._playing_teams_cache = {}
self._gameweek_cache = {}
def _process_players(
self, players: list[dict], current_squad: Optional[list[dict]]
) -> list[Player]:
"""Process raw player data into Player objects."""
current_squad_map = {
p["element"]: p["selling_price"] for p in (current_squad or [])
}
return [
Player(
id=p["id"],
name=p["web_name"],
position=Position.from_element_type(p["element_type"]),
team=p["team"],
form=float(p["form"] or 0),
points_per_game=float(p["points_per_game"] or 0),
now_cost=p["now_cost"],
selling_price=current_squad_map.get(p["id"]),
)
for p in players
if p["status"] in ["a", "d"] or p["id"] in current_squad_map
]
def _process_fixtures(self, fixtures: list[dict]) -> list[Fixture]:
"""Process fixtures with field filtering."""
fixture_fields = {field.name for field in fields(Fixture)}
return [
Fixture(**{k: v for k, v in f.items() if k in fixture_fields})
for f in fixtures
]
def _process_phases(self, phases: list[dict]) -> list[Phase]:
"""Process phases with field filtering."""
phase_fields = {field.name for field in fields(Phase)}
return [
Phase(**{k: v for k, v in p.items() if k in phase_fields})
for p in phases[1:]
]
def _get_gameweek_for_event(self, event_id: int) -> Optional[int]:
"""Get the gameweek (phase) ID for a given event ID."""
if event_id in self._gameweek_cache:
return self._gameweek_cache[event_id]
for phase in self.phases:
if phase.start_event <= event_id <= phase.stop_event:
self._gameweek_cache[event_id] = phase.id
return phase.id
self._gameweek_cache[event_id] = None
return None
def _get_playing_teams(self, event_id: int) -> frozenset:
"""Get set of teams playing in a given event."""
if event_id in self._playing_teams_cache:
return self._playing_teams_cache[event_id]
teams = frozenset(
team_id
for fixture in self.fixtures
if fixture.event == event_id
for team_id in (fixture.team_h, fixture.team_a)
)
self._playing_teams_cache[event_id] = teams
return teams
def _scale_points(self, points: float) -> int:
"""Scale floating point scores to integers for CP-SAT solver."""
return int(points * 1000)
def _process_player_for_output(self, player):
"""Helper to convert player data to user-friendly format"""
return {
"id": player.id,
"name": player.name,
"team": self.team_names.get(player.team),
"team_short": self.team_short_names.get(player.team),
"position": player.position,
"points": (
player.form if self.scoring_metric == "form" else player.points_per_game
),
"cost": player.selling_price if player.selling_price else player.now_cost,
}
def _create_optimization_model(
self, event_ids: list[int]
) -> tuple[cp_model.CpModel, dict, dict]:
"""Create CP-SAT model and variables."""
model = cp_model.CpModel()
player_vars = {} # Boolean vars for FINAL squad selection
# --- Multi-Stage Roster Variables ---
self.player_in_squad_vars = {}
self.event_transfer_in_vars = {}
self.event_transfer_out_vars = {}
# Batch variable creation
sorted_events = sorted(list(event_ids))
gameweeks = {self._get_gameweek_for_event(e) for e in event_ids}
for player in self.players:
# Final squad selection var
player_vars[player.id] = model.NewBoolVar(f"player_{player.id}")
for event_id in sorted_events:
# Squad status variable
self.player_in_squad_vars[(player.id, event_id)] = model.NewBoolVar(
f"player_in_squad_{player.id}_{event_id}"
)
# Transfer variables
self.event_transfer_in_vars[(player.id, event_id)] = model.NewBoolVar(
f"transfer_in_{player.id}_{event_id}"
)
self.event_transfer_out_vars[(player.id, event_id)] = model.NewBoolVar(
f"transfer_out_{player.id}_{event_id}"
)
# Add symmetry breaking constraints for similar players
self._add_symmetry_breaking(model, player_vars)
# Return model and only the relevant variable dictionaries
return model, player_vars
def _add_symmetry_breaking(self, model, player_vars):
"""Add symmetry breaking constraints as a separate method for clarity"""
for position in [Position.FRONT_COURT, Position.BACK_COURT]:
position_players = sorted(
self.players_by_position[position], key=lambda p: p.id
)
# Group players by similar stats and costs
similar_players = defaultdict(list)
for player in position_players:
key = (
round(player.get_score(self.scoring_metric), 1),
player.get_cost(),
)
similar_players[key].append(player)
# Add constraints only for groups with multiple similar players
for players in similar_players.values():
if len(players) > 1:
for i in range(len(players) - 1):
model.Add(
player_vars[players[i].id] >= player_vars[players[i + 1].id]
)
def _add_transfer_constraints(
self, model, player_vars, event_ids, sorted_events, current_squad
):
"""Add transfer-related constraints to the model."""
objective_terms = []
current_players = {p["element"] for p in current_squad}
# Group events by phase - do this once
phases = defaultdict(list)
for event_id in event_ids:
phase = self._get_gameweek_for_event(event_id)
if phase:
phases[phase].append(event_id)
# Set flag to indicate we created event transfer variables
self._event_transfer_vars_created = True
# Add constraints in batches for better performance
self._add_initial_squad_constraints(model, sorted_events, current_players)
self._add_squad_evolution_constraints(model, sorted_events)
self._add_final_squad_link_constraints(model, player_vars, sorted_events)
self._add_transfer_balance_constraints(model, sorted_events)
self._add_position_balance_constraints(model, sorted_events)
self._add_budget_constraints(model, sorted_events)
# Add transfer penalties
objective_terms.extend(self._add_transfer_penalties(model, phases))
return objective_terms
def _add_initial_squad_constraints(self, model, sorted_events, current_players):
"""Add constraints for initial squad status"""
if not sorted_events:
return
first_event = sorted_events[0]
# Batch constraints by player status
initial_squad_players = []
non_squad_players = []
for player in self.players:
if player.id in current_players:
initial_squad_players.append(player)
else:
non_squad_players.append(player)
# Add constraints in batches
for player in initial_squad_players:
model.Add(
self.player_in_squad_vars[(player.id, first_event)]
== 1 - self.event_transfer_out_vars[(player.id, first_event)]
)
model.Add(self.event_transfer_in_vars[(player.id, first_event)] == 0)
for player in non_squad_players:
model.Add(
self.player_in_squad_vars[(player.id, first_event)]
== self.event_transfer_in_vars[(player.id, first_event)]
)
model.Add(self.event_transfer_out_vars[(player.id, first_event)] == 0)
def _add_squad_evolution_constraints(self, model, sorted_events):
"""Add constraints for squad evolution across events"""
if len(sorted_events) <= 1:
return
# For subsequent events, update squad based on previous event
for i in range(1, len(sorted_events)):
prev_event = sorted_events[i - 1]
curr_event = sorted_events[i]
for player in self.players:
# Squad status update
model.Add(
self.player_in_squad_vars[(player.id, curr_event)]
== self.player_in_squad_vars[(player.id, prev_event)]
+ self.event_transfer_in_vars[(player.id, curr_event)]
- self.event_transfer_out_vars[(player.id, curr_event)]
)
# Transfer logic constraints
model.Add(
self.event_transfer_in_vars[(player.id, curr_event)]
<= 1 - self.player_in_squad_vars[(player.id, prev_event)]
)
model.Add(
self.event_transfer_out_vars[(player.id, curr_event)]
<= self.player_in_squad_vars[(player.id, prev_event)]
)
def _add_final_squad_link_constraints(self, model, player_vars, sorted_events):
"""Link player_vars to final event's squad status"""
if not sorted_events:
return
final_event = sorted_events[-1]
for player in self.players:
model.Add(
player_vars[player.id]
== self.player_in_squad_vars[(player.id, final_event)]
)
def _add_transfer_balance_constraints(self, model, sorted_events):
"""Ensure transfers in = transfers out for each event"""
for event_id in sorted_events:
model.Add(
sum(self.event_transfer_in_vars[(p.id, event_id)] for p in self.players)
== sum(
self.event_transfer_out_vars[(p.id, event_id)] for p in self.players
)
)
def _add_position_balance_constraints(self, model, sorted_events):
"""Ensure position balance is maintained for each event"""
# Pre-compute player lists by position for faster access
front_court_players = self.players_by_position[Position.FRONT_COURT]
back_court_players = self.players_by_position[Position.BACK_COURT]
for event_id in sorted_events:
# Front court count constraint
front_court_vars = [
self.player_in_squad_vars[(p.id, event_id)] for p in front_court_players
]
model.Add(sum(front_court_vars) == self.config.FRONT_COURT_COUNT)
# Back court count constraint
back_court_vars = [
self.player_in_squad_vars[(p.id, event_id)] for p in back_court_players
]
model.Add(sum(back_court_vars) == self.config.BACK_COURT_COUNT)
# For transfers specifically, ensure position balance
if event_id != sorted_events[0]:
# Front court transfers in = front court transfers out
front_in_vars = [
self.event_transfer_in_vars[(p.id, event_id)]
for p in front_court_players
]
front_out_vars = [
self.event_transfer_out_vars[(p.id, event_id)]
for p in front_court_players
]
model.Add(sum(front_in_vars) == sum(front_out_vars))
# Back court transfers in = back court transfers out
back_in_vars = [
self.event_transfer_in_vars[(p.id, event_id)]
for p in back_court_players
]
back_out_vars = [
self.event_transfer_out_vars[(p.id, event_id)]
for p in back_court_players
]
model.Add(sum(back_in_vars) == sum(back_out_vars))
def _add_budget_constraints(self, model, sorted_events):
"""Add budget constraints for each event"""
for event_id in sorted_events:
event_squad_cost = model.NewIntVar(
0,
self.budget * 10, # Use a reasonable upper bound
f"event_squad_cost_{event_id}",
)
model.Add(
event_squad_cost
== sum(
self.player_in_squad_vars[(p.id, event_id)] * p.get_cost()
for p in self.players
)
)
model.Add(event_squad_cost <= self.budget)
def _add_transfer_penalties(self, model, phases):
"""Add transfer penalties to the objective function"""
objective_terms = []
self._event_transfer_vars_created = True # Flag for solution processing
# Track transfers at phase level for penalty calculation
for phase, phase_events in phases.items():
if not phase_events:
continue
phase_transfers_in = []
# Collect all transfers for the phase
for event_id in phase_events:
phase_transfers_in.extend(
[
self.event_transfer_in_vars[(p.id, event_id)]
for p in self.players
]
)
# Sum up transfers for the phase
phase_transfer_sum = model.NewIntVar(
0,
len(self.players) * len(phase_events),
f"phase_transfer_sum_{phase}",
)
model.Add(phase_transfer_sum == sum(phase_transfers_in))
# Calculate excess transfers for the phase
phase_excess_transfers = model.NewIntVar(
0,
len(self.players) * len(phase_events),
f"phase_excess_transfers_{phase}",
)
# Determine free transfers for this phase
# First phase uses the provided transfers info, subsequent phases get fresh FREE_TRANSFERS
phase_free_transfers = (
self.free_transfers if phase == min(phases.keys())
else self.config.FREE_TRANSFERS
)
# Use AddMaxEquality for max(0, sum - free)
model.AddMaxEquality(
phase_excess_transfers, [phase_transfer_sum - phase_free_transfers, 0]
)
# Add transfer penalty to objective - scale it the same way as points
objective_terms.append(
-phase_excess_transfers
* self._scale_points(self.config.TRANSFER_PENALTY)
)
return objective_terms
def _add_objective_terms(
self, model: cp_model.CpModel, event_ids: list[int]
) -> list:
"""
Adds the objective terms to the model.
This implementation uses the 10-man roster score as a proxy.
"""
objective_terms = []
for event_id in event_ids:
playing_teams = self._get_playing_teams(event_id)
for player in self.players:
if player.team in playing_teams:
points = player.get_score(self.scoring_metric)
scaled_points = self._scale_points(points)
# Add points to objective if player is in the squad for this event
objective_terms.append(
self.player_in_squad_vars[(player.id, event_id)] * scaled_points
)
return objective_terms
def _add_constraints(
self,
model: cp_model.CpModel,
player_vars: dict,
event_ids: list[int],
current_squad: Optional[list[dict]],
wildcard: Optional[bool] = False,
) -> list:
"""Add constraints to the optimization model."""
objective_terms = []
sorted_events = sorted(event_ids)
# ===== 1. SQUAD COMPOSITION CONSTRAINTS (for FINAL squad) =====
# These constraints are applied to player_vars, which is the *final* roster.
# The per-event constraints are handled in the evolution/transfer logic.
model.Add(sum(player_vars.values()) == self.config.SQUAD_SIZE)
for position, players in self.players_by_position.items():
count = (
self.config.FRONT_COURT_COUNT
if position == Position.FRONT_COURT
else self.config.BACK_COURT_COUNT
)
model.Add(sum(player_vars[p.id] for p in players) == count)
model.Add(
sum(player_vars[p.id] * p.get_cost() for p in self.players) <= self.budget
)
for team_players in self.players_by_team.values():
model.Add(
sum(player_vars[p.id] for p in team_players)
<= self.config.MAX_PLAYERS_PER_TEAM
)
# ===== 2. TRANSFER/SQUAD EVOLUTION CONSTRAINTS (if applicable) =====
if current_squad and not wildcard:
objective_terms.extend(
self._add_transfer_constraints(
model, player_vars, event_ids, sorted_events, current_squad
)
)
else:
# No current squad (build from scratch) or wildcard
# We still need to set up the evolution and link the final squad
self._add_squad_evolution_constraints(model, sorted_events)
self._add_final_squad_link_constraints(model, player_vars, sorted_events)
# Apply per-event constraints
self._add_transfer_balance_constraints(model, sorted_events)
self._add_position_balance_constraints(model, sorted_events)
self._add_budget_constraints(model, sorted_events)
# Group events by phase for penalty calculation
phases = defaultdict(list)
for event_id in event_ids:
phase = self._get_gameweek_for_event(event_id)
if phase:
phases[phase].append(event_id)
objective_terms.extend(self._add_transfer_penalties(model, phases))
# ===== 3. OBJECTIVE FUNCTION TERMS (based on roster, not starters) =====
objective_terms.extend(self._add_objective_terms(model, event_ids))
return objective_terms
def optimize(
self,
event_ids: list[int],
current_squad: Optional[list[dict]] = None,
wildcard: Optional[bool] = False,
) -> dict:
"""
Optimize team selection for given events with performance optimizations.
"""
# Early validation
if not event_ids:
return {"error": "No events provided for optimization"}
try:
# Create model and variables
print("Creating optimization model...")
model, player_vars = self._create_optimization_model(
event_ids
)
# Add constraints and get objective terms
print("Adding constraints and objective terms to the model...")
objective_terms = self._add_constraints(
model,
player_vars,
event_ids,
current_squad,
wildcard,
)
# Set objective
model.Maximize(sum(objective_terms))
print("Configuring the model and adding hints...")
# Configure solver with optimized parameters
solver = cp_model.CpSolver()
solver.parameters.max_time_in_seconds = self.config.SOLVER_TIMEOUT
solver.parameters.linearization_level = 2
solver.parameters.cp_model_presolve = True
solver.parameters.cp_model_probing_level = 2
# For large problems, use more aggressive settings
if len(event_ids) > 3 or len(self.players) > 200:
solver.parameters.num_search_workers = min(8, os.cpu_count() or 4)
solver.parameters.log_search_progress = True
# Add hints for high-value players
self._add_solver_hints(model, player_vars, event_ids)
# Solve the model
print("Solving...")
status = solver.Solve(model)
print(f"Solver status :>> {status} ({solver.StatusName()})")
if status in [cp_model.OPTIMAL, cp_model.FEASIBLE]:
return self._process_solution(
solver,
player_vars,
event_ids,
current_squad,
)
else:
return {
"status": "Infeasible",
"solver": "cpsat",
"error": "No solution found by the solver.",
}
except Exception as e:
print(f"Optimization failed: {str(e)}")
import traceback
traceback.print_exc()
return {
"status": "Error",
"solver": "cpsat",
"error": f"An exception occurred: {str(e)}",
}
def _add_solver_hints(self, model, player_vars, event_ids):
"""Add hints to guide the solver toward good solutions."""
# Track which players we've already added hints for
hinted_players = set()
# Hint: Start with high-value players
for player in sorted(
self.players,
key=lambda p: (
p.get_score(self.scoring_metric) / p.get_cost()
if p.get_cost() > 0
else 0
),
reverse=True,
)[
:20
]: # Focus on top 20 value players
if player.id not in hinted_players:
model.AddHint(player_vars[player.id], 1)
hinted_players.add(player.id)
# Hint: Start with players from teams that play in most events
team_event_counts = defaultdict(int)
for event_id in event_ids:
playing_teams = self._get_playing_teams(event_id)
for team_id in playing_teams:
team_event_counts[team_id] += 1
# Prioritize players from teams with most games
for team_id, count in sorted(
team_event_counts.items(), key=lambda x: x[1], reverse=True
):
if count >= len(event_ids) * 0.7: # Teams playing in at least 70% of events
for player in self.players_by_team.get(team_id, [])[
:5
]: # Top 5 players per team
if player.id not in hinted_players:
model.AddHint(player_vars[player.id], 1)
hinted_players.add(player.id)
def _calculate_true_gameday_score(
self, roster_player_ids: list[int], event_id: int
) -> tuple[float, list[dict]]:
"""
Calculates the *true* maximized score for a given 10-player roster
by finding the optimal 3-2 or 2-3 starting lineup for the gameday.
"""
playing_type_1 = [] # Back Court
playing_type_2 = [] # Front Court
playing_teams = self._get_playing_teams(event_id)
for p_id in roster_player_ids:
# Use the fast player_map lookup
player = self.player_map.get(p_id)
if not player:
continue
if player.team in playing_teams:
# Use 'points_per_game' for actual scoring, not the optimization metric
ppg = player.form if self.scoring_metric == "form" else player.points_per_game
player_data = {
"id": player.id,
"name": player.name,
"team": self.team_short_names.get(player.team),
"position": player.position,
"points": ppg,
}
if player.position == Position.BACK_COURT:
playing_type_1.append(player_data)
else:
playing_type_2.append(player_data)
# Sort by points_per_game to easily find the top N
playing_type_1.sort(key=lambda x: x["points"], reverse=True)
playing_type_2.sort(key=lambda x: x["points"], reverse=True)
# Case 1: 3 Back Court (Type 1), 2 Front Court (Type 2)
score_3_2 = sum(p["points"] for p in playing_type_1[:3]) + sum(
p["points"] for p in playing_type_2[:2]
)
starters_3_2 = playing_type_1[:3] + playing_type_2[:2]
# Case 2: 2 Back Court (Type 1), 3 Front Court (Type 2)
score_2_3 = sum(p["points"] for p in playing_type_1[:2]) + sum(
p["points"] for p in playing_type_2[:3]
)
starters_2_3 = playing_type_1[:2] + playing_type_2[:3]
if score_3_2 >= score_2_3:
return score_3_2, starters_3_2
else:
return score_2_3, starters_2_3
def _process_solution(
self,
solver: cp_model.CpSolver,
player_vars: dict,
event_ids: list[int],
current_squad: Optional[list[dict]],
) -> dict:
"""Process optimization solution into result format."""
# Get selected players for the *final* squad
final_player_ids = {
p_id for p_id, var in player_vars.items() if solver.Value(var) == 1
}
final_squad_players = [
self._process_player_for_output(self.player_map[p_id])
for p_id in final_player_ids
if p_id in self.player_map
]
# --- Process results for all events at once ---
daily_starters = {}
all_gameday_rosters = {}
total_true_score = 0
sorted_events = sorted(list(event_ids))
# Check if the multi-stage variables were created
has_event_vars = (
hasattr(self, "_event_transfer_vars_created")
and self._event_transfer_vars_created
)
if has_event_vars:
for event_id in sorted_events:
event_squad_ids = [
p.id
for p in self.players
if (p.id, event_id) in self.player_in_squad_vars
and solver.Value(self.player_in_squad_vars[(p.id, event_id)]) == 1
]
all_gameday_rosters[event_id] = [
self._process_player_for_output(self.player_map[p_id])
for p_id in event_squad_ids
if p_id in self.player_map
]
# Calculate true score for this event's roster
true_score, starters = self._calculate_true_gameday_score(
event_squad_ids, event_id
)
total_true_score += true_score
daily_starters[event_id] = {"score": true_score, "starters": starters}
else:
# This block might be hit if current_squad=None and wildcard=True (not fully modeled)
# Fallback to assuming the final roster was used for all events
for event_id in sorted_events:
all_gameday_rosters[event_id] = final_squad_players
true_score, starters = self._calculate_true_gameday_score(
final_player_ids, event_id
)
total_true_score += true_score
daily_starters[event_id] = {"score": true_score, "starters": starters}
# Collate transfers
transfers_by_event = {
event_id: [] for event_id in sorted_events # Each event will just contain an array of transfers
}
paid_transfers_total = 0
if has_event_vars:
# Group events by phase
phases = defaultdict(list)
for event_id in event_ids:
phase = self._get_gameweek_for_event(event_id)
if phase:
phases[phase].append(event_id)
for phase, phase_events in phases.items():
phase_transfers_in_count = 0
phase_transfers = [] # Track transfers for this phase
# Use the same free transfer logic as in optimization
phase_free_transfers = (
self.free_transfers if phase == min(phases.keys())
else self.config.FREE_TRANSFERS
)
# First collect all transfers for this phase
for event_id in phase_events:
event_transfers_in = []
event_transfers_out = []
for player in self.players:
if (
solver.Value(
self.event_transfer_in_vars[(player.id, event_id)]
)
== 1
):
event_transfers_in.append(player)
phase_transfers_in_count += 1
if (
solver.Value(
self.event_transfer_out_vars[(player.id, event_id)]
)
== 1
):
event_transfers_out.append(player)
# Match transfers in/out and calculate net value
for in_player, out_player in zip(event_transfers_in, event_transfers_out):
transfer_number = len(phase_transfers) + 1
is_paid = transfer_number > phase_free_transfers
penalty = self.config.TRANSFER_PENALTY if is_paid else 0
# Calculate actual contribution for remaining events
remaining_events = [e for e in sorted_events if e >= event_id]
in_player_contributions = []
out_player_contributions = []
# For the outgoing player, we need to look at events where they would have played
# if they hadn't been transferred out
prev_event = event_id - 1 if event_id > min(sorted_events) else event_id
was_in_squad = (
solver.Value(self.player_in_squad_vars[(out_player.id, prev_event)]) == 1
if prev_event >= min(sorted_events)
else True # If it's the first event and they're being transferred out, they were in squad
)
for future_event in remaining_events:
playing_teams = self._get_playing_teams(future_event)
# For incoming player, check if they're actually in squad after transfer
if in_player.team in playing_teams and solver.Value(self.player_in_squad_vars[(in_player.id, future_event)]) == 1:
in_player_contributions.append(in_player.get_score(self.scoring_metric))
# For outgoing player, check what games they would have played if not transferred
# (i.e., if their team plays and they were in the squad before transfer)
if out_player.team in playing_teams and was_in_squad:
out_player_contributions.append(out_player.get_score(self.scoring_metric))
points_gained = sum(in_player_contributions)
points_lost = sum(out_player_contributions)
net_points = points_gained - points_lost - penalty
transfer_info = {
"phase": phase,
"transfer_number": transfer_number,
"is_paid": is_paid,
"penalty": penalty,
"points_gained": points_gained,
"points_lost": points_lost,
"net_points": net_points,
"games_gained": len(in_player_contributions),
"games_lost": len(out_player_contributions),
"in": self._process_player_for_output(in_player),
"out": self._process_player_for_output(out_player),
}
phase_transfers.append(transfer_info)
# Add transfer info directly to the event's transfer array
transfers_by_event[event_id].append(transfer_info)
# Calculate paid transfers for this phase
phase_paid_transfers = max(0, phase_transfers_in_count - phase_free_transfers)
paid_transfers_total += phase_paid_transfers
elif current_squad:
# Fallback for simple case: Compare initial and final
current_squad_ids = {p["element"] for p in current_squad}
t_out_ids = current_squad_ids - final_player_ids
t_in_ids = final_player_ids - current_squad_ids
# Attach those transfers to the first event (best-effort single-event placement)
first_event = sorted_events[0] if sorted_events else None
phase_for_event = (
self._get_gameweek_for_event(first_event) if first_event else None
)
if first_event is not None:
for p_id in t_in_ids:
if p_id in self.player_map:
# Create transfer info for each pair of in/out players
transfer_number = len(transfers_by_event[first_event]) + 1
is_paid = transfer_number > self.free_transfers
penalty = self.config.TRANSFER_PENALTY if is_paid else 0
transfer_info = {
"phase": phase_for_event,
"transfer_number": transfer_number,
"is_paid": is_paid,
"penalty": penalty,
"points_gained": 0, # Can't calculate without optimization
"points_lost": 0, # Can't calculate without optimization
"net_points": -penalty,
"games_gained": 0, # Can't calculate without optimization
"games_lost": 0, # Can't calculate without optimization
"in": self._process_player_for_output(self.player_map[p_id]),
"out": None # Will be set in the t_out_ids loop
}
transfers_by_event[first_event].append(transfer_info)
# Update the transfer info with out player details
for i, (p_id_in, p_id_out) in enumerate(zip(t_in_ids, t_out_ids)):
if p_id_out in self.player_map:
transfers_by_event[first_event][i]["out"] = self._process_player_for_output(
self.player_map[p_id_out]
)
paid_transfers_total = max(0, len(t_in_ids) - self.free_transfers)
# Filter out events that have no transfers at all
transfers_by_event = {
str(event_id): data
for event_id, data in transfers_by_event.items()
if data # Keep events that have any transfers
}
total_transfers = sum(
len(transfers) for transfers in transfers_by_event.values()
)
total_games = sum(len(daily["starters"]) for daily in daily_starters.values())