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\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;
|
2010-09-18 21:19:51 -04:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
// We intentionally have no Posterior schedule since the only purpose here is to
|
2010-09-25 10:15:51 -04:00
|
|
|
// start the process.
|
2010-09-18 11:11:44 -04:00
|
|
|
class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
|
|
|
|
{
|
2022-07-05 16:21:06 +02:00
|
|
|
public function __construct(TrueSkillFactorGraph $parentGraph, private readonly array $_teams)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
parent::__construct($parentGraph);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildLayer()
|
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$teams = $this->_teams;
|
|
|
|
foreach ($teams as $currentTeam) {
|
|
|
|
$localCurrentTeam = $currentTeam;
|
2022-07-05 15:55:47 +02:00
|
|
|
$currentTeamSkills = [];
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2010-10-02 21:15:47 -04:00
|
|
|
$currentTeamAllPlayers = $localCurrentTeam->getAllPlayers();
|
2016-05-24 16:31:21 +02:00
|
|
|
foreach ($currentTeamAllPlayers as $currentTeamPlayer) {
|
|
|
|
$localCurrentTeamPlayer = $currentTeamPlayer;
|
2010-10-02 21:15:47 -04:00
|
|
|
$currentTeamPlayerRating = $currentTeam->getRating($localCurrentTeamPlayer);
|
2016-05-24 16:31:21 +02:00
|
|
|
$playerSkill = $this->createSkillOutputVariable($localCurrentTeamPlayer);
|
2022-07-05 16:21:06 +02:00
|
|
|
$priorFactor = $this->createPriorFactor($currentTeamPlayerRating, $playerSkill);
|
2010-09-25 18:25:56 -04:00
|
|
|
$this->addLayerFactor($priorFactor);
|
2010-09-18 11:11:44 -04:00
|
|
|
$currentTeamSkills[] = $playerSkill;
|
|
|
|
}
|
|
|
|
|
2010-09-25 18:25:56 -04:00
|
|
|
$outputVariablesGroups = &$this->getOutputVariablesGroups();
|
2010-09-18 21:19:51 -04:00
|
|
|
$outputVariablesGroups[] = $currentTeamSkills;
|
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: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'
|
|
|
|
);
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:21:06 +02:00
|
|
|
private function createPriorFactor(Rating $priorRating, Variable $skillsVariable)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2016-05-24 14:10:39 +02:00
|
|
|
return new GaussianPriorFactor(
|
|
|
|
$priorRating->getMean(),
|
2016-05-24 15:12:29 +02:00
|
|
|
BasicMath::square($priorRating->getStandardDeviation()) +
|
|
|
|
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
|
2016-05-24 14:10:39 +02:00
|
|
|
$skillsVariable
|
|
|
|
);
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
private function createSkillOutputVariable($key)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$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");
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
2022-07-05 15:55:47 +02:00
|
|
|
}
|