| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 14:53:38 +00:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | 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 | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-01 13:35:44 +00:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         TrueSkillFactorGraph $parentGraph, | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |         private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $TeamPerformancesToTeamPerformanceDifferencesLayer, | 
					
						
							|  |  |  |         private readonly TeamDifferencesComparisonLayer $TeamDifferencesComparisonLayer | 
					
						
							| 
									
										
										
										
											2023-08-01 13:35:44 +00:00
										 |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |         parent::__construct($parentGraph); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 11:26:38 +00:00
										 |  |  |     public function getLocalFactors(): array | 
					
						
							| 
									
										
										
										
											2010-10-02 22:28:25 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |         return array_merge( | 
					
						
							|  |  |  |             $this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(), | 
					
						
							|  |  |  |             $this->TeamDifferencesComparisonLayer->getLocalFactors() | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-10-02 22:28:25 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 12:39:42 +00:00
										 |  |  |     public function buildLayer(): void | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |         $inputVariablesGroups = $this->getInputVariablesGroups(); | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |         $this->TeamPerformancesToTeamPerformanceDifferencesLayer->setInputVariablesGroups($inputVariablesGroups); | 
					
						
							|  |  |  |         $this->TeamPerformancesToTeamPerformanceDifferencesLayer->buildLayer(); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |         $teamDifferencesOutputVariablesGroups = $this->TeamPerformancesToTeamPerformanceDifferencesLayer->getOutputVariablesGroups(); | 
					
						
							|  |  |  |         $this->TeamDifferencesComparisonLayer->setInputVariablesGroups($teamDifferencesOutputVariablesGroups); | 
					
						
							|  |  |  |         $this->TeamDifferencesComparisonLayer->buildLayer(); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 14:14:10 +00:00
										 |  |  |     public function createPriorSchedule(): ?ScheduleSequence | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-03 13:08:04 +00: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
 | 
					
						
							| 
									
										
										
										
											2023-08-02 13:29:14 +00:00
										 |  |  |         $totalTeamDifferences = count($this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors()); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |         $localFactors = $this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors(); | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $firstDifferencesFactor = $localFactors[0]; | 
					
						
							|  |  |  |         $lastDifferencesFactor = $localFactors[$totalTeamDifferences - 1]; | 
					
						
							| 
									
										
										
										
											2010-10-02 14:17:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-05 16:21:06 +02:00
										 |  |  |         return 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', | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                     $firstDifferencesFactor, | 
					
						
							|  |  |  |                     1 | 
					
						
							|  |  |  |                 ), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |                 new ScheduleStep( | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |                     sprintf('teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2', $totalTeamDifferences), | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                     $lastDifferencesFactor, | 
					
						
							|  |  |  |                     2 | 
					
						
							|  |  |  |                 ), | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |             ] | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 14:14:10 +00:00
										 |  |  |     private function createTwoTeamInnerPriorLoopSchedule(): ScheduleSequence | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00: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, | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                 0 | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |             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, | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                 0 | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2010-09-25 22:40:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $this->scheduleSequence( | 
					
						
							|  |  |  |             $itemsToSequence, | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |             'loop of just two teams inner sequence' | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 14:14:10 +00:00
										 |  |  |     private function createMultipleTeamInnerPriorLoopSchedule(): ScheduleLoop | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-02 14:14:10 +00:00
										 |  |  |         $totalTeamDifferences = count($this->TeamPerformancesToTeamPerformanceDifferencesLayer->getLocalFactors()); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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++) { | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00: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), | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                             $currentTeamPerfToTeamPerfDiff, | 
					
						
							|  |  |  |                             0 | 
					
						
							|  |  |  |                         ), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |                         new ScheduleStep( | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |                             sprintf('greater than or within result factor %d', $i), | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                             $currentTeamDiffComparison, | 
					
						
							|  |  |  |                             0 | 
					
						
							|  |  |  |                         ), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |                         new ScheduleStep( | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |                             sprintf('team perf to perf diff factors [%d], 2', $i), | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                             $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++) { | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00: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), | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                         $differencesFactor, | 
					
						
							|  |  |  |                         0 | 
					
						
							| 
									
										
										
										
											2023-08-01 13:35:44 +00:00
										 |  |  |                     ), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |                     new ScheduleStep( | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |                         sprintf('greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0', $i), | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                         $comparisonFactor, | 
					
						
							|  |  |  |                         0 | 
					
						
							| 
									
										
										
										
											2023-08-01 13:35:44 +00:00
										 |  |  |                     ), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |                     new ScheduleStep( | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |                         sprintf('teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1', $i), | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                         $performancesToDifferencesFactor, | 
					
						
							|  |  |  |                         1 | 
					
						
							| 
									
										
										
										
											2023-08-01 13:35:44 +00:00
										 |  |  |                     ), | 
					
						
							| 
									
										
										
										
											2023-08-01 13:53:19 +00:00
										 |  |  |                 ] | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											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', | 
					
						
							| 
									
										
										
										
											2023-08-01 13:35:44 +00:00
										 |  |  |                 [$forwardSchedule, $backwardSchedule] | 
					
						
							|  |  |  |             ); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $initialMaxDelta = 0.0001; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-05 16:21:06 +02:00
										 |  |  |         return 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, | 
					
						
							| 
									
										
										
										
											2023-08-01 13:35:44 +00:00
										 |  |  |             $initialMaxDelta | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | } |