Commits

David Jones committed 38084fa

added stuff to test rating things.

Comments (0)

Files changed (3)

     def create_fleets(player, orders):
         result = []
         for line in orders.split("\n"):
+            # print "looking at line: <%s>" % line
             if line.startswith("go"):
                 break
+            if not line: continue
             src, dst, num = [int(x) for x in line.split()]
             if src == dst:
                 return ([], "Invalid Move: Sent from %d to itself" % src)

stuff/analyze_pgn.py

+
+import trueskill
+import os
+import sys
+
+
+
+def read_pgn(filename):
+    tmp = {}
+    data = open(filename)
+    for line in data:
+        line = line.strip()
+        if line.startswith("[") and line.endswith("]"):
+            key, value = line[1:-1].split(" ", 1)
+            tmp[key] = value[1:-1]
+        elif not line:
+            yield tmp
+            tmp = {}
+    yield tmp
+
+def make_game_data(pgn_data):
+    try:
+        white = pgn_data["White"].split(",")[1]
+        black = pgn_data["Black"].split(",")[1]
+        if pgn_data["Result"] == "1-0":
+            return white, black, False
+        elif pgn_data["Result"] == "0-1":
+            return black, white, False
+        elif pgn_data["Result"] == "1/2-1/2":
+            return white, black, True
+        else:
+            raise Exception("invalid result: %s" % pgn_data)
+    except KeyError:
+        print pgn_data
+        return None
+    
+
+def process_game_data(all_game_data):    
+    ratings = {}
+    mu = 25.0
+    sigma = 25.0 / 3
+    beta = 25.0 / 6
+    epsilon = 0.74
+    count = 0
+    for stuff in all_game_data:
+        if stuff is None: continue
+        p1, p2, is_draw = stuff
+        count += 1
+        p1r = ratings.get(p1, (mu, sigma))
+        p2r = ratings.get(p2, (mu, sigma))
+        p1new, p2new = trueskill.update(p1r, p2r, is_draw, epsilon, beta)
+        ratings[p1] = p1new
+        ratings[p2] = p2new
+        if count % 100000 == 0:
+            print count
+    return ratings
+
+def run_games(filename):
+    result = process_game_data(make_game_data(x) for x in read_pgn(filename))
+    return result
+
+def make_leaderboard(ratings):
+    board = [(mu - 3 * sigma, name, mu, sigma) for
+             name, (mu,sigma) in ratings.iteritems()]
+    board.sort()
+    board.reverse()
+    return board
+
+
+    
+

stuff/trueskill.py

 
     print "expecting: 29.395, 7.171, 20.605, 7.171"
     print update((a,b), (a,b))
-    
+
+"""
+Probability of p1 beating p2 is (p1mu - p2mu) / (sqrt(p1sigma + p2sigma))
+"""
+
+
 
 if __name__ == "__main__":
     import doctest