trueskill/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php

89 lines
3.2 KiB
PHP
Raw Normal View History

<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\PartialPlay;
use Moserware\Skills\FactorGraphs\ScheduleLoop;
use Moserware\Skills\FactorGraphs\ScheduleSequence;
use Moserware\Skills\FactorGraphs\ScheduleStep;
use Moserware\Skills\TrueSkill\Factors\GaussianWeightedSumFactor;
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLayer
{
public function __construct(TrueSkillFactorGraph &$parentGraph)
{
parent::__construct($parentGraph);
}
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();
$outputVariablesGroups[] = array($teamPerformance);
}
}
public function createPriorSchedule()
{
2010-09-30 08:25:31 -04:00
$localFactors = &$this->getLocalFactors();
$sequence = &$this->scheduleSequence(
array_map(
function ($weightedSumFactor) {
return new ScheduleStep("Perf to Team Perf Step", $weightedSumFactor, 0);
},
$localFactors),
"all player perf to team perf schedule");
2010-09-30 08:25:31 -04:00
return $sequence;
}
protected function createPlayerToTeamSumFactor(&$teamMembers, &$sumVariable)
{
$weights = array_map(
function ($v) {
$player = &$v->getKey();
return PartialPlay::getPartialPlayPercentage($player);
},
$teamMembers);
return new GaussianWeightedSumFactor(
$sumVariable,
$teamMembers,
$weights);
}
public function createPosteriorSchedule()
{
2010-09-18 15:24:36 -04:00
$allFactors = array();
2010-09-30 08:25:31 -04:00
$localFactors = &$this->getLocalFactors();
foreach ($localFactors as &$currentFactor) {
$localCurrentFactor = &$currentFactor;
$numberOfMessages = $localCurrentFactor->getNumberOfMessages();
for ($currentIteration = 1; $currentIteration < $numberOfMessages; $currentIteration++) {
$allFactors[] = new ScheduleStep("team sum perf @" . $currentIteration,
$localCurrentFactor, $currentIteration);
2010-09-18 15:24:36 -04:00
}
}
return $this->scheduleSequence($allFactors, "all of the team's sum iterations");
}
private function &createOutputVariable(&$team)
{
$memberNames = \array_map(function ($currentPlayer) {
return (string)($currentPlayer->getKey());
},
$team);
$teamMemberNames = \join(", ", $memberNames);
$outputVariable = &$this->getParentFactorGraph()->getVariableFactory()->createBasicVariable("Team[" . $teamMemberNames . "]'s performance");
return $outputVariable;
}
}