Skip to content

Commit

Permalink
Tenative pass at getting prior layer working. Found out about divisio…
Browse files Browse the repository at this point in the history
…n by zero and closure differences.
  • Loading branch information
moserware committed Sep 25, 2010
1 parent 086d948 commit fa10d27
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 23 deletions.
9 changes: 4 additions & 5 deletions PHPSkills/FactorGraphs/Factor.php
Expand Up @@ -9,7 +9,6 @@
use Moserware\Skills\Guard;
use Moserware\Skills\HashMap;


abstract class Factor
{
private $_messages = array();
Expand All @@ -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
Expand Down Expand Up @@ -81,12 +80,12 @@ protected abstract function sendMessageVariable(Message &$message, Variable &$va

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;
}

Expand Down
2 changes: 1 addition & 1 deletion PHPSkills/FactorGraphs/FactorGraphLayer.php
Expand Up @@ -38,7 +38,7 @@ public function &getLocalFactors()
return $this->_localFactors;
}

public function &setInputVariablesGroups(&$value)
public function setInputVariablesGroups(&$value)
{
$this->_inputVariablesGroups = $value;
}
Expand Down
5 changes: 3 additions & 2 deletions PHPSkills/FactorGraphs/VariableFactory.php
Expand Up @@ -11,7 +11,7 @@ class VariableFactory

public function __construct($variablePriorInitializer)
{
$this->_variablePriorInitializer = $variablePriorInitializer;
$this->_variablePriorInitializer = &$variablePriorInitializer;
}

public function createBasicVariable()
Expand All @@ -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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions PHPSkills/HashMap.php
@@ -1,5 +1,7 @@
<?php

namespace Moserware\Skills;

class HashMap
{
private $_hashToValue = array();
Expand Down
16 changes: 13 additions & 3 deletions PHPSkills/Numerics/GaussianDistribution.php
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion PHPSkills/RatingContainer.php
Expand Up @@ -9,7 +9,7 @@ class RatingContainer

public function __construct()
{
$this->_playerToRating = new \HashMap();
$this->_playerToRating = new HashMap();
}

public function getRating($player)
Expand Down
2 changes: 1 addition & 1 deletion PHPSkills/TrueSkill/Factors/GaussianFactor.php
Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions PHPSkills/TrueSkill/Factors/GaussianPriorFactor.php
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php
Expand Up @@ -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;
}
}
Expand Down
Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions PHPSkills/TrueSkill/TrueSkillFactorGraph.php
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -44,8 +44,8 @@ function()
{
return GaussianDistribution::fromPrecisionMean(0, 0);
});

$this->setVariableFactory($newFactory);

$this->_layers = array(
$this->_priorLayer,
new PlayerSkillsToPerformancesLayer($this),
Expand Down

0 comments on commit fa10d27

Please sign in to comment.