Files
trueskill/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php

84 lines
2.7 KiB
PHP
Raw Normal View History

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;
2022-07-05 15:33:34 +02:00
use DNW\Skills\FactorGraphs\KeyedVariable;
2024-02-29 10:42:31 +00:00
use DNW\Skills\FactorGraphs\Variable;
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;
2023-08-01 12:13:24 +00:00
use DNW\Skills\FactorGraphs\ScheduleSequence;
final class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
{
2025-01-28 09:20:03 +00:00
#[\Override]
2023-08-02 09:04:56 +00:00
public function buildLayer(): void
{
2024-07-04 09:38:03 +00:00
$inputVarGroups = $this->getInputVariablesGroups();
$outputVarGroups = &$this->getOutputVariablesGroups();
2010-09-30 08:25:31 -04:00
2024-07-04 09:38:03 +00:00
foreach ($inputVarGroups as $currentTeam) {
2022-07-05 15:55:47 +02:00
$currentTeamPlayerPerformances = [];
2024-02-29 10:42:31 +00:00
/**
2024-07-04 09:38:03 +00:00
* @var Variable $playerSkillVar
2024-02-29 10:42:31 +00:00
*/
2024-07-04 09:38:03 +00:00
foreach ($currentTeam as $playerSkillVar) {
$localPlayerSkillVar = $playerSkillVar;
$currentPlayer = ($localPlayerSkillVar instanceof KeyedVariable) ? $localPlayerSkillVar->getKey() : "";
$playerPerformance = $this->createOutputVariable($currentPlayer);
2024-07-04 09:38:03 +00:00
$newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVar, $playerPerformance);
$this->addLayerFactor($newLikelihoodFactor);
$currentTeamPlayerPerformances[] = $playerPerformance;
}
2024-07-04 09:38:03 +00:00
$outputVarGroups[] = $currentTeamPlayerPerformances;
}
}
2024-02-29 10:42:31 +00:00
private function createLikelihood(Variable $playerSkill, Variable $playerPerformance): GaussianLikelihoodFactor
{
return new GaussianLikelihoodFactor(
2016-05-24 15:12:29 +02:00
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),
$playerPerformance,
$playerSkill
);
}
2023-08-02 09:36:44 +00:00
private function createOutputVariable(mixed $key): KeyedVariable
{
return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key);
}
2025-01-28 09:20:03 +00:00
#[\Override]
2025-04-29 13:21:47 +00:00
public function createPriorSchedule(): ScheduleSequence
{
$localFactors = $this->getLocalFactors();
2022-07-05 15:55:47 +02:00
2024-03-19 16:24:19 +00:00
//All skill to performance sending
return $this->scheduleSequence(
array_map(
//Skill to Perf step
static fn($likelihood): ScheduleStep => new ScheduleStep($likelihood, 0),
2023-08-01 13:35:44 +00:00
$localFactors
2024-03-19 16:24:19 +00:00
)
2023-08-01 13:35:44 +00:00
);
}
2025-01-28 09:20:03 +00:00
#[\Override]
2025-04-29 13:21:47 +00:00
public function createPosteriorSchedule(): ScheduleSequence
{
$localFactors = $this->getLocalFactors();
2022-07-05 15:55:47 +02:00
2024-03-19 16:24:19 +00:00
//All skill to performance sending
return $this->scheduleSequence(
array_map(
static fn($likelihood): ScheduleStep => new ScheduleStep($likelihood, 1),
2023-08-01 13:35:44 +00:00
$localFactors
2024-03-19 16:24:19 +00:00
)
2023-08-01 13:35:44 +00:00
);
}
}