trueskill/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php

73 lines
2.7 KiB
PHP
Raw Normal View History

<?php namespace Moserware\Skills\TrueSkill\Layers;
use Moserware\Skills\Numerics\BasicMatch;
2010-09-30 08:25:31 -04:00
use Moserware\Skills\Rating;
use Moserware\Skills\FactorGraphs\ScheduleStep;
2010-09-30 08:25:31 -04:00
use Moserware\Skills\FactorGraphs\Variable;
use Moserware\Skills\TrueSkill\TrueSkillFactorGraph;
use Moserware\Skills\TrueSkill\Factors\GaussianPriorFactor;
// We intentionally have no Posterior schedule since the only purpose here is to
// start the process.
class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
{
private $_teams;
public function __construct(TrueSkillFactorGraph &$parentGraph, array &$teams)
{
parent::__construct($parentGraph);
$this->_teams = $teams;
}
public function buildLayer()
{
2010-09-30 08:25:31 -04:00
$teams = &$this->_teams;
foreach ($teams as &$currentTeam) {
$localCurrentTeam = &$currentTeam;
$currentTeamSkills = array();
$currentTeamAllPlayers = $localCurrentTeam->getAllPlayers();
foreach ($currentTeamAllPlayers as &$currentTeamPlayer) {
$localCurrentTeamPlayer = &$currentTeamPlayer;
$currentTeamPlayerRating = $currentTeam->getRating($localCurrentTeamPlayer);
$playerSkill = &$this->createSkillOutputVariable($localCurrentTeamPlayer);
$priorFactor = &$this->createPriorFactor($localCurrentTeamPlayer, $currentTeamPlayerRating, $playerSkill);
$this->addLayerFactor($priorFactor);
$currentTeamSkills[] = $playerSkill;
}
$outputVariablesGroups = &$this->getOutputVariablesGroups();
$outputVariablesGroups[] = $currentTeamSkills;
}
}
public function createPriorSchedule()
{
2010-09-30 08:25:31 -04:00
$localFactors = &$this->getLocalFactors();
return $this->scheduleSequence(
array_map(
function (&$prior) {
return new ScheduleStep("Prior to Skill Step", $prior, 0);
},
$localFactors),
"All priors");
}
2010-09-30 08:25:31 -04:00
private function createPriorFactor(&$player, Rating &$priorRating, Variable &$skillsVariable)
{
return new GaussianPriorFactor(
$priorRating->getMean(),
BasicMatch::square($priorRating->getStandardDeviation()) +
BasicMatch::square($this->getParentFactorGraph()->getGameInfo()->getDynamicsFactor()),
$skillsVariable
);
}
private function &createSkillOutputVariable(&$key)
{
2010-09-30 08:25:31 -04:00
$parentFactorGraph = &$this->getParentFactorGraph();
$variableFactory = &$parentFactorGraph->getVariableFactory();
$skillOutputVariable = &$variableFactory->createKeyedVariable($key, $key . "'s skill");
return $skillOutputVariable;
}
}