Created
January 3, 2021 05:50
-
-
Save Noxville/9c26e80e55048601b900c2c11088c488 to your computer and use it in GitHub Desktop.
This file contains 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 copy | |
import math | |
import random | |
MINUS_BIG = -100000 | |
def gen_teams(n, d=20): | |
return list(range(1000, 1000 + (n * d), d)) | |
def elo(a, b, boX=1): | |
a_score, b_score = 0, 0 | |
d = (0.0 + a - b) / 400.0 | |
threshold = 1. / (1.0 + pow(10.0, d)) | |
while max(a_score, b_score) < (1 + boX) / 2: | |
if random.random() > threshold: | |
a_score += 1 | |
else: | |
b_score += 1 | |
return (a, b) if a_score > b_score else (b,a) | |
def chunks(lst, n): | |
for s in range(0, len(lst), n): | |
yield lst[s:s + n] | |
def dotapit(teams): | |
# Single run, top 4 move forward | |
while len(teams) > 4: | |
random.shuffle(teams) | |
winners = [] | |
for pairs in chunks(teams, 2): | |
match = elo(pairs[0], pairs[1], 3 if len(teams) == 8 else 1) # Quarterfinals are bo3 | |
winners.append(match[0]) | |
teams = winners | |
return teams | |
def N_runs_K_winners(teams, n, k): | |
# N events, top K from each move forward | |
qualified = [] | |
for r in range(n): | |
qualified.extend(do_run([t for t in copy.copy(teams) if t not in qualified], k)) | |
return qualified | |
def do_run(teams, k): | |
while len(teams) >= pow(2,k): | |
random.shuffle(teams) | |
byes = pow(2, math.ceil(math.log(len(teams), 2))) - len(teams) | |
winners = [] | |
for _ in range(byes): | |
winners.append(teams.pop()) | |
for pairs in chunks(teams, 2): | |
match = elo(pairs[0], pairs[1], 3 if len(teams) == k * 2 else 1) # Final matches are | |
winners.append(match[0]) | |
teams = winners | |
return teams | |
teams = gen_teams(128) | |
# Counter of times qualified for each skill | |
dp_format = {t:0 for t in teams} | |
two_two = {t:0 for t in teams} | |
four_one = {t:0 for t in teams} | |
N = 10**4 | |
for _ in range(N): | |
if _ % 10**3 == 0: print("poke") | |
for t in dotapit(copy.copy(teams)): | |
dp_format[t] = 1 + dp_format[t] | |
for t in N_runs_K_winners(copy.copy(teams), 2, 2): | |
two_two[t] = 1 + two_two[t] | |
for t in N_runs_K_winners(copy.copy(teams), 4, 1): | |
four_one[t] = 1 + four_one[t] | |
print("team_skill,dotapit,two_two,four_one") | |
for t in teams: | |
print("{},{},{},{}".format(t, dp_format[t], two_two[t], four_one[t])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment