Spaces:
Runtime error
Runtime error
Update elo.py
Browse files
elo.py
CHANGED
@@ -1,37 +1,57 @@
|
|
1 |
import pandas as pd
|
2 |
from datasets import Dataset
|
3 |
|
4 |
-
def calculate_elo(old_rating, opponent_rating, score, k_factor
|
5 |
"""
|
6 |
Calculate the new ELO rating for a player.
|
7 |
|
8 |
:param old_rating: The current ELO rating of the player.
|
9 |
:param opponent_rating: The ELO rating of the opponent.
|
10 |
:param score: The score of the game (1 for win, 0.5 for draw, 0 for loss).
|
11 |
-
:param k_factor: The K-factor used in ELO rating
|
12 |
:return: The new ELO rating.
|
13 |
"""
|
14 |
expected_score = 1 / (1 + 10 ** ((opponent_rating - old_rating) / 400))
|
15 |
new_rating = old_rating + k_factor * (score - expected_score)
|
16 |
return new_rating
|
17 |
|
18 |
-
def update_elo_ratings(ratings_dataset, winner, loser
|
19 |
# Convert the Hugging Face dataset to a pandas DataFrame
|
20 |
ratings_df = pd.DataFrame(ratings_dataset)
|
21 |
|
22 |
# Check and add new players if they don't exist in the dataset
|
23 |
for player in [winner, loser]:
|
24 |
if player not in ratings_df['bot_name'].values:
|
25 |
-
new_player = {'bot_name': player, 'elo_rating': 1200}
|
26 |
-
ratings_df = pd.concat([ratings_df,pd.DataFrame([new_player])],ignore_index=True)
|
27 |
|
28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
winner_old_rating = ratings_df.loc[ratings_df['bot_name'] == winner, 'elo_rating'].iloc[0]
|
30 |
loser_old_rating = ratings_df.loc[ratings_df['bot_name'] == loser, 'elo_rating'].iloc[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Calculate new ratings
|
33 |
-
winner_new_rating = calculate_elo(winner_old_rating, loser_old_rating, 1,
|
34 |
-
loser_new_rating = calculate_elo(loser_old_rating, winner_old_rating, 0,
|
35 |
|
36 |
# Update the DataFrame
|
37 |
ratings_df.loc[ratings_df['bot_name'] == winner, 'elo_rating'] = winner_new_rating
|
@@ -40,4 +60,4 @@ def update_elo_ratings(ratings_dataset, winner, loser, k_factor=32):
|
|
40 |
# Convert the DataFrame back to a Hugging Face dataset
|
41 |
updated_ratings_dataset = Dataset.from_pandas(ratings_df)
|
42 |
|
43 |
-
return updated_ratings_dataset
|
|
|
1 |
import pandas as pd
|
2 |
from datasets import Dataset
|
3 |
|
4 |
+
def calculate_elo(old_rating, opponent_rating, score, k_factor):
|
5 |
"""
|
6 |
Calculate the new ELO rating for a player.
|
7 |
|
8 |
:param old_rating: The current ELO rating of the player.
|
9 |
:param opponent_rating: The ELO rating of the opponent.
|
10 |
:param score: The score of the game (1 for win, 0.5 for draw, 0 for loss).
|
11 |
+
:param k_factor: The K-factor used in ELO rating.
|
12 |
:return: The new ELO rating.
|
13 |
"""
|
14 |
expected_score = 1 / (1 + 10 ** ((opponent_rating - old_rating) / 400))
|
15 |
new_rating = old_rating + k_factor * (score - expected_score)
|
16 |
return new_rating
|
17 |
|
18 |
+
def update_elo_ratings(ratings_dataset, winner, loser):
|
19 |
# Convert the Hugging Face dataset to a pandas DataFrame
|
20 |
ratings_df = pd.DataFrame(ratings_dataset)
|
21 |
|
22 |
# Check and add new players if they don't exist in the dataset
|
23 |
for player in [winner, loser]:
|
24 |
if player not in ratings_df['bot_name'].values:
|
25 |
+
new_player = {'bot_name': player, 'elo_rating': 1200, 'games_played': 0}
|
26 |
+
ratings_df = pd.concat([ratings_df, pd.DataFrame([new_player])], ignore_index=True)
|
27 |
|
28 |
+
# Function to determine the K-factor based on games played
|
29 |
+
def determine_k_factor(games_played):
|
30 |
+
# Define K-factor based on number of games played. Adjust these thresholds as needed.
|
31 |
+
if games_played < 30:
|
32 |
+
return 40
|
33 |
+
elif games_played < 100:
|
34 |
+
return 20
|
35 |
+
else:
|
36 |
+
return 10
|
37 |
+
|
38 |
+
# Update games played
|
39 |
+
ratings_df.loc[ratings_df['bot_name'] == winner, 'games_played'] += 1
|
40 |
+
ratings_df.loc[ratings_df['bot_name'] == loser, 'games_played'] += 1
|
41 |
+
|
42 |
+
# Extract old ratings and games played
|
43 |
winner_old_rating = ratings_df.loc[ratings_df['bot_name'] == winner, 'elo_rating'].iloc[0]
|
44 |
loser_old_rating = ratings_df.loc[ratings_df['bot_name'] == loser, 'elo_rating'].iloc[0]
|
45 |
+
winner_games_played = ratings_df.loc[ratings_df['bot_name'] == winner, 'games_played'].iloc[0]
|
46 |
+
loser_games_played = ratings_df.loc[ratings_df['bot_name'] == loser, 'games_played'].iloc[0]
|
47 |
+
|
48 |
+
# Determine K-factors
|
49 |
+
winner_k_factor = determine_k_factor(winner_games_played)
|
50 |
+
loser_k_factor = determine_k_factor(loser_games_played)
|
51 |
|
52 |
# Calculate new ratings
|
53 |
+
winner_new_rating = calculate_elo(winner_old_rating, loser_old_rating, 1, winner_k_factor)
|
54 |
+
loser_new_rating = calculate_elo(loser_old_rating, winner_old_rating, 0, loser_k_factor)
|
55 |
|
56 |
# Update the DataFrame
|
57 |
ratings_df.loc[ratings_df['bot_name'] == winner, 'elo_rating'] = winner_new_rating
|
|
|
60 |
# Convert the DataFrame back to a Hugging Face dataset
|
61 |
updated_ratings_dataset = Dataset.from_pandas(ratings_df)
|
62 |
|
63 |
+
return updated_ratings_dataset
|