From fa10d276d6121f390b930b655a66edd9376e114e Mon Sep 17 00:00:00 2001 From: Jeff Moser Date: Sat, 25 Sep 2010 18:25:56 -0400 Subject: [PATCH] Tenative pass at getting prior layer working. Found out about division by zero and closure differences. --- PHPSkills/FactorGraphs/Factor.php | 9 ++++----- PHPSkills/FactorGraphs/FactorGraphLayer.php | 2 +- PHPSkills/FactorGraphs/VariableFactory.php | 5 +++-- PHPSkills/HashMap.php | 2 ++ PHPSkills/Numerics/GaussianDistribution.php | 16 +++++++++++++--- PHPSkills/RatingContainer.php | 2 +- PHPSkills/TrueSkill/Factors/GaussianFactor.php | 2 +- .../TrueSkill/Factors/GaussianPriorFactor.php | 8 ++++---- .../Layers/PlayerPriorValuesToSkillsLayer.php | 5 +++-- .../Layers/PlayerSkillsToPerformancesLayer.php | 2 +- PHPSkills/TrueSkill/TrueSkillFactorGraph.php | 6 +++--- 11 files changed, 36 insertions(+), 23 deletions(-) diff --git a/PHPSkills/FactorGraphs/Factor.php b/PHPSkills/FactorGraphs/Factor.php index c421b4f..bbb64fc 100644 --- a/PHPSkills/FactorGraphs/Factor.php +++ b/PHPSkills/FactorGraphs/Factor.php @@ -9,7 +9,6 @@ require_once(dirname(__FILE__) . "/Variable.php"); use Moserware\Skills\Guard; use Moserware\Skills\HashMap; - abstract class Factor { private $_messages = array(); @@ -21,7 +20,7 @@ abstract class Factor protected function __construct($name) { $this->_name = "Factor[" . $name . "]"; - $this->_messagesToVariableBinding = new HashMap(); + $this->_messageToVariableBinding = new HashMap(); } /// Returns the log-normalization constant of that factor @@ -81,12 +80,12 @@ abstract class Factor public abstract function createVariableToMessageBinding(Variable &$variable); - protected function createVariableToMessageBindingWithMessage(Variable &$variable, Variable &$message) + protected function createVariableToMessageBindingWithMessage(Variable &$variable, Message &$message) { $index = count($this->_messages); - $this->_messages[] = $message; + $this->_messages[] = $message; $this->_messageToVariableBinding->setValue($message, $variable); - $this->_variables[] = $variable; + $this->_variables[] = &$variable; return $message; } diff --git a/PHPSkills/FactorGraphs/FactorGraphLayer.php b/PHPSkills/FactorGraphs/FactorGraphLayer.php index 26558a6..7d025ad 100644 --- a/PHPSkills/FactorGraphs/FactorGraphLayer.php +++ b/PHPSkills/FactorGraphs/FactorGraphLayer.php @@ -38,7 +38,7 @@ abstract class FactorGraphLayer return $this->_localFactors; } - public function &setInputVariablesGroups(&$value) + public function setInputVariablesGroups(&$value) { $this->_inputVariablesGroups = $value; } diff --git a/PHPSkills/FactorGraphs/VariableFactory.php b/PHPSkills/FactorGraphs/VariableFactory.php index 37146d8..79226ab 100644 --- a/PHPSkills/FactorGraphs/VariableFactory.php +++ b/PHPSkills/FactorGraphs/VariableFactory.php @@ -11,7 +11,7 @@ class VariableFactory public function __construct($variablePriorInitializer) { - $this->_variablePriorInitializer = $variablePriorInitializer; + $this->_variablePriorInitializer = &$variablePriorInitializer; } public function createBasicVariable() @@ -22,7 +22,8 @@ class VariableFactory public function createKeyedVariable($key) { - $newVar = new KeyedVariable($key, $this->_variablePriorInitializer()); + $initializer = $this->_variablePriorInitializer; + $newVar = new KeyedVariable($key, "key variable", $initializer()); return $newVar; } } diff --git a/PHPSkills/HashMap.php b/PHPSkills/HashMap.php index 1669a0a..dc4f3c1 100644 --- a/PHPSkills/HashMap.php +++ b/PHPSkills/HashMap.php @@ -1,5 +1,7 @@ _precision = $precision; $result->_precisionMean = $precisionMean; - $result->_variance = 1.0/$precision; - $result->_standardDeviation = sqrt($result->_variance); - $result->_mean = $result->_precisionMean/$result->_precision; + + if($precision != 0) + { + $result->_variance = 1.0/$precision; + $result->_standardDeviation = sqrt($result->_variance); + $result->_mean = $result->_precisionMean/$result->_precision; + } + else + { + $result->_variance = \INF; + $result->_standardDeviation = \INF; + $result->_mean = \NAN; + } return $result; } diff --git a/PHPSkills/RatingContainer.php b/PHPSkills/RatingContainer.php index 6b2a117..456712a 100644 --- a/PHPSkills/RatingContainer.php +++ b/PHPSkills/RatingContainer.php @@ -9,7 +9,7 @@ class RatingContainer public function __construct() { - $this->_playerToRating = new \HashMap(); + $this->_playerToRating = new HashMap(); } public function getRating($player) diff --git a/PHPSkills/TrueSkill/Factors/GaussianFactor.php b/PHPSkills/TrueSkill/Factors/GaussianFactor.php index a525a05..1069c62 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianFactor.php @@ -31,7 +31,7 @@ abstract class GaussianFactor extends Factor public function createVariableToMessageBinding(Variable &$variable) { - return parent::createVariableToMessageBinding($variable, + return parent::createVariableToMessageBindingWithMessage($variable, new Message( GaussianDistribution::fromPrecisionMean(0, 0), "message from {0} to {1}", $this)); diff --git a/PHPSkills/TrueSkill/Factors/GaussianPriorFactor.php b/PHPSkills/TrueSkill/Factors/GaussianPriorFactor.php index 445845b..1bb0c0a 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianPriorFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianPriorFactor.php @@ -23,11 +23,11 @@ class GaussianPriorFactor extends GaussianFactor { parent::__construct("Prior value going to {0}"); $this->_newMessage = new GaussianDistribution($mean, sqrt($variance)); - $this->createVariableToMessageBinding($variable, - new Message( - GaussianDistribution::fromPrecisionMean(0, 0), + $newMessage = new Message(GaussianDistribution::fromPrecisionMean(0, 0), "message from {0} to {1}", - $this, variable)); + $this, $variable); + + $this->createVariableToMessageBindingWithMessage($variable, $newMessage); } protected function updateMessageVariable(Message &$message, Variable &$variable) diff --git a/PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php b/PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php index 4de1faf..ef4db9c 100644 --- a/PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php +++ b/PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php @@ -35,11 +35,12 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer { $currentTeamPlayerRating = $currentTeam->getRating($currentTeamPlayer); $playerSkill = $this->createSkillOutputVariable($currentTeamPlayer); - $this->addLayerFactor($this->createPriorFactor($currentTeamPlayer, $currentTeamPlayerRating, $playerSkill)); + $priorFactor = $this->createPriorFactor($currentTeamPlayer, $currentTeamPlayerRating, $playerSkill); + $this->addLayerFactor($priorFactor); $currentTeamSkills[] = $playerSkill; } - $outputVariablesGroups = $this->getOutputVariablesGroups(); + $outputVariablesGroups = &$this->getOutputVariablesGroups(); $outputVariablesGroups[] = $currentTeamSkills; } } diff --git a/PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php b/PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php index 72ce887..6318d46 100644 --- a/PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php +++ b/PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php @@ -52,7 +52,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer array_map( function($likelihood) { - return $this->scheduleStep("Skill to Perf step", $likelihood, 0); + return new ScheduleStep("Skill to Perf step", $likelihood, 0); }, $this->getLocalFactors()), "All skill to performance sending"); diff --git a/PHPSkills/TrueSkill/TrueSkillFactorGraph.php b/PHPSkills/TrueSkill/TrueSkillFactorGraph.php index ef217b7..f484aea 100644 --- a/PHPSkills/TrueSkill/TrueSkillFactorGraph.php +++ b/PHPSkills/TrueSkill/TrueSkillFactorGraph.php @@ -15,6 +15,7 @@ require_once(dirname(__FILE__) . '/Layers/PlayerSkillsToPerformancesLayer.php'); require_once(dirname(__FILE__) . '/Layers/TeamDifferencesComparisonLayer.php'); require_once(dirname(__FILE__) . '/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php'); +use Moserware\Numerics\GaussianDistribution; use Moserware\Skills\GameInfo; use Moserware\Skills\Rating; use Moserware\Skills\FactorGraphs\FactorGraph; @@ -32,8 +33,7 @@ class TrueSkillFactorGraph extends FactorGraph { private $_gameInfo; private $_layers; - private $_priorLayer; - private $_variableFactory; + private $_priorLayer; public function __construct(GameInfo &$gameInfo, &$teams, array $teamRanks) { @@ -44,8 +44,8 @@ class TrueSkillFactorGraph extends FactorGraph { return GaussianDistribution::fromPrecisionMean(0, 0); }); + $this->setVariableFactory($newFactory); - $this->_layers = array( $this->_priorLayer, new PlayerSkillsToPerformancesLayer($this),