Sampler-Arena / elo.py
rwitz's picture
Update elo.py
c071bfb verified
raw
history blame
1.93 kB
import math
def update_elo_ratings(ratings_dict, winner, loser):
# Extract old ratings and games played
winner_old_rating = ratings_dict.get(winner, {}).get('elo_rating', 1200)
loser_old_rating = ratings_dict.get(loser, {}).get('elo_rating', 1200)
winner_games_played = ratings_dict.get(winner, {}).get('games_played', 0)
loser_games_played = ratings_dict.get(loser, {}).get('games_played', 0)
# Function to determine the K-factor based on games played
def determine_k_factor(games_played):
# Define K-factor based on number of games played. Adjust these thresholds as needed.
if games_played < 30:
return 40
elif games_played < 100:
return 20
else:
return 10
# Determine K-factors
winner_k_factor = determine_k_factor(winner_games_played)
loser_k_factor = determine_k_factor(loser_games_played)
def elo(winner_rating, loser_rating, k_factor_winner=32, k_factor_loser=32):
# Calculate the expected scores for each player
winner_expected = 1 / (1 + 10 ** ((loser_rating - winner_rating) / 400))
loser_expected = 1 / (1 + 10 ** ((winner_rating - loser_rating) / 400))
# Calculate the new ratings for each player
winner_new_rating = winner_rating + k_factor_winner * (1 - winner_expected)
loser_new_rating = loser_rating + k_factor_loser * (0 - loser_expected)
return winner_new_rating, loser_new_rating
# Calculate new ratings
winner_new_rating, loser_new_rating = elo(winner_old_rating, loser_old_rating, k_factor_winner=winner_k_factor, k_factor_loser=loser_k_factor)
# Update ratings and games played in the dictionary
ratings_dict[winner] = {'elo_rating': winner_new_rating, 'games_played': winner_games_played + 1}
ratings_dict[loser] = {'elo_rating': loser_new_rating, 'games_played': loser_games_played + 1}
return ratings_dict