Skip to content

Instantly share code, notes, and snippets.

@drwebb
Created July 12, 2022 20:07
Show Gist options
  • Save drwebb/bf4d67d45310b4f728fc0322d207cdc4 to your computer and use it in GitHub Desktop.
Save drwebb/bf4d67d45310b4f728fc0322d207cdc4 to your computer and use it in GitHub Desktop.
def balance_teams(players, n_players, balance_mode):
# Partition or all set of team combinations
possible_teams = sympy.utilities.iterables.multiset_partitions(players,2)
# Filter possible teams to
filtered_possible_teams = []
for p in possible_teams:
if(all([len(p_)==int(n_players/2) for p_ in p])):
filtered_possible_teams.append(p)
if balance_mode == "win_prob":
#Testing quality instead of win_porob
#win_probs = list(map(lambda x: win_probability(x[0], x[1], mode="system"), filtered_possible_teams))
win_probs = list(map(lambda x: ts_env.quality(
(tuple(map(lambda y: y.system_rating, x[0])),
tuple(map(lambda y: y.system_rating, x[1])))), filtered_possible_teams))
# Balance by max quality
best_balance_idx = np.argmax(win_probs)
if balance_mode == "ts_sum":
ts_deviation = list(
map(lambda x: abs(
sum(team_mus(x[0], mode="system")) - sum(team_mus(x[1], mode="system")))
, filtered_possible_teams))
# Balance by smallest true skill variance
best_balance_idx = np.argmin(list(map(lambda x: abs(x), ts_deviation)))
if balance_mode == "trusted_sum":
trusted_deviation = list(map(lambda x: abs(
(sum(team_mus(x[0])) - 3*sum(team_sigmas(x[0]))) - (sum(team_mus(x[1])) - 3*sum(team_sigmas(x[1])))),
filtered_possible_teams))
# Balance by smallest trusted skill variance
best_balance_idx = np.argmin(list(map(lambda x: abs(x), trusted_deviation)))
return(filtered_possible_teams[best_balance_idx])
def team_ratings(team, mode="system"):
if mode == "system":
return [p.system_rating for p in team]
else:
return [p.actual_rating for p in team]
def team_mus(team, mode="system"):
"""Returns a list of teams mus"""
if mode == "system":
return [p.system_rating.mu for p in team]
else:
return [p.actual_rating.mu for p in team]
def team_sigmas(team, mode="system"):
"""Returns a list of teams sigmas"""
if mode == "system":
return [p.system_rating.sigma for p in team]
else:
return [p.actual_rating.sigma for p in team]
def win_probability(team1, team2, mode):
""" TS win probability calc used for sim"""
delta_mu = sum(team_mus(team1, mode)) - sum(team_mus(team2, mode))
sum_sigma = sum([r ** 2 for r in team_sigmas(team1 + team2, mode)])
size = len(team1) + len(team2)
denom = math.sqrt(size * (ts_beta * ts_beta) + sum_sigma)
return ts_env.cdf(delta_mu / denom)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment