Skip to content

Instantly share code, notes, and snippets.

@USM-F
Last active November 23, 2021 10:09
Show Gist options
  • Save USM-F/88725a635d0a68c58aff408b3d0851ad to your computer and use it in GitHub Desktop.
Save USM-F/88725a635d0a68c58aff408b3d0851ad to your computer and use it in GitHub Desktop.
Rating Elo for Rocky franchise
SCORES = {'win': 1, 'lose': 0, 'tie': 0.5}
class Player:
INITIAL_RATING = 1000
def __init__(self, name, rating=INITIAL_RATING, total_matches=0):
self.name = name
self.rating = rating
self.total_matches = total_matches
def __str__(self):
return 'Name: %s; rating Elo: %s; total_matches: %d' %(self.name, self.rating, self.total_matches)
def update_rating(self, rating):
self.rating = rating
self.total_matches += 1
class Elo:
K_DEFAULT = 32
RATING_POINTS = 400
def __init__(self, K=K_DEFAULT, rating_points=RATING_POINTS):
self.K = K
self.rating_points = rating_points
def q(self, player):
return 10**(player.rating/self.rating_points)
def expected_rating(self, player_a, player_b):
q_a, q_b = self.q(player_a), self.q(player_b)
return q_a/(q_a+q_b), q_b/(q_a+q_b)
def updated_rating(self, player, score, expected_rating):
return player.rating + self.K * (score - expected_rating)
class Match:
@staticmethod
def match(players, result, elo):
player_a, player_b = players
result_a, result_b = result
exp_a, exp_b = elo.expected_rating(player_a, player_b)
player_a.update_rating(elo.updated_rating(player_a, result_a, exp_a))
player_b.update_rating(elo.updated_rating(player_b, result_b, exp_b))
# Create rating of Rocky franchise
apollo = Player('Apollo Creed')
rocky = Player('Robert "Rocky" Balboa')
clubber = Player('James "Clubber" Lang')
ivan = Player('Ivan Drago')
tommy = Player('Tommy "The Machine" Gunn')
mason = Player('Mason "The Line" Dixon')
boxers = [apollo, rocky, clubber, ivan, tommy, mason]
elo = Elo()
# Match 1, Rocky vs Apollo, Apollo won
Match.match([rocky, apollo], [0, 1], elo)
# Match 2, Rocky vs Apollo, Rocky won
Match.match([rocky, apollo], [1, 0], elo)
# Match 3, Rocky vs Clubber, Clubber won
Match.match([rocky, clubber], [0, 1], elo)
# Match 4, Rocky vs Clubber, Rocky won
Match.match([rocky, clubber], [1, 0], elo)
# Match 5, Apollo vs Ivan, Ivan won
Match.match([apollo, ivan], [0, 1], elo)
# Match 6, Rocky vs Ivan, Rocky won
Match.match([rocky, ivan], [1, 0], elo)
# Match 7, Rocky vs Gunn, Rocky won
Match.match([rocky, tommy], [1, 0], elo)
# Match 8, Rocky vs Mason, Mason won
Match.match([rocky, mason], [0, 1], elo)
for boxer in sorted(boxers, key=lambda x: x.rating, reverse=True):
print(boxer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment