trueskill/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php

74 lines
2.5 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;
2022-07-05 15:55:47 +02:00
use DNW\Skills\Numerics\BasicMath;
use DNW\Skills\Rating;
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;
// We intentionally have no Posterior schedule since the only purpose here is to
// start the process.
class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
{
2022-07-05 16:21:06 +02:00
public function __construct(TrueSkillFactorGraph $parentGraph, private readonly array $_teams)
{
parent::__construct($parentGraph);
}
public function buildLayer()
{
$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;
}
}
public function createPriorSchedule()
{
$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'
);
}
2022-07-05 16:21:06 +02:00
private function createPriorFactor(Rating $priorRating, Variable $skillsVariable)
{
return new GaussianPriorFactor(
$priorRating->getMean(),
2016-05-24 15:12:29 +02:00
BasicMath::square($priorRating->getStandardDeviation()) +
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
$skillsVariable
);
}
private function createSkillOutputVariable($key)
{
$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
}