forked from dmj111/trueskill
-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_trueskill.py
120 lines (86 loc) · 3.07 KB
/
run_trueskill.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import trueskill
class Player(object):
pass
def run(games, ratings, use_default_rating=False):
"""Update ratings based on games, and return new ratings
GAMES is a list of dict objects, containing the following keys:
players: a list of player identifiers
rank: a list of places in the game
RATINGS is a dictionary of player identifiers matched with their
(mu, sigma) rating. RATINGS is modified in this function.
If USE_DEFAULT_RATING is False, an error is thrown if a player
in games is not in RATINGS. Otherwise, the default mu and sigma
are used.
"""
trueskill.SetParameters(gamma = 0.0)
for game in games:
players = []
for k in xrange(len(game['players'])):
p = Player()
p.id = game['players'][k]
p.rank = game['rank'][k]
if ratings.has_key(p.id):
p.skill = ratings[p.id]
elif use_default_rating:
p.skill = (trueskill.INITIAL_MU, trueskill.INITIAL_SIGMA)
else:
raise Exception("Rating missing for: %s" % p.id)
players.append(p)
trueskill.AdjustPlayers(players)
for p in players:
ratings[p.id] = p.skill
if __name__ == "__main__":
# examples
def print_ratings(ratings):
out = []
for key in ratings.keys():
mu, sigma = ratings[key]
out.append((mu - 3 * sigma, key, mu, sigma))
out.sort()
out.reverse()
for r, key, mu, sigma in out:
print key, mu, sigma
def example_1():
games = [ { 'game_id': 0,
'map': 'random4',
'players': ['a', 'b', 'c', 'd'],
'rank': [0,1,2,3],
'score': [103, 153, 91, 167] } ]
ratings = {}
run(games, ratings, True)
print_ratings(ratings)
example_1()
def example_1a():
games = [ { 'game_id': 0,
'map': 'random4',
'players': ['a', 'b'],
'rank': [0,1]} ]
ratings = {}
run(games, ratings, True)
print_ratings(ratings)
example_1a()
print
def example_1b():
games = [ { 'game_id': 0,
'map': 'random4',
'players': 'a b c d e f g h'.split(),
'rank': [0,1,2,3,4,5,6,7]} ]
ratings = {}
run(games, ratings, True)
out = [(ratings[k], k) for k in ratings.keys()]
out.sort()
out.reverse()
print_ratings(ratings)
example_1b()
def example_2():
games = [ { 'players': ['a', 'b', 'c', 'd'],
'rank': [0,1,2,3]},
{ 'players': ['a', 'b', 'c', 'd'],
'rank': [0,1,2,3]},
{ 'players': ['a', 'b', 'c', 'd'],
'rank': [0,1,2,3]}]
ratings = {'c': (25, 8), 'b': (23, 5), 'd': (28,5),
'a': (24, 6)}
run(games, ratings, False)
print_ratings(ratings)
example_2()