Cleanup in src/, adding namespaces, removing php closing tag and general code cleanup

This commit is contained in:
Alexander Liljengård
2016-05-24 14:10:39 +02:00
parent 9f97eb1653
commit 5694a2fb30
64 changed files with 891 additions and 1328 deletions

View File

@ -1,11 +1,4 @@
<?php
namespace Moserware\Skills\TrueSkill\Layers;
require_once(dirname(__FILE__) . "/../../FactorGraphs/Schedule.php");
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
require_once(dirname(__FILE__) . "/TeamPerformancesToTeamPerformanceDifferencesLayer.php");
require_once(dirname(__FILE__) . "/TeamDifferencesComparisonLayer.php");
<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\FactorGraphs\ScheduleLoop;
use Moserware\Skills\FactorGraphs\ScheduleSequence;
@ -30,8 +23,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
public function &getLocalFactors()
{
$localFactors =
\array_merge($this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(),
$this->_TeamDifferencesComparisonLayer->getLocalFactors());
array_merge($this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(),
$this->_TeamDifferencesComparisonLayer->getLocalFactors());
return $localFactors;
}
@ -47,9 +40,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
}
public function createPriorSchedule()
{
switch (count($this->getInputVariablesGroups()))
{
{
switch (count($this->getInputVariablesGroups())) {
case 0:
case 1:
throw new InvalidOperationException();
@ -72,15 +64,15 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$innerSchedule = new ScheduleSequence(
"inner schedule",
array(
$loop,
new ScheduleStep(
"teamPerformanceToPerformanceDifferenceFactors[0] @ 1",
$firstDifferencesFactor, 1),
new ScheduleStep(
sprintf("teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2", $totalTeamDifferences),
$lastDifferencesFactor, 2)
)
);
$loop,
new ScheduleStep(
"teamPerformanceToPerformanceDifferenceFactors[0] @ 1",
$firstDifferencesFactor, 1),
new ScheduleStep(
sprintf("teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2", $totalTeamDifferences),
$lastDifferencesFactor, 2)
)
);
return $innerSchedule;
}
@ -93,15 +85,15 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$firstPerfToTeamDiff = &$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0];
$firstTeamDiffComparison = &$teamDifferencesComparisonLayerLocalFactors[0];
$itemsToSequence = array(
new ScheduleStep(
"send team perf to perf differences",
$firstPerfToTeamDiff,
0),
new ScheduleStep(
"send to greater than or within factor",
$firstTeamDiffComparison,
0)
);
new ScheduleStep(
"send team perf to perf differences",
$firstPerfToTeamDiff,
0),
new ScheduleStep(
"send to greater than or within factor",
$firstTeamDiffComparison,
0)
);
return $this->scheduleSequence(
$itemsToSequence,
@ -114,8 +106,7 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$forwardScheduleList = array();
for ($i = 0; $i < $totalTeamDifferences - 1; $i++)
{
for ($i = 0; $i < $totalTeamDifferences - 1; $i++) {
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$teamDifferencesComparisonLayerLocalFactors = &$this->_TeamDifferencesComparisonLayer->getLocalFactors();
@ -125,16 +116,16 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$currentForwardSchedulePiece =
$this->scheduleSequence(
array(
new ScheduleStep(
sprintf("team perf to perf diff %d", $i),
$currentTeamPerfToTeamPerfDiff, 0),
new ScheduleStep(
sprintf("greater than or within result factor %d", $i),
$currentTeamDiffComparison, 0),
new ScheduleStep(
sprintf("team perf to perf diff factors [%d], 2", $i),
$currentTeamPerfToTeamPerfDiff, 2)
), sprintf("current forward schedule piece %d", $i));
new ScheduleStep(
sprintf("team perf to perf diff %d", $i),
$currentTeamPerfToTeamPerfDiff, 0),
new ScheduleStep(
sprintf("greater than or within result factor %d", $i),
$currentTeamDiffComparison, 0),
new ScheduleStep(
sprintf("team perf to perf diff factors [%d], 2", $i),
$currentTeamPerfToTeamPerfDiff, 2)
), sprintf("current forward schedule piece %d", $i));
$forwardScheduleList[] = $currentForwardSchedulePiece;
}
@ -143,8 +134,7 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$backwardScheduleList = array();
for ($i = 0; $i < $totalTeamDifferences - 1; $i++)
{
for ($i = 0; $i < $totalTeamDifferences - 1; $i++) {
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = &$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
$teamDifferencesComparisonLayerLocalFactors = &$this->_TeamDifferencesComparisonLayer->getLocalFactors();
@ -155,15 +145,15 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$currentBackwardSchedulePiece = new ScheduleSequence(
"current backward schedule piece",
array(
new ScheduleStep(
sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0", $i),
$differencesFactor, 0),
new ScheduleStep(
sprintf("greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0", $i),
$comparisonFactor, 0),
new ScheduleStep(
sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1", $i),
$performancesToDifferencesFactor, 1)
new ScheduleStep(
sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0", $i),
$differencesFactor, 0),
new ScheduleStep(
sprintf("greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0", $i),
$comparisonFactor, 0),
new ScheduleStep(
sprintf("teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1", $i),
$performancesToDifferencesFactor, 1)
));
$backwardScheduleList[] = $currentBackwardSchedulePiece;
}
@ -184,6 +174,4 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
return $loop;
}
}
?>
}

View File

@ -1,13 +1,4 @@
<?php
namespace Moserware\Skills\TrueSkill\Layers;
require_once(dirname(__FILE__) . "/../../PartialPlay.php");
require_once(dirname(__FILE__) . "/../../FactorGraphs/Schedule.php");
require_once(dirname(__FILE__) . "/../Factors/GaussianWeightedSumFactor.php");
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
require_once(dirname(__FILE__) . "/TeamPerformancesToTeamPerformanceDifferencesLayer.php");
require_once(dirname(__FILE__) . "/TeamDifferencesComparisonLayer.php");
<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\PartialPlay;
use Moserware\Skills\FactorGraphs\ScheduleLoop;
@ -26,18 +17,17 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
public function buildLayer()
{
$inputVariablesGroups = &$this->getInputVariablesGroups();
foreach ($inputVariablesGroups as &$currentTeam)
{
foreach ($inputVariablesGroups as &$currentTeam) {
$localCurrentTeam = &$currentTeam;
$teamPerformance = &$this->createOutputVariable($localCurrentTeam);
$newSumFactor = $this->createPlayerToTeamSumFactor($localCurrentTeam, $teamPerformance);
$this->addLayerFactor($newSumFactor);
// REVIEW: Does it make sense to have groups of one?
$outputVariablesGroups = &$this->getOutputVariablesGroups();
$outputVariablesGroups[] = array($teamPerformance);
}
}
}
public function createPriorSchedule()
@ -45,45 +35,41 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
$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");
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)
{
$weights = array_map(
function($v)
{
$player = &$v->getKey();
return PartialPlay::getPartialPlayPercentage($player);
},
$teamMembers);
function ($v) {
$player = &$v->getKey();
return PartialPlay::getPartialPlayPercentage($player);
},
$teamMembers);
return new GaussianWeightedSumFactor(
$sumVariable,
$teamMembers,
$weights);
$sumVariable,
$teamMembers,
$weights);
}
public function createPosteriorSchedule()
{
{
$allFactors = array();
$localFactors = &$this->getLocalFactors();
foreach($localFactors as &$currentFactor)
{
foreach ($localFactors as &$currentFactor) {
$localCurrentFactor = &$currentFactor;
$numberOfMessages = $localCurrentFactor->getNumberOfMessages();
for($currentIteration = 1; $currentIteration < $numberOfMessages; $currentIteration++)
{
for ($currentIteration = 1; $currentIteration < $numberOfMessages; $currentIteration++) {
$allFactors[] = new ScheduleStep("team sum perf @" . $currentIteration,
$localCurrentFactor, $currentIteration);
$localCurrentFactor, $currentIteration);
}
}
return $this->scheduleSequence($allFactors, "all of the team's sum iterations");
@ -91,16 +77,13 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
private function &createOutputVariable(&$team)
{
$memberNames = \array_map(function ($currentPlayer)
{
return (string)($currentPlayer->getKey());
},
$team);
$memberNames = \array_map(function ($currentPlayer) {
return (string)($currentPlayer->getKey());
},
$team);
$teamMemberNames = \join(", ", $memberNames);
$outputVariable = &$this->getParentFactorGraph()->getVariableFactory()->createBasicVariable("Team[" . $teamMemberNames . "]'s performance");
return $outputVariable;
}
}
?>
}

View File

@ -1,20 +1,9 @@
<?php
namespace Moserware\Skills\TrueSkill\Layers;
require_once(dirname(__FILE__) . "/../../Rating.php");
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/GaussianPriorFactor.php");
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\Numerics\BasicMatch;
use Moserware\Skills\Rating;
use Moserware\Skills\FactorGraphs\ScheduleLoop;
use Moserware\Skills\FactorGraphs\ScheduleSequence;
use Moserware\Skills\FactorGraphs\ScheduleStep;
use Moserware\Skills\FactorGraphs\Variable;
use Moserware\Numerics\GaussianDistribution;
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
use Moserware\Skills\TrueSkill\Factors\GaussianPriorFactor;
@ -33,14 +22,12 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
public function buildLayer()
{
$teams = &$this->_teams;
foreach ($teams as &$currentTeam)
{
foreach ($teams as &$currentTeam) {
$localCurrentTeam = &$currentTeam;
$currentTeamSkills = array();
$currentTeamAllPlayers = $localCurrentTeam->getAllPlayers();
foreach ($currentTeamAllPlayers as &$currentTeamPlayer)
{
foreach ($currentTeamAllPlayers as &$currentTeamPlayer) {
$localCurrentTeamPlayer = &$currentTeamPlayer;
$currentTeamPlayerRating = $currentTeam->getRating($localCurrentTeamPlayer);
$playerSkill = &$this->createSkillOutputVariable($localCurrentTeamPlayer);
@ -58,21 +45,22 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
{
$localFactors = &$this->getLocalFactors();
return $this->scheduleSequence(
array_map(
function(&$prior)
{
return new ScheduleStep("Prior to Skill Step", $prior, 0);
},
$localFactors),
"All priors");
array_map(
function (&$prior) {
return new ScheduleStep("Prior to Skill Step", $prior, 0);
},
$localFactors),
"All priors");
}
private function createPriorFactor(&$player, Rating &$priorRating, Variable &$skillsVariable)
{
return new GaussianPriorFactor($priorRating->getMean(),
square($priorRating->getStandardDeviation()) +
square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
$skillsVariable);
return new GaussianPriorFactor(
$priorRating->getMean(),
BasicMatch::square($priorRating->getStandardDeviation()) +
BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
$skillsVariable
);
}
private function &createSkillOutputVariable(&$key)
@ -82,6 +70,4 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
$skillOutputVariable = &$variableFactory->createKeyedVariable($key, $key . "'s skill");
return $skillOutputVariable;
}
}
?>
}

View File

@ -1,15 +1,8 @@
<?php
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");
<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\FactorGraphs\ScheduleStep;
use Moserware\Skills\FactorGraphs\KeyedVariable;
use Moserware\Skills\Numerics\BasicMatch;
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
use Moserware\Skills\TrueSkill\Factors\GaussianLikelihoodFactor;
@ -25,27 +18,29 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
$inputVariablesGroups = &$this->getInputVariablesGroups();
$outputVariablesGroups = &$this->getOutputVariablesGroups();
foreach ($inputVariablesGroups as &$currentTeam)
{
foreach ($inputVariablesGroups as &$currentTeam) {
$currentTeamPlayerPerformances = array();
foreach ($currentTeam as &$playerSkillVariable)
{
foreach ($currentTeam as &$playerSkillVariable) {
$localPlayerSkillVariable = &$playerSkillVariable;
$currentPlayer = &$localPlayerSkillVariable->getKey();
$playerPerformance = &$this->createOutputVariable($currentPlayer);
$newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVariable, $playerPerformance);
$this->addLayerFactor($newLikelihoodFactor);
$currentTeamPlayerPerformances[] = $playerPerformance;
}
}
$outputVariablesGroups[] = $currentTeamPlayerPerformances;
}
}
private function createLikelihood(KeyedVariable &$playerSkill, KeyedVariable &$playerPerformance)
{
return new GaussianLikelihoodFactor(square($this->getParentFactorGraph()->getGameInfo()->getBeta()), $playerPerformance, $playerSkill);
return new GaussianLikelihoodFactor(
BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),
$playerPerformance,
$playerSkill
);
}
private function &createOutputVariable(&$key)
@ -58,27 +53,23 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
{
$localFactors = &$this->getLocalFactors();
return $this->scheduleSequence(
array_map(
function($likelihood)
{
return new ScheduleStep("Skill to Perf step", $likelihood, 0);
},
$localFactors),
"All skill to performance sending");
array_map(
function ($likelihood) {
return new ScheduleStep("Skill to Perf step", $likelihood, 0);
},
$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);
},
$localFactors),
"All skill to performance sending");
array_map(
function ($likelihood) {
return new ScheduleStep("name", $likelihood, 1);
},
$localFactors),
"All skill to performance sending");
}
}
?>

View File

@ -1,11 +1,4 @@
<?php
namespace Moserware\Skills\TrueSkill\Layers;
require_once(dirname(__FILE__) . "/../DrawMargin.php");
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
require_once(dirname(__FILE__) . "/../Factors/GaussianGreaterThanFactor.php");
require_once(dirname(__FILE__) . "/../Factors/GaussianWithinFactor.php");
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\TrueSkill\DrawMargin;
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
@ -30,8 +23,7 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer
$inputVarGroups = &$this->getInputVariablesGroups();
$inputVarGroupsCount = count($inputVarGroups);
for ($i = 0; $i < $inputVarGroupsCount; $i++)
{
for ($i = 0; $i < $inputVarGroupsCount; $i++) {
$isDraw = ($this->_teamRanks[$i] == $this->_teamRanks[$i + 1]);
$teamDifference = &$inputVarGroups[$i][0];
@ -43,6 +35,4 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer
$this->addLayerFactor($factor);
}
}
}
?>
}

View File

@ -1,13 +1,6 @@
<?php
namespace Moserware\Skills\TrueSkill\Layers;
require_once(dirname(__FILE__) . "/../../FactorGraphs/Variable.php");
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
require_once(dirname(__FILE__) . "/../Factors/GaussianWeightedSumFactor.php");
require_once(dirname(__FILE__) . "/TrueSkillFactorGraphLayer.php");
<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\FactorGraphs\Variable;
use Moserware\Skills\TrueSkill\DrawMargin;
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
use Moserware\Skills\TrueSkill\Factors\GaussianWeightedSumFactor;
@ -51,6 +44,4 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG
$outputVariable = &$this->getParentFactorGraph()->getVariableFactory()->createBasicVariable("Team performance difference");
return $outputVariable;
}
}
?>
}

View File

@ -1,8 +1,4 @@
<?php
namespace Moserware\Skills\TrueSkill\Layers;
require_once(dirname(__FILE__) . "/../../FactorGraphs/FactorGraphLayer.php");
require_once(dirname(__FILE__) . "/../TrueSkillFactorGraph.php");
<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\FactorGraphs\FactorGraphLayer;
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
@ -13,6 +9,4 @@ abstract class TrueSkillFactorGraphLayer extends FactorGraphLayer
{
parent::__construct($parentGraph);
}
}
?>
}