trueskill/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php

87 lines
2.9 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;
2023-08-01 12:43:58 +00:00
use DNW\Skills\FactorGraphs\ScheduleSequence;
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];
}
}
2023-08-01 12:43:58 +00:00
public function createPriorSchedule(): ScheduleSequence
{
$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),
2023-08-01 13:53:19 +00:00
$localFactors
),
'all player perf to team perf schedule'
);
}
2023-08-01 13:53:19 +00:00
protected function createPlayerToTeamSumFactor($teamMembers, $sumVariable): GaussianWeightedSumFactor
{
$weights = array_map(
function ($v) {
$player = $v->getKey();
2022-07-05 15:55:47 +02:00
return PartialPlay::getPartialPlayPercentage($player);
},
2023-08-01 13:53:19 +00:00
$teamMembers
);
return new GaussianWeightedSumFactor(
$sumVariable,
$teamMembers,
2023-08-01 13:53:19 +00:00
$weights
);
}
2023-08-01 12:43:58 +00:00
public function createPosteriorSchedule(): ScheduleSequence
{
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++) {
2023-08-01 13:53:19 +00: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);
2023-08-01 13:53:19 +00:00
return $this->getParentFactorGraph()->getVariableFactory()->createBasicVariable('Team[' . $teamMemberNames . "]'s performance");
}
2022-07-05 15:55:47 +02:00
}