2022-07-05 15:55:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DNW\Skills\TrueSkill\Layers;
|
2010-09-18 17:56:57 -04:00
|
|
|
|
2022-07-05 15:33:34 +02:00
|
|
|
use DNW\Skills\FactorGraphs\ScheduleLoop;
|
|
|
|
use DNW\Skills\FactorGraphs\ScheduleSequence;
|
|
|
|
use DNW\Skills\FactorGraphs\ScheduleStep;
|
|
|
|
use DNW\Skills\TrueSkill\TrueSkillFactorGraph;
|
2022-07-05 15:55:47 +02:00
|
|
|
use Exception;
|
2010-09-18 17:56:57 -04:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
// The whole purpose of this is to do a loop on the bottom
|
|
|
|
class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
|
|
|
|
{
|
|
|
|
private $_TeamDifferencesComparisonLayer;
|
2022-07-05 15:55:47 +02:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
private $_TeamPerformancesToTeamPerformanceDifferencesLayer;
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
public function __construct(TrueSkillFactorGraph $parentGraph,
|
|
|
|
TeamPerformancesToTeamPerformanceDifferencesLayer $teamPerformancesToPerformanceDifferences,
|
|
|
|
TeamDifferencesComparisonLayer $teamDifferencesComparisonLayer)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
parent::__construct($parentGraph);
|
|
|
|
$this->_TeamPerformancesToTeamPerformanceDifferencesLayer = $teamPerformancesToPerformanceDifferences;
|
|
|
|
$this->_TeamDifferencesComparisonLayer = $teamDifferencesComparisonLayer;
|
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
public function getLocalFactors()
|
2010-10-02 22:28:25 -04:00
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$localFactors = array_merge($this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(),
|
|
|
|
$this->_TeamDifferencesComparisonLayer->getLocalFactors()
|
|
|
|
);
|
|
|
|
|
2010-10-02 22:28:25 -04:00
|
|
|
return $localFactors;
|
|
|
|
}
|
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
public function buildLayer()
|
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$inputVariablesGroups = $this->getInputVariablesGroups();
|
2010-09-25 22:16:47 -04:00
|
|
|
$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->setInputVariablesGroups($inputVariablesGroups);
|
2010-09-18 11:11:44 -04:00
|
|
|
$this->_TeamPerformancesToTeamPerformanceDifferencesLayer->buildLayer();
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
$teamDifferencesOutputVariablesGroups = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getOutputVariablesGroups();
|
2010-09-30 08:25:31 -04:00
|
|
|
$this->_TeamDifferencesComparisonLayer->setInputVariablesGroups($teamDifferencesOutputVariablesGroups);
|
2010-09-18 11:11:44 -04:00
|
|
|
$this->_TeamDifferencesComparisonLayer->buildLayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createPriorSchedule()
|
2016-05-24 14:10:39 +02:00
|
|
|
{
|
|
|
|
switch (count($this->getInputVariablesGroups())) {
|
2010-09-18 11:11:44 -04:00
|
|
|
case 0:
|
|
|
|
case 1:
|
2016-05-24 16:31:21 +02:00
|
|
|
throw new Exception('InvalidOperation');
|
2010-09-18 11:11:44 -04:00
|
|
|
case 2:
|
|
|
|
$loop = $this->createTwoTeamInnerPriorLoopSchedule();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$loop = $this->createMultipleTeamInnerPriorLoopSchedule();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When dealing with differences, there are always (n-1) differences, so add in the 1
|
|
|
|
$totalTeamDifferences = count($this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors());
|
|
|
|
$totalTeams = $totalTeamDifferences + 1;
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
$localFactors = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
|
|
|
|
|
|
|
|
$firstDifferencesFactor = $localFactors[0];
|
|
|
|
$lastDifferencesFactor = $localFactors[$totalTeamDifferences - 1];
|
2010-10-02 14:17:48 -04:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
$innerSchedule = new ScheduleSequence(
|
2022-07-05 15:55:47 +02:00
|
|
|
'inner schedule',
|
|
|
|
[
|
2016-05-24 14:10:39 +02:00
|
|
|
$loop,
|
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
'teamPerformanceToPerformanceDifferenceFactors[0] @ 1',
|
2016-05-24 14:10:39 +02:00
|
|
|
$firstDifferencesFactor, 1),
|
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
sprintf('teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2', $totalTeamDifferences),
|
|
|
|
$lastDifferencesFactor, 2),
|
|
|
|
]
|
2016-05-24 14:10:39 +02:00
|
|
|
);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2010-09-25 22:40:56 -04:00
|
|
|
return $innerSchedule;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createTwoTeamInnerPriorLoopSchedule()
|
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
|
|
|
|
$teamDifferencesComparisonLayerLocalFactors = $this->_TeamDifferencesComparisonLayer->getLocalFactors();
|
2010-09-18 15:24:36 -04:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
$firstPerfToTeamDiff = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0];
|
|
|
|
$firstTeamDiffComparison = $teamDifferencesComparisonLayerLocalFactors[0];
|
2022-07-05 15:55:47 +02:00
|
|
|
$itemsToSequence = [
|
2016-05-24 14:10:39 +02:00
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
'send team perf to perf differences',
|
2016-05-24 14:10:39 +02:00
|
|
|
$firstPerfToTeamDiff,
|
|
|
|
0),
|
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
'send to greater than or within factor',
|
2016-05-24 14:10:39 +02:00
|
|
|
$firstTeamDiffComparison,
|
2022-07-05 15:55:47 +02:00
|
|
|
0),
|
|
|
|
];
|
2010-09-25 22:40:56 -04:00
|
|
|
|
|
|
|
return $this->scheduleSequence(
|
|
|
|
$itemsToSequence,
|
2022-07-05 15:55:47 +02:00
|
|
|
'loop of just two teams inner sequence');
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createMultipleTeamInnerPriorLoopSchedule()
|
|
|
|
{
|
|
|
|
$totalTeamDifferences = count($this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors());
|
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
$forwardScheduleList = [];
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2016-05-24 14:10:39 +02:00
|
|
|
for ($i = 0; $i < $totalTeamDifferences - 1; $i++) {
|
2016-05-24 16:31:21 +02:00
|
|
|
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
|
|
|
|
$teamDifferencesComparisonLayerLocalFactors = $this->_TeamDifferencesComparisonLayer->getLocalFactors();
|
2010-09-18 15:24:36 -04:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
$currentTeamPerfToTeamPerfDiff = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$i];
|
|
|
|
$currentTeamDiffComparison = $teamDifferencesComparisonLayerLocalFactors[$i];
|
2010-10-02 21:15:47 -04:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
$currentForwardSchedulePiece =
|
|
|
|
$this->scheduleSequence(
|
2022-07-05 15:55:47 +02:00
|
|
|
[
|
2016-05-24 14:10:39 +02:00
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
sprintf('team perf to perf diff %d', $i),
|
2016-05-24 14:10:39 +02:00
|
|
|
$currentTeamPerfToTeamPerfDiff, 0),
|
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
sprintf('greater than or within result factor %d', $i),
|
2016-05-24 14:10:39 +02:00
|
|
|
$currentTeamDiffComparison, 0),
|
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
sprintf('team perf to perf diff factors [%d], 2', $i),
|
|
|
|
$currentTeamPerfToTeamPerfDiff, 2),
|
|
|
|
], sprintf('current forward schedule piece %d', $i));
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
$forwardScheduleList[] = $currentForwardSchedulePiece;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
$forwardSchedule = new ScheduleSequence('forward schedule', $forwardScheduleList);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
$backwardScheduleList = [];
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2016-05-24 14:10:39 +02:00
|
|
|
for ($i = 0; $i < $totalTeamDifferences - 1; $i++) {
|
2016-05-24 16:31:21 +02:00
|
|
|
$teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors = $this->_TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors();
|
|
|
|
$teamDifferencesComparisonLayerLocalFactors = $this->_TeamDifferencesComparisonLayer->getLocalFactors();
|
2010-09-18 15:24:36 -04:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
$differencesFactor = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i];
|
|
|
|
$comparisonFactor = $teamDifferencesComparisonLayerLocalFactors[$totalTeamDifferences - 1 - $i];
|
|
|
|
$performancesToDifferencesFactor = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i];
|
2010-09-30 08:25:31 -04:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
$currentBackwardSchedulePiece = new ScheduleSequence(
|
2022-07-05 15:55:47 +02:00
|
|
|
'current backward schedule piece',
|
|
|
|
[
|
2016-05-24 14:10:39 +02:00
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
sprintf('teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0', $i),
|
2016-05-24 14:10:39 +02:00
|
|
|
$differencesFactor, 0),
|
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
sprintf('greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0', $i),
|
2016-05-24 14:10:39 +02:00
|
|
|
$comparisonFactor, 0),
|
|
|
|
new ScheduleStep(
|
2022-07-05 15:55:47 +02:00
|
|
|
sprintf('teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1', $i),
|
|
|
|
$performancesToDifferencesFactor, 1),
|
|
|
|
]);
|
2010-09-30 08:25:31 -04:00
|
|
|
$backwardScheduleList[] = $currentBackwardSchedulePiece;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
$backwardSchedule = new ScheduleSequence('backward schedule', $backwardScheduleList);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
|
|
|
$forwardBackwardScheduleToLoop =
|
|
|
|
new ScheduleSequence(
|
2022-07-05 15:55:47 +02:00
|
|
|
'forward Backward Schedule To Loop',
|
|
|
|
[$forwardSchedule, $backwardSchedule]);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
|
|
|
$initialMaxDelta = 0.0001;
|
|
|
|
|
|
|
|
$loop = new ScheduleLoop(
|
2022-07-05 15:55:47 +02:00
|
|
|
sprintf('loop with max delta of %f', $initialMaxDelta),
|
2010-09-18 11:11:44 -04:00
|
|
|
$forwardBackwardScheduleToLoop,
|
|
|
|
$initialMaxDelta);
|
|
|
|
|
|
|
|
return $loop;
|
|
|
|
}
|
2022-07-05 15:55:47 +02:00
|
|
|
}
|