rwitz commited on
Commit
c071bfb
1 Parent(s): e6db662

Update elo.py

Browse files
Files changed (1) hide show
  1. elo.py +14 -44
elo.py CHANGED
@@ -1,23 +1,11 @@
1
- import pandas as pd
2
-
3
  import math
4
 
5
  def update_elo_ratings(ratings_dict, winner, loser):
6
- # Check if the ratings_dict is empty
7
- if not ratings_dict:
8
- # Create a blank DataFrame with the required columns
9
- ratings_df = pd.DataFrame(columns=['bot_name', 'elo_rating', 'games_played'])
10
- else:
11
- # Convert the dictionary to a pandas DataFrame
12
- ratings_df = pd.DataFrame.from_dict(ratings_dict, orient='index')
13
- ratings_df.reset_index(inplace=True)
14
- ratings_df.columns = ['bot_name', 'elo_rating', 'games_played']
15
-
16
- # Check and add new players if they don't exist in the dataset
17
- for player in [winner, loser]:
18
- if player not in ratings_df['bot_name'].values:
19
- new_player = {'bot_name': player, 'elo_rating': 1200, 'games_played': 0}
20
- ratings_df = pd.concat([ratings_df, pd.DataFrame([new_player])], ignore_index=True)
21
 
22
  # Function to determine the K-factor based on games played
23
  def determine_k_factor(games_played):
@@ -28,6 +16,11 @@ def update_elo_ratings(ratings_dict, winner, loser):
28
  return 20
29
  else:
30
  return 10
 
 
 
 
 
31
  def elo(winner_rating, loser_rating, k_factor_winner=32, k_factor_loser=32):
32
  # Calculate the expected scores for each player
33
  winner_expected = 1 / (1 + 10 ** ((loser_rating - winner_rating) / 400))
@@ -39,34 +32,11 @@ def update_elo_ratings(ratings_dict, winner, loser):
39
 
40
  return winner_new_rating, loser_new_rating
41
 
42
- # ...
43
-
44
  # Calculate new ratings
45
  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)
46
 
 
 
 
47
 
48
- # Update games played
49
- ratings_df.loc[ratings_df['bot_name'] == winner, 'games_played'] += 1
50
- ratings_df.loc[ratings_df['bot_name'] == loser, 'games_played'] += 1
51
-
52
- # Extract old ratings and games played
53
- winner_old_rating = ratings_df.loc[ratings_df['bot_name'] == winner, 'elo_rating'].iloc[0]
54
- loser_old_rating = ratings_df.loc[ratings_df['bot_name'] == loser, 'elo_rating'].iloc[0]
55
- winner_games_played = ratings_df.loc[ratings_df['bot_name'] == winner, 'games_played'].iloc[0]
56
- loser_games_played = ratings_df.loc[ratings_df['bot_name'] == loser, 'games_played'].iloc[0]
57
-
58
- # Determine K-factors
59
- winner_k_factor = determine_k_factor(winner_games_played)
60
- loser_k_factor = determine_k_factor(loser_games_played)
61
-
62
- # Calculate new ratings
63
- 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)
64
-
65
- # Update the DataFrame
66
- ratings_df.loc[ratings_df['bot_name'] == winner, 'elo_rating'] = winner_new_rating
67
- ratings_df.loc[ratings_df['bot_name'] == loser, 'elo_rating'] = loser_new_rating
68
-
69
- # Convert the DataFrame to a dictionary
70
- updated_ratings_dict = ratings_df.set_index('bot_name').to_dict(orient='index')
71
-
72
- return updated_ratings_dict
 
 
 
1
  import math
2
 
3
  def update_elo_ratings(ratings_dict, winner, loser):
4
+ # Extract old ratings and games played
5
+ winner_old_rating = ratings_dict.get(winner, {}).get('elo_rating', 1200)
6
+ loser_old_rating = ratings_dict.get(loser, {}).get('elo_rating', 1200)
7
+ winner_games_played = ratings_dict.get(winner, {}).get('games_played', 0)
8
+ loser_games_played = ratings_dict.get(loser, {}).get('games_played', 0)
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Function to determine the K-factor based on games played
11
  def determine_k_factor(games_played):
 
16
  return 20
17
  else:
18
  return 10
19
+
20
+ # Determine K-factors
21
+ winner_k_factor = determine_k_factor(winner_games_played)
22
+ loser_k_factor = determine_k_factor(loser_games_played)
23
+
24
  def elo(winner_rating, loser_rating, k_factor_winner=32, k_factor_loser=32):
25
  # Calculate the expected scores for each player
26
  winner_expected = 1 / (1 + 10 ** ((loser_rating - winner_rating) / 400))
 
32
 
33
  return winner_new_rating, loser_new_rating
34
 
 
 
35
  # Calculate new ratings
36
  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)
37
 
38
+ # Update ratings and games played in the dictionary
39
+ ratings_dict[winner] = {'elo_rating': winner_new_rating, 'games_played': winner_games_played + 1}
40
+ ratings_dict[loser] = {'elo_rating': loser_new_rating, 'games_played': loser_games_played + 1}
41
 
42
+ return ratings_dict