From 04c911742d711a01143de56f5bf5a94b694861ec Mon Sep 17 00:00:00 2001 From: Jeff Moser Date: Sat, 25 Sep 2010 22:40:56 -0400 Subject: [PATCH] Some reference updates and adding the needed array(...) scope to existing data for single team values --- .../TrueSkill/Factors/GaussianFactor.php | 4 +-- .../Factors/GaussianGreaterThanFactor.php | 10 +++--- .../Factors/GaussianWeightedSumFactor.php | 32 ++++++++++--------- .../Factors/GaussianWithinFactor.php | 8 ++--- .../IteratedTeamDifferencesInnerLayer.php | 30 +++++++++-------- .../Layers/TeamDifferencesComparisonLayer.php | 2 +- ...ancesToTeamPerformanceDifferencesLayer.php | 8 ++--- PHPSkills/TrueSkill/TrueSkillFactorGraph.php | 4 +-- 8 files changed, 51 insertions(+), 47 deletions(-) diff --git a/PHPSkills/TrueSkill/Factors/GaussianFactor.php b/PHPSkills/TrueSkill/Factors/GaussianFactor.php index 1069c62..e2d02d1 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianFactor.php @@ -22,8 +22,8 @@ abstract class GaussianFactor extends Factor /// Sends the factor-graph message with and returns the log-normalization constant protected function sendMessageVariable(Message &$message, Variable &$variable) { - $marginal = $variable->getValue(); - $messageValue = $message->getValue(); + $marginal = &$variable->getValue(); + $messageValue = &$message->getValue(); $logZ = GaussianDistribution::logProductNormalization($marginal, $messageValue); $variable->setValue($marginal*$messageValue); return $logZ; diff --git a/PHPSkills/TrueSkill/Factors/GaussianGreaterThanFactor.php b/PHPSkills/TrueSkill/Factors/GaussianGreaterThanFactor.php index a4dfbba..d9468d3 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianGreaterThanFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianGreaterThanFactor.php @@ -22,17 +22,17 @@ class GaussianGreaterThanFactor extends GaussianFactor public function __construct($epsilon, Variable &$variable) { - parent::_construct("{0} > {1:0.000}"); + parent::__construct("{0} > {1:0.000}"); $this->_epsilon = $epsilon; $this->createVariableToMessageBinding($variable); } public function getLogNormalization() { - $vars = $this->getVariables(); - $marginal = $vars[0]->getValue(); - $messages = $this->getMessages(); - $message = $messages[0]->getValue(); + $vars = &$this->getVariables(); + $marginal = &$vars[0]->getValue(); + $messages = &$this->getMessages(); + $message = &$messages[0]->getValue(); $messageFromVariable = GaussianDistribution::divide($marginal, $message); return -GaussianDistribution::logProductNormalization($messageFromVariable, $message) + diff --git a/PHPSkills/TrueSkill/Factors/GaussianWeightedSumFactor.php b/PHPSkills/TrueSkill/Factors/GaussianWeightedSumFactor.php index c96947e..b8f6a8b 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianWeightedSumFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianWeightedSumFactor.php @@ -2,14 +2,16 @@ namespace Moserware\Skills\TrueSkill\Factors; + require_once(dirname(__FILE__) . "/GaussianFactor.php"); +require_once(dirname(__FILE__) . "/../../Guard.php"); require_once(dirname(__FILE__) . "/../../FactorGraphs/Message.php"); require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php"); require_once(dirname(__FILE__) . "/../../Numerics/GaussianDistribution.php"); require_once(dirname(__FILE__) . "/../../Numerics/BasicMath.php"); - use Moserware\Numerics\GaussianDistribution; +use Moserware\Skills\Guard; use Moserware\Skills\FactorGraphs\Message; use Moserware\Skills\FactorGraphs\Variable; @@ -40,9 +42,9 @@ class GaussianWeightedSumFactor extends GaussianFactor for($i = 0; $i < $variableWeightsLength; $i++) { - $weight = $variableWeights[$i]; + $weight = &$variableWeights[$i]; $this->_weights[0][$i] = $weight; - $this->_weightsSquared[0][$i] = $weight * $weight; + $this->_weightsSquared[0][$i] = square($weight); } $variablesToSumLength = count($variablesToSum); @@ -123,8 +125,8 @@ class GaussianWeightedSumFactor extends GaussianFactor public function getLogNormalization() { - $vars = $this->getVariables(); - $messages = $this->getMessages(); + $vars = &$this->getVariables(); + $messages = &$this->getMessages(); $result = 0.0; @@ -132,7 +134,7 @@ class GaussianWeightedSumFactor extends GaussianFactor $varCount = count($vars); for ($i = 1; $i < $varCount; $i++) { - $result += GaussianDistribution::logRatioNormalization($vars[i]->getValue(), $messages[i]->getValue()); + $result += GaussianDistribution::logRatioNormalization($vars[$i]->getValue(), $messages[$i]->getValue()); } return $result; @@ -145,7 +147,7 @@ class GaussianWeightedSumFactor extends GaussianFactor // Potentially look at http://mathworld.wolfram.com/NormalSumDistribution.html for clues as // to what it's doing - $messages = $this->getMessages(); + $messages = &$this->getMessages(); $message0 = clone $messages[0]->getValue(); $marginal0 = clone $variables[0]->getValue(); @@ -161,13 +163,13 @@ class GaussianWeightedSumFactor extends GaussianFactor { // These flow directly from the paper - $inverseOfNewPrecisionSum += $weightsSquared[i]/ + $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(); + $anotherInverseOfNewPrecisionSum += $weightsSquared[$i]/$diff->getPrecision(); - $weightedMeanSum += $weights[i] + $weightedMeanSum += $weights[$i] * ($variables[$i + 1]->getValue()->getPrecisionMean() - $messages[$i + 1]->getValue()->getPrecisionMean()) / @@ -199,21 +201,21 @@ class GaussianWeightedSumFactor extends GaussianFactor public function updateMessageIndex($messageIndex) { - $allMessages = $this->getMessages(); - $allVariables = $this->getVariables(); + $allMessages = &$this->getMessages(); + $allVariables = &$this->getVariables(); - Guard::argumentIsValidIndex($messageIndex, count($allMessages),"messageIndex"); + Guard::argumentIsValidIndex($messageIndex, count($allMessages), "messageIndex"); $updatedMessages = array(); $updatedVariables = array(); - $indicesToUse = $this->_variableIndexOrdersForWeights[$messageIndex]; + $indicesToUse = &$this->_variableIndexOrdersForWeights[$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 $allMessagesCount = count($allMessages); - for ($i = 0; i < $allMessagesCount; $i++) + for ($i = 0; $i < $allMessagesCount; $i++) { $updatedMessages[] =$allMessages[$indicesToUse[$i]]; $updatedVariables[] = $allVariables[$indicesToUse[$i]]; diff --git a/PHPSkills/TrueSkill/Factors/GaussianWithinFactor.php b/PHPSkills/TrueSkill/Factors/GaussianWithinFactor.php index 2ad86ea..d16d20c 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianWithinFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianWithinFactor.php @@ -28,11 +28,11 @@ class GaussianWithinFactor extends GaussianFactor public function getLogNormalization() { - $variables = $this->getVariables(); - $marginal = $variables[0]->getValue(); + $variables = &$this->getVariables(); + $marginal = &$variables[0]->getValue(); - $messages = $this->getMessages(); - $message = $messages[0]->getValue(); + $messages = &$this->getMessages(); + $message = &$messages[0]->getValue(); $messageFromVariable = GaussianDistribution::divide($marginal, $message); $mean = $messageFromVariable->getMean(); $std = $messageFromVariable->getStandardDeviation(); diff --git a/PHPSkills/TrueSkill/Layers/IteratedTeamDifferencesInnerLayer.php b/PHPSkills/TrueSkill/Layers/IteratedTeamDifferencesInnerLayer.php index 1ce6f87..9a26388 100644 --- a/PHPSkills/TrueSkill/Layers/IteratedTeamDifferencesInnerLayer.php +++ b/PHPSkills/TrueSkill/Layers/IteratedTeamDifferencesInnerLayer.php @@ -29,7 +29,7 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer public function buildLayer() { - $inputVariablesGroups = $this->getInputVariablesGroups(); + $inputVariablesGroups = &$this->getInputVariablesGroups(); $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->setInputVariablesGroups($inputVariablesGroups); $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->buildLayer(); @@ -58,7 +58,7 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer $totalTeamDifferences = count($this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors()); $totalTeams = $totalTeamDifferences + 1; - $localFactors = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); + $localFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); $innerSchedule = new ScheduleSequence( "inner schedule", array( @@ -72,16 +72,15 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer ) ); - return innerSchedule; + return $innerSchedule; } private function createTwoTeamInnerPriorLoopSchedule() { - $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); - $teamDifferencesComparisonLayerLocalFactors = $this->_TeamDifferencesComparisonLayer->getLocalFactors(); + $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); + $teamDifferencesComparisonLayerLocalFactors = &$this->_TeamDifferencesComparisonLayer->getLocalFactors(); - return $this->scheduleSequence( - array( + $itemsToSequence = array( new ScheduleStep( "send team perf to perf differences", $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0], @@ -90,7 +89,10 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer "send to greater than or within factor", $teamDifferencesComparisonLayerLocalFactors[0], 0) - ), + ); + + return $this->scheduleSequence( + $itemsToSequence, "loop of just two teams inner sequence"); } @@ -102,8 +104,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer for ($i = 0; $i < $totalTeamDifferences - 1; $i++) { - $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); - $teamDifferencesComparisonLayerLocalFactors = $this->_TeamDifferencesComparisonLayer->getLocalFactors(); + $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); + $teamDifferencesComparisonLayerLocalFactors = &$this->_TeamDifferencesComparisonLayer->getLocalFactors(); $currentForwardSchedulePiece = $this->scheduleSequence( @@ -119,7 +121,7 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$i], 2) ), sprintf("current forward schedule piece %d", $i)); - $forwardScheduleList[] = $currentForwardSchedulePiece; + $forwardScheduleList[] = &$currentForwardSchedulePiece; } $forwardSchedule = new ScheduleSequence("forward schedule", $forwardScheduleList); @@ -128,8 +130,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer for ($i = 0; $i < $totalTeamDifferences - 1; $i++) { - $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); - $teamDifferencesComparisonLayerLocalFactors = $this->_TeamDifferencesComparisonLayer->getLocalFactors(); + $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); + $teamDifferencesComparisonLayerLocalFactors = &$this->_TeamDifferencesComparisonLayer->getLocalFactors(); $currentBackwardSchedulePiece = new ScheduleSequence( "current backward schedule piece", @@ -144,7 +146,7 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1", $i), $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i], 1) )); - $backwardScheduleList[] = $currentBackwardSchedulePiece; + $backwardScheduleList[] = &$currentBackwardSchedulePiece; } $backwardSchedule = new ScheduleSequence("backward schedule", $backwardScheduleList); diff --git a/PHPSkills/TrueSkill/Layers/TeamDifferencesComparisonLayer.php b/PHPSkills/TrueSkill/Layers/TeamDifferencesComparisonLayer.php index fc7384c..8cf2993 100644 --- a/PHPSkills/TrueSkill/Layers/TeamDifferencesComparisonLayer.php +++ b/PHPSkills/TrueSkill/Layers/TeamDifferencesComparisonLayer.php @@ -27,7 +27,7 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer public function buildLayer() { - $inputVarGroups = $this->getInputVariablesGroups(); + $inputVarGroups = &$this->getInputVariablesGroups(); $inputVarGroupsCount = count($inputVarGroups); for ($i = 0; $i < $inputVarGroupsCount; $i++) diff --git a/PHPSkills/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php b/PHPSkills/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php index 75803ae..78753a7 100644 --- a/PHPSkills/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php +++ b/PHPSkills/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php @@ -28,13 +28,13 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG $strongerTeam = $inputVariablesGroups[$i][0]; $weakerTeam = $inputVariablesGroups[$i + 1][0]; - $currentDifference = $this->createOutputVariable(); - $newDifferencesFactor = $this->createTeamPerformanceToDifferenceFactor($strongerTeam, $weakerTeam, $currentDifference); + $currentDifference = &$this->createOutputVariable(); + $newDifferencesFactor = &$this->createTeamPerformanceToDifferenceFactor($strongerTeam, $weakerTeam, $currentDifference); $this->addLayerFactor($newDifferencesFactor); // REVIEW: Does it make sense to have groups of one? - $outputVariablesGroup = $this->getOutputVariablesGroups(); - $outputVariablesGroup[] = $currentDifference; + $outputVariablesGroup = &$this->getOutputVariablesGroups(); + $outputVariablesGroup[] = array($currentDifference); } } diff --git a/PHPSkills/TrueSkill/TrueSkillFactorGraph.php b/PHPSkills/TrueSkill/TrueSkillFactorGraph.php index 640c23c..dbff5fe 100644 --- a/PHPSkills/TrueSkill/TrueSkillFactorGraph.php +++ b/PHPSkills/TrueSkill/TrueSkillFactorGraph.php @@ -107,10 +107,10 @@ class TrueSkillFactorGraph extends FactorGraph foreach ($this->_layers as $currentLayer) { - $currentPriorSchedule = $currentLayer->createPriorSchedule(); + $currentPriorSchedule = &$currentLayer->createPriorSchedule(); if ($currentPriorSchedule != null) { - $fullSchedule[] = $currentPriorSchedule; + $fullSchedule[] = &$currentPriorSchedule; } }