-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.py
72 lines (56 loc) · 1.77 KB
/
sim.py
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
import random
import time
import elo
from functools import cmp_to_key
random.seed(time.time())
def simulate_league(team_elo, table, schedule, hfa, h2h=None, temp_h2h=None):
res = []
for game in schedule:
(w,d,l) = elo.calculate_result_probs(team_elo[game['home']], team_elo[game['away']], HFA=hfa)
result = random.choices(
population=['w', 'd', 'l'],
weights=[w,d,l],
k=1
)[0]
if result == 'w':
table[game['home']] += 3
elif result == 'd':
table[game['home']] += 1
table[game['away']] += 1
else:
table[game['away']] += 3
table = list(table.items())
random.shuffle(table)
def sort_h2h(teamA, teamB):
if teamA[1] > teamB[1]:
return -1
if teamA[1] < teamB[1]:
return 1
return 0
table = list(sorted(table, key=cmp_to_key(sort_h2h)))
for team in table:
res.append(team[0])
return res
def simulate_cup(team_elo, schedule):
while(len(schedule) > 0):
winners = []
for game in schedule:
(w,d,l) = elo.calculate_result_probs(team_elo[game['home']], team_elo[game['away']], draw_possible=False)
result = random.choices(
population=['w', 'l'],
weights=[w,l],
k=1
)[0]
if result == 'w':
winners.append(game['home'])
else:
winners.append(game['away'])
random.shuffle(winners)
schedule = []
for i, team in enumerate(winners):
if i % 2 == 1:
schedule.append({
'home': winners[i-1],
'away': winners[i]
})
return winners[0]