Created
June 21, 2019 22:40
-
-
Save Noxville/bc0764ecccccf67e21d5d21a7ffce0fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import random | |
class Team: | |
def __init__(self, name, team_id): | |
self.name = name | |
self.team_id = team_id | |
def __repr__(self): | |
return self.name | |
oddsCache = {} | |
def odds(a, b): | |
global oddsCache | |
key = "{}>{}".format(a.team_id, b.team_id) | |
if key in oddsCache: | |
return oddsCache[key] | |
revkey = "{}>{}".format(b.team_id, a.team_id) | |
if revkey in oddsCache: | |
return 1.0 - oddsCache[revkey] | |
odds = single_odds(a, b) | |
oddsCache[key] = odds | |
return odds | |
def vs(a, b, bo=3): | |
towin = (bo + 1) / 2 | |
score_a, score_b = 0, 0 | |
awins = odds(a, b) | |
#print("odds are {}".format(awins)) | |
while (score_a < towin) and (score_b < towin): | |
if random.random() < awins: | |
score_a += 1 | |
else: | |
score_b += 1 | |
return [a, b] if score_a > score_b else [b, a] | |
def single_odds(a, b): | |
# implement your own method here | |
return None | |
groups = { | |
'a': [ | |
Team("Team Secret", 1838315), | |
Team("Fnatic", 350190), | |
Team("Royal Never Give Up", 6209804), | |
Team("paiN Gaming", 67) | |
], | |
'b': [ | |
Team("Evil Geniuses", 39), | |
Team("Team Liquid", 2163), | |
Team("Gambit Esports", 6209143), | |
Team("Infamous Gaming", 2672298) | |
], | |
'c': [ | |
Team("Virtus.pro", 1883502), | |
Team("Vici Gaming", 726228), | |
Team("Forward Gaming", 6214538), | |
Team("Alliance", 111474) | |
], | |
'd': [ | |
Team("PSG.LGD", 15), | |
Team("OG", 2586976), | |
Team("TNC Predator", 2108395), | |
Team("Ninjas in Pyjamas", 6214973) | |
] | |
} | |
N = 10**5 | |
group_res = {} | |
place = {} | |
teams = [] | |
for _ in groups.values(): teams.extend(_) | |
for t in teams: | |
place[t] = {} | |
group_res[t] = {} | |
for res in [1,2,3,4]: | |
group_res[t][res] = 0 | |
placements = ['13-16th', '9-12th', '7-8th', '5-6th', '4th', '3rd', '2nd', '1st'] | |
for i in range(N): | |
if (i % 10**3) == 0: | |
print("percent done {}".format(1 + (i/(10**3)))) | |
res = {} | |
for gr in ['a', 'b', 'c', 'd']: | |
opener_1 = vs(groups[gr][0], groups[gr][3]) | |
opener_2 = vs(groups[gr][1], groups[gr][2]) | |
winners = vs(opener_1[0], opener_2[0]) | |
losers = vs(opener_1[1], opener_2[1]) | |
decider = vs(winners[1], losers[0]) | |
res[gr] = [winners[0], decider[0], decider[1], losers[1]] | |
for i, t in enumerate(res[gr]): | |
group_res[t][1+i] = 1 + group_res[t].get(1+i, 0) | |
# wb r1 | |
wb_r1_1 = vs(res['a'][0], res['c'][1]) | |
wb_r1_2 = vs(res['b'][0], res['d'][1]) | |
wb_r1_3 = vs(res['c'][0], res['b'][1]) | |
wb_r1_4 = vs(res['d'][0], res['a'][1]) | |
# wb r2 | |
wb_r2_1 = vs(wb_r1_1[0], wb_r1_2[0]) | |
wb_r2_2 = vs(wb_r1_3[0], wb_r1_4[0]) | |
# wb r3 | |
wb_finals = vs(wb_r2_1[0], wb_r2_2[0]) | |
# lb r1 | |
lb_r1_1 = vs(res['a'][2], res['c'][3], bo=1) | |
lb_r1_2 = vs(res['b'][2], res['d'][3], bo=1) | |
lb_r1_3 = vs(res['c'][2], res['b'][3], bo=1) | |
lb_r1_4 = vs(res['d'][2], res['a'][3], bo=1) | |
lpk = placements[0] # 13-16 | |
for _ in [lb_r1_1, lb_r1_2, lb_r1_3, lb_r1_4]: | |
place[_[1]][lpk] = 1 + place[_[1]].get(lpk, 0) | |
# lb r2 | |
lb_r2_1 = vs(wb_r1_1[1], lb_r1_1[0]) | |
lb_r2_2 = vs(wb_r1_2[1], lb_r1_2[0]) | |
lb_r2_3 = vs(wb_r1_3[1], lb_r1_3[0]) | |
lb_r2_4 = vs(wb_r1_4[1], lb_r1_4[0]) | |
lpk = placements[1] # 9-12 | |
for _ in [lb_r2_1, lb_r2_2, lb_r2_3, lb_r2_4]: | |
place[_[1]][lpk] = 1 + place[_[1]].get(lpk, 0) | |
# lb r3 | |
lb_r3_1 = vs(lb_r2_1[0], lb_r2_2[0]) | |
lb_r3_2 = vs(lb_r2_3[0], lb_r2_4[0]) | |
lpk = placements[2] # 7-8 | |
for _ in [lb_r3_1, lb_r3_2]: | |
place[_[1]][lpk] = 1 + place[_[1]].get(lpk, 0) | |
# lb r4 | |
lb_r4_1 = vs(lb_r3_1[0], wb_r2_2[1]) | |
lb_r4_2 = vs(lb_r3_2[0], wb_r2_1[1]) | |
lpk = placements[3] # 5-6 | |
for _ in [lb_r4_1, lb_r4_2]: | |
place[_[1]][lpk] = 1 + place[_[1]].get(lpk, 0) | |
# lb r5 | |
lb_r5 = vs(lb_r4_1[0], lb_r4_2[0]) | |
lpk = placements[4] # 4th | |
place[lb_r5[1]][lpk] = 1 + place[lb_r5[1]].get(lpk, 0) | |
# lb final | |
lb_final = vs(lb_r5[0], wb_finals[1]) | |
lpk = placements[5] # 3rd | |
place[lb_final[1]][lpk] = 1 + place[lb_final[1]].get(lpk, 0) | |
# finals | |
grand_finals = vs(lb_final[0], wb_finals[0], bo=5) | |
place[grand_finals[1]]['2nd'] = 1 + place[grand_finals[1]].get('2nd', 0) | |
place[grand_finals[0]]['1st'] = 1 + place[grand_finals[0]].get('1st', 0) | |
def score(t): | |
global placements | |
global place | |
ret = 0 | |
weight = 1 | |
for p in placements: | |
ret += (weight * place[t].get(p, 0)) | |
weight *= 5 | |
return ret | |
print("Team," + ",".join(placements)) | |
for t in sorted(teams, key=score, reverse=True): | |
print(t.name + "," + ",".join(map(str, [float(place[t].get(_, 0))/N for _ in placements]))) | |
print("") | |
for g in groups: | |
print("Group {},1st,2nd,3rd,4th".format(str.upper(g))) | |
for t in sorted(groups[g], key=lambda x: tuple([group_res[t][_] for _ in [1,2,3,4]]), reverse=True): | |
print(t.name + "," + ",".join(map(str, [float(group_res[t].get(_, 0))/N for _ in [1,2,3,4]]))) | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment