Skip to content

Commit d59ec8a

Browse files
committed
Fix bug related to self-promotion in rating system.
Relevant blog: https://codeforces.com/blog/entry/44214
1 parent 428dd99 commit d59ec8a

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

lib/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ var CodeforcesRatingCalculator2 = function () {
116116

117117
contestants = contestants.map(function (contestant) {
118118
var midRank = Math.sqrt(contestant.rank * contestant.seed);
119-
contestant.needRating = _this.getRatingToRank(contestants, midRank);
119+
contestant.needRating = _this.getRatingToRank(contestants, contestant, midRank);
120120
contestant.delta = Math.trunc((contestant.needRating - contestant.rating) / 2);
121121
return contestant;
122122
});
@@ -199,13 +199,13 @@ var CodeforcesRatingCalculator2 = function () {
199199
}
200200
}, {
201201
key: 'getRatingToRank',
202-
value: function getRatingToRank(contestants, rank) {
202+
value: function getRatingToRank(contestants, contestant, rank) {
203203
var left = 1;
204204
var right = 8000;
205205

206206
while (right - left > 1) {
207207
var mid = Math.trunc((left + right) / 2);
208-
if (this.getSeed(contestants, mid) < rank) {
208+
if (this.getSeed(contestants, contestant, mid) < rank) {
209209
right = mid;
210210
} else {
211211
left = mid;
@@ -215,13 +215,15 @@ var CodeforcesRatingCalculator2 = function () {
215215
}
216216
}, {
217217
key: 'getSeed',
218-
value: function getSeed(contestants, rating) {
218+
value: function getSeed(contestants, contestant, rating) {
219219
var _this2 = this;
220220

221221
var extraContestant = new Contestant(null, 0, 0, rating);
222222
var result = 1;
223223
contestants.forEach(function (other) {
224-
result += _this2.getEloWinProbability(other, extraContestant);
224+
if (other != contestant) {
225+
result += _this2.getEloWinProbability(other, extraContestant);
226+
}
225227
});
226228
return result;
227229
}

src/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class CodeforcesRatingCalculator2 {
9090

9191
contestants = contestants.map((contestant)=> {
9292
const midRank = Math.sqrt(contestant.rank * contestant.seed);
93-
contestant.needRating = this.getRatingToRank(contestants, midRank);
93+
contestant.needRating = this.getRatingToRank(contestants, contestant, midRank);
9494
contestant.delta = Math.trunc(
9595
(contestant.needRating - contestant.rating) / 2
9696
);
@@ -172,13 +172,13 @@ class CodeforcesRatingCalculator2 {
172172
return this.getEloWinProbabilityRating(a.rating, b.rating);
173173
}
174174

175-
getRatingToRank(contestants, rank) {
175+
getRatingToRank(contestants, contestant, rank) {
176176
let left = 1;
177177
let right = 8000;
178178

179179
while (right - left > 1) {
180180
const mid = Math.trunc((left + right) / 2);
181-
if (this.getSeed(contestants, mid) < rank) {
181+
if (this.getSeed(contestants, contestant, mid) < rank) {
182182
right = mid;
183183
} else {
184184
left = mid;
@@ -187,11 +187,13 @@ class CodeforcesRatingCalculator2 {
187187
return left;
188188
}
189189

190-
getSeed(contestants, rating) {
190+
getSeed(contestants, contestant, rating) {
191191
const extraContestant = new Contestant(null, 0, 0, rating);
192192
let result = 1;
193193
contestants.forEach((other)=>{
194-
result += this.getEloWinProbability(other, extraContestant);
194+
if (other != contestant) {
195+
result += this.getEloWinProbability(other, extraContestant);
196+
}
195197
});
196198
return result;
197199
}

0 commit comments

Comments
 (0)