diff --git a/PHPSkills/Elo/FideEloCalculator.php b/PHPSkills/Elo/FideEloCalculator.php index da3cbdf..adc2fd9 100644 --- a/PHPSkills/Elo/FideEloCalculator.php +++ b/PHPSkills/Elo/FideEloCalculator.php @@ -5,7 +5,7 @@ namespace Moserware\Skills\Elo; require_once(dirname(__FILE__) . "/TwoPlayerEloCalculator.php"); require_once(dirname(__FILE__) . "/FideKFactor.php"); -/** Including ELO's scheme as a simple comparison. +/** Including Elo's scheme as a simple comparison. * See http://en.wikipedia.org/wiki/Elo_rating_system#Theory * for more details */ diff --git a/PHPSkills/FactorGraphs/Factor.php b/PHPSkills/FactorGraphs/Factor.php index 427c517..f7e3157 100644 --- a/PHPSkills/FactorGraphs/Factor.php +++ b/PHPSkills/FactorGraphs/Factor.php @@ -50,7 +50,7 @@ abstract class Factor { Guard::argumentIsValidIndex($messageIndex, count($this->_messages), "messageIndex"); $message = &$this->_messages[$messageIndex]; - $variable = &$this->_messageToVariableBinding->getValue($this->_messages[$messageIndex]); + $variable = &$this->_messageToVariableBinding->getValue($message); return $this->updateMessageVariable($message, $variable); } @@ -62,7 +62,8 @@ abstract class Factor /// Resets the marginal of the variables a factor is connected to public function resetMarginals() { - foreach ($this->_messageToVariableBinding->getAllValues() as $currentVariable) + $allValues = &$this->_messageToVariableBinding->getAllValues(); + foreach ($allValues as $currentVariable) { $currentVariable->resetToPrior(); } @@ -73,21 +74,21 @@ abstract class Factor { Guard::argumentIsValidIndex($messageIndex, count($this->_messages), "messageIndex"); - $message = $this->_messages[$messageIndex]; - $variable = $this->_messageToVariableBinding->getValue($message); + $message = &$this->_messages[$messageIndex]; + $variable = &$this->_messageToVariableBinding->getValue($message); return $this->sendMessageVariable($message, $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, Message &$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/FactorGraph.php b/PHPSkills/FactorGraphs/FactorGraph.php index b8fe431..3685f1f 100644 --- a/PHPSkills/FactorGraphs/FactorGraph.php +++ b/PHPSkills/FactorGraphs/FactorGraph.php @@ -1,16 +1,19 @@ _variableFactory; + $factory = &$this->_variableFactory; + return $factory; } - public function setVariableFactory(&$factory) + public function setVariableFactory(VariableFactory &$factory) { $this->_variableFactory = &$factory; } diff --git a/PHPSkills/FactorGraphs/FactorGraphLayer.php b/PHPSkills/FactorGraphs/FactorGraphLayer.php index ec0e93a..74036b6 100644 --- a/PHPSkills/FactorGraphs/FactorGraphLayer.php +++ b/PHPSkills/FactorGraphs/FactorGraphLayer.php @@ -1,6 +1,7 @@ _inputVariablesGroups; + $inputVariablesGroups = &$this->_inputVariablesGroups; + return $inputVariablesGroups; } // HACK public function &getParentFactorGraph() { - return $this->_parentFactorGraph; + $parentFactorGraph = &$this->_parentFactorGraph; + return $parentFactorGraph; } public function &getOutputVariablesGroups() { - return $this->_outputVariablesGroups; + $outputVariablesGroups = &$this->_outputVariablesGroups; + return $outputVariablesGroups; } public function &getLocalFactors() { - return $this->_localFactors; + $localFactors = &$this->_localFactors; + return $localFactors; } public function setInputVariablesGroups(&$value) @@ -43,12 +48,12 @@ abstract class FactorGraphLayer $this->_inputVariablesGroups = $value; } - protected function scheduleSequence($itemsToSequence, $name) + protected function scheduleSequence(array $itemsToSequence, $name) { return new ScheduleSequence($name, $itemsToSequence); } - protected function addLayerFactor(&$factor) + protected function addLayerFactor(Factor &$factor) { $this->_localFactors[] = $factor; } diff --git a/PHPSkills/FactorGraphs/FactorList.php b/PHPSkills/FactorGraphs/FactorList.php index 527591c..199f0ae 100644 --- a/PHPSkills/FactorGraphs/FactorList.php +++ b/PHPSkills/FactorGraphs/FactorList.php @@ -13,7 +13,8 @@ class FactorList public function getLogNormalization() { - foreach($this->_list as $currentFactor) + $list = &$this->_list; + foreach($list as &$currentFactor) { $currentFactor->resetMarginals(); } @@ -36,7 +37,7 @@ class FactorList $sumLogS = 0; - foreach($this->_list as $currentFactor) + foreach($list as &$currentFactor) { $sumLogS = $sumLogS + $currentFactor->getLogNormalization(); } @@ -49,7 +50,7 @@ class FactorList return count($this->_list); } - public function addFactor(Factor &$factor) + public function &addFactor(Factor &$factor) { $this->_list[] = $factor; return $factor; diff --git a/PHPSkills/FactorGraphs/Message.php b/PHPSkills/FactorGraphs/Message.php index c318dcf..7d49e80 100644 --- a/PHPSkills/FactorGraphs/Message.php +++ b/PHPSkills/FactorGraphs/Message.php @@ -6,7 +6,7 @@ class Message private $_name; private $_value; - public function __construct($value = null, $name = null) + public function __construct(&$value = null, $name = null) { $this->_name = $name; $this->_value = $value; @@ -14,7 +14,8 @@ class Message public function& getValue() { - return $this->_value; + $value = &$this->_value; + return $value; } public function setValue(&$value) diff --git a/PHPSkills/FactorGraphs/Schedule.php b/PHPSkills/FactorGraphs/Schedule.php index 4ac941a..a144ef8 100644 --- a/PHPSkills/FactorGraphs/Schedule.php +++ b/PHPSkills/FactorGraphs/Schedule.php @@ -1,6 +1,8 @@ _factor = $factor; @@ -41,7 +43,7 @@ class ScheduleSequence extends Schedule { private $_schedules; - public function __construct($name, $schedules) + public function __construct($name, array $schedules) { parent::__construct($name); $this->_schedules = $schedules; @@ -51,7 +53,8 @@ class ScheduleSequence extends Schedule { $maxDelta = 0; - foreach ($this->_schedules as $currentSchedule) + $schedules = &$this->_schedules; + foreach ($schedules as &$currentSchedule) { $maxDelta = max($currentSchedule->visit($depth + 1, $maxDepth), $maxDelta); } @@ -65,7 +68,7 @@ class ScheduleLoop extends Schedule private $_maxDelta; private $_scheduleToLoop; - public function __construct($name, Schedule $scheduleToLoop, $maxDelta) + public function __construct($name, Schedule &$scheduleToLoop, $maxDelta) { parent::__construct($name); $this->_scheduleToLoop = $scheduleToLoop; diff --git a/PHPSkills/FactorGraphs/Variable.php b/PHPSkills/FactorGraphs/Variable.php index bad9cbb..a936916 100644 --- a/PHPSkills/FactorGraphs/Variable.php +++ b/PHPSkills/FactorGraphs/Variable.php @@ -7,7 +7,7 @@ class Variable private $_prior; private $_value; - public function __construct($name, $prior) + public function __construct($name, &$prior) { $this->_name = "Variable[" . $name . "]"; $this->_prior = $prior; @@ -16,7 +16,8 @@ class Variable public function &getValue() { - return $this->_value; + $value = &$this->_value; + return $value; } public function setValue(&$value) @@ -42,12 +43,12 @@ class DefaultVariable extends Variable parent::__construct("Default", null); } - public function getValue() + public function &getValue() { return null; } - public function setValue($value) + public function setValue(&$value) { throw new Exception(); } @@ -56,7 +57,7 @@ class DefaultVariable extends Variable class KeyedVariable extends Variable { private $_key; - public function __construct($key, $name, $prior) + public function __construct($key, $name, &$prior) { parent::__construct($name, $prior); $this->_key = $key; diff --git a/PHPSkills/FactorGraphs/VariableFactory.php b/PHPSkills/FactorGraphs/VariableFactory.php index 3d72f8e..12061f2 100644 --- a/PHPSkills/FactorGraphs/VariableFactory.php +++ b/PHPSkills/FactorGraphs/VariableFactory.php @@ -14,14 +14,14 @@ class VariableFactory $this->_variablePriorInitializer = &$variablePriorInitializer; } - public function createBasicVariable($name) + public function &createBasicVariable($name) { $initializer = $this->_variablePriorInitializer; $newVar = new Variable($name, $initializer()); return $newVar; } - public function createKeyedVariable($key, $name) + public function &createKeyedVariable(&$key, $name) { $initializer = $this->_variablePriorInitializer; $newVar = new KeyedVariable($key, $name, $initializer()); diff --git a/PHPSkills/HashMap.php b/PHPSkills/HashMap.php index c1e4446..efc3153 100644 --- a/PHPSkills/HashMap.php +++ b/PHPSkills/HashMap.php @@ -7,10 +7,11 @@ class HashMap private $_hashToValue = array(); private $_hashToKey = array(); - public function getValue($key) + public function &getValue($key) { $hash = self::getHash($key); - return $this->_hashToValue[$hash]; + $hashValue = &$this->_hashToValue[$hash]; + return $hashValue; } public function setValue($key, $value) diff --git a/PHPSkills/Numerics/BasicMath.php b/PHPSkills/Numerics/BasicMath.php index 37f8a1a..cac534c 100644 --- a/PHPSkills/Numerics/BasicMath.php +++ b/PHPSkills/Numerics/BasicMath.php @@ -15,7 +15,7 @@ function square($x) return $x * $x; } -function sum($itemsToSum, $funcName ) +function sum(array $itemsToSum, $funcName ) { $mappedItems = array_map($funcName, $itemsToSum); return array_sum($mappedItems); diff --git a/PHPSkills/Numerics/GaussianDistribution.php b/PHPSkills/Numerics/GaussianDistribution.php index 55cf685..406f9a3 100644 --- a/PHPSkills/Numerics/GaussianDistribution.php +++ b/PHPSkills/Numerics/GaussianDistribution.php @@ -30,8 +30,25 @@ class GaussianDistribution $this->_mean = $mean; $this->_standardDeviation = $standardDeviation; $this->_variance = square($standardDeviation); - $this->_precision = 1.0/$this->_variance; - $this->_precisionMean = $this->_precision*$this->_mean; + + if($this->_variance != 0) + { + $this->_precision = 1.0/$this->_variance; + $this->_precisionMean = $this->_precision*$this->_mean; + } + else + { + $this->_precision = \INF; + + if($this->_mean == 0) + { + $this->_precisionMean = 0; + } + else + { + $this->_precisionMean = \INF; + } + } } public function getMean() diff --git a/PHPSkills/Player.php b/PHPSkills/Player.php index 9819101..76d0f46 100644 --- a/PHPSkills/Player.php +++ b/PHPSkills/Player.php @@ -38,8 +38,9 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate /// /// The identifier for the player, such as a name. /// - public function getId() + public function &getId() { + $id = &$this->_Id; return $this->_Id; } diff --git a/PHPSkills/RatingContainer.php b/PHPSkills/RatingContainer.php index 456712a..d1e06ff 100644 --- a/PHPSkills/RatingContainer.php +++ b/PHPSkills/RatingContainer.php @@ -12,9 +12,10 @@ class RatingContainer $this->_playerToRating = new HashMap(); } - public function getRating($player) + public function &getRating($player) { - return $this->_playerToRating->getValue($player); + $rating = &$this->_playerToRating->getValue($player); + return $rating; } public function setRating($player, $rating) diff --git a/PHPSkills/Team.php b/PHPSkills/Team.php index a4e6637..c33343e 100644 --- a/PHPSkills/Team.php +++ b/PHPSkills/Team.php @@ -5,7 +5,7 @@ require_once(dirname(__FILE__) . '/RatingContainer.php'); class Team extends RatingContainer { - public function __construct($player = null, $rating = null) + public function __construct(&$player = null, &$rating = null) { parent::__construct(); @@ -15,7 +15,7 @@ class Team extends RatingContainer } } - public function addPlayer($player, $rating) + public function addPlayer(&$player, &$rating) { $this->setRating($player, $rating); return $this; diff --git a/PHPSkills/Teams.php b/PHPSkills/Teams.php index a34fd74..b55e8b5 100644 --- a/PHPSkills/Teams.php +++ b/PHPSkills/Teams.php @@ -8,7 +8,7 @@ class Teams $args = \func_get_args(); $result = array(); - foreach ($args as $currentTeam) { + foreach ($args as &$currentTeam) { $result[] = $currentTeam; } diff --git a/PHPSkills/TrueSkill/Factors/GaussianFactor.php b/PHPSkills/TrueSkill/Factors/GaussianFactor.php index 98d98db..62d1ed8 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianFactor.php @@ -29,12 +29,13 @@ abstract class GaussianFactor extends Factor return $logZ; } - public function createVariableToMessageBinding(Variable &$variable) + public function &createVariableToMessageBinding(Variable &$variable) { - return parent::createVariableToMessageBindingWithMessage($variable, + $binding = &parent::createVariableToMessageBindingWithMessage($variable, new Message( GaussianDistribution::fromPrecisionMean(0, 0), sprintf("message from %s to %s", $this, $variable))); + return $binding; } } diff --git a/PHPSkills/TrueSkill/Factors/GaussianLikelihoodFactor.php b/PHPSkills/TrueSkill/Factors/GaussianLikelihoodFactor.php index a30dedd..f5982c7 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianLikelihoodFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianLikelihoodFactor.php @@ -28,8 +28,8 @@ class GaussianLikelihoodFactor extends GaussianFactor public function getLogNormalization() { - $vars = $this->getVariables(); - $messages = $this->getMessages(); + $vars = &$this->getVariables(); + $messages = &$this->getMessages(); return GaussianDistribution::logRatioNormalization( $vars[0]->getValue(), diff --git a/PHPSkills/TrueSkill/Factors/GaussianWeightedSumFactor.php b/PHPSkills/TrueSkill/Factors/GaussianWeightedSumFactor.php index 03a9729..d4921b8 100644 --- a/PHPSkills/TrueSkill/Factors/GaussianWeightedSumFactor.php +++ b/PHPSkills/TrueSkill/Factors/GaussianWeightedSumFactor.php @@ -118,7 +118,7 @@ class GaussianWeightedSumFactor extends GaussianFactor $this->createVariableToMessageBinding($sumVariable); - foreach ($variablesToSum as $currentVariable) + foreach ($variablesToSum as &$currentVariable) { $this->createVariableToMessageBinding($currentVariable); } diff --git a/PHPSkills/TrueSkill/Layers/IteratedTeamDifferencesInnerLayer.php b/PHPSkills/TrueSkill/Layers/IteratedTeamDifferencesInnerLayer.php index 40ec599..15ffc22 100644 --- a/PHPSkills/TrueSkill/Layers/IteratedTeamDifferencesInnerLayer.php +++ b/PHPSkills/TrueSkill/Layers/IteratedTeamDifferencesInnerLayer.php @@ -33,8 +33,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->setInputVariablesGroups($inputVariablesGroups); $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->buildLayer(); - $this->_TeamDifferencesComparisonLayer->setInputVariablesGroups( - $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getOutputVariablesGroups()); + $teamDifferencesOutputVariablesGroups = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getOutputVariablesGroups(); + $this->_TeamDifferencesComparisonLayer->setInputVariablesGroups($teamDifferencesOutputVariablesGroups); $this->_TeamDifferencesComparisonLayer->buildLayer(); } @@ -65,10 +65,10 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer $loop, new ScheduleStep( "teamPerformanceToPerformanceDifferenceFactors[0] @ 1", - $localFactors[0], 1), + &$localFactors[0], 1), new ScheduleStep( sprintf("teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2", $totalTeamDifferences), - $localFactors[$totalTeamDifferences - 1], 2) + &$localFactors[$totalTeamDifferences - 1], 2) ) ); @@ -83,11 +83,11 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer $itemsToSequence = array( new ScheduleStep( "send team perf to perf differences", - $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0], + &$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0], 0), new ScheduleStep( "send to greater than or within factor", - $teamDifferencesComparisonLayerLocalFactors[0], + &$teamDifferencesComparisonLayerLocalFactors[0], 0) ); @@ -112,16 +112,16 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer array( new ScheduleStep( sprintf("team perf to perf diff %d", $i), - $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$i], 0), + &$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$i], 0), new ScheduleStep( sprintf("greater than or within result factor %d", $i), - $teamDifferencesComparisonLayerLocalFactors[$i], 0), + &$teamDifferencesComparisonLayerLocalFactors[$i], 0), new ScheduleStep( sprintf("team perf to perf diff factors [%d], 2", $i), - $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$i], 2) + &$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$i], 2) ), sprintf("current forward schedule piece %d", $i)); - $forwardScheduleList[] = &$currentForwardSchedulePiece; + $forwardScheduleList[] = $currentForwardSchedulePiece; } $forwardSchedule = new ScheduleSequence("forward schedule", $forwardScheduleList); @@ -133,20 +133,24 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); $teamDifferencesComparisonLayerLocalFactors = &$this->_TeamDifferencesComparisonLayer->getLocalFactors(); + $differencesFactor = &$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i]; + $comparisonFactor = &$teamDifferencesComparisonLayerLocalFactors[$totalTeamDifferences - 1 - $i]; + $performancesToDifferencesFactor = &$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i]; + $currentBackwardSchedulePiece = new ScheduleSequence( "current backward schedule piece", array( new ScheduleStep( sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0", $i), - $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i], 0), + $differencesFactor, 0), new ScheduleStep( sprintf("greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0", $i), - $teamDifferencesComparisonLayerLocalFactors[$totalTeamDifferences - 1 - $i], 0), + $comparisonFactor, 0), new ScheduleStep( sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1", $i), - $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i], 1) + $performancesToDifferencesFactor, 1) )); - $backwardScheduleList[] = &$currentBackwardSchedulePiece; + $backwardScheduleList[] = $currentBackwardSchedulePiece; } $backwardSchedule = new ScheduleSequence("backward schedule", $backwardScheduleList); diff --git a/PHPSkills/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php b/PHPSkills/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php index 71080fc..90c8fb0 100644 --- a/PHPSkills/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php +++ b/PHPSkills/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php @@ -39,14 +39,17 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye public function createPriorSchedule() { - return $this->scheduleSequence( - array_map( - function($weightedSumFactor) - { - return new ScheduleStep("Perf to Team Perf Step", $weightedSumFactor, 0); - }, - $this->getLocalFactors()), - "all player perf to team perf schedule"); + $localFactors = &$this->getLocalFactors(); + + $sequence = &$this->scheduleSequence( + array_map( + function($weightedSumFactor) + { + return new ScheduleStep("Perf to Team Perf Step", $weightedSumFactor, 0); + }, + $localFactors), + "all player perf to team perf schedule"); + return $sequence; } protected function createPlayerToTeamSumFactor(&$teamMembers, &$sumVariable) @@ -69,7 +72,8 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye { // BLOG $allFactors = array(); - foreach($this->getLocalFactors() as $currentFactor) + $localFactors = &$this->getLocalFactors(); + foreach($localFactors as &$currentFactor) { $numberOfMessages = $currentFactor->getNumberOfMessages(); for($currentIteration = 1; $currentIteration < $numberOfMessages; $currentIteration++) diff --git a/PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php b/PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php index 1e0843a..617cbe0 100644 --- a/PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php +++ b/PHPSkills/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php @@ -1,15 +1,20 @@ _teams as $currentTeam) + $teams = &$this->_teams; + foreach ($teams as &$currentTeam) { $currentTeamSkills = array(); - foreach ($currentTeam->getAllPlayers() as $currentTeamPlayer) + $currentTeamAllPlayers = &$currentTeam->getAllPlayers(); + foreach ($currentTeamAllPlayers as &$currentTeamPlayer) { $currentTeamPlayerRating = $currentTeam->getRating($currentTeamPlayer); $playerSkill = $this->createSkillOutputVariable($currentTeamPlayer); @@ -47,17 +54,18 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer public function createPriorSchedule() { + $localFactors = &$this->getLocalFactors(); return $this->scheduleSequence( array_map( - function($prior) + function(&$prior) { return new ScheduleStep("Prior to Skill Step", $prior, 0); }, - $this->getLocalFactors()), + $localFactors), "All priors"); } - private function createPriorFactor(&$player, &$priorRating, &$skillsVariable) + private function createPriorFactor(&$player, Rating &$priorRating, Variable &$skillsVariable) { return new GaussianPriorFactor($priorRating->getMean(), square($priorRating->getStandardDeviation()) + @@ -67,8 +75,8 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer private function createSkillOutputVariable($key) { - $parentFactorGraph = $this->getParentFactorGraph(); - $variableFactory = $parentFactorGraph->getVariableFactory(); + $parentFactorGraph = &$this->getParentFactorGraph(); + $variableFactory = &$parentFactorGraph->getVariableFactory(); return $variableFactory->createKeyedVariable($key, $key . "'s skill"); } } diff --git a/PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php b/PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php index 4478616..19adef9 100644 --- a/PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php +++ b/PHPSkills/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php @@ -2,12 +2,14 @@ namespace Moserware\Skills\TrueSkill\Layers; require_once(dirname(__FILE__) . "/../../FactorGraphs/Schedule.php"); +require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php"); require_once(dirname(__FILE__) . "/../../Numerics/BasicMath.php"); require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php"); require_once(dirname(__FILE__) . "/../Factors/GaussianLikelihoodFactor.php"); require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php"); use Moserware\Skills\FactorGraphs\ScheduleStep; +use Moserware\Skills\FactorGraphs\KeyedVariable; use Moserware\Skills\TrueSkill\TrueSkillFactorGraph; use Moserware\Skills\TrueSkill\Factors\GaussianLikelihoodFactor; @@ -20,7 +22,10 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer public function buildLayer() { - foreach ($this->getInputVariablesGroups() as $currentTeam) + $inputVariablesGroups = &$this->getInputVariablesGroups(); + $outputVariablesGroups = &$this->getOutputVariablesGroups(); + + foreach ($inputVariablesGroups as &$currentTeam) { $currentTeamPlayerPerformances = array(); @@ -30,14 +35,13 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer $newLikelihoodFactor = $this->createLikelihood($playerSkillVariable, $playerPerformance); $this->addLayerFactor($newLikelihoodFactor); $currentTeamPlayerPerformances[] = $playerPerformance; - } + } - $outputVariablesGroups = &$this->getOutputVariablesGroups(); $outputVariablesGroups[] = $currentTeamPlayerPerformances; } } - private function createLikelihood(&$playerSkill, &$playerPerformance) + private function createLikelihood(KeyedVariable &$playerSkill, KeyedVariable &$playerPerformance) { return new GaussianLikelihoodFactor(square($this->getParentFactorGraph()->getGameInfo()->getBeta()), $playerPerformance, $playerSkill); } @@ -49,25 +53,27 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer public function createPriorSchedule() { + $localFactors = &$this->getLocalFactors(); return $this->scheduleSequence( array_map( function($likelihood) { return new ScheduleStep("Skill to Perf step", $likelihood, 0); }, - $this->getLocalFactors()), + $localFactors), "All skill to performance sending"); } public function createPosteriorSchedule() { + $localFactors = &$this->getLocalFactors(); return $this->scheduleSequence( array_map( function($likelihood) { return new ScheduleStep("name", $likelihood, 1); }, - $this->getLocalFactors()), + $localFactors), "All skill to performance sending"); } } diff --git a/PHPSkills/TrueSkill/Layers/TeamDifferencesComparisonLayer.php b/PHPSkills/TrueSkill/Layers/TeamDifferencesComparisonLayer.php index 8cf2993..c66bb67 100644 --- a/PHPSkills/TrueSkill/Layers/TeamDifferencesComparisonLayer.php +++ b/PHPSkills/TrueSkill/Layers/TeamDifferencesComparisonLayer.php @@ -21,7 +21,7 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer { parent::__construct($parentGraph); $this->_teamRanks = $teamRanks; - $gameInfo = $this->getParentFactorGraph()->getGameInfo(); + $gameInfo = &$this->getParentFactorGraph()->getGameInfo(); $this->_epsilon = DrawMargin::getDrawMarginFromDrawProbability($gameInfo->getDrawProbability(), $gameInfo->getBeta()); } @@ -33,7 +33,7 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer for ($i = 0; $i < $inputVarGroupsCount; $i++) { $isDraw = ($this->_teamRanks[$i] == $this->_teamRanks[$i + 1]); - $teamDifference = $inputVarGroups[$i][0]; + $teamDifference = &$inputVarGroups[$i][0]; $factor = $isDraw diff --git a/PHPSkills/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php b/PHPSkills/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php index 132c23f..a5a6e38 100644 --- a/PHPSkills/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php +++ b/PHPSkills/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php @@ -22,18 +22,18 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG { $inputVariablesGroups = &$this->getInputVariablesGroups(); $inputVariablesGroupsCount = count($inputVariablesGroups); + $outputVariablesGroup = &$this->getOutputVariablesGroups(); for ($i = 0; $i < $inputVariablesGroupsCount - 1; $i++) { - $strongerTeam = $inputVariablesGroups[$i][0]; - $weakerTeam = $inputVariablesGroups[$i + 1][0]; + $strongerTeam = &$inputVariablesGroups[$i][0]; + $weakerTeam = &$inputVariablesGroups[$i + 1][0]; $currentDifference = &$this->createOutputVariable(); $newDifferencesFactor = $this->createTeamPerformanceToDifferenceFactor($strongerTeam, $weakerTeam, $currentDifference); $this->addLayerFactor($newDifferencesFactor); - // REVIEW: Does it make sense to have groups of one? - $outputVariablesGroup = &$this->getOutputVariablesGroups(); + // REVIEW: Does it make sense to have groups of one? $outputVariablesGroup[] = array($currentDifference); } } diff --git a/PHPSkills/TrueSkill/TrueSkillFactorGraph.php b/PHPSkills/TrueSkill/TrueSkillFactorGraph.php index 69f07fc..b000ef0 100644 --- a/PHPSkills/TrueSkill/TrueSkillFactorGraph.php +++ b/PHPSkills/TrueSkill/TrueSkillFactorGraph.php @@ -68,7 +68,8 @@ class TrueSkillFactorGraph extends FactorGraph { $lastOutput = null; - foreach ($this->_layers as $currentLayer) + $layers = &$this->_layers; + foreach ($layers as &$currentLayer) { if ($lastOutput != null) { @@ -91,9 +92,11 @@ class TrueSkillFactorGraph extends FactorGraph { $factorList = new FactorList(); - foreach ($this->_layers as $currentLayer) + $layers = &$this->_layers; + foreach ($layers as &$currentLayer) { - foreach ($currentLayer->getLocalFactors() as $currentFactor) + $localFactors = &$currentLayer->getLocalFactors(); + foreach ($localFactors as &$currentFactor) { $factorList->addFactor($currentFactor); } @@ -107,7 +110,8 @@ class TrueSkillFactorGraph extends FactorGraph { $fullSchedule = array(); - foreach ($this->_layers as $currentLayer) + $layers = &$this->_layers; + foreach ($layers as &$currentLayer) { $currentPriorSchedule = $currentLayer->createPriorSchedule(); if ($currentPriorSchedule != null) @@ -118,7 +122,7 @@ class TrueSkillFactorGraph extends FactorGraph $allLayersReverse = \array_reverse($this->_layers); - foreach ($allLayersReverse as $currentLayer) + foreach ($allLayersReverse as &$currentLayer) { $currentPosteriorSchedule = $currentLayer->createPosteriorSchedule(); if ($currentPosteriorSchedule != null) @@ -134,9 +138,10 @@ class TrueSkillFactorGraph extends FactorGraph { $result = new RatingContainer(); - foreach ($this->_priorLayer->getOutputVariablesGroups() as $currentTeam) + $priorLayerOutputVariablesGroups = &$this->_priorLayer->getOutputVariablesGroups(); + foreach ($priorLayerOutputVariablesGroups as &$currentTeam) { - foreach ($currentTeam as $currentPlayer) + foreach ($currentTeam as &$currentPlayer) { $newRating = new Rating($currentPlayer->getValue()->getMean(), $currentPlayer->getValue()->getStandardDeviation()); diff --git a/PHPSkills/TrueSkill/TwoPlayerTrueSkillCalculator.php b/PHPSkills/TrueSkill/TwoPlayerTrueSkillCalculator.php index dea523c..0e49026 100644 --- a/PHPSkills/TrueSkill/TwoPlayerTrueSkillCalculator.php +++ b/PHPSkills/TrueSkill/TwoPlayerTrueSkillCalculator.php @@ -2,6 +2,7 @@ namespace Moserware\Skills\TrueSkill; +require_once(dirname(__FILE__) . "/../GameInfo.php"); require_once(dirname(__FILE__) . "/../Guard.php"); require_once(dirname(__FILE__) . "/../PairwiseComparison.php"); require_once(dirname(__FILE__) . "/../RankSorter.php"); @@ -17,6 +18,7 @@ require_once(dirname(__FILE__) . "/../Numerics/BasicMath.php"); require_once(dirname(__FILE__) . "/DrawMargin.php"); require_once(dirname(__FILE__) . "/TruncatedGaussianCorrectionFunctions.php"); +use Moserware\Skills\GameInfo; use Moserware\Skills\Guard; use Moserware\Skills\PairwiseComparison; use Moserware\Skills\RankSorter; @@ -42,7 +44,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::exactly(1)); } - public function calculateNewRatings($gameInfo, + public function calculateNewRatings(GameInfo &$gameInfo, array $teams, array $teamRanks) { @@ -82,7 +84,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator return $results; } - private static function calculateNewRating($gameInfo, $selfRating, $opponentRating, $comparison) + private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, $comparison) { $drawMargin = DrawMargin::getDrawMarginFromDrawProbability($gameInfo->getDrawProbability(), $gameInfo->getBeta()); @@ -138,7 +140,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator } /// - public function calculateMatchQuality($gameInfo, array $teams) + public function calculateMatchQuality(GameInfo $gameInfo, array $teams) { Guard::argumentNotNull($gameInfo, "gameInfo"); $this->validateTeamCountAndPlayersCountPerTeam($teams); diff --git a/PHPSkills/TrueSkill/TwoTeamTrueSkillCalculator.php b/PHPSkills/TrueSkill/TwoTeamTrueSkillCalculator.php index 5ce33e4..cf742f4 100644 --- a/PHPSkills/TrueSkill/TwoTeamTrueSkillCalculator.php +++ b/PHPSkills/TrueSkill/TwoTeamTrueSkillCalculator.php @@ -47,7 +47,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator parent::__construct(SkillCalculatorSupportedOptions::NONE, TeamsRange::exactly(2), PlayersRange::atLeast(1)); } - public function calculateNewRatings($gameInfo, + public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks) { @@ -164,7 +164,7 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator } /// - public function calculateMatchQuality($gameInfo, array $teams) + public function calculateMatchQuality(GameInfo $gameInfo, array $teams) { Guard::argumentNotNull($gameInfo, "gameInfo"); $this->validateTeamCountAndPlayersCountPerTeam($teams);