2022-07-05 15:55:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DNW\Skills\TrueSkill;
|
2022-07-05 15:33:34 +02:00
|
|
|
|
|
|
|
use DNW\Skills\GameInfo;
|
|
|
|
use DNW\Skills\Guard;
|
|
|
|
use DNW\Skills\Numerics\BasicMath;
|
|
|
|
use DNW\Skills\PairwiseComparison;
|
2022-07-05 15:55:47 +02:00
|
|
|
use DNW\Skills\PlayersRange;
|
2022-07-05 15:33:34 +02:00
|
|
|
use DNW\Skills\RankSorter;
|
|
|
|
use DNW\Skills\Rating;
|
|
|
|
use DNW\Skills\RatingContainer;
|
|
|
|
use DNW\Skills\SkillCalculator;
|
|
|
|
use DNW\Skills\SkillCalculatorSupportedOptions;
|
|
|
|
use DNW\Skills\TeamsRange;
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Calculates the new ratings for only two players.
|
2022-07-05 15:33:34 +02:00
|
|
|
*
|
2010-10-08 21:44:36 -04:00
|
|
|
* 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));
|
|
|
|
}
|
|
|
|
|
2023-08-01 13:53:19 +00:00
|
|
|
public function calculateNewRatings(
|
|
|
|
GameInfo $gameInfo,
|
|
|
|
array $teams,
|
|
|
|
array $teamRanks
|
|
|
|
): RatingContainer {
|
2010-08-29 16:40:03 -04:00
|
|
|
// Basic argument checking
|
2022-07-05 15:55:47 +02:00
|
|
|
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);
|
2022-07-05 15:33:34 +02:00
|
|
|
|
2010-08-28 22:05:41 -04:00
|
|
|
// 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);
|
2022-07-05 15:33:34 +02:00
|
|
|
|
2010-08-28 22:05:41 -04:00
|
|
|
$losingTeamPlayers = $teams[1]->getAllPlayers();
|
|
|
|
$loser = $losingTeamPlayers[0];
|
|
|
|
$loserPreviousRating = $teams[1]->getRating($loser);
|
|
|
|
|
|
|
|
$wasDraw = ($teamRanks[0] == $teamRanks[1]);
|
|
|
|
|
|
|
|
$results = new RatingContainer();
|
|
|
|
|
2023-08-01 13:53:19 +00:00
|
|
|
$results->setRating(
|
|
|
|
$winner,
|
|
|
|
self::calculateNewRating(
|
|
|
|
$gameInfo,
|
|
|
|
$winnerPreviousRating,
|
|
|
|
$loserPreviousRating,
|
|
|
|
$wasDraw ? PairwiseComparison::DRAW
|
|
|
|
: PairwiseComparison::WIN
|
|
|
|
)
|
|
|
|
);
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2023-08-01 13:53:19 +00:00
|
|
|
$results->setRating(
|
|
|
|
$loser,
|
|
|
|
self::calculateNewRating(
|
|
|
|
$gameInfo,
|
|
|
|
$loserPreviousRating,
|
|
|
|
$winnerPreviousRating,
|
|
|
|
$wasDraw ? PairwiseComparison::DRAW
|
|
|
|
: PairwiseComparison::LOSE
|
|
|
|
)
|
|
|
|
);
|
2010-08-28 22:05:41 -04:00
|
|
|
|
|
|
|
// And we're done!
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2023-08-02 09:04:56 +00:00
|
|
|
private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, $comparison): Rating
|
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();
|
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
switch ($comparison) {
|
2010-08-28 22:05:41 -04:00
|
|
|
case PairwiseComparison::WIN:
|
|
|
|
case PairwiseComparison::DRAW:
|
|
|
|
// NOP
|
|
|
|
break;
|
|
|
|
case PairwiseComparison::LOSE:
|
|
|
|
$winningMean = $opponentRating->getMean();
|
|
|
|
$losingMean = $selfRating->getMean();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$meanDelta = $winningMean - $losingMean;
|
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
if ($comparison != PairwiseComparison::DRAW) {
|
2010-08-28 22:05:41 -04:00
|
|
|
// non-draw case
|
|
|
|
$v = TruncatedGaussianCorrectionFunctions::vExceedsMarginScaled($meanDelta, $drawMargin, $c);
|
|
|
|
$w = TruncatedGaussianCorrectionFunctions::wExceedsMarginScaled($meanDelta, $drawMargin, $c);
|
2023-08-01 11:26:38 +00:00
|
|
|
$rankMultiplier = $comparison->value;
|
2022-07-05 15:55:47 +02:00
|
|
|
} else {
|
2010-08-28 22:05:41 -04:00
|
|
|
$v = TruncatedGaussianCorrectionFunctions::vWithinMarginScaled($meanDelta, $drawMargin, $c);
|
|
|
|
$w = TruncatedGaussianCorrectionFunctions::wWithinMarginScaled($meanDelta, $drawMargin, $c);
|
|
|
|
$rankMultiplier = 1;
|
|
|
|
}
|
|
|
|
|
2022-07-05 15:55:47 +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());
|
2022-07-05 15:55:47 +02:00
|
|
|
$stdDevMultiplier = $varianceWithDynamics / BasicMath::square($c);
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
$newMean = $selfRating->getMean() + ($rankMultiplier * $meanMultiplier * $v);
|
|
|
|
$newStdDev = sqrt($varianceWithDynamics * (1 - $w * $stdDevMultiplier));
|
2010-08-28 22:05:41 -04:00
|
|
|
|
|
|
|
return new Rating($newMean, $newStdDev);
|
|
|
|
}
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
2022-07-05 15:55:47 +02:00
|
|
|
* {@inheritdoc}
|
2010-10-08 21:44:36 -04:00
|
|
|
*/
|
2023-08-01 12:43:58 +00:00
|
|
|
public function calculateMatchQuality(GameInfo $gameInfo, array $teams): float
|
2010-08-29 16:40:03 -04:00
|
|
|
{
|
2022-07-05 15:55:47 +02: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(
|
2022-07-05 15:55:47 +02:00
|
|
|
(2 * $betaSquared)
|
2016-05-24 16:31:21 +02:00
|
|
|
/
|
2022-07-05 15:55:47 +02:00
|
|
|
(2 * $betaSquared + $player1SigmaSquared + $player2SigmaSquared)
|
2016-05-24 16:31:21 +02:00
|
|
|
);
|
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(
|
2022-07-05 15:55:47 +02:00
|
|
|
(-1 * BasicMath::square($player1Rating->getMean() - $player2Rating->getMean()))
|
2016-05-24 16:31:21 +02:00
|
|
|
/
|
2022-07-05 15:55:47 +02:00
|
|
|
(2 * (2 * $betaSquared + $player1SigmaSquared + $player2SigmaSquared))
|
2016-05-24 16:31:21 +02:00
|
|
|
);
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
return $sqrtPart * $expPart;
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
2022-07-05 15:55:47 +02:00
|
|
|
}
|