Spaces:
Runtime error
Runtime error
File size: 2,812 Bytes
dd89a50 077b9b1 79132a6 077b9b1 79132a6 d5f3e47 077b9b1 d5f3e47 dd89a50 d5f3e47 077b9b1 d5f3e47 077b9b1 a7460e5 077b9b1 a7460e5 dd89a50 077b9b1 79132a6 dd89a50 a7460e5 dd89a50 79132a6 077b9b1 |
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 |
import pandas as pd
from datasets import Dataset
def calculate_elo(old_rating, opponent_rating, score, k_factor):
"""
Calculate the new ELO rating for a player.
:param old_rating: The current ELO rating of the player.
:param opponent_rating: The ELO rating of the opponent.
:param score: The score of the game (1 for win, 0.5 for draw, 0 for loss).
:param k_factor: The K-factor used in ELO rating.
:return: The new ELO rating.
"""
expected_score = 1 / (1 + 10 ** ((opponent_rating - old_rating) / 400))
new_rating = old_rating + k_factor * (score - expected_score)
return new_rating
def update_elo_ratings(ratings_dataset, winner, loser):
# Convert the Hugging Face dataset to a pandas DataFrame
ratings_df = pd.DataFrame(ratings_dataset)
# Check and add new players if they don't exist in the dataset
for player in [winner, loser]:
if player not in ratings_df['bot_name'].values:
new_player = {'bot_name': player, 'elo_rating': 1200, 'games_played': 0}
ratings_df = pd.concat([ratings_df, pd.DataFrame([new_player])], ignore_index=True)
# 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
# Update games played
ratings_df.loc[ratings_df['bot_name'] == winner, 'games_played'] += 1
ratings_df.loc[ratings_df['bot_name'] == loser, 'games_played'] += 1
# Extract old ratings and games played
winner_old_rating = ratings_df.loc[ratings_df['bot_name'] == winner, 'elo_rating'].iloc[0]
loser_old_rating = ratings_df.loc[ratings_df['bot_name'] == loser, 'elo_rating'].iloc[0]
winner_games_played = ratings_df.loc[ratings_df['bot_name'] == winner, 'games_played'].iloc[0]
loser_games_played = ratings_df.loc[ratings_df['bot_name'] == loser, 'games_played'].iloc[0]
# Determine K-factors
winner_k_factor = determine_k_factor(winner_games_played)
loser_k_factor = determine_k_factor(loser_games_played)
# Calculate new ratings
winner_new_rating = calculate_elo(winner_old_rating, loser_old_rating, 1, winner_k_factor)
loser_new_rating = calculate_elo(loser_old_rating, winner_old_rating, 0, loser_k_factor)
# Update the DataFrame
ratings_df.loc[ratings_df['bot_name'] == winner, 'elo_rating'] = winner_new_rating
ratings_df.loc[ratings_df['bot_name'] == loser, 'elo_rating'] = loser_new_rating
# Convert the DataFrame back to a Hugging Face dataset
updated_ratings_dataset = Dataset.from_pandas(ratings_df)
return updated_ratings_dataset
|