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

81 lines
2.7 KiB
PHP
Raw Normal View History

2022-07-05 15:55:47 +02:00
<?php
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.
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);
}
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 = [];
$currentTeamAllPlayers = $localCurrentTeam->getAllPlayers();
foreach ($currentTeamAllPlayers as $currentTeamPlayer) {
$localCurrentTeamPlayer = $currentTeamPlayer;
$currentTeamPlayerRating = $currentTeam->getRating($localCurrentTeamPlayer);
$playerSkill = $this->createSkillOutputVariable($localCurrentTeamPlayer);
2022-07-05 16:21:06 +02:00
$priorFactor = $this->createPriorFactor($currentTeamPlayerRating, $playerSkill);
$this->addLayerFactor($priorFactor);
$currentTeamSkills[] = $playerSkill;
}
$outputVariablesGroups = &$this->getOutputVariablesGroups();
$outputVariablesGroups[] = $currentTeamSkills;
}
}
2023-08-02 14:14:10 +00:00
public function createPriorSchedule(): ?ScheduleSequence
{
$localFactors = $this->getLocalFactors();
2022-07-05 15:55:47 +02:00
return $this->scheduleSequence(
array_map(
2022-07-05 16:32:18 +02:00
fn ($prior) => new ScheduleStep('Prior to Skill Step', $prior, 0),
2023-08-01 13:53:19 +00:00
$localFactors
),
'All priors'
);
}
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
2023-08-01 13:53:19 +00:00
return $variableFactory->createKeyedVariable($key, $key . "'s skill");
}
2022-07-05 15:55:47 +02:00
}