PHPMD reintroduced.

This commit is contained in:
2024-07-04 09:38:03 +00:00
parent 5dc0c7058d
commit cf588f3fc2
28 changed files with 56692 additions and 165 deletions

View File

@ -171,7 +171,8 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
$currentColumn = 0;
for ($i = 0; $i < count($teamAssignmentsList) - 1; ++$i) {
$teamCnt = count($teamAssignmentsList);
for ($i = 0; $i < $teamCnt - 1; ++$i) {
$currentTeam = $teamAssignmentsList[$i];
// Need to add in 0's for all the previous players, since they're not

View File

@ -60,9 +60,9 @@ class GaussianLikelihoodFactor extends GaussianFactor
$a * ($marginal2->getPrecision() - $message2Value->getPrecision())
);
$oldMarginalWithoutMessage = GaussianDistribution::divide($marginal1, $message1Value);
$oldMarginalWithoutMsg = GaussianDistribution::divide($marginal1, $message1Value);
$newMarginal = GaussianDistribution::multiply($oldMarginalWithoutMessage, $newMessage);
$newMarginal = GaussianDistribution::multiply($oldMarginalWithoutMsg, $newMessage);
// Update the message and marginal

View File

@ -19,9 +19,9 @@ use DNW\Skills\Numerics\GaussianDistribution;
class GaussianWeightedSumFactor extends GaussianFactor
{
/**
* @var array<int[]> $variableIndexOrdersForWeights
* @var array<int[]> $varIndexOrdersForWeights
*/
private array $variableIndexOrdersForWeights = [];
private array $varIndexOrdersForWeights = [];
/**
* This following is used for convenience, for example, the first entry is [0, 1, 2]
@ -58,9 +58,9 @@ class GaussianWeightedSumFactor extends GaussianFactor
$variablesToSumLength = count($variablesToSum);
// 0..n-1
$this->variableIndexOrdersForWeights[0] = [];
$this->varIndexOrdersForWeights[0] = [];
for ($i = 0; $i < ($variablesToSumLength + 1); ++$i) {
$this->variableIndexOrdersForWeights[0][] = $i;
$this->varIndexOrdersForWeights[0][] = $i;
}
$variableWeightsLength = count($variableWeights);
@ -113,7 +113,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
$currentWeights[$currentDestinationWeightIndex] = $finalWeight;
$currentWeightsSquared[$currentDestinationWeightIndex] = BasicMath::square($finalWeight);
$variableIndices[count($variableWeights)] = 0;
$this->variableIndexOrdersForWeights[] = $variableIndices;
$this->varIndexOrdersForWeights[] = $variableIndices;
$this->weights[$weightsIndex] = $currentWeights;
$this->weightsSquared[$weightsIndex] = $currentWeightsSquared;
@ -160,9 +160,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
// The math works out so that 1/newPrecision = sum of a_i^2 /marginalsWithoutMessages[i]
$inverseOfNewPrecisionSum = 0.0;
$anotherInverseOfNewPrecisionSum = 0.0;
$weightedMeanSum = 0.0;
$anotherWeightedMeanSum = 0.0;
$weightsSquaredLength = count($weightsSquared);
@ -172,16 +170,11 @@ class GaussianWeightedSumFactor extends GaussianFactor
$inverseOfNewPrecisionSum += $weightsSquared[$i] /
($variables[$i + 1]->getValue()->getPrecision() - $messages[$i + 1]->getValue()->getPrecision());
$diff = GaussianDistribution::divide($variables[$i + 1]->getValue(), $messages[$i + 1]->getValue());
$anotherInverseOfNewPrecisionSum += $weightsSquared[$i] / $diff->getPrecision();
$weightedMeanSum += $weights[$i]
*
($variables[$i + 1]->getValue()->getPrecisionMean() - $messages[$i + 1]->getValue()->getPrecisionMean())
/
($variables[$i + 1]->getValue()->getPrecision() - $messages[$i + 1]->getValue()->getPrecision());
$anotherWeightedMeanSum += $weights[$i] * $diff->getPrecisionMean() / $diff->getPrecision();
}
$newPrecision = 1.0 / $inverseOfNewPrecisionSum;
@ -214,7 +207,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
$updatedMessages = [];
$updatedVariables = [];
$indicesToUse = $this->variableIndexOrdersForWeights[$messageIndex];
$indicesToUse = $this->varIndexOrdersForWeights[$messageIndex];
// The tricky part here is that we have to put the messages and variables in the same
// order as the weights. Thankfully, the weights and messages share the same index numbers,
// so we just need to make sure they're consistent

View File

@ -15,8 +15,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
{
public function __construct(
TrueSkillFactorGraph $parentGraph,
private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $TeamPerformancesToTeamPerformanceDifferencesLayer,
private readonly TeamDifferencesComparisonLayer $TeamDifferencesComparisonLayer
private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $teamPerformancesToTeamPerformanceDifferencesLayer,
private readonly TeamDifferencesComparisonLayer $teamDifferencesComparisonLayer
)
{
parent::__construct($parentGraph);
@ -25,20 +25,20 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
public function getLocalFactors(): array
{
return array_merge(
$this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(),
$this->TeamDifferencesComparisonLayer->getLocalFactors()
$this->teamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(),
$this->teamDifferencesComparisonLayer->getLocalFactors()
);
}
public function buildLayer(): void
{
$inputVariablesGroups = $this->getInputVariablesGroups();
$this->TeamPerformancesToTeamPerformanceDifferencesLayer->setInputVariablesGroups($inputVariablesGroups);
$this->TeamPerformancesToTeamPerformanceDifferencesLayer->buildLayer();
$this->teamPerformancesToTeamPerformanceDifferencesLayer->setInputVariablesGroups($inputVariablesGroups);
$this->teamPerformancesToTeamPerformanceDifferencesLayer->buildLayer();
$teamDifferencesOutputVariablesGroups = $this->TeamPerformancesToTeamPerformanceDifferencesLayer->getOutputVariablesGroups();
$this->TeamDifferencesComparisonLayer->setInputVariablesGroups($teamDifferencesOutputVariablesGroups);
$this->TeamDifferencesComparisonLayer->buildLayer();
$teamDifferencesOutputVariablesGroups = $this->teamPerformancesToTeamPerformanceDifferencesLayer->getOutputVariablesGroups();
$this->teamDifferencesComparisonLayer->setInputVariablesGroups($teamDifferencesOutputVariablesGroups);
$this->teamDifferencesComparisonLayer->buildLayer();
}
public function createPriorSchedule(): ?ScheduleSequence
@ -56,9 +56,9 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
}
// When dealing with differences, there are always (n-1) differences, so add in the 1
$totalTeamDifferences = count($this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors());
$totalTeamDifferences = count($this->teamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors());
$localFactors = $this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$localFactors = $this->teamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$firstDifferencesFactor = $localFactors[0];
$lastDifferencesFactor = $localFactors[$totalTeamDifferences - 1];
@ -83,8 +83,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
private function createTwoTeamInnerPriorLoopSchedule(): ScheduleSequence
{
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$teamDifferencesComparisonLayerLocalFactors = $this->TeamDifferencesComparisonLayer->getLocalFactors();
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->teamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$teamDifferencesComparisonLayerLocalFactors = $this->teamDifferencesComparisonLayer->getLocalFactors();
$firstPerfToTeamDiff = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0];
$firstTeamDiffComparison = $teamDifferencesComparisonLayerLocalFactors[0];
@ -109,13 +109,13 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
private function createMultipleTeamInnerPriorLoopSchedule(): ScheduleLoop
{
$totalTeamDifferences = count($this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors());
$totalTeamDifferences = count($this->teamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors());
$forwardScheduleList = [];
for ($i = 0; $i < $totalTeamDifferences - 1; ++$i) {
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$teamDifferencesComparisonLayerLocalFactors = $this->TeamDifferencesComparisonLayer->getLocalFactors();
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->teamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$teamDifferencesComparisonLayerLocalFactors = $this->teamDifferencesComparisonLayer->getLocalFactors();
$currentTeamPerfToTeamPerfDiff = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$i];
$currentTeamDiffComparison = $teamDifferencesComparisonLayerLocalFactors[$i];
@ -151,8 +151,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$backwardScheduleList = [];
for ($i = 0; $i < $totalTeamDifferences - 1; ++$i) {
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$teamDifferencesComparisonLayerLocalFactors = $this->TeamDifferencesComparisonLayer->getLocalFactors();
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->teamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$teamDifferencesComparisonLayerLocalFactors = $this->teamDifferencesComparisonLayer->getLocalFactors();
$differencesFactor = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i];
$comparisonFactor = $teamDifferencesComparisonLayerLocalFactors[$totalTeamDifferences - 1 - $i];

View File

@ -27,8 +27,8 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
$this->addLayerFactor($newSumFactor);
// REVIEW: Does it make sense to have groups of one?
$outputVariablesGroups = &$this->getOutputVariablesGroups();
$outputVariablesGroups[] = [$teamPerformance];
$outputVarGroups = &$this->getOutputVariablesGroups();
$outputVarGroups[] = [$teamPerformance];
}
}

View File

@ -34,12 +34,12 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
$localCurrentTeam = $currentTeam;
$currentTeamSkills = [];
$currentTeamAllPlayers = $localCurrentTeam->getAllPlayers();
foreach ($currentTeamAllPlayers as $currentTeamPlayer) {
$localCurrentTeamPlayer = $currentTeamPlayer;
$currentTeamPlayerRating = $currentTeam->getRating($localCurrentTeamPlayer);
$playerSkill = $this->createSkillOutputVariable($localCurrentTeamPlayer);
$priorFactor = $this->createPriorFactor($currentTeamPlayerRating, $playerSkill);
$curTeamAllPlayers = $localCurrentTeam->getAllPlayers();
foreach ($curTeamAllPlayers as $curTeamPlayer) {
$localCurTeamPlayer = $curTeamPlayer;
$curTeamPlayerRating = $currentTeam->getRating($localCurTeamPlayer);
$playerSkill = $this->createSkillOutputVariable($localCurTeamPlayer);
$priorFactor = $this->createPriorFactor($curTeamPlayerRating, $playerSkill);
$this->addLayerFactor($priorFactor);
$currentTeamSkills[] = $playerSkill;
}

View File

@ -15,25 +15,25 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
{
public function buildLayer(): void
{
$inputVariablesGroups = $this->getInputVariablesGroups();
$outputVariablesGroups = &$this->getOutputVariablesGroups();
$inputVarGroups = $this->getInputVariablesGroups();
$outputVarGroups = &$this->getOutputVariablesGroups();
foreach ($inputVariablesGroups as $currentTeam) {
foreach ($inputVarGroups as $currentTeam) {
$currentTeamPlayerPerformances = [];
/**
* @var Variable $playerSkillVariable
* @var Variable $playerSkillVar
*/
foreach ($currentTeam as $playerSkillVariable) {
$localPlayerSkillVariable = $playerSkillVariable;
$currentPlayer = ($localPlayerSkillVariable instanceof KeyedVariable) ? $localPlayerSkillVariable->getKey() : "";
foreach ($currentTeam as $playerSkillVar) {
$localPlayerSkillVar = $playerSkillVar;
$currentPlayer = ($localPlayerSkillVar instanceof KeyedVariable) ? $localPlayerSkillVar->getKey() : "";
$playerPerformance = $this->createOutputVariable($currentPlayer);
$newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVariable, $playerPerformance);
$newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVar, $playerPerformance);
$this->addLayerFactor($newLikelihoodFactor);
$currentTeamPlayerPerformances[] = $playerPerformance;
}
$outputVariablesGroups[] = $currentTeamPlayerPerformances;
$outputVarGroups[] = $currentTeamPlayerPerformances;
}
}

View File

@ -118,9 +118,9 @@ class TrueSkillFactorGraph extends FactorGraph
$allLayersReverse = array_reverse($this->layers);
foreach ($allLayersReverse as $currentLayer) {
$currentPosteriorSchedule = $currentLayer->createPosteriorSchedule();
if ($currentPosteriorSchedule != NULL) {
$fullSchedule[] = $currentPosteriorSchedule;
$curPosteriorSchedule = $currentLayer->createPosteriorSchedule();
if ($curPosteriorSchedule != NULL) {
$fullSchedule[] = $curPosteriorSchedule;
}
}

View File

@ -102,33 +102,33 @@ class TruncatedGaussianCorrectionFunctions
}
// the multiplicative correction of a double-sided truncated Gaussian with unit variance
public static function wWithinMarginScaled(float $teamPerformanceDifference, float $drawMargin, float $c): float
public static function wWithinMarginScaled(float $teamPerformanceDiff, float $drawMargin, float $c): float
{
return self::wWithinMargin($teamPerformanceDifference / $c, $drawMargin / $c);
return self::wWithinMargin($teamPerformanceDiff / $c, $drawMargin / $c);
}
// From F#:
public static function wWithinMargin(float $teamPerformanceDifference, float $drawMargin): float
public static function wWithinMargin(float $teamPerformanceDiff, float $drawMargin): float
{
$teamPerformanceDifferenceAbsoluteValue = abs($teamPerformanceDifference);
$denominator = GaussianDistribution::cumulativeTo($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
$teamPerformanceDiffAbsValue = abs($teamPerformanceDiff);
$denominator = GaussianDistribution::cumulativeTo($drawMargin - $teamPerformanceDiffAbsValue)
-
GaussianDistribution::cumulativeTo(-$drawMargin - $teamPerformanceDifferenceAbsoluteValue);
GaussianDistribution::cumulativeTo(-$drawMargin - $teamPerformanceDiffAbsValue);
if ($denominator < 2.222758749e-162) {
return 1.0;
}
$vt = self::vWithinMargin($teamPerformanceDifferenceAbsoluteValue, $drawMargin);
$vt = self::vWithinMargin($teamPerformanceDiffAbsValue, $drawMargin);
return $vt * $vt +
(($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
(($drawMargin - $teamPerformanceDiffAbsValue)
*
GaussianDistribution::at(
$drawMargin - $teamPerformanceDifferenceAbsoluteValue
$drawMargin - $teamPerformanceDiffAbsValue
)
- (-$drawMargin - $teamPerformanceDifferenceAbsoluteValue)
- (-$drawMargin - $teamPerformanceDiffAbsValue)
*
GaussianDistribution::at(-$drawMargin - $teamPerformanceDifferenceAbsoluteValue)) / $denominator;
GaussianDistribution::at(-$drawMargin - $teamPerformanceDiffAbsValue)) / $denominator;
}
}

View File

@ -125,9 +125,9 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
}
$selfTeamAllPlayers = $selfTeam->getAllPlayers();
foreach ($selfTeamAllPlayers as $selfTeamCurrentPlayer) {
$localSelfTeamCurrentPlayer = $selfTeamCurrentPlayer;
$previousPlayerRating = $selfTeam->getRating($localSelfTeamCurrentPlayer);
foreach ($selfTeamAllPlayers as $selfTeamCurPlayer) {
$localSelfTeamCurPlayer = $selfTeamCurPlayer;
$previousPlayerRating = $selfTeam->getRating($localSelfTeamCurPlayer);
$meanMultiplier = (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / $c;
$stdDevMultiplier = (BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) / BasicMath::square($c);
@ -139,7 +139,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
(BasicMath::square($previousPlayerRating->getStandardDeviation()) + $tauSquared) * (1 - $w * $stdDevMultiplier)
);
$newPlayerRatings->setRating($localSelfTeamCurrentPlayer, new Rating($newMean, $newStdDev));
$newPlayerRatings->setRating($localSelfTeamCurPlayer, new Rating($newMean, $newStdDev));
}
}