trueskill/src/TrueSkill/TwoPlayerTrueSkillCalculator.php

162 lines
6.2 KiB
PHP
Raw Normal View History

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;
/**
* Calculates the new ratings for only two players.
2022-07-05 15:33:34 +02: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.
*/
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,
array $teams,
2023-08-01 12:13:24 +00:00
array $teamRanks): RatingContainer
{
// Basic argument checking
2022-07-05 15:55:47 +02:00
Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams);
// Make sure things are in order
RankSorter::sort($teams, $teamRanks);
2022-07-05 15:33:34 +02: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
$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)
{
$drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
$gameInfo->getDrawProbability(),
$gameInfo->getBeta()
);
$c = sqrt(
2016-05-24 15:12:29 +02:00
BasicMath::square($selfRating->getStandardDeviation())
+
2016-05-24 15:12:29 +02:00
BasicMath::square($opponentRating->getStandardDeviation())
+
2016-05-24 15:12:29 +02:00
2 * BasicMath::square($gameInfo->getBeta())
);
$winningMean = $selfRating->getMean();
$losingMean = $opponentRating->getMean();
2022-07-05 15:55:47 +02:00
switch ($comparison) {
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) {
// 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 {
$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;
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);
2022-07-05 15:55:47 +02:00
$newMean = $selfRating->getMean() + ($rankMultiplier * $meanMultiplier * $v);
$newStdDev = sqrt($varianceWithDynamics * (1 - $w * $stdDevMultiplier));
return new Rating($newMean, $newStdDev);
}
/**
2022-07-05 15:55:47 +02:00
* {@inheritdoc}
*/
public function calculateMatchQuality(GameInfo $gameInfo, array $teams)
{
2022-07-05 15:55:47 +02:00
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:
2016-05-24 15:12:29 +02:00
$betaSquared = BasicMath::square($gameInfo->getBeta());
$player1SigmaSquared = BasicMath::square($player1Rating->getStandardDeviation());
$player2SigmaSquared = BasicMath::square($player2Rating->getStandardDeviation());
// This is the square root part of the equation:
$sqrtPart = sqrt(
2022-07-05 15:55:47 +02:00
(2 * $betaSquared)
/
2022-07-05 15:55:47 +02:00
(2 * $betaSquared + $player1SigmaSquared + $player2SigmaSquared)
);
// This is the exponent part of the equation:
$expPart = exp(
2022-07-05 15:55:47 +02:00
(-1 * BasicMath::square($player1Rating->getMean() - $player2Rating->getMean()))
/
2022-07-05 15:55:47 +02:00
(2 * (2 * $betaSquared + $player1SigmaSquared + $player2SigmaSquared))
);
2022-07-05 15:55:47 +02:00
return $sqrtPart * $expPart;
}
2022-07-05 15:55:47 +02:00
}