Browse files

Tenative pass at getting prior layer working. Found out about divisio…

…n by zero and closure differences.
  • Loading branch information...
1 parent 086d948 commit fa10d276d6121f390b930b655a66edd9376e114e @moserware committed Sep 25, 2010
View
9 PHPSkills/FactorGraphs/Factor.php
@@ -9,7 +9,6 @@
use Moserware\Skills\Guard;
use Moserware\Skills\HashMap;
-
abstract class Factor
{
private $_messages = array();
@@ -21,7 +20,7 @@
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 @@ public function sendMessageIndex($messageIndex)
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;
}
View
2 PHPSkills/FactorGraphs/FactorGraphLayer.php
@@ -38,7 +38,7 @@ public function &getLocalFactors()
return $this->_localFactors;
}
- public function &setInputVariablesGroups(&$value)
+ public function setInputVariablesGroups(&$value)
{
$this->_inputVariablesGroups = $value;
}
View
5 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 @@ public function createBasicVariable()
public function createKeyedVariable($key)
{
- $newVar = new KeyedVariable($key, $this->_variablePriorInitializer());
+ $initializer = $this->_variablePriorInitializer;
+ $newVar = new KeyedVariable($key, "key variable", $initializer());
return $newVar;
}
}
View
2 PHPSkills/HashMap.php
@@ -1,5 +1,7 @@
<?php
+namespace Moserware\Skills;
+
class HashMap
{
private $_hashToValue = array();
View
16 PHPSkills/Numerics/GaussianDistribution.php
@@ -81,9 +81,19 @@ public static function fromPrecisionMean($precisionMean, $precision)
$result = new GaussianDistribution();
$result->_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;
}
View
2 PHPSkills/RatingContainer.php
@@ -9,7 +9,7 @@ class RatingContainer
public function __construct()
{
- $this->_playerToRating = new \HashMap();
+ $this->_playerToRating = new HashMap();
}
public function getRating($player)
View
2 PHPSkills/TrueSkill/Factors/GaussianFactor.php
@@ -31,7 +31,7 @@ protected function sendMessageVariable(Message &$message, Variable &$variable)
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));
View
8 PHPSkills/TrueSkill/Factors/GaussianPriorFactor.php
@@ -23,11 +23,11 @@ public function __construct($mean, $variance, Variable &$variable)
{
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)
View
5 PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php
@@ -35,11 +35,12 @@ public function buildLayer()
{
$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;
}
}
View
2 PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php
@@ -52,7 +52,7 @@ public function createPriorSchedule()
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");
View
6 PHPSkills/TrueSkill/TrueSkillFactorGraph.php
@@ -15,6 +15,7 @@
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 @@ function()
{
return GaussianDistribution::fromPrecisionMean(0, 0);
});
+
$this->setVariableFactory($newFactory);
-
$this->_layers = array(
$this->_priorLayer,
new PlayerSkillsToPerformancesLayer($this),

0 comments on commit fa10d27

Please sign in to comment.