/
Glicko2.cs
252 lines (197 loc) · 7.67 KB
/
Glicko2.cs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.Linq;
namespace ImperaPlus.Domain.Services.Scoring
{
internal class Config
{
public const int DefaultRating = 1500;
public const double DefaultRD = 350.0;
public const double DefaultVol = 0.06;
public const double Scale = 173.7177928;
public const double Tau = 0.5;
}
public class ScoreTeam
{
public ScoreTeam()
{
Players = new List<ScorePlayer>();
}
public List<ScorePlayer> Players { get; set; }
}
public class ScorePlayer
{
public static ScorePlayer DefaultPlayer(string id)
{
return new ScorePlayer(id);
}
public ScorePlayer()
: this(null, Config.DefaultRating, Config.DefaultRD, Config.DefaultVol)
{
}
public ScorePlayer(string id)
: this(id, Config.DefaultRating, Config.DefaultRD, Config.DefaultVol)
{
}
public ScorePlayer(string id, int rating, double rd, double vol)
{
Id = id;
Rating = rating;
Rd = rd;
Vol = vol;
}
public string Id { get; private set; }
public double Rating { get; set; }
public double Rd { get; set; }
public double Vol { get; set; }
internal double RdScaled
{
get => Rd / Config.Scale;
set => Rd = value * Config.Scale;
}
internal double RatingScaled
{
get => (Rating - Config.DefaultRating) / Config.Scale;
set => Rating = value * Config.Scale + Config.DefaultRating;
}
public static ScorePlayer operator +(ScorePlayer lhs, ScorePlayer rhs)
{
return new ScorePlayer { Rd = lhs.Rd + rhs.Rd, Vol = lhs.Vol + rhs.Vol, Rating = lhs.Rating + rhs.Rating };
}
public static ScorePlayer operator /(ScorePlayer lhs, double divisor)
{
return new ScorePlayer { Rd = lhs.Rd / divisor, Vol = lhs.Vol / divisor, Rating = lhs.Rating / divisor };
}
}
public class Glicko2
{
private double G(double rd)
{
return 1.0 / Math.Sqrt(1 + 3.0 * rd * rd / (Math.PI * Math.PI));
}
private double E(double rating1, double rating2, double rd2)
{
return 1.0 / (1.0 + Math.Exp(-G(rd2) * (rating1 - rating2)));
}
private double F(double delta, double a, double rd, double v, double x)
{
var ex = Math.Pow(Math.E, x);
var t1 = ex * (delta * delta - rd * rd - v - ex) / Math.Pow(2 * (rd * rd + v + ex), 2);
var t2 = (x - a) / (Config.Tau * Config.Tau);
return t1 - t2;
}
/// <summary>
/// Calculcate score, modify players inline
/// </summary>
/// <returns>
/// List of updated teams. First entry is winning team
/// </returns>
public IEnumerable<ScoreTeam> Calculate(ScoreTeam winningTeam, params ScoreTeam[] others)
{
var winningTeamPlayer = winningTeam.Players.Aggregate((a, b) => a + b) / (double)winningTeam.Players.Count;
var otherPlayers = others.Select(t => t.Players.Aggregate((a, b) => a + b) / (double)t.Players.Count);
var updatedWinningTeam = new ScoreTeam();
foreach (var winningPlayer in winningTeam.Players)
{
var updatedWinningPlayer = Calculate(winningPlayer, otherPlayers.Select(p => Tuple.Create(p, 1.0)),
1.0 / winningTeam.Players.Count);
updatedWinningTeam.Players.Add(updatedWinningPlayer);
}
var updatedLosingTeams = new List<ScoreTeam>();
foreach (var otherTeam in others)
{
var updatedLosingTeam = new ScoreTeam();
foreach (var player in otherTeam.Players)
{
var otherLosingTeamPlayers = others.Where(x => x != otherTeam)
.Select(t => t.Players.Aggregate((a, b) => a + b) / (double)t.Players.Count);
var opponents =
new[] { Tuple.Create(winningTeamPlayer, 0.0) }.Concat(
otherLosingTeamPlayers.Select(x => Tuple.Create(x, 0.5)));
var updatedOtherPlayer = Calculate(player, opponents, 1.0 / otherTeam.Players.Count);
updatedLosingTeam.Players.Add(updatedOtherPlayer);
}
updatedLosingTeams.Add(updatedLosingTeam);
}
return new[] { updatedWinningTeam }.Concat(updatedLosingTeams);
}
public ScorePlayer Calculate(ScorePlayer player, IEnumerable<Tuple<ScorePlayer, double>> opponents,
double factor = 1.0)
{
// Step 3
var v = 0.0;
foreach (var opponent in opponents)
{
var e = E(player.RatingScaled, opponent.Item1.RatingScaled, opponent.Item1.RdScaled);
v += Math.Pow(G(opponent.Item1.RdScaled), 2) * e * (1 - e);
}
v = Math.Pow(v, -1.0);
// Step 4
var delta = 0.0;
foreach (var opponent in opponents)
{
delta += G(opponent.Item1.RdScaled) * (opponent.Item2 - E(player.RatingScaled,
opponent.Item1.RatingScaled, opponent.Item1.RdScaled));
}
delta *= v;
// Step 5
const double epsilon = 0.000001;
double A, a, B;
A = a = Math.Log(Math.Pow(player.Vol, 2));
var delta2 = delta * delta;
var rd2 = player.RdScaled * player.RdScaled;
if (delta2 > rd2 + v)
{
B = Math.Log(delta2 - rd2 - v);
}
else
{
var k = 0;
while (F(delta, a, player.RdScaled, v, a - k * Config.Tau) < 0)
{
++k;
}
B = a - k * Config.Tau;
}
// Step 5.3
var fa = F(delta, a, player.RdScaled, v, A);
var fb = F(delta, a, player.RdScaled, v, B);
// Step 5.4
while (Math.Abs(B - A) > epsilon)
{
var C = A + (A - B) * fa / (fb - fa);
var fc = F(delta, a, player.RdScaled, v, C);
if (fc * fb < 0)
{
A = B;
fa = fb;
}
else
{
fa = fa / 2.0;
}
B = C;
fb = fc;
}
// Step 5.5
var vol1 = Math.Pow(Math.E, A / 2.0);
// Step 6
var rdstar = Math.Sqrt(rd2 + vol1 * vol1);
// Step 7
var rdnew = 1.0 / Math.Sqrt(1 / (rdstar * rdstar) + 1 / v);
var ratingnew = 0.0;
foreach (var opponent in opponents)
{
ratingnew += rdnew * rdnew * G(opponent.Item1.RatingScaled) * (opponent.Item2 -
E(player.RatingScaled,
opponent.Item1.RatingScaled,
opponent.Item1.RdScaled));
}
// Step 8
var result = new ScorePlayer(player.Id);
result.RatingScaled = player.RatingScaled + ratingnew * factor;
result.RdScaled = (rdnew - player.RdScaled) * factor + player.RdScaled;
return result;
}
}
}