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