不同编程语言复现ELO匹配机制与机制原理理解

简介: 不同编程语言复现ELO匹配机制与机制原理理解

本章概述

从数学角度分析 分别用c java python演示算法机制

数学理论

预期胜率计算公式

积分算法

  • elo使玩家尽量其股相当 双方实力进来保证持平
  • elo算法基于预先假设:
  • 一名选手的当前实力受各种因素的影响会在一定范围内波动,某个时刻的用来描述其实力的函数应当符合正态分布


  • 两名选手进行对战时的预期胜率


分数迭代公式

  • 以上积分公式只能看出来一个预期范围 如果想确定一名选手的准确"elo表现分"还需要总结出另外一个公式
参数 含义
Rn 玩家比赛结束后的新的排位分值
Ro 比赛前玩家的排位分
K 一个加成系数,由玩家当前分值水平决定(分值越高K越小)
W 玩家实际对局结果得分(赢=1,输=0)
P(D) 预期胜率
Ra A选手当前分数
Rb B选手当前分数
Rn = Ro +K* (W - P(D))
  • 预期A选手的胜负值
Ea = 1/(1+10^ [(Rb-Ra)/400])
  • 预期B选手的胜负值
Eb = 1/(1+10^ [(Ra-Rb)/400])

c

#include <bits/stdc++.h>
using namespace std;
void updateRatingUsingELoRating(float rating1, float rating2, int ratingConstant, bool player1SuccessProb) {
   float P1, P2;
   if(rating1 > rating2){
      P1 = (1.0 / (1.0 + pow(10.0, ((rating1 - rating2) / 400.0)) ) );
      P2 = 1 - P1;
   }
   else {
      P2 = (1.0 / (1.0 + pow(10.0, ((rating2 - rating1) / 400.0)) ) );
      P1 = 1 - P2;
   }
   if (player1SuccessProb == 1) {
      rating1 = rating1 + ratingConstant * (1 - P1);
      rating2 = rating2 + ratingConstant * (0 - P2);
   }
   else {
      rating1 = rating1 + ratingConstant * (0 - P1);
      rating1 = rating1 + ratingConstant * (1 - P2);
   }
   cout<<"Ratings After the game\n";
   cout<<"Player 1 : "<<rating1<<"\t Player 2 : "<<rating2;
}
int main()
{
   float rating1 = 782, rating2 = 1432;
   int ratingConstant = 100;
   bool player1SuccessProb = 1;
   cout<<"Ratings before the game: \n";
   cout<<"Player 1 : "<<rating1<<"\t Player 2 : "<<rating2<<endl;
   if(player1SuccessProb)
      cout<<"Player 1 wins the game!\n";
   else
      cout<<"Player 2 wins the game!\n";
   updateRatingUsingELoRating(rating1, rating2, ratingConstant, player1SuccessProb);
   return 0;
}

c++

使用Elo评级差异计算每个玩家获胜的概率。玩家A战胜玩家B的概率可以用公式来计算:P(A) = 1 / (1 + pow(10, (ratingB - ratingA) / 400))。同样,玩家B战胜玩家A的概率为:P(B) = 1 / (1 + pow(10, (ratingA - ratingB) / 400))。(来源:GeeksforGeeks)


确定游戏的实际结果。如果玩家A获胜,则实际得分为1。如果玩家B获胜,则实际得分为0。


根据实际结果和预期结果更新两名球员的评分。玩家A的新评分可以使用以下公式计算:newRatingA = ratingA + K * (actualScoreA - expectedScoreA),其中K是代表评分变化幅度的常数。玩家B的新评级也可以类似地计算出来。(来源:GeeksforGeeks)

Calculate the probabilities of winning for each player using the Elo rating difference. The probability of player A winning against player B can be calculated using the formula: P(A) = 1 / (1 + pow(10, (ratingB - ratingA) / 400)). Similarly, the probability of player B winning against player A is: P(B) = 1 / (1 + pow(10, (ratingA - ratingB) / 400)). (Source: GeeksforGeeks)


Determine the actual outcome of the game. If player A wins, the actual score is 1. If player B wins, the actual score is 0.


Update the ratings of both players based on the actual outcome and the expected outcome. The new rating for player A can be calculated using the formula: newRatingA = ratingA + K * (actualScoreA - expectedScoreA), where K is a constant representing the magnitude of rating changes. The new rating for player B can be calculated similarly. (Source: GeeksforGeeks)

#include <iostream>
#include <cmath>
// Function to calculate the probability of winning for player A
float Probability(int ratingA, int ratingB) {
    return 1.0 / (1.0 + std::pow(10, (ratingB - ratingA) / 400.0));
}
// Function to update Elo ratings based on the outcome of the game
void EloRating(int& ratingA, int& ratingB, int K, int actualScoreA) {
    float expectedScoreA = Probability(ratingA, ratingB);
    float expectedScoreB = 1 - expectedScoreA;
    ratingA += K * (actualScoreA - expectedScoreA);
    ratingB += K * ((1 - actualScoreA) - expectedScoreB);
}
int main() {
    int ratingA = 1200;
    int ratingB = 1000;
    int K = 30;
    int actualScoreA = 1;
    EloRating(ratingA, ratingB, K, actualScoreA);
    std::cout << "Updated Ratings:\n";
    std::cout << "Player A: " << ratingA << "\n";
    std::cout << "Player B: " << ratingB << "\n";
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the Probability
float Probability(int rating1, int rating2)
{
    return 1.0 * 1.0 / (1 + 1.0 * pow(10, 1.0 * (rating1 - rating2) / 400));
}
// Function to calculate Elo rating
// K is a constant.
// d determines whether Player A wins or Player B.
void EloRating(float Ra, float Rb, int K, bool d)
{
    // To calculate the Winning Probability of Player B
    float Pb = Probability(Ra, Rb);
    // To calculate the Winning Probability of Player A
    float Pa = Probability(Rb, Ra);
    // Case -1 When Player A wins
    // Updating the Elo Ratings
    if (d == 1) {
        Ra = Ra + K * (1 - Pa);
        Rb = Rb + K * (0 - Pb);
    }
    // Case -2 When Player B wins
    // Updating the Elo Ratings
    else {
        Ra = Ra + K * (0 - Pa);
        Rb = Rb + K * (1 - Pb);
    }
    cout << "Updated Ratings:-\n";
    cout << "Ra = " << Ra << " Rb = " << Rb;
}
// Driver code
int main()
{
    // Ra and Rb are current ELO ratings
    float Ra = 1200, Rb = 1000;
    int K = 30;
    bool d = 1;
    // Function call
    EloRating(Ra, Rb, K, d);
    return 0;
}

java

To implement the ELO algorithm for game matchmaking in Java

Define the ELO rating system: The ELO rating system is a widely used algorithm to rank players in competitive games. It assigns players a skill level based on their performance in matches. The ELO rating is updated after each match based on the outcome and the expected probability of winning.
Calculate the expected probability of winning: Before a match, you need to calculate the expected probability of winning for each player. This can be done using the following formula:

P1 = 1 / (1 + Math.pow(10, (rating2 - rating1) / 400));

P2 = 1 / (1 + Math.pow(10, (rating1 - rating2) / 400));

Where rating1 and rating2 are the ELO ratings of the two players.

Update the ratings after the match: After the match is finished, you need to update the ELO ratings of the players based on the actual outcome and the expected probability of winning. The formula for updating the ratings is:

rating1 = rating1 + K * (actual - expected);

rating2 = rating2 + K * (actual - expected);


Where actual is the actual outcome of the match (0 for a loss, 1 for a win), expected is the expected probability of winning calculated in step 2, and K is a constant that determines the magnitude of rating changes.

public class EloMatchmaking {
    public static void main(String[] args) {
        int rating1 = 1200;
        int rating2 = 1000;
        int K = 30;
        double P1 = 1 / (1 + Math.pow(10, (rating2 - rating1) / 400));
        double P2 = 1 / (1 + Math.pow(10, (rating1 - rating2) / 400));
        // Suppose player 1 wins
        double actual = 1;
        double expected = P1;
        rating1 = (int) (rating1 + K * (actual - expected));
        rating2 = (int) (rating2 + K * (actual - expected));
        System.out.println("Updated ratings:");
        System.out.println("Player 1: " + rating1);
        System.out.println("Player 2: " + rating2);
    }
}

c#

public class EloRating
{
    private const int K = 20;
    public static void UpdateEloRating(ref int rating1, ref int rating2, bool isFirstPlayerWin)
    {
        int delta = CalculateRatingChange(rating1, rating2, isFirstPlayerWin ? 1 : 0);
        rating1 += delta;
        rating2 -= delta;
    }
    private static int CalculateRatingChange(int rating1, int rating2, int actualScore)
    {
        double expectedScore = 1 / (1 + Math.Pow(10, (rating2 - rating1) / 400.0));
        return (int) (K * (actualScore - expectedScore));
    }
}


目录
相关文章
|
机器学习/深度学习 人工智能 自然语言处理
微软Copilot官网入口- Copilot中文版国内使用入口
微软Copilot应运而生,它不仅仅是一款软件,更像是一位人工智能副驾驶,旨在通过强大的AI技术,解放你的双手
6753 6
|
12月前
|
存储 安全 Ubuntu
从Linux到Windows:阿里云服务器系统镜像适配场景与选择参考
阿里云为用户提供了丰富多样的服务器操作系统选择,以满足不同场景下的应用需求。目前,云服务器的操作系统镜像主要分为公共镜像、自定义镜像、共享镜像、镜像市场和社区镜像五大类。以下是对这些镜像类型的详细介绍及选择云服务器系统时需要考虑的因素,以供参考。
|
XML 存储 机器人
06 ROS配置launch文件
本文介绍了如何在ROS(机器人操作系统)中配置launch文件,包括设置节点、参数、局部变量、重映射以及嵌套launch文件的方法,并通过XML格式实现了多节点的一键配置与启动。
496 0
|
存储 人工智能 运维
AI-Native的路要怎么走?一群技术“老炮儿”指明了方向
上世纪70年代,沃兹尼亚克、乔布斯等人成立Homebrew Computer Club,推动个人电脑普及。如今,创原会承袭这一精神,由CNCF执行董事Priyanka Sharma等构建,聚焦云原生和AI技术,汇聚各行业技术骨干,探索前沿科技。2024年创原会年度峰会达成“全面拥抱AI-Native”共识,解决算力与存储瓶颈,推动AI原生应用开发,助力千行万业智能化转型,成为行业创新风向标。
397 8
|
前端开发 Java API
Swagger接口文档 —— 手把手教学,全方位超详细小白能看懂,百分百能用Java版
本文提供了一份详细的Swagger接口文档生成工具的使用教程,包括了导入依赖、配置类设置、资源映射、拦截器配置、Swagger注解使用、生成接口文档、在线调试页面访问以及如何设置全局参数(如token),旨在帮助Java开发者快速上手Swagger。
11640 0
Swagger接口文档 —— 手把手教学,全方位超详细小白能看懂,百分百能用Java版
|
Shell 网络安全 开发工具
Git,GitHub,Gitee&IDEA集成Git
Git提交项目到GitHub简洁版、版本控制、安装、常用命令、分支、团队协作机制、Github、Gitee远程仓库、IDEA集成Git、IDEA集成Github、IDEA集成Gitee
Git,GitHub,Gitee&IDEA集成Git
|
存储 开发框架 自然语言处理
【Uniapp 专栏】Uniapp 的多语言支持功能详解
【5月更文挑战第14天】Uniapp是一款跨平台开发框架,提供强大多语言支持,助力开发者轻松构建支持多种语言的应用,提升用户体验和市场拓展。其特点包括灵活的语言管理、跨平台一致性。通过语言文件存储内容,切换机制让用户自由切换。注重翻译准确性和文化适应性,集成到页面和组件中,同时关注性能优化。面对翻译不一致和更新及时性等问题,Uniapp将持续发展和完善,为全球化应用开发提供强有力支持。
1007 3
【Uniapp 专栏】Uniapp 的多语言支持功能详解
|
存储 中间件 API
Nest.js 实战 (六):使用 Session 在不同请求间存储信息
这篇文章介绍了在Nest.js中如何使用Session来记录客户状态。文章首先解释了Session的概念,然后详细说明了如何在Nest.js中安装和使用express-session,包括全局配置、参数说明、使用方式和常用方法。
514 0
Nest.js 实战 (六):使用 Session 在不同请求间存储信息
|
SQL 缓存 前端开发
PHP性能优化实战:从代码到服务器的全方位攻略
【7月更文挑战第30天】在Web开发的世界里,PHP以其灵活性和易用性赢得了广泛的赞誉。然而,随着应用规模的扩大,性能问题逐渐浮现。本文将深入探讨PHP的性能优化策略,不仅涉及代码层面的精细调整,还包括服务器配置的优化技巧。我们将一起探索如何通过减少资源消耗、优化数据库交互以及利用缓存技术来提升PHP应用的性能表现。无论你是PHP新手还是资深开发者,这篇文章都将为你提供实用的优化建议,帮助你打造更快、更稳定的PHP应用。
339 4
|
Rust JavaScript 前端开发
我们都是调包侠
这篇内容讨论了从应用层到硬件层的编程工作,指出每个层次的程序员都是“调包侠”,即通过调用不同层次的接口来完成任务。应用层开发者使用高级语言控制设备,无需深入硬件细节。低级编程语言用于高性能需求,仍依赖操作系统。系统编程涉及硬件接口,需要了解硬件特性。硬件层面则涉及逻辑门电路设计与制造,需要考虑多种因素如性能、功耗和兼容性。文章强调各层次间的相互依赖,并提倡明确软件的局限性,选择细分方向,避免盲目跟风学习。
397 5