mirror of
https://github.com/furyfire/trueskill.git
synced 2025-04-18 20:04:28 +00:00
Cleanup in src/, adding namespaces, removing php closing tag and general code cleanup
This commit is contained in:
@ -1,27 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Moserware\Skills\TrueSkill;
|
||||
|
||||
require_once(dirname(__FILE__) . "/../GameInfo.php");
|
||||
require_once(dirname(__FILE__) . "/../Guard.php");
|
||||
require_once(dirname(__FILE__) . "/../PairwiseComparison.php");
|
||||
require_once(dirname(__FILE__) . "/../RankSorter.php");
|
||||
require_once(dirname(__FILE__) . "/../Rating.php");
|
||||
require_once(dirname(__FILE__) . "/../RatingContainer.php");
|
||||
require_once(dirname(__FILE__) . "/../SkillCalculator.php");
|
||||
|
||||
require_once(dirname(__FILE__) . "/../Team.php");
|
||||
|
||||
require_once(dirname(__FILE__) . "/../PlayersRange.php");
|
||||
require_once(dirname(__FILE__) . "/../TeamsRange.php");
|
||||
|
||||
require_once(dirname(__FILE__) . "/../Numerics/BasicMath.php");
|
||||
|
||||
require_once(dirname(__FILE__) . "/DrawMargin.php");
|
||||
require_once(dirname(__FILE__) . "/TruncatedGaussianCorrectionFunctions.php");
|
||||
<?php namespace Moserware\Skills\TrueSkill;
|
||||
|
||||
use Moserware\Skills\GameInfo;
|
||||
use Moserware\Skills\Guard;
|
||||
use Moserware\Skills\Numerics\BasicMatch;
|
||||
use Moserware\Skills\PairwiseComparison;
|
||||
use Moserware\Skills\RankSorter;
|
||||
use Moserware\Skills\Rating;
|
||||
@ -36,7 +17,7 @@ use Moserware\Skills\Team;
|
||||
|
||||
/**
|
||||
* Calculates new ratings for only two teams where each team has 1 or more players.
|
||||
*
|
||||
*
|
||||
* When you only have two teams, the math is still simple: no factor graphs are used yet.
|
||||
*/
|
||||
class TwoTeamTrueSkillCalculator extends SkillCalculator
|
||||
@ -63,16 +44,16 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
||||
$results = new RatingContainer();
|
||||
|
||||
self::updatePlayerRatings($gameInfo,
|
||||
$results,
|
||||
$team1,
|
||||
$team2,
|
||||
$wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::WIN);
|
||||
$results,
|
||||
$team1,
|
||||
$team2,
|
||||
$wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::WIN);
|
||||
|
||||
self::updatePlayerRatings($gameInfo,
|
||||
$results,
|
||||
$team2,
|
||||
$team1,
|
||||
$wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::LOSE);
|
||||
$results,
|
||||
$team2,
|
||||
$team1,
|
||||
$wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::LOSE);
|
||||
|
||||
return $results;
|
||||
}
|
||||
@ -83,41 +64,39 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
||||
Team $otherTeam,
|
||||
$selfToOtherTeamComparison)
|
||||
{
|
||||
$drawMargin = DrawMargin::getDrawMarginFromDrawProbability($gameInfo->getDrawProbability(),
|
||||
$gameInfo->getBeta());
|
||||
$drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
|
||||
$gameInfo->getDrawProbability(),
|
||||
$gameInfo->getBeta()
|
||||
);
|
||||
|
||||
$betaSquared = square($gameInfo->getBeta());
|
||||
$tauSquared = square($gameInfo->getDynamicsFactor());
|
||||
$betaSquared = BasicMatch::square($gameInfo->getBeta());
|
||||
$tauSquared = BasicMatch::square($gameInfo->getDynamicsFactor());
|
||||
|
||||
$totalPlayers = $selfTeam->count() + $otherTeam->count();
|
||||
|
||||
$meanGetter =
|
||||
function($currentRating)
|
||||
{
|
||||
return $currentRating->getMean();
|
||||
};
|
||||
$meanGetter = function ($currentRating) {
|
||||
return $currentRating->getMean();
|
||||
};
|
||||
|
||||
$selfMeanSum = sum($selfTeam->getAllRatings(), $meanGetter);
|
||||
$otherTeamMeanSum = sum($otherTeam->getAllRatings(), $meanGetter);
|
||||
$selfMeanSum = BasicMatch::sum($selfTeam->getAllRatings(), $meanGetter);
|
||||
$otherTeamMeanSum = BasicMatch::sum($otherTeam->getAllRatings(), $meanGetter);
|
||||
|
||||
$varianceGetter =
|
||||
function($currentRating)
|
||||
{
|
||||
return square($currentRating->getStandardDeviation());
|
||||
};
|
||||
$varianceGetter = function ($currentRating) {
|
||||
return BasicMatch::square($currentRating->getStandardDeviation());
|
||||
};
|
||||
|
||||
$c = sqrt(
|
||||
sum($selfTeam->getAllRatings(), $varianceGetter)
|
||||
+
|
||||
sum($otherTeam->getAllRatings(), $varianceGetter)
|
||||
+
|
||||
$totalPlayers*$betaSquared);
|
||||
BasicMatch::sum($selfTeam->getAllRatings(), $varianceGetter)
|
||||
+
|
||||
BasicMatch::sum($otherTeam->getAllRatings(), $varianceGetter)
|
||||
+
|
||||
$totalPlayers * $betaSquared
|
||||
);
|
||||
|
||||
$winningMean = $selfMeanSum;
|
||||
$losingMean = $otherTeamMeanSum;
|
||||
|
||||
switch ($selfToOtherTeamComparison)
|
||||
{
|
||||
switch ($selfToOtherTeamComparison) {
|
||||
case PairwiseComparison::WIN:
|
||||
case PairwiseComparison::DRAW:
|
||||
// NOP
|
||||
@ -130,15 +109,12 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
||||
|
||||
$meanDelta = $winningMean - $losingMean;
|
||||
|
||||
if ($selfToOtherTeamComparison != PairwiseComparison::DRAW)
|
||||
{
|
||||
if ($selfToOtherTeamComparison != PairwiseComparison::DRAW) {
|
||||
// non-draw case
|
||||
$v = TruncatedGaussianCorrectionFunctions::vExceedsMarginScaled($meanDelta, $drawMargin, $c);
|
||||
$w = TruncatedGaussianCorrectionFunctions::wExceedsMarginScaled($meanDelta, $drawMargin, $c);
|
||||
$rankMultiplier = (int) $selfToOtherTeamComparison;
|
||||
}
|
||||
else
|
||||
{
|
||||
$rankMultiplier = (int)$selfToOtherTeamComparison;
|
||||
} else {
|
||||
// assume draw
|
||||
$v = TruncatedGaussianCorrectionFunctions::vWithinMarginScaled($meanDelta, $drawMargin, $c);
|
||||
$w = TruncatedGaussianCorrectionFunctions::wWithinMarginScaled($meanDelta, $drawMargin, $c);
|
||||
@ -146,19 +122,18 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
||||
}
|
||||
|
||||
$selfTeamAllPlayers = &$selfTeam->getAllPlayers();
|
||||
foreach ($selfTeamAllPlayers as &$selfTeamCurrentPlayer)
|
||||
{
|
||||
foreach ($selfTeamAllPlayers as &$selfTeamCurrentPlayer) {
|
||||
$localSelfTeamCurrentPlayer = &$selfTeamCurrentPlayer;
|
||||
$previousPlayerRating = $selfTeam->getRating($localSelfTeamCurrentPlayer);
|
||||
|
||||
$meanMultiplier = (square($previousPlayerRating->getStandardDeviation()) + $tauSquared)/$c;
|
||||
$stdDevMultiplier = (square($previousPlayerRating->getStandardDeviation()) + $tauSquared)/square($c);
|
||||
$meanMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c;
|
||||
$stdDevMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMatch::square($c);
|
||||
|
||||
$playerMeanDelta = ($rankMultiplier*$meanMultiplier*$v);
|
||||
$playerMeanDelta = ($rankMultiplier * $meanMultiplier * $v);
|
||||
$newMean = $previousPlayerRating->getMean() + $playerMeanDelta;
|
||||
|
||||
$newStdDev =
|
||||
sqrt((square($previousPlayerRating->getStandardDeviation()) + $tauSquared)*(1 - $w*$stdDevMultiplier));
|
||||
sqrt((BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier));
|
||||
|
||||
$newPlayerRatings->setRating($localSelfTeamCurrentPlayer, new Rating($newMean, $newStdDev));
|
||||
}
|
||||
@ -182,45 +157,38 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
|
||||
|
||||
$totalPlayers = $team1Count + $team2Count;
|
||||
|
||||
$betaSquared = square($gameInfo->getBeta());
|
||||
$betaSquared = BasicMatch::square($gameInfo->getBeta());
|
||||
|
||||
$meanGetter =
|
||||
function($currentRating)
|
||||
{
|
||||
return $currentRating->getMean();
|
||||
};
|
||||
$meanGetter = function ($currentRating) {
|
||||
return $currentRating->getMean();
|
||||
};
|
||||
|
||||
$varianceGetter =
|
||||
function($currentRating)
|
||||
{
|
||||
return square($currentRating->getStandardDeviation());
|
||||
};
|
||||
$varianceGetter = function ($currentRating) {
|
||||
return BasicMatch::square($currentRating->getStandardDeviation());
|
||||
};
|
||||
|
||||
$team1MeanSum = sum($team1Ratings, $meanGetter);
|
||||
$team1StdDevSquared = sum($team1Ratings, $varianceGetter);
|
||||
$team1MeanSum = BasicMatch::sum($team1Ratings, $meanGetter);
|
||||
$team1StdDevSquared = BasicMatch::sum($team1Ratings, $varianceGetter);
|
||||
|
||||
$team2MeanSum = sum($team2Ratings, $meanGetter);
|
||||
$team2SigmaSquared = sum($team2Ratings, $varianceGetter);
|
||||
$team2MeanSum = BasicMatch::sum($team2Ratings, $meanGetter);
|
||||
$team2SigmaSquared = BasicMatch::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)
|
||||
);
|
||||
$sqrtPart = sqrt(
|
||||
($totalPlayers * $betaSquared)
|
||||
/
|
||||
($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared)
|
||||
);
|
||||
|
||||
$expPart
|
||||
= exp(
|
||||
(-1*square($team1MeanSum - $team2MeanSum))
|
||||
/
|
||||
(2*($totalPlayers*$betaSquared + $team1StdDevSquared + $team2SigmaSquared))
|
||||
);
|
||||
$expPart = exp(
|
||||
(-1 * BasicMatch::square($team1MeanSum - $team2MeanSum))
|
||||
/
|
||||
(2 * ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared))
|
||||
);
|
||||
|
||||
return $expPart*$sqrtPart;
|
||||
return $expPart * $sqrtPart;
|
||||
}
|
||||
}
|
||||
?>
|
||||
}
|
Reference in New Issue
Block a user