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;
|
2010-09-18 17:56:57 -04:00
|
|
|
|
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;
|
2010-09-18 17:56:57 -04:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLayer
|
|
|
|
{
|
2023-08-02 10:10:57 +00:00
|
|
|
public function buildLayer(): void
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$inputVariablesGroups = $this->getInputVariablesGroups();
|
|
|
|
foreach ($inputVariablesGroups as $currentTeam) {
|
|
|
|
$localCurrentTeam = $currentTeam;
|
|
|
|
$teamPerformance = $this->createOutputVariable($localCurrentTeam);
|
2010-10-02 21:15:47 -04:00
|
|
|
$newSumFactor = $this->createPlayerToTeamSumFactor($localCurrentTeam, $teamPerformance);
|
2016-05-24 14:10:39 +02:00
|
|
|
|
2010-09-25 22:16:47 -04:00
|
|
|
$this->addLayerFactor($newSumFactor);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
|
|
|
// REVIEW: Does it make sense to have groups of one?
|
2010-09-25 22:16:47 -04:00
|
|
|
$outputVariablesGroups = &$this->getOutputVariablesGroups();
|
2022-07-05 15:55:47 +02:00
|
|
|
$outputVariablesGroups[] = [$teamPerformance];
|
2016-05-24 14:10:39 +02:00
|
|
|
}
|
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();
|
2010-09-30 08:25:31 -04:00
|
|
|
|
2022-07-05 16:21:06 +02:00
|
|
|
return $this->scheduleSequence(
|
2016-05-24 14:10:39 +02:00
|
|
|
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'
|
|
|
|
);
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
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
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2010-09-25 19:26:57 -04:00
|
|
|
$weights = array_map(
|
2016-05-24 14:10:39 +02:00
|
|
|
function ($v) {
|
2016-05-24 16:31:21 +02:00
|
|
|
$player = $v->getKey();
|
2022-07-05 15:55:47 +02:00
|
|
|
|
2016-05-24 14:10:39 +02:00
|
|
|
return PartialPlay::getPartialPlayPercentage($player);
|
|
|
|
},
|
2023-08-01 13:53:19 +00:00
|
|
|
$teamMembers
|
|
|
|
);
|
2010-09-25 19:26:57 -04:00
|
|
|
|
|
|
|
return new GaussianWeightedSumFactor(
|
2016-05-24 14:10:39 +02:00
|
|
|
$sumVariable,
|
|
|
|
$teamMembers,
|
2023-08-01 13:53:19 +00:00
|
|
|
$weights
|
|
|
|
);
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2023-08-02 14:14:10 +00:00
|
|
|
public function createPosteriorSchedule(): ?ScheduleSequence
|
2016-05-24 14:10:39 +02:00
|
|
|
{
|
2022-07-05 15:55:47 +02:00
|
|
|
$allFactors = [];
|
2016-05-24 16:31:21 +02:00
|
|
|
$localFactors = $this->getLocalFactors();
|
|
|
|
foreach ($localFactors as $currentFactor) {
|
|
|
|
$localCurrentFactor = $currentFactor;
|
2010-10-02 21:15:47 -04:00
|
|
|
$numberOfMessages = $localCurrentFactor->getNumberOfMessages();
|
2016-05-24 14:10:39 +02:00
|
|
|
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");
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
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
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2024-02-02 14:53:38 +00:00
|
|
|
$memberNames = array_map(fn ($currentPlayer) => (string)($currentPlayer->getKey()), $team);
|
2010-09-26 21:43:17 -04:00
|
|
|
|
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");
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
2022-07-05 15:55:47 +02:00
|
|
|
}
|