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

86 lines
2.8 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\ScheduleStep;
use DNW\Skills\FactorGraphs\Variable;
2023-08-02 09:36:44 +00:00
use DNW\Skills\FactorGraphs\KeyedVariable;
2022-07-05 15:55:47 +02:00
use DNW\Skills\Numerics\BasicMath;
use DNW\Skills\Rating;
2023-08-03 09:13:19 +00:00
use DNW\Skills\Team;
2023-08-08 07:51:05 +00:00
use DNW\Skills\Player;
2022-07-05 15:33:34 +02:00
use DNW\Skills\TrueSkill\Factors\GaussianPriorFactor;
2022-07-05 15:55:47 +02:00
use DNW\Skills\TrueSkill\TrueSkillFactorGraph;
2023-08-02 10:10:57 +00:00
use DNW\Skills\FactorGraphs\ScheduleSequence;
// We intentionally have no Posterior schedule since the only purpose here is to
// start the process.
final class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
{
2023-08-03 09:13:19 +00:00
/**
* @param Team[] $teams
*/
public function __construct(TrueSkillFactorGraph $parentGraph, private readonly array $teams)
{
parent::__construct($parentGraph);
}
2025-01-28 09:20:03 +00:00
#[\Override]
2023-08-02 10:10:57 +00:00
public function buildLayer(): void
{
$teams = $this->teams;
foreach ($teams as $currentTeam) {
$localCurrentTeam = $currentTeam;
2022-07-05 15:55:47 +02:00
$currentTeamSkills = [];
2024-07-04 09:38:03 +00:00
$curTeamAllPlayers = $localCurrentTeam->getAllPlayers();
foreach ($curTeamAllPlayers as $curTeamPlayer) {
$localCurTeamPlayer = $curTeamPlayer;
$curTeamPlayerRating = $currentTeam->getRating($localCurTeamPlayer);
$playerSkill = $this->createSkillOutputVariable($localCurTeamPlayer);
$priorFactor = $this->createPriorFactor($curTeamPlayerRating, $playerSkill);
$this->addLayerFactor($priorFactor);
$currentTeamSkills[] = $playerSkill;
}
$outputVariablesGroups = &$this->getOutputVariablesGroups();
$outputVariablesGroups[] = $currentTeamSkills;
}
}
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 priors
return $this->scheduleSequence(
array_map(
//Prior to Skill Step
static fn($prior): ScheduleStep => new ScheduleStep($prior, 0),
2023-08-01 13:53:19 +00:00
$localFactors
2024-03-19 16:24:19 +00:00
)
2023-08-01 13:53:19 +00:00
);
}
2023-08-02 10:10:57 +00:00
private function createPriorFactor(Rating $priorRating, Variable $skillsVariable): GaussianPriorFactor
{
return new GaussianPriorFactor(
$priorRating->getMean(),
2016-05-24 15:12:29 +02:00
BasicMath::square($priorRating->getStandardDeviation()) +
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
$skillsVariable
);
}
2023-08-08 07:51:05 +00:00
private function createSkillOutputVariable(Player $key): KeyedVariable
{
$parentFactorGraph = $this->getParentFactorGraph();
$variableFactory = $parentFactorGraph->getVariableFactory();
2022-07-05 15:55:47 +02:00
return $variableFactory->createKeyedVariable($key);
}
2022-07-05 15:55:47 +02:00
}