More debugging and realizing how PHP does references

This commit is contained in:
Jeff Moser
2010-09-25 15:46:23 -04:00
parent e8d444e7da
commit 086d94865f
21 changed files with 77 additions and 72 deletions

View File

@ -40,7 +40,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
parent::__construct(SkillCalculatorSupportedOptions::PARTIAL_PLAY | SkillCalculatorSupportedOptions::PARTIAL_UPDATE, TeamsRange::atLeast(2), PlayersRange::atLeast(1));
}
public function calculateNewRatings(GameInfo $gameInfo,
public function calculateNewRatings(GameInfo &$gameInfo,
array $teams,
array $teamRanks)
{
@ -58,8 +58,8 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
return $factorGraph->getUpdatedRatings();
}
public function calculateMatchQuality(GameInfo $gameInfo,
array $teams)
public function calculateMatchQuality(GameInfo &$gameInfo,
array &$teams)
{
// We need to create the A matrix which is the player team assigments.
$teamAssignmentsList = $teams;
@ -100,7 +100,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
return $result;
}
private static function getPlayerMeansVector(array $teamAssignmentsList)
private static function getPlayerMeansVector(array &$teamAssignmentsList)
{
// A simple vector of all the player means.
return new Vector($this->getPlayerRatingValues($teamAssignmentsList,
@ -110,7 +110,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
}));
}
private static function getPlayerCovarianceMatrix(array $teamAssignmentsList)
private static function getPlayerCovarianceMatrix(array &$teamAssignmentsList)
{
// This is a square matrix whose diagonal values represent the variance (square of standard deviation) of all
// players.
@ -123,7 +123,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
}
// Helper function that gets a list of values for all player ratings
private static function getPlayerRatingValues(array $teamAssignmentsList,
private static function getPlayerRatingValues(array &$teamAssignmentsList,
$playerRatingFunction)
{
$playerRatingValues = array();
@ -139,7 +139,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
return $playerRatingValues;
}
private static function createPlayerTeamAssignmentMatrix($teamAssignmentsList, $totalPlayers)
private static function createPlayerTeamAssignmentMatrix(&$teamAssignmentsList, &$totalPlayers)
{
// The team assignment matrix is often referred to as the "A" matrix. It's a matrix whose rows represent the players
// and the columns represent teams. At Matrix[row, column] represents that player[row] is on team[col]

View File

@ -20,7 +20,7 @@ abstract class GaussianFactor extends Factor
}
/// Sends the factor-graph message with and returns the log-normalization constant
protected function sendMessageVariable(Message $message, Variable $variable)
protected function sendMessageVariable(Message &$message, Variable &$variable)
{
$marginal = $variable->getValue();
$messageValue = $message->getValue();
@ -29,7 +29,7 @@ abstract class GaussianFactor extends Factor
return $logZ;
}
public function createVariableToMessageBinding(Variable $variable)
public function createVariableToMessageBinding(Variable &$variable)
{
return parent::createVariableToMessageBinding($variable,
new Message(

View File

@ -20,7 +20,7 @@ class GaussianGreaterThanFactor extends GaussianFactor
{
private $_epsilon;
public function __construct($epsilon, Variable $variable)
public function __construct($epsilon, Variable &$variable)
{
parent::_construct("{0} > {1:0.000}");
$this->_epsilon = $epsilon;
@ -42,7 +42,7 @@ class GaussianGreaterThanFactor extends GaussianFactor
}
protected function updateMessageVariable(Message $message, Variable $variable)
protected function updateMessageVariable(Message &$message, Variable &$variable)
{
$oldMarginal = clone $variable->getValue();
$oldMessage = clone $message->getValue();

View File

@ -18,7 +18,7 @@ class GaussianLikelihoodFactor extends GaussianFactor
{
private $_precision;
public function __construct($betaSquared, Variable $variable1, Variable $variable2)
public function __construct($betaSquared, Variable &$variable1, Variable &$variable2)
{
parent::__construct("Likelihood of {0} going to {1}");
$this->_precision = 1.0/$betaSquared;
@ -36,8 +36,8 @@ class GaussianLikelihoodFactor extends GaussianFactor
$messages[0]->getValue());
}
private function updateHelper(Message $message1, Message $message2,
Variable $variable1, Variable $variable2)
private function updateHelper(Message &$message1, Message &$message2,
Variable &$variable1, Variable &$variable2)
{
$message1Value = clone $message1->getValue();
$message2Value = clone $message2->getValue();

View File

@ -19,7 +19,7 @@ class GaussianPriorFactor extends GaussianFactor
{
private $_newMessage;
public function __construct($mean, $variance, Variable $variable)
public function __construct($mean, $variance, Variable &$variable)
{
parent::__construct("Prior value going to {0}");
$this->_newMessage = new GaussianDistribution($mean, sqrt($variance));
@ -27,10 +27,10 @@ class GaussianPriorFactor extends GaussianFactor
new Message(
GaussianDistribution::fromPrecisionMean(0, 0),
"message from {0} to {1}",
this, variable));
$this, variable));
}
protected function updateMessageVariable(Message $message, Variable $variable)
protected function updateMessageVariable(Message &$message, Variable &$variable)
{
$oldMarginal = clone $variable->getValue();
$oldMessage = $message;

View File

@ -23,7 +23,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
private $_weights;
private $_weightsSquared;
public function __construct(Variable $sumVariable, array $variablesToSum, array $variableWeights = null)
public function __construct(Variable &$sumVariable, array &$variablesToSum, array &$variableWeights = null)
{
parent::__construct($this->createName($sumVariable, $variablesToSum, $variableWeights));
$this->_weights = array();
@ -133,9 +133,9 @@ class GaussianWeightedSumFactor extends GaussianFactor
return $result;
}
private function updateHelper(array $weights, array $weightsSquared,
array $messages,
array $variables)
private function updateHelper(array &$weights, array &$weightsSquared,
array &$messages,
array &$variables)
{
// Potentially look at http://mathworld.wolfram.com/NormalSumDistribution.html for clues as
// to what it's doing

View File

@ -20,7 +20,7 @@ class GaussianWithinFactor extends GaussianFactor
{
private $_epsilon;
public function __construct($epsilon, Variable $variable)
public function __construct($epsilon, Variable &$variable)
{
$this->_epsilon = $epsilon;
$this->createVariableToMessageBinding($variable);
@ -43,7 +43,7 @@ class GaussianWithinFactor extends GaussianFactor
return -GaussianDistribution::logProductNormalization($messageFromVariable, $message) + log($z);
}
protected function updateMessage(Message $message, Variable $variable)
protected function updateMessage(Message &$message, Variable &$variable)
{
$oldMarginal = clone $variable->getValue();
$oldMessage = clone $message->getValue();

View File

@ -18,9 +18,9 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
private $_TeamDifferencesComparisonLayer;
private $_TeamPerformancesToTeamPerformanceDifferencesLayer;
public function __construct(TrueSkillFactorGraph $parentGraph,
TeamPerformancesToTeamPerformanceDifferencesLayer $teamPerformancesToPerformanceDifferences,
TeamDifferencesComparisonLayer $teamDifferencesComparisonLayer)
public function __construct(TrueSkillFactorGraph &$parentGraph,
TeamPerformancesToTeamPerformanceDifferencesLayer &$teamPerformancesToPerformanceDifferences,
TeamDifferencesComparisonLayer &$teamDifferencesComparisonLayer)
{
parent::__construct($parentGraph);
$this->_TeamPerformancesToTeamPerformanceDifferencesLayer = $teamPerformancesToPerformanceDifferences;

View File

@ -14,7 +14,7 @@ use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLayer
{
public function __construct(TrueSkillFactorGraph $parentGraph)
public function __construct(TrueSkillFactorGraph &$parentGraph)
{
parent::__construct($parentGraph);
}
@ -44,7 +44,7 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
"all player perf to team perf schedule");
}
protected function createPlayerToTeamSumFactor($teamMembers, $sumVariable)
protected function createPlayerToTeamSumFactor(&$teamMembers, &$sumVariable)
{
return new GaussianWeightedSumFactor(
$sumVariable,
@ -74,7 +74,7 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
return $this->scheduleSequence($allFactors, "all of the team's sum iterations");
}
private function createOutputVariable($team)
private function createOutputVariable(&$team)
{
///$teamMemberNames = String.Join(", ", team.Select(teamMember => teamMember.Key.ToString()).ToArray());
$teamMemberNames = "TODO";

View File

@ -19,7 +19,7 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
{
private $_teams;
public function __construct(TrueSkillFactorGraph $parentGraph, $teams)
public function __construct(TrueSkillFactorGraph &$parentGraph, &$teams)
{
parent::__construct($parentGraph);
$this->_teams = $teams;
@ -31,10 +31,11 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
{
$currentTeamSkills = array();
foreach ($currentTeam as $currentTeamPlayer)
foreach ($currentTeam->getAllPlayers() as $currentTeamPlayer)
{
$playerSkill = $this->createSkillOutputVariable($currentTeamPlayer.Key);
$this->addLayerFactor($this->createPriorFactor($currentTeamPlayer.Key, $currentTeamPlayer.Value, $playerSkill));
$currentTeamPlayerRating = $currentTeam->getRating($currentTeamPlayer);
$playerSkill = $this->createSkillOutputVariable($currentTeamPlayer);
$this->addLayerFactor($this->createPriorFactor($currentTeamPlayer, $currentTeamPlayerRating, $playerSkill));
$currentTeamSkills[] = $playerSkill;
}
@ -55,7 +56,7 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
"All priors");
}
private function createPriorFactor($player, $priorRating, $skillsVariable)
private function createPriorFactor(&$player, &$priorRating, &$skillsVariable)
{
return new GaussianPriorFactor($priorRating->getMean(),
square($priorRating->getStandardDeviation()) +
@ -65,7 +66,9 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
private function createSkillOutputVariable($key)
{
return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, "{0}'s skill", $key);
$parentFactorGraph = $this->getParentFactorGraph();
$variableFactory = $parentFactorGraph->getVariableFactory();
return $variableFactory->createKeyedVariable($key, "{0}'s skill", $key);
}
}

View File

@ -13,7 +13,7 @@ use Moserware\Skills\TrueSkill\Factors\GaussianLikelihoodFactor;
class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
{
public function __construct(TrueSkillFactorGraph $parentGraph)
public function __construct(TrueSkillFactorGraph &$parentGraph)
{
parent::__construct($parentGraph);
}
@ -36,12 +36,12 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
}
}
private function createLikelihood($playerSkill, $playerPerformance)
private function createLikelihood(&$playerSkill, &$playerPerformance)
{
return new GaussianLikelihoodFactor(square($this->getParentFactorGraph()->getGameInfo()->getBeta()), $playerPerformance, $playerSkill);
}
private function createOutputVariable($key)
private function createOutputVariable(&$key)
{
return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, "{0}'s performance", $key);
}

View File

@ -17,7 +17,7 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer
private $_epsilon;
private $_teamRanks;
public function __construct(TrueSkillFactorGraph $parentGraph, array $teamRanks)
public function __construct(TrueSkillFactorGraph &$parentGraph, array &$teamRanks)
{
parent::__construct($parentGraph);
$this->_teamRanks = $teamRanks;

View File

@ -13,7 +13,7 @@ use Moserware\Skills\TrueSkill\Factors\GaussianWeightedSumFactor;
class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorGraphLayer
{
public function __construct(TrueSkillFactorGraph $parentGraph)
public function __construct(TrueSkillFactorGraph &$parentGraph)
{
parent::__construct($parentGraph);
}
@ -38,7 +38,7 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG
}
private function createTeamPerformanceToDifferenceFactor(
Variable $strongerTeam, Variable $weakerTeam, Variable $output)
Variable &$strongerTeam, Variable &$weakerTeam, Variable &$output)
{
return new GaussianWeightedSumFactor($output, array($strongerTeam, $weakerTeam), array(1.0, -1.0));
}

View File

@ -9,7 +9,7 @@ use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
abstract class TrueSkillFactorGraphLayer extends FactorGraphLayer
{
public function __construct(TrueSkillFactorGraph $parentGraph)
public function __construct(TrueSkillFactorGraph &$parentGraph)
{
parent::__construct($parentGraph);
}

View File

@ -35,15 +35,16 @@ class TrueSkillFactorGraph extends FactorGraph
private $_priorLayer;
private $_variableFactory;
public function __construct(GameInfo $gameInfo, $teams, array $teamRanks)
public function __construct(GameInfo &$gameInfo, &$teams, array $teamRanks)
{
$this->_priorLayer = new PlayerPriorValuesToSkillsLayer($this, $teams);
$this->_gameInfo = $gameInfo;
$this->_variableFactory = new VariableFactory(
$newFactory = new VariableFactory(
function()
{
return GaussianDistribution::fromPrecisionMean(0, 0);
});
$this->setVariableFactory($newFactory);
$this->_layers = array(
$this->_priorLayer,