Exemple #1
0
 /**
  * Sends the ith message to the marginal and returns the log-normalization constant
  */
 public function sendMessageIndex($messageIndex)
 {
     Guard::argumentIsValidIndex($messageIndex, count($this->_messages), "messageIndex");
     $message =& $this->_messages[$messageIndex];
     $variable =& $this->_messageToVariableBinding->getValue($message);
     return $this->sendMessageVariable($message, $variable);
 }
 public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks)
 {
     Guard::argumentNotNull($gameInfo, "gameInfo");
     $this->validateTeamCountAndPlayersCountPerTeam($teams);
     RankSorter::sort($teams, $teamRanks);
     $factorGraph = new TrueSkillFactorGraph($gameInfo, $teams, $teamRanks);
     $factorGraph->buildGraph();
     $factorGraph->runSchedule();
     $probabilityOfOutcome = $factorGraph->getProbabilityOfRanking();
     return $factorGraph->getUpdatedRatings();
 }
 /**
  * {@inheritdoc }
  */
 public function calculateMatchQuality(GameInfo $gameInfo, array $teams)
 {
     Guard::argumentNotNull($gameInfo, "gameInfo");
     $this->validateTeamCountAndPlayersCountPerTeam($teams);
     $team1 = $teams[0];
     $team2 = $teams[1];
     $team1Ratings = $team1->getAllRatings();
     $team2Ratings = $team2->getAllRatings();
     $player1Rating = $team1Ratings[0];
     $player2Rating = $team2Ratings[0];
     // We just use equation 4.1 found on page 8 of the TrueSkill 2006 paper:
     $betaSquared = square($gameInfo->getBeta());
     $player1SigmaSquared = square($player1Rating->getStandardDeviation());
     $player2SigmaSquared = square($player2Rating->getStandardDeviation());
     // This is the square root part of the equation:
     $sqrtPart = sqrt(2 * $betaSquared / (2 * $betaSquared + $player1SigmaSquared + $player2SigmaSquared));
     // This is the exponent part of the equation:
     $expPart = exp(-1 * square($player1Rating->getMean() - $player2Rating->getMean()) / (2 * (2 * $betaSquared + $player1SigmaSquared + $player2SigmaSquared)));
     return $sqrtPart * $expPart;
 }
 /**
  * {@inheritdoc }
  */
 public function calculateMatchQuality(GameInfo &$gameInfo, array &$teams)
 {
     Guard::argumentNotNull($gameInfo, "gameInfo");
     $this->validateTeamCountAndPlayersCountPerTeam($teams);
     // We've verified that there's just two teams
     $team1Ratings = $teams[0]->getAllRatings();
     $team1Count = count($team1Ratings);
     $team2Ratings = $teams[1]->getAllRatings();
     $team2Count = count($team2Ratings);
     $totalPlayers = $team1Count + $team2Count;
     $betaSquared = square($gameInfo->getBeta());
     $meanGetter = function ($currentRating) {
         return $currentRating->getMean();
     };
     $varianceGetter = function ($currentRating) {
         return square($currentRating->getStandardDeviation());
     };
     $team1MeanSum = sum($team1Ratings, $meanGetter);
     $team1StdDevSquared = sum($team1Ratings, $varianceGetter);
     $team2MeanSum = sum($team2Ratings, $meanGetter);
     $team2SigmaSquared = sum($team2Ratings, $varianceGetter);
     // This comes from equation 4.1 in the TrueSkill paper on page 8
     // The equation was broken up into the part under the square root sign and
     // the exponential part to make the code easier to read.
     $sqrtPart = sqrt($totalPlayers * $betaSquared / ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared));
     $expPart = exp(-1 * square($team1MeanSum - $team2MeanSum) / (2 * ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared)));
     return $expPart * $sqrtPart;
 }
 public function updateMessageIndex($messageIndex)
 {
     $allMessages = $this->getMessages();
     $allVariables = $this->getVariables();
     Guard::argumentIsValidIndex($messageIndex, count($allMessages), "messageIndex");
     $updatedMessages = array();
     $updatedVariables = array();
     $indicesToUse = $this->_variableIndexOrdersForWeights[$messageIndex];
     // The tricky part here is that we have to put the messages and variables in the same
     // order as the weights. Thankfully, the weights and messages share the same index numbers,
     // so we just need to make sure they're consistent
     $allMessagesCount = count($allMessages);
     for ($i = 0; $i < $allMessagesCount; $i++) {
         $updatedMessages[] = $allMessages[$indicesToUse[$i]];
         $updatedVariables[] = $allVariables[$indicesToUse[$i]];
     }
     return $this->updateHelper($this->_weights[$messageIndex], $this->_weightsSquared[$messageIndex], $updatedMessages, $updatedVariables);
 }