Skip to content

Commit fa9cdaf

Browse files
committed
fix incorrect points hits if transfers already made in gw
1 parent 1e0657a commit fa9cdaf

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

airsenal/framework/data_fetcher.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,9 @@ def get_num_free_transfers(self, fpl_team_id=None):
377377
Requires login
378378
"""
379379
squad_data = self.get_current_squad_data(fpl_team_id)
380-
return squad_data["transfers"]["limit"]
380+
return max(
381+
0, squad_data["transfers"]["limit"] - squad_data["transfers"]["made"]
382+
)
381383

382384
def get_current_bank(self, fpl_team_id=None):
383385
"""

airsenal/framework/optimization_utils.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def next_week_transfers(
436436
"""Given a previous strategy and some optimisation constraints, determine the valid
437437
options for the number of transfers (or chip played) in the following gameweek.
438438
439-
strat is a tuple (free_transfers, hit_so_far, strat_dict)
439+
strat is a tuple (free_transfers, total_points_hits, strat_dict)
440440
strat_dict must have key chips_played, which is a dict indexed by gameweek with
441441
possible values None, "wildcard", "free_hit", "bench_boost" or triple_captain"
442442
@@ -446,7 +446,9 @@ def next_week_transfers(
446446
max_free_transfers - maximum number of free transfers saved in the game rules
447447
(2 before 2024/25, 5 from 2024/25 season)
448448
449-
Returns (new_transfers, new_ft_available, new_points_hits) tuples.
449+
Returns (new_transfers, new_ft_available, total_points_hits, hit_this_gw) tuples.
450+
- total_points_hits is the total points hit so far including this gw
451+
- hit_this_gw is the points hit incurred this gameweek
450452
"""
451453
# check that the 'chips' dict we are given makes sense:
452454
if chips is None:
@@ -524,17 +526,20 @@ def next_week_transfers(
524526
if allow_triple_captain:
525527
new_transfers += [f"T{nt}" for nt in ft_choices]
526528

527-
new_points_hits = [
528-
hit_so_far + calc_points_hit(nt, ft_available) for nt in new_transfers
529-
]
529+
hit_this_gw = [calc_points_hit(nt, ft_available) for nt in new_transfers]
530+
total_points_hits = [hit_so_far + hit for hit in hit_this_gw]
530531
new_ft_available = [
531532
calc_free_transfers(nt, ft_available, max_free_transfers)
532533
for nt in new_transfers
533534
]
534535

535536
# return list of (num_transfers, free_transfers, hit_so_far) tuples for each new
536537
# strategy
537-
return list(zip(new_transfers, new_ft_available, new_points_hits, strict=False))
538+
return list(
539+
zip(
540+
new_transfers, new_ft_available, total_points_hits, hit_this_gw, strict=True
541+
)
542+
)
538543

539544

540545
def count_expected_outputs(
@@ -590,7 +595,7 @@ def count_expected_outputs(
590595
max_free_transfers=max_free_transfers,
591596
)
592597

593-
for n_transfers, new_free_transfers, new_hit in possibilities:
598+
for n_transfers, new_free_transfers, new_hit, _ in possibilities:
594599
# make a copy of the strategy up to this point, then add on this gw
595600
new_dict = deepcopy(s[2])
596601

airsenal/scripts/fill_transfersuggestion_table.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
from airsenal.framework.optimization_transfers import make_best_transfers
3838
from airsenal.framework.optimization_utils import (
3939
MAX_FREE_TRANSFERS,
40-
calc_points_hit,
4140
check_tag_valid,
4241
count_expected_outputs,
4342
fill_suggestion_table,
@@ -131,7 +130,15 @@ def optimize(
131130
else:
132131
profiler = None
133132

134-
num_transfers, free_transfers, hit_so_far, squad, strat_dict, sid = status
133+
(
134+
num_transfers,
135+
free_transfers,
136+
hit_so_far,
137+
hit_this_gw,
138+
squad,
139+
strat_dict,
140+
sid,
141+
) = status
135142
# num_transfers will be 0, 1, 2, OR 'W' or 'F', OR 'T0', T1', 'T2',
136143
# OR 'B0', 'B1', or 'B2' (the latter six represent triple captain or
137144
# bench boost along with 0, 1, or 2 transfers).
@@ -203,18 +210,17 @@ def optimize(
203210
(updater, increment, pid) if updater is not None else None,
204211
)
205212

206-
points_hit = calc_points_hit(num_transfers, free_transfers)
213+
# points_hit = calc_points_hit(num_transfers, free_transfers)
207214
discount_factor = get_discount_factor(root_gw, gw)
208-
points -= points_hit * discount_factor
215+
points -= hit_this_gw * discount_factor
209216
strat_dict["total_score"] += points
210217
strat_dict["points_per_gw"][gw] = points
211218
strat_dict["free_transfers"][gw] = free_transfers
212219
strat_dict["num_transfers"][gw] = num_transfers
213-
strat_dict["points_hit"][gw] = points_hit
220+
strat_dict["points_hit"][gw] = hit_this_gw
214221
strat_dict["discount_factor"][gw] = discount_factor
215222
strat_dict["players_in"][gw] = transfers["in"]
216223
strat_dict["players_out"][gw] = transfers["out"]
217-
218224
depth += 1
219225

220226
if depth >= len(gameweek_range):
@@ -240,16 +246,15 @@ def optimize(
240246
chips=chips_gw_dict[gw + 1],
241247
max_free_transfers=max_free_transfers,
242248
)
243-
244249
for strat in strategies:
245-
# strat: (num_transfers, free_transfers, hit_so_far)
246-
num_transfers, free_transfers, hit_so_far = strat
250+
num_transfers, free_transfers, hit_so_far, hit_this_gw = strat
247251

248252
queue.put(
249253
(
250254
num_transfers,
251255
free_transfers,
252256
hit_so_far,
257+
hit_this_gw,
253258
new_squad,
254259
strat_dict,
255260
sid,
@@ -319,7 +324,8 @@ def print_strat(strat: dict) -> None:
319324
print(" ===============================================")
320325
for gw in gameweeks_as_int:
321326
print(f"\n =========== Gameweek {gw} ================\n")
322-
print(f"Chips played: {strat['chips_played'][str(gw)]}\n")
327+
print(f"Chips played: {strat['chips_played'][str(gw)]}")
328+
print(f"Points Hits: {strat['points_hit'][str(gw)]}\n")
323329
print("Players in:\t\t\tPlayers out:")
324330
print("-----------\t\t\t------------")
325331
for i in range(len(strat["players_in"][str(gw)])):
@@ -493,6 +499,7 @@ def run_optimization(
493499
apifetcher=fetcher,
494500
is_replay=is_replay,
495501
)
502+
print(f"Starting with {num_free_transfers} free transfers")
496503

497504
# create the output directory for temporary json files
498505
# giving the points prediction for each strategy
@@ -596,7 +603,7 @@ def update_progress(increment=1, index=None):
596603
processor.start()
597604
procs.append(processor)
598605
# add starting node to the queue
599-
squeue.put((0, num_free_transfers, 0, starting_squad, {}, "starting"))
606+
squeue.put((0, num_free_transfers, 0, 0, starting_squad, {}, "starting"))
600607

601608
for i, p in enumerate(procs):
602609
progress_bars[i].close()

0 commit comments

Comments
 (0)