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;
|
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;
|
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
|
|
|
|
{
|
2023-08-03 09:13:19 +00:00
|
|
|
/**
|
|
|
|
* @param Team[] $teams
|
|
|
|
*/
|
2023-08-02 09:15:01 +00:00
|
|
|
public function __construct(TrueSkillFactorGraph $parentGraph, private readonly array $teams)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
parent::__construct($parentGraph);
|
|
|
|
}
|
|
|
|
|
2023-08-02 10:10:57 +00:00
|
|
|
public function buildLayer(): void
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2023-08-02 09:15:01 +00:00
|
|
|
$teams = $this->teams;
|
2016-05-24 16:31:21 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 14:14:10 +00:00
|
|
|
public function createPriorSchedule(): ?ScheduleSequence
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2023-08-02 10:10:57 +00:00
|
|
|
private function createPriorFactor(Rating $priorRating, Variable $skillsVariable): GaussianPriorFactor
|
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
|
|
|
}
|
|
|
|
|
2023-08-08 07:51:05 +00:00
|
|
|
private function createSkillOutputVariable(Player $key): KeyedVariable
|
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
|
|
|
}
|