trueskill/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php

99 lines
3.2 KiB
PHP
Raw Normal View History

2022-07-05 15:55:47 +02:00
<?php
2024-02-02 14:53:38 +00:00
declare(strict_types=1);
2022-07-05 15:55:47 +02:00
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;
2023-08-03 09:13:19 +00:00
use DNW\Skills\Player;
2023-08-03 10:09:24 +00:00
use DNW\Skills\Team;
2022-07-05 15:33:34 +02:00
use DNW\Skills\TrueSkill\Factors\GaussianWeightedSumFactor;
2023-08-03 14:10:17 +00:00
use DNW\Skills\FactorGraphs\Variable;
use DNW\Skills\FactorGraphs\KeyedVariable;
class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLayer
{
2023-08-02 10:10:57 +00:00
public function buildLayer(): void
{
$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-02 14:14:10 +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-03 09:13:19 +00:00
/**
2023-08-03 13:08:04 +00:00
* @param KeyedVariable[] $teamMembers
2023-08-03 09:13:19 +00:00
*/
2023-08-02 10:10:57 +00:00
protected function createPlayerToTeamSumFactor(array $teamMembers, Variable $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-02 14:14:10 +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");
}
2023-08-03 13:08:04 +00:00
/**
* @param KeyedVariable[] $team
*/
2023-08-02 10:10:57 +00:00
private function createOutputVariable(array $team): Variable
{
2024-02-02 14:53:38 +00: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
}