| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace DNW\Skills\TrueSkill\Layers; | 
					
						
							| 
									
										
										
										
											2010-09-18 21:19:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-05 15:33:34 +02:00
										 |  |  | use DNW\Skills\FactorGraphs\KeyedVariable; | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | use DNW\Skills\FactorGraphs\ScheduleStep; | 
					
						
							| 
									
										
										
										
											2022-07-05 15:33:34 +02:00
										 |  |  | use DNW\Skills\Numerics\BasicMath; | 
					
						
							|  |  |  | use DNW\Skills\TrueSkill\Factors\GaussianLikelihoodFactor; | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | use DNW\Skills\TrueSkill\TrueSkillFactorGraph; | 
					
						
							| 
									
										
										
										
											2010-09-18 21:19:51 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function buildLayer() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |         $inputVariablesGroups = $this->getInputVariablesGroups(); | 
					
						
							| 
									
										
										
										
											2010-09-30 08:25:31 -04:00
										 |  |  |         $outputVariablesGroups = &$this->getOutputVariablesGroups(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |         foreach ($inputVariablesGroups as $currentTeam) { | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |             $currentTeamPlayerPerformances = []; | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |             foreach ($currentTeam as $playerSkillVariable) { | 
					
						
							|  |  |  |                 $localPlayerSkillVariable = $playerSkillVariable; | 
					
						
							|  |  |  |                 $currentPlayer = $localPlayerSkillVariable->getKey(); | 
					
						
							|  |  |  |                 $playerPerformance = $this->createOutputVariable($currentPlayer); | 
					
						
							| 
									
										
										
										
											2010-10-02 21:15:47 -04:00
										 |  |  |                 $newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVariable, $playerPerformance); | 
					
						
							| 
									
										
										
										
											2010-09-25 22:16:47 -04:00
										 |  |  |                 $this->addLayerFactor($newLikelihoodFactor); | 
					
						
							| 
									
										
										
										
											2010-09-27 22:26:28 -04:00
										 |  |  |                 $currentTeamPlayerPerformances[] = $playerPerformance; | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-27 22:26:28 -04:00
										 |  |  |             $outputVariablesGroups[] = $currentTeamPlayerPerformances; | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |     private function createLikelihood(KeyedVariable $playerSkill, KeyedVariable $playerPerformance) | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |         return new GaussianLikelihoodFactor( | 
					
						
							| 
									
										
										
										
											2016-05-24 15:12:29 +02:00
										 |  |  |             BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getBeta()), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |             $playerPerformance, | 
					
						
							|  |  |  |             $playerSkill | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |     private function createOutputVariable($key) | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-05 16:21:06 +02:00
										 |  |  |         return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, $key."'s performance"); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function createPriorSchedule() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |         $localFactors = $this->getLocalFactors(); | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |         return $this->scheduleSequence( | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |             array_map( | 
					
						
							| 
									
										
										
										
											2022-07-05 16:21:06 +02:00
										 |  |  |                 fn($likelihood) => new ScheduleStep('Skill to Perf step', $likelihood, 0), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |                 $localFactors), | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |             'All skill to performance sending'); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function createPosteriorSchedule() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-24 16:31:21 +02:00
										 |  |  |         $localFactors = $this->getLocalFactors(); | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |         return $this->scheduleSequence( | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |             array_map( | 
					
						
							| 
									
										
										
										
											2022-07-05 16:21:06 +02:00
										 |  |  |                 fn($likelihood) => new ScheduleStep('name', $likelihood, 1), | 
					
						
							| 
									
										
										
										
											2016-05-24 14:10:39 +02:00
										 |  |  |                 $localFactors), | 
					
						
							| 
									
										
										
										
											2022-07-05 15:55:47 +02:00
										 |  |  |             'All skill to performance sending'); | 
					
						
							| 
									
										
										
										
											2010-09-18 11:11:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } |