elo - 等级评分算法advanced
优质
小牛编辑
130浏览
2023-12-01
使用 Elo 评分系统 计算两个或两个以上对手之间的新评分。它需要一个预定义数组,并返回一个包含事后评级的数组。 数组应该从最高评分到最低评分排序(赢家 -> 失败者)。
使用指数 **
操作符和数学运算符来计算预期分数(获胜几率),并计算每个对手的新评级。对每个对手计算新的评分。 循环评分,使用每个排列组合,以成对方式计算每个玩家的 Elo 评分。 忽略第二个参数,使用默认的 k-factor 为 32。
const elo = ([...ratings], kFactor = 32, selfRating) => { const [a, b] = ratings; const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400)); const newRating = (rating, i) => (selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a)); if (ratings.length === 2) { return [newRating(a, 1), newRating(b, 0)]; } else { for (let i = 0; i < ratings.length; i++) { let j = i; while (j < ratings.length - 1) { [ratings[i], ratings[j + 1]] = elo([ratings[i], ratings[j + 1]], kFactor); j++; } } } return ratings; };
// Standard 1v1s elo([1200, 1200]); // [1216, 1184] elo([1200, 1200], 64); // [1232, 1168] // 4 player FFA, all same rank elo([1200, 1200, 1200, 1200]).map(Math.round); // [1246, 1215, 1185, 1154] /* For teams, each rating can adjusted based on own team's average rating vs. average rating of opposing team, with the score being added to their own individual rating by supplying it as the third argument. */