2013-09-16 39 views
2

嘿傢伙我想實現elo算法來評價我的局域網中的玩家。我的PHP腳本包含以下內容。評級的ELO算法的實現

<?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如果第一個球員剛剛贏得了比賽......

+0

我downvoted,因爲我看不到任何代碼的問題。我沒有看到任何問題,任何錯誤或任何錯誤。我認爲這是不合時宜的。 IMO。當然,我可能不對。更好地描述你的問題,展示解決方案的嘗試。 – BlitZ

+0

正確地看到問題,給予兩個玩家的評分,它沒有做到真的,對於玩家評分都必須有一個價值,我要求初始價值,如果第一個玩家剛剛贏得了比賽 –

回答

1

的做法是錯誤的....

<?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); 
?> 

現在可以使用給定的類...