2016-05-24 14:10:39 +02:00
|
|
|
<?php namespace Moserware\Skills\TrueSkill;
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
use Moserware\Skills\GameInfo;
|
2010-08-29 16:40:03 -04:00
|
|
|
use Moserware\Skills\Guard;
|
2016-05-24 15:12:29 +02:00
|
|
|
use Moserware\Skills\Numerics\BasicMath;
|
2010-08-28 22:05:41 -04:00
|
|
|
use Moserware\Skills\PairwiseComparison;
|
|
|
|
use Moserware\Skills\RankSorter;
|
|
|
|
use Moserware\Skills\Rating;
|
|
|
|
use Moserware\Skills\RatingContainer;
|
|
|
|
use Moserware\Skills\SkillCalculator;
|
|
|
|
use Moserware\Skills\SkillCalculatorSupportedOptions;
|
|
|
|
use Moserware\Skills\PlayersRange;
|
|
|
|
use Moserware\Skills\TeamsRange;
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Calculates the new ratings for only two players.
|
|
|
|
*
|
|
|
|
* When you only have two players, a lot of the math simplifies. The main purpose of this class
|
|
|
|
* is to show the bare minimum of what a TrueSkill implementation should have.
|
|
|
|
*/
|
2010-08-28 22:05:41 -04:00
|
|
|
class TwoPlayerTrueSkillCalculator extends SkillCalculator
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::exactly(1));
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:12:29 +02:00
|
|
|
public function calculateNewRatings(GameInfo $gameInfo,
|
2010-08-28 22:05:41 -04:00
|
|
|
array $teams,
|
|
|
|
array $teamRanks)
|
|
|
|
{
|
2010-08-29 16:40:03 -04:00
|
|
|
// Basic argument checking
|
|
|
|
Guard::argumentNotNull($gameInfo, "gameInfo");
|
2010-08-28 22:05:41 -04:00
|
|
|
$this->validateTeamCountAndPlayersCountPerTeam($teams);
|
|
|
|
|
|
|
|
// Make sure things are in order
|
|
|
|
RankSorter::sort($teams, $teamRanks);
|
|
|
|
|
|
|
|
// Since we verified that each team has one player, we know the player is the first one
|
|
|
|
$winningTeamPlayers = $teams[0]->getAllPlayers();
|
|
|
|
$winner = $winningTeamPlayers[0];
|
|
|
|
$winnerPreviousRating = $teams[0]->getRating($winner);
|
|
|
|
|
|
|
|
$losingTeamPlayers = $teams[1]->getAllPlayers();
|
|
|
|
$loser = $losingTeamPlayers[0];
|
|
|
|
$loserPreviousRating = $teams[1]->getRating($loser);
|
|
|
|
|
|
|
|
$wasDraw = ($teamRanks[0] == $teamRanks[1]);
|
|
|
|
|
|
|
|
$results = new RatingContainer();
|
|
|
|
|
|
|
|
$results->setRating($winner, self::calculateNewRating($gameInfo,
|
|
|
|
$winnerPreviousRating,
|
|
|
|
$loserPreviousRating,
|
|
|
|
$wasDraw ? PairwiseComparison::DRAW
|
|
|
|
: PairwiseComparison::WIN));
|
|
|
|
|
|
|
|
$results->setRating($loser, self::calculateNewRating($gameInfo,
|
|
|
|
$loserPreviousRating,
|
|
|
|
$winnerPreviousRating,
|
|
|
|
$wasDraw ? PairwiseComparison::DRAW
|
|
|
|
: PairwiseComparison::LOSE));
|
|
|
|
|
|
|
|
// And we're done!
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, $comparison)
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
2016-05-24 14:10:39 +02:00
|
|
|
$drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
|
|
|
|
$gameInfo->getDrawProbability(),
|
|
|
|
$gameInfo->getBeta()
|
|
|
|
);
|
|
|
|
|
|
|
|
$c = sqrt(
|
2016-05-24 15:12:29 +02:00
|
|
|
BasicMath::square($selfRating->getStandardDeviation())
|
2016-05-24 14:10:39 +02:00
|
|
|
+
|
2016-05-24 15:12:29 +02:00
|
|
|
BasicMath::square($opponentRating->getStandardDeviation())
|
2016-05-24 14:10:39 +02:00
|
|
|
+
|
2016-05-24 15:12:29 +02:00
|
|
|
2 * BasicMath::square($gameInfo->getBeta())
|
2016-05-24 14:10:39 +02:00
|
|
|
);
|
2010-08-28 22:05:41 -04:00
|
|
|
|
|
|
|
$winningMean = $selfRating->getMean();
|
|
|
|
$losingMean = $opponentRating->getMean();
|
|
|
|
|
|
|
|
switch ($comparison)
|
|
|
|
{
|
|
|
|
case PairwiseComparison::WIN:
|
|
|
|
case PairwiseComparison::DRAW:
|
|
|
|
// NOP
|
|
|
|
break;
|
|
|
|
case PairwiseComparison::LOSE:
|
|
|
|
$winningMean = $opponentRating->getMean();
|
|
|
|
$losingMean = $selfRating->getMean();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$meanDelta = $winningMean - $losingMean;
|
|
|
|
|
|
|
|
if ($comparison != PairwiseComparison::DRAW)
|
|
|
|
{
|
|
|
|
// non-draw case
|
|
|
|
$v = TruncatedGaussianCorrectionFunctions::vExceedsMarginScaled($meanDelta, $drawMargin, $c);
|
|
|
|
$w = TruncatedGaussianCorrectionFunctions::wExceedsMarginScaled($meanDelta, $drawMargin, $c);
|
|
|
|
$rankMultiplier = (int) $comparison;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$v = TruncatedGaussianCorrectionFunctions::vWithinMarginScaled($meanDelta, $drawMargin, $c);
|
|
|
|
$w = TruncatedGaussianCorrectionFunctions::wWithinMarginScaled($meanDelta, $drawMargin, $c);
|
|
|
|
$rankMultiplier = 1;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:12:29 +02:00
|
|
|
$meanMultiplier = (BasicMath::square($selfRating->getStandardDeviation()) + BasicMath::square($gameInfo->getDynamicsFactor()))/$c;
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2016-05-24 15:12:29 +02:00
|
|
|
$varianceWithDynamics = BasicMath::square($selfRating->getStandardDeviation()) + BasicMath::square($gameInfo->getDynamicsFactor());
|
|
|
|
$stdDevMultiplier = $varianceWithDynamics/BasicMath::square($c);
|
2010-08-28 22:05:41 -04:00
|
|
|
|
|
|
|
$newMean = $selfRating->getMean() + ($rankMultiplier*$meanMultiplier*$v);
|
|
|
|
$newStdDev = sqrt($varianceWithDynamics*(1 - $w*$stdDevMultiplier));
|
|
|
|
|
|
|
|
return new Rating($newMean, $newStdDev);
|
|
|
|
}
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* {@inheritdoc }
|
|
|
|
*/
|
2016-05-24 16:31:21 +02:00
|
|
|
public function calculateMatchQuality(GameInfo $gameInfo, array $teams)
|
2010-08-29 16:40:03 -04:00
|
|
|
{
|
|
|
|
Guard::argumentNotNull($gameInfo, "gameInfo");
|
2010-08-28 22:05:41 -04:00
|
|
|
$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:
|
2016-05-24 15:12:29 +02:00
|
|
|
$betaSquared = BasicMath::square($gameInfo->getBeta());
|
|
|
|
$player1SigmaSquared = BasicMath::square($player1Rating->getStandardDeviation());
|
|
|
|
$player2SigmaSquared = BasicMath::square($player2Rating->getStandardDeviation());
|
2010-08-28 22:05:41 -04:00
|
|
|
|
|
|
|
// This is the square root part of the equation:
|
2016-05-24 16:31:21 +02:00
|
|
|
$sqrtPart = sqrt(
|
|
|
|
(2*$betaSquared)
|
|
|
|
/
|
|
|
|
(2*$betaSquared + $player1SigmaSquared + $player2SigmaSquared)
|
|
|
|
);
|
2010-08-28 22:05:41 -04:00
|
|
|
|
|
|
|
// This is the exponent part of the equation:
|
2016-05-24 16:31:21 +02:00
|
|
|
$expPart = exp(
|
|
|
|
(-1*BasicMath::square($player1Rating->getMean() - $player2Rating->getMean()))
|
|
|
|
/
|
|
|
|
(2*(2*$betaSquared + $player1SigmaSquared + $player2SigmaSquared))
|
|
|
|
);
|
2010-08-28 22:05:41 -04:00
|
|
|
|
|
|
|
return $sqrtPart*$expPart;
|
|
|
|
}
|
2016-05-24 14:10:39 +02:00
|
|
|
}
|