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

@ -36,12 +36,12 @@ abstract class Factor
return count($this->_messages);
}
protected function getVariables()
protected function &getVariables()
{
return $this->_variables;
}
protected function getMessages()
protected function &getMessages()
{
return $this->_messages;
}
@ -77,11 +77,11 @@ abstract class Factor
return $this->sendMessageVariable($message, $variable);
}
protected abstract function sendMessageVariable(Message $message, Variable $variable);
protected abstract function sendMessageVariable(Message &$message, Variable &$variable);
public abstract function createVariableToMessageBinding(Variable $variable);
public abstract function createVariableToMessageBinding(Variable &$variable);
protected function createVariableToMessageBindingWithMessage(Variable $variable, Variable $message)
protected function createVariableToMessageBindingWithMessage(Variable &$variable, Variable &$message)
{
$index = count($this->_messages);
$this->_messages[] = $message;

@ -5,14 +5,14 @@ class FactorGraph
{
private $_variableFactory;
public function getVariableFactory()
public function &getVariableFactory()
{
return $this->_variableFactory;
}
public function setVariableFactory($factory)
public function setVariableFactory(&$factory)
{
$this->_variableFactory = $factory;
$this->_variableFactory = &$factory;
}
}
?>

@ -1,6 +1,7 @@
<?php
namespace Moserware\Skills\FactorGraphs;
require_once(dirname(__FILE__) . "/FactorGraph.php");
require_once(dirname(__FILE__) . "/Schedule.php");
abstract class FactorGraphLayer
@ -10,44 +11,44 @@ abstract class FactorGraphLayer
private $_inputVariablesGroups = array();
private $_parentFactorGraph;
protected function __construct($parentGraph)
protected function __construct(FactorGraph &$parentGraph)
{
$this->_parentFactorGraph = $parentGraph;
$this->_parentFactorGraph = &$parentGraph;
}
protected function getInputVariablesGroups()
protected function &getInputVariablesGroups()
{
return $this->_inputVariablesGroups;
}
// HACK
public function getParentFactorGraph()
public function &getParentFactorGraph()
{
return $this->_parentFactorGraph;
}
public function getOutputVariablesGroups()
public function &getOutputVariablesGroups()
{
return $this->_outputVariablesGroups;
}
public function getLocalFactors()
public function &getLocalFactors()
{
return $this->_localFactors;
}
public function setInputVariablesGroups($value)
public function &setInputVariablesGroups(&$value)
{
$this->_inputVariablesGroups = $value;
}
protected function scheduleSequence($itemsToSequence)
protected function scheduleSequence(&$itemsToSequence)
{
return new ScheduleSequence("TODO", $itemsToSequence);
}
protected function addLayerFactor($factor)
protected function addLayerFactor(&$factor)
{
$this->_localFactors[] = $factor;
}

@ -49,7 +49,7 @@ class FactorList
return count($this->_list);
}
public function addFactor(Factor $factor)
public function addFactor(Factor &$factor)
{
$this->_list[] = $factor;
return $factor;

@ -14,12 +14,12 @@ class Variable
$this->resetToPrior();
}
public function getValue()
public function &getValue()
{
return $this->_value;
}
public function setValue($value)
public function setValue(&$value)
{
$this->_value = $value;
}

@ -14,7 +14,7 @@ abstract class SkillCalculator
private $_playersPerTeamAllowed;
private $_totalTeamsAllowed;
protected function __construct($supportedOptions, TeamsRange $totalTeamsAllowed, PlayersRange $playerPerTeamAllowed)
protected function __construct($supportedOptions, TeamsRange &$totalTeamsAllowed, PlayersRange &$playerPerTeamAllowed)
{
$this->_supportedOptions = $supportedOptions;
$this->_totalTeamsAllowed = $totalTeamsAllowed;
@ -29,7 +29,7 @@ abstract class SkillCalculator
/// <param name="teams">A mapping of team players and their ratings.</param>
/// <param name="teamRanks">The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2)</param>
/// <returns>All the players and their new ratings.</returns>
public abstract function calculateNewRatings(GameInfo $gameInfo,
public abstract function calculateNewRatings(GameInfo &$gameInfo,
array $teamsOfPlayerToRatings,
array $teamRanks);
@ -40,23 +40,23 @@ abstract class SkillCalculator
/// <param name="gameInfo">Parameters for the game.</param>
/// <param name="teams">A mapping of team players and their ratings.</param>
/// <returns>The quality of the match between the teams as a percentage (0% = bad, 100% = well matched).</returns>
public abstract function calculateMatchQuality(GameInfo $gameInfo,
array $teamsOfPlayerToRatings);
public abstract function calculateMatchQuality(GameInfo &$gameInfo,
array &$teamsOfPlayerToRatings);
public function isSupported($option)
{
return ($this->_supportedOptions & $option) == $option;
}
protected function validateTeamCountAndPlayersCountPerTeam(array $teamsOfPlayerToRatings)
protected function validateTeamCountAndPlayersCountPerTeam(array &$teamsOfPlayerToRatings)
{
self::validateTeamCountAndPlayersCountPerTeamWithRanges($teamsOfPlayerToRatings, $this->_totalTeamsAllowed, $this->_playersPerTeamAllowed);
}
private static function validateTeamCountAndPlayersCountPerTeamWithRanges(
array $teams,
TeamsRange $totalTeams,
PlayersRange $playersPerTeam)
array &$teams,
TeamsRange &$totalTeams,
PlayersRange &$playersPerTeam)
{
$countOfTeams = 0;

@ -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]

@ -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(

@ -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();

@ -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();

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

@ -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

@ -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();

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

@ -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";

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

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

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

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

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

@ -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,