2022-07-05 15:55:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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;
|
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;
|
|
|
|
use DNW\Skills\TrueSkill\TrueSkillFactorGraph;
|
2010-09-18 17:56:57 -04:00
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLayer
|
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
public function __construct(TrueSkillFactorGraph $parentGraph)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
parent::__construct($parentGraph);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildLayer()
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function createPriorSchedule()
|
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$localFactors = $this->getLocalFactors();
|
2010-09-30 08:25:31 -04:00
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
$sequence = $this->scheduleSequence(
|
2016-05-24 14:10:39 +02:00
|
|
|
array_map(
|
|
|
|
function ($weightedSumFactor) {
|
2022-07-05 15:55:47 +02:00
|
|
|
return new ScheduleStep('Perf to Team Perf Step', $weightedSumFactor, 0);
|
2016-05-24 14:10:39 +02:00
|
|
|
},
|
|
|
|
$localFactors),
|
2022-07-05 15:55:47 +02:00
|
|
|
'all player perf to team perf schedule');
|
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
return $sequence;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
protected function createPlayerToTeamSumFactor($teamMembers, $sumVariable)
|
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);
|
|
|
|
},
|
|
|
|
$teamMembers);
|
2010-09-25 19:26:57 -04:00
|
|
|
|
|
|
|
return new GaussianWeightedSumFactor(
|
2016-05-24 14:10:39 +02:00
|
|
|
$sumVariable,
|
|
|
|
$teamMembers,
|
|
|
|
$weights);
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function createPosteriorSchedule()
|
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++) {
|
2022-07-05 15:55:47 +02:00
|
|
|
$allFactors[] = new ScheduleStep('team sum perf @'.$currentIteration,
|
2016-05-24 14:10:39 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2016-05-24 16:31:21 +02:00
|
|
|
private function createOutputVariable($team)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2016-05-24 16:31:21 +02:00
|
|
|
$memberNames = array_map(function ($currentPlayer) {
|
2022-07-05 15:55:47 +02:00
|
|
|
return (string) ($currentPlayer->getKey());
|
2016-05-24 16:31:21 +02:00
|
|
|
}, $team);
|
2010-09-26 21:43:17 -04:00
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
$teamMemberNames = \implode(', ', $memberNames);
|
|
|
|
$outputVariable = $this->getParentFactorGraph()->getVariableFactory()->createBasicVariable('Team['.$teamMemberNames."]'s performance");
|
|
|
|
|
2010-10-02 21:15:47 -04:00
|
|
|
return $outputVariable;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
2022-07-05 15:55:47 +02:00
|
|
|
}
|