trueskill/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php

79 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;
2022-07-05 15:55:47 +02:00
use DNW\Skills\PartialPlay;
2022-07-05 15:33:34 +02:00
use DNW\Skills\TrueSkill\Factors\GaussianWeightedSumFactor;
class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLayer
{
public function buildLayer()
{
$inputVariablesGroups = $this->getInputVariablesGroups();
foreach ($inputVariablesGroups as $currentTeam) {
$localCurrentTeam = $currentTeam;
$teamPerformance = $this->createOutputVariable($localCurrentTeam);
$newSumFactor = $this->createPlayerToTeamSumFactor($localCurrentTeam, $teamPerformance);
$this->addLayerFactor($newSumFactor);
// REVIEW: Does it make sense to have groups of one?
$outputVariablesGroups = &$this->getOutputVariablesGroups();
2022-07-05 15:55:47 +02:00
$outputVariablesGroups[] = [$teamPerformance];
}
}
public function createPriorSchedule()
{
$localFactors = $this->getLocalFactors();
2010-09-30 08:25:31 -04:00
2022-07-05 16:21:06 +02:00
return $this->scheduleSequence(
array_map(
2022-07-05 16:32:18 +02:00
fn ($weightedSumFactor) => new ScheduleStep('Perf to Team Perf Step', $weightedSumFactor, 0),
$localFactors),
2022-07-05 15:55:47 +02:00
'all player perf to team perf schedule');
}
protected function createPlayerToTeamSumFactor($teamMembers, $sumVariable)
{
$weights = array_map(
function ($v) {
$player = $v->getKey();
2022-07-05 15:55:47 +02:00
return PartialPlay::getPartialPlayPercentage($player);
},
$teamMembers);
return new GaussianWeightedSumFactor(
$sumVariable,
$teamMembers,
$weights);
}
public function createPosteriorSchedule()
{
2022-07-05 15:55:47 +02:00
$allFactors = [];
$localFactors = $this->getLocalFactors();
foreach ($localFactors as $currentFactor) {
$localCurrentFactor = $currentFactor;
$numberOfMessages = $localCurrentFactor->getNumberOfMessages();
for ($currentIteration = 1; $currentIteration < $numberOfMessages; $currentIteration++) {
2022-07-05 15:55:47 +02:00
$allFactors[] = new ScheduleStep('team sum perf @'.$currentIteration,
$localCurrentFactor, $currentIteration);
2010-09-18 15:24:36 -04:00
}
}
2022-07-05 15:55:47 +02:00
2010-09-18 15:24:36 -04:00
return $this->scheduleSequence($allFactors, "all of the team's sum iterations");
}
private function createOutputVariable($team)
{
2022-07-05 16:32:18 +02:00
$memberNames = array_map(fn ($currentPlayer) => (string) ($currentPlayer->getKey()), $team);
2022-07-05 15:55:47 +02:00
$teamMemberNames = \implode(', ', $memberNames);
2022-07-05 16:21:06 +02:00
return $this->getParentFactorGraph()->getVariableFactory()->createBasicVariable('Team['.$teamMemberNames."]'s performance");
}
2022-07-05 15:55:47 +02:00
}