php - ELO评级算法的实现

标签 php algorithm rating

大家好,我想实现 elo 算法来对我的局域网中的玩家进行评分。我的 php 脚本包含以下内容。

<?php
class Rating
{
    const KFACTOR = 16;
    protected $_ratingA;
    protected $_ratingB;
    protected $_scoreA;
    protected $_scoreB;
    protected $_expectedA;
    protected $_expectedB;
    protected $_newRatingA;
    protected $_newRatingB;
    public function  __construct($ratingA,$ratingB,$scoreA,$scoreB)
    {
        $this->_ratingA = $ratingA;
        $this->_ratingB = $ratingB;
        $this->_scoreA = $scoreA;
        $this->_scoreB = $scoreB;
        $expectedScores = $this -> _getExpectedScores($this -> _ratingA,$this -> _ratingB);
        $this->_expectedA = $expectedScores['a'];
        $this->_expectedB = $expectedScores['b'];
        $newRatings = $this ->_getNewRatings($this -> _ratingA, $this -> _ratingB, $this -> _expectedA, $this -> _expectedB, $this -> _scoreA, $this -> _scoreB);
        $this->_newRatingA = $newRatings['a'];
        $this->_newRatingB = $newRatings['b'];
    }
    public function setNewSettings($ratingA,$ratingB,$scoreA,$scoreB)
    {
        $this -> _ratingA = $ratingA;
        $this -> _ratingB = $ratingB;
        $this -> _scoreA = $scoreA;
        $this -> _scoreB = $scoreB;
        $expectedScores = $this -> _getExpectedScores($this -> _ratingA,$this -> _ratingB);
        $this -> _expectedA = $expectedScores['a'];
        $this -> _expectedB = $expectedScores['b'];
        $newRatings = $this ->_getNewRatings($this -> _ratingA, $this -> _ratingB, $this -> _expectedA, $this -> _expectedB, $this -> _scoreA, $this -> _scoreB);
        $this -> _newRatingA = $newRatings['a'];
        $this -> _newRatingB = $newRatings['b'];
    }
    public function getNewRatings()
    {
        return array (
            'a' => $this -> _newRatingA,
            'b' => $this -> _newRatingB
        );
    }
    protected function _getExpectedScores($ratingA,$ratingB)
    {
        $expectedScoreA = 1 / ( 1 + ( pow( 10 , ( $ratingB - $ratingA ) / 400 ) ) );
        $expectedScoreB = 1 / ( 1 + ( pow( 10 , ( $ratingA - $ratingB ) / 400 ) ) );
        return array (
            'a' => $expectedScoreA,
            'b' => $expectedScoreB
        );
    }
    protected function _getNewRatings($ratingA,$ratingB,$expectedA,$expectedB,$scoreA,$scoreB)
    {
        $newRatingA = $ratingA + ( self::KFACTOR * ( $scoreA - $expectedA ) );
        $newRatingB = $ratingB + ( self::KFACTOR * ( $scoreB - $expectedB ) );
        return array (
            'a' => $newRatingA,
            'b' => $newRatingB
        );
    }
}
//first player won
$first_player_rating=200;
$second_player_rating=200;
$n=new Rating($first_pic_rating,$second_pic_rating,1400,1400);
$a=$n->getNewRatings();
var_dump($a);
?>

我将first_player_ rating和second_player_ rating都设置为200,这是错误的,但问题是,如果第一个玩家刚刚赢得了比赛,first_player_ rating和second_player_ rating的值应该是多少......

最佳答案

方法是错误的......

<?php
class elo
{
    private $Ra,$Rb,$Pa,$Pb;
    public $won,$NRa,$NRb;
    function __construct($Ra,$Rb,$won)//$Ra and $Rb are rating given...
    {
        $k=24;
        $this->Ra=$Ra;
        $this->Rb=$Rb;
        $Pa=1/(1+10^(($Rb-$Ra)/400));
        $Pb=1/(1+10^(($Ra-$Rb)/400));
        $this->won=$won;
        if($won=="first")
        {
            $this->NRa=$this->Ra+($k*$Pa);
            $this->NRb=$this->Rb-($k*$Pb);
        }else
        {
            $this->NRa=$this->Ra-($k*$Pa);
            $this->NRb=$this->Rb+($k*$Pb);
        }
    }
    function getNewRatings()
    {
        $result=array($this->NRa,$this->NRb);
        return $result;
    }
}

?>
<?php
require_once('elo.php');
$n=new elo(1400,1400,"first");
$a=$n->getNewRatings();
var_dump($a);
?>

现在可以使用给定的类...

关于php - ELO评级算法的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18833523/

相关文章:

php - 如何考虑本地化对数组进行排序?

algorithm - 所有 i 的 2^Pi mod 1000000007 的总和,其中 Pi 是集合 X 的第 i 个子集中的数字总和

php - 贝叶斯评级

php - 通过两次 GET 进行星级评定

c++ - 添加两个表示为字符串的大数字的段错误

angularjs - Angular 引导评级

javascript - 如何将 simple_html_dom 与 phantomjs 一起使用

php - MySQL - 从 PHP 插入日语 - 编码问题

php - BuddyPress 成员(member)资料的分页

php - 使用 unix 权限等属性组合传递变量的最佳方式