trueskill/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.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\KeyedVariable;
2022-07-05 15:55:47 +02:00
use DNW\Skills\FactorGraphs\ScheduleStep;
2022-07-05 15:33:34 +02:00
use DNW\Skills\Numerics\BasicMath;
use DNW\Skills\TrueSkill\Factors\GaussianLikelihoodFactor;
2023-08-01 12:13:24 +00:00
use DNW\Skills\FactorGraphs\ScheduleSequence;
class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
{
2023-08-02 09:04:56 +00:00
public function buildLayer(): void
{
$inputVariablesGroups = $this->getInputVariablesGroups();
2010-09-30 08:25:31 -04:00
$outputVariablesGroups = &$this->getOutputVariablesGroups();
foreach ($inputVariablesGroups as $currentTeam) {
2022-07-05 15:55:47 +02:00
$currentTeamPlayerPerformances = [];
foreach ($currentTeam as $playerSkillVariable) {
$localPlayerSkillVariable = $playerSkillVariable;
$currentPlayer = $localPlayerSkillVariable->getKey();
$playerPerformance = $this->createOutputVariable($currentPlayer);
$newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVariable, $playerPerformance);
$this->addLayerFactor($newLikelihoodFactor);
$currentTeamPlayerPerformances[] = $playerPerformance;
}
$outputVariablesGroups[] = $currentTeamPlayerPerformances;
}
}
2023-08-01 12:13:24 +00:00
private function createLikelihood(KeyedVariable $playerSkill, KeyedVariable $playerPerformance): GaussianLikelihoodFactor
{
return new GaussianLikelihoodFactor(
2016-05-24 15:12:29 +02:00
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),
$playerPerformance,
$playerSkill
);
}
2023-08-02 09:36:44 +00:00
private function createOutputVariable(mixed $key): KeyedVariable
{
2023-08-01 13:35:44 +00:00
return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, $key . "'s performance");
}
2023-08-01 12:13:24 +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 ($likelihood) => new ScheduleStep('Skill to Perf step', $likelihood, 0),
2023-08-01 13:35:44 +00:00
$localFactors
),
'All skill to performance sending'
);
}
2023-08-01 12:13:24 +00:00
public function createPosteriorSchedule(): 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 ($likelihood) => new ScheduleStep('name', $likelihood, 1),
2023-08-01 13:35:44 +00:00
$localFactors
),
'All skill to performance sending'
);
}
}