-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsims.py
151 lines (115 loc) · 4.3 KB
/
sims.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
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
from teams import *
from collections import OrderedDict
# figures out which teams made the playoffs
def check_playoffs(teams):
teams_list = list(teams.values())
teams_list.sort()
teams_list.reverse()
for i in range(0, 6):
teams_list[i].increment_playoffs()
for i in range(0, 2):
teams_list[i].increment_bye()
# return ranked teams
return teams_list
# pair 1v2, 3v4, etc.
def swiss_pairings(teams):
teams_list = list(teams.values())
teams_list.sort()
teams_list.reverse()
return [[teams_list[i], teams_list[i+1]] for i in range(0, 12, 2)]
# accumulates averages based on simulation -- prob should do this with a class method instead
def update_stats(teams, sim):
n = float(sim)
for team in teams.values():
team.avg_W = (1.0 / (n + 1.0)) * (team.avg_W * sim + team.tot_W())
team.avg_pts = (1.0 / (n + 1.0)) * (team.avg_pts * sim + team.tot_pts())
# create teams from input file
def create_teams(past_data):
teams = OrderedDict()
for matchup in past_data:
team1 = matchup[0]
team2 = matchup[1]
score1 = float(matchup[2])
score2 = float(matchup[3])
if score1 > score2:
result = [1, 0] # win, loss
elif score1 < score2:
result = [0, 1] # loss, win
else:
result = [0.5, 0.5] # tie
# check if team 1 exists, init team or add game
if team1 in teams.keys():
teams[team1].add_game(result[0], score1)
else:
teams[team1] = team(team1, result[0], score1)
# same for team 2
if team2 in teams.keys():
teams[team2].add_game(result[1], score2)
else:
teams[team2] = team(team2, result[1], score2)
return teams
# simulate a game
def faceoff(team1, team2, playoffs=False, two_week_faceoff=False):
score1 = team1.play_game()
score2 = team2.play_game()
# we play two weeks if this is the champs
if two_week_faceoff:
score1 += team1.play_game()
score2 += team2.play_game()
if (score1 > score2):
res = [1, 0]
winner = team1
else:
res = [0, 1]
winner = team2
# seems like this doesn't do anything important :shrug:
if not playoffs:
team1.add_fut_res(res[0], score1)
team2.add_fut_res(res[1], score2)
return winner
# this will need to be modified based on playoff format. in my league
# it's w1 top 6 (2 byes), w2 top 4, and w3+w4 championship
def compute_playoffs(ranked_teams, two_week_champs):
# divisional
winner1 = faceoff(ranked_teams[2], ranked_teams[5], playoffs=True)
winner2 = faceoff(ranked_teams[3], ranked_teams[4], playoffs=True)
ranked_teams[0].add_final_4()
ranked_teams[1].add_final_4()
winner1.add_final_4()
winner2.add_final_4()
# ranked_teams[0].add_final_4()
# ranked_teams[1].add_final_4()
# ranked_teams[2].add_final_4()
# ranked_teams[3].add_final_4()
# semis
winner3=faceoff(winner2, ranked_teams[0], playoffs=True)
winner4=faceoff(winner1, ranked_teams[1], playoffs=True)
# winner3 = faceoff(ranked_teams[0], ranked_teams[3])
# winner4 = faceoff(ranked_teams[1], ranked_teams[2])
winner3.add_final_2()
winner4.add_final_2()
# finals
if two_week_champs:
champ = faceoff(winner3, winner4, playoffs=True, two_week_faceoff=True)
else:
champ = faceoff(winner3, winner4, playoffs=True, two_week_faceoff=False)
champ.add_champ()
return
def run_sim(sim_ii, teams, future_matchups, add_swiss_round=False, two_week_champs=False):
# simulate each future matchup
for matchup in future_matchups:
faceoff(teams[matchup[0]], teams[matchup[1]])
# simulate week 12 by swiss pairings (if nec)
if add_swiss_round:
pairings = swiss_pairings(teams)
for pp in pairings:
faceoff(pp[0], pp[1])
# increment playoff counters
ranked_teams = check_playoffs(teams)
# update avgW and avgPts
update_stats(teams, sim_ii)
# run playoff simulation
compute_playoffs(ranked_teams, two_week_champs)
# reset future wins/pts
for team in teams.values():
team.reset()