Fixing failing tests and misc cleanup

This commit is contained in:
Alexander Liljengård
2016-05-24 15:12:29 +02:00
parent 5694a2fb30
commit 25b64d53f0
20 changed files with 132 additions and 125 deletions

View File

@ -4,7 +4,7 @@ use Moserware\Skills\GameInfo;
use Moserware\Skills\Guard;
use Moserware\Skills\ISupportPartialPlay;
use Moserware\Skills\ISupportPartialUpdate;
use Moserware\Skills\Numerics\BasicMatch;
use Moserware\Skills\Numerics\BasicMath;
use Moserware\Skills\Numerics\DiagonalMatrix;
use Moserware\Skills\Numerics\Matrix;
use Moserware\Skills\Numerics\Vector;
@ -25,7 +25,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
parent::__construct(SkillCalculatorSupportedOptions::PARTIAL_PLAY | SkillCalculatorSupportedOptions::PARTIAL_UPDATE, TeamsRange::atLeast(2), PlayersRange::atLeast(1));
}
public function calculateNewRatings(GameInfo &$gameInfo,
public function calculateNewRatings(GameInfo $gameInfo,
array $teams,
array $teamRanks)
{
@ -43,7 +43,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
return $factorGraph->getUpdatedRatings();
}
public function calculateMatchQuality(GameInfo &$gameInfo,
public function calculateMatchQuality(GameInfo $gameInfo,
array &$teams)
{
// We need to create the A matrix which is the player team assigments.
@ -55,7 +55,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
$playerTeamAssignmentsMatrix = $this->createPlayerTeamAssignmentMatrix($teamAssignmentsList, $meanVector->getRowCount());
$playerTeamAssignmentsMatrixTranspose = $playerTeamAssignmentsMatrix->getTranspose();
$betaSquared = BasicMatch::square($gameInfo->getBeta());
$betaSquared = BasicMath::square($gameInfo->getBeta());
$start = Matrix::multiply($meanVectorTranspose, $playerTeamAssignmentsMatrix);
@ -103,7 +103,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
return new DiagonalMatrix(
self::getPlayerRatingValues($teamAssignmentsList,
function ($rating) {
return BasicMatch::square($rating->getStandardDeviation());
return BasicMath::square($rating->getStandardDeviation());
}));
}

View File

@ -3,7 +3,7 @@
use Moserware\Skills\Guard;
use Moserware\Skills\FactorGraphs\Message;
use Moserware\Skills\FactorGraphs\Variable;
use Moserware\Skills\Numerics\BasicMatch;
use Moserware\Skills\Numerics\BasicMath;
use Moserware\Skills\Numerics\GaussianDistribution;
/**
@ -34,7 +34,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
for ($i = 0; $i < $variableWeightsLength; $i++) {
$weight = &$variableWeights[$i];
$this->_weights[0][$i] = $weight;
$this->_weightsSquared[0][$i] = BasicMatch::square($weight);
$this->_weightsSquared[0][$i] = BasicMath::square($weight);
}
$variablesToSumLength = count($variablesToSum);
@ -94,7 +94,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
$finalWeight = 0;
}
$currentWeights[$currentDestinationWeightIndex] = $finalWeight;
$currentWeightsSquared[$currentDestinationWeightIndex] = BasicMatch::square($finalWeight);
$currentWeightsSquared[$currentDestinationWeightIndex] = BasicMath::square($finalWeight);
$variableIndices[count($variableWeights)] = 0;
$this->_variableIndexOrdersForWeights[] = $variableIndices;

View File

@ -1,6 +1,6 @@
<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\Numerics\BasicMatch;
use Moserware\Skills\Numerics\BasicMath;
use Moserware\Skills\Rating;
use Moserware\Skills\FactorGraphs\ScheduleStep;
use Moserware\Skills\FactorGraphs\Variable;
@ -57,8 +57,8 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
{
return new GaussianPriorFactor(
$priorRating->getMean(),
BasicMatch::square($priorRating->getStandardDeviation()) +
BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
BasicMath::square($priorRating->getStandardDeviation()) +
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
$skillsVariable
);
}

View File

@ -2,7 +2,7 @@
use Moserware\Skills\FactorGraphs\ScheduleStep;
use Moserware\Skills\FactorGraphs\KeyedVariable;
use Moserware\Skills\Numerics\BasicMatch;
use Moserware\Skills\Numerics\BasicMath;
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
use Moserware\Skills\TrueSkill\Factors\GaussianLikelihoodFactor;
@ -37,7 +37,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
private function createLikelihood(KeyedVariable &$playerSkill, KeyedVariable &$playerPerformance)
{
return new GaussianLikelihoodFactor(
BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),
$playerPerformance,
$playerSkill
);

View File

@ -2,7 +2,7 @@
use Moserware\Skills\GameInfo;
use Moserware\Skills\Guard;
use Moserware\Skills\Numerics\BasicMatch;
use Moserware\Skills\Numerics\BasicMath;
use Moserware\Skills\PairwiseComparison;
use Moserware\Skills\RankSorter;
use Moserware\Skills\Rating;
@ -26,7 +26,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::exactly(1));
}
public function calculateNewRatings(GameInfo &$gameInfo,
public function calculateNewRatings(GameInfo $gameInfo,
array $teams,
array $teamRanks)
{
@ -74,11 +74,11 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
);
$c = sqrt(
BasicMatch::square($selfRating->getStandardDeviation())
BasicMath::square($selfRating->getStandardDeviation())
+
BasicMatch::square($opponentRating->getStandardDeviation())
BasicMath::square($opponentRating->getStandardDeviation())
+
2 * BasicMatch::square($gameInfo->getBeta())
2 * BasicMath::square($gameInfo->getBeta())
);
$winningMean = $selfRating->getMean();
@ -112,10 +112,10 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
$rankMultiplier = 1;
}
$meanMultiplier = (BasicMatch::square($selfRating->getStandardDeviation()) + BasicMatch::square($gameInfo->getDynamicsFactor()))/$c;
$meanMultiplier = (BasicMath::square($selfRating->getStandardDeviation()) + BasicMath::square($gameInfo->getDynamicsFactor()))/$c;
$varianceWithDynamics = BasicMatch::square($selfRating->getStandardDeviation()) + BasicMatch::square($gameInfo->getDynamicsFactor());
$stdDevMultiplier = $varianceWithDynamics/BasicMatch::square($c);
$varianceWithDynamics = BasicMath::square($selfRating->getStandardDeviation()) + BasicMath::square($gameInfo->getDynamicsFactor());
$stdDevMultiplier = $varianceWithDynamics/BasicMath::square($c);
$newMean = $selfRating->getMean() + ($rankMultiplier*$meanMultiplier*$v);
$newStdDev = sqrt($varianceWithDynamics*(1 - $w*$stdDevMultiplier));
@ -126,7 +126,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
/**
* {@inheritdoc }
*/
public function calculateMatchQuality(GameInfo &$gameInfo, array &$teams)
public function calculateMatchQuality(GameInfo $gameInfo, array &$teams)
{
Guard::argumentNotNull($gameInfo, "gameInfo");
$this->validateTeamCountAndPlayersCountPerTeam($teams);
@ -141,9 +141,9 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
$player2Rating = $team2Ratings[0];
// We just use equation 4.1 found on page 8 of the TrueSkill 2006 paper:
$betaSquared = BasicMatch::square($gameInfo->getBeta());
$player1SigmaSquared = BasicMatch::square($player1Rating->getStandardDeviation());
$player2SigmaSquared = BasicMatch::square($player2Rating->getStandardDeviation());
$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 =
@ -155,7 +155,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
// This is the exponent part of the equation:
$expPart =
exp(
(-1*BasicMatch::square($player1Rating->getMean() - $player2Rating->getMean()))
(-1*BasicMath::square($player1Rating->getMean() - $player2Rating->getMean()))
/
(2*(2*$betaSquared + $player1SigmaSquared + $player2SigmaSquared)));

View File

@ -2,7 +2,7 @@
use Moserware\Skills\GameInfo;
use Moserware\Skills\Guard;
use Moserware\Skills\Numerics\BasicMatch;
use Moserware\Skills\Numerics\BasicMath;
use Moserware\Skills\PairwiseComparison;
use Moserware\Skills\RankSorter;
use Moserware\Skills\Rating;
@ -27,9 +27,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::atLeast(1));
}
public function calculateNewRatings(GameInfo &$gameInfo,
array $teams,
array $teamRanks)
public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks)
{
Guard::argumentNotNull($gameInfo, "gameInfo");
$this->validateTeamCountAndPlayersCountPerTeam($teams);
@ -69,8 +67,8 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
$gameInfo->getBeta()
);
$betaSquared = BasicMatch::square($gameInfo->getBeta());
$tauSquared = BasicMatch::square($gameInfo->getDynamicsFactor());
$betaSquared = BasicMath::square($gameInfo->getBeta());
$tauSquared = BasicMath::square($gameInfo->getDynamicsFactor());
$totalPlayers = $selfTeam->count() + $otherTeam->count();
@ -78,17 +76,17 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
return $currentRating->getMean();
};
$selfMeanSum = BasicMatch::sum($selfTeam->getAllRatings(), $meanGetter);
$otherTeamMeanSum = BasicMatch::sum($otherTeam->getAllRatings(), $meanGetter);
$selfMeanSum = BasicMath::sum($selfTeam->getAllRatings(), $meanGetter);
$otherTeamMeanSum = BasicMath::sum($otherTeam->getAllRatings(), $meanGetter);
$varianceGetter = function ($currentRating) {
return BasicMatch::square($currentRating->getStandardDeviation());
return BasicMath::square($currentRating->getStandardDeviation());
};
$c = sqrt(
BasicMatch::sum($selfTeam->getAllRatings(), $varianceGetter)
BasicMath::sum($selfTeam->getAllRatings(), $varianceGetter)
+
BasicMatch::sum($otherTeam->getAllRatings(), $varianceGetter)
BasicMath::sum($otherTeam->getAllRatings(), $varianceGetter)
+
$totalPlayers * $betaSquared
);
@ -126,24 +124,24 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
$localSelfTeamCurrentPlayer = &$selfTeamCurrentPlayer;
$previousPlayerRating = $selfTeam->getRating($localSelfTeamCurrentPlayer);
$meanMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c;
$stdDevMultiplier = (BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMatch::square($c);
$meanMultiplier = (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c;
$stdDevMultiplier = (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMath::square($c);
$playerMeanDelta = ($rankMultiplier * $meanMultiplier * $v);
$newMean = $previousPlayerRating->getMean() + $playerMeanDelta;
$newStdDev =
sqrt((BasicMatch::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier));
$newStdDev = sqrt(
(BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier)
);
$newPlayerRatings->setRating($localSelfTeamCurrentPlayer, new Rating($newMean, $newStdDev));
}
}
/**
* {@inheritdoc }
* {@inheritdoc}
*/
public function calculateMatchQuality(GameInfo &$gameInfo,
array &$teams)
public function calculateMatchQuality(GameInfo $gameInfo, array &$teams)
{
Guard::argumentNotNull($gameInfo, "gameInfo");
$this->validateTeamCountAndPlayersCountPerTeam($teams);
@ -157,21 +155,21 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
$totalPlayers = $team1Count + $team2Count;
$betaSquared = BasicMatch::square($gameInfo->getBeta());
$betaSquared = BasicMath::square($gameInfo->getBeta());
$meanGetter = function ($currentRating) {
return $currentRating->getMean();
};
$varianceGetter = function ($currentRating) {
return BasicMatch::square($currentRating->getStandardDeviation());
return BasicMath::square($currentRating->getStandardDeviation());
};
$team1MeanSum = BasicMatch::sum($team1Ratings, $meanGetter);
$team1StdDevSquared = BasicMatch::sum($team1Ratings, $varianceGetter);
$team1MeanSum = BasicMath::sum($team1Ratings, $meanGetter);
$team1StdDevSquared = BasicMath::sum($team1Ratings, $varianceGetter);
$team2MeanSum = BasicMatch::sum($team2Ratings, $meanGetter);
$team2SigmaSquared = BasicMatch::sum($team2Ratings, $varianceGetter);
$team2MeanSum = BasicMath::sum($team2Ratings, $meanGetter);
$team2SigmaSquared = BasicMath::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
@ -184,7 +182,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
);
$expPart = exp(
(-1 * BasicMatch::square($team1MeanSum - $team2MeanSum))
(-1 * BasicMath::square($team1MeanSum - $team2MeanSum))
/
(2 * ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared))
);