2010-09-18 11:11:44 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Moserware\Skills\TrueSkill;
|
|
|
|
|
2010-09-18 21:19:51 -04:00
|
|
|
require_once(dirname(__FILE__) . "/../GameInfo.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../Guard.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../ISupportPartialPlay.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../ISupportPartialUpdate.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../PartialPlay.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../PlayersRange.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../RankSorter.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../SkillCalculator.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../TeamsRange.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../Numerics/BasicMath.php");
|
|
|
|
require_once(dirname(__FILE__) . "/../Numerics/Matrix.php");
|
|
|
|
require_once(dirname(__FILE__) . "/TrueSkillFactorGraph.php");
|
|
|
|
|
|
|
|
use Moserware\Numerics\DiagonalMatrix;
|
|
|
|
use Moserware\Numerics\Matrix;
|
|
|
|
use Moserware\Numerics\Vector;
|
|
|
|
|
|
|
|
use Moserware\Skills\GameInfo;
|
|
|
|
use Moserware\Skills\Guard;
|
|
|
|
use Moserware\Skills\ISupportPartialPlay;
|
|
|
|
use Moserware\Skills\ISupportPartialUpdate;
|
|
|
|
use Moserware\Skills\PartialPlay;
|
|
|
|
use Moserware\Skills\PlayersRange;
|
|
|
|
use Moserware\Skills\RankSorter;
|
|
|
|
use Moserware\Skills\SkillCalculator;
|
|
|
|
use Moserware\Skills\SkillCalculatorSupportedOptions;
|
|
|
|
use Moserware\Skills\TeamsRange;
|
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Calculates TrueSkill using a full factor graph.
|
|
|
|
/// </summary>
|
|
|
|
class FactorGraphTrueSkillCalculator extends SkillCalculator
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct(SkillCalculatorSupportedOptions::PARTIAL_PLAY | SkillCalculatorSupportedOptions::PARTIAL_UPDATE, TeamsRange::atLeast(2), PlayersRange::atLeast(1));
|
|
|
|
}
|
|
|
|
|
2010-09-25 15:46:23 -04:00
|
|
|
public function calculateNewRatings(GameInfo &$gameInfo,
|
2010-09-18 11:11:44 -04:00
|
|
|
array $teams,
|
|
|
|
array $teamRanks)
|
|
|
|
{
|
|
|
|
Guard::argumentNotNull($gameInfo, "gameInfo");
|
|
|
|
$this->validateTeamCountAndPlayersCountPerTeam($teams);
|
|
|
|
|
|
|
|
RankSorter::sort($teams, $teamRanks);
|
|
|
|
|
|
|
|
$factorGraph = new TrueSkillFactorGraph($gameInfo, $teams, $teamRanks);
|
|
|
|
$factorGraph->buildGraph();
|
|
|
|
$factorGraph->runSchedule();
|
2010-10-02 22:28:25 -04:00
|
|
|
|
2010-10-02 22:02:42 -04:00
|
|
|
$probabilityOfOutcome = $factorGraph->getProbabilityOfRanking();
|
2010-09-18 11:11:44 -04:00
|
|
|
|
|
|
|
return $factorGraph->getUpdatedRatings();
|
|
|
|
}
|
|
|
|
|
2010-09-25 15:46:23 -04:00
|
|
|
public function calculateMatchQuality(GameInfo &$gameInfo,
|
|
|
|
array &$teams)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
// We need to create the A matrix which is the player team assigments.
|
2010-09-18 21:19:51 -04:00
|
|
|
$teamAssignmentsList = $teams;
|
2010-09-18 11:11:44 -04:00
|
|
|
$skillsMatrix = $this->getPlayerCovarianceMatrix($teamAssignmentsList);
|
|
|
|
$meanVector = $this->getPlayerMeansVector($teamAssignmentsList);
|
|
|
|
$meanVectorTranspose = $meanVector->getTranspose();
|
|
|
|
|
|
|
|
$playerTeamAssignmentsMatrix = $this->createPlayerTeamAssignmentMatrix($teamAssignmentsList, $meanVector->getRowCount());
|
|
|
|
$playerTeamAssignmentsMatrixTranspose = $playerTeamAssignmentsMatrix->getTranspose();
|
|
|
|
|
|
|
|
$betaSquared = square($gameInfo->getBeta());
|
|
|
|
|
|
|
|
$start = Matrix::multiply($meanVectorTranspose, $playerTeamAssignmentsMatrix);
|
|
|
|
$aTa = Matrix::multiply(
|
|
|
|
Matrix::scalarMultiply($betaSquared,
|
|
|
|
$playerTeamAssignmentsMatrixTranspose),
|
|
|
|
$playerTeamAssignmentsMatrix);
|
|
|
|
|
|
|
|
$aTSA = Matrix::multiply(
|
|
|
|
Matrix::multiply($playerTeamAssignmentsMatrixTranspose, $skillsMatrix),
|
|
|
|
$playerTeamAssignmentsMatrix);
|
|
|
|
|
|
|
|
$middle = Matrix::add($aTa, $aTSA);
|
|
|
|
|
|
|
|
$middleInverse = $middle->getInverse();
|
|
|
|
|
|
|
|
$end = Matrix::multiply($playerTeamAssignmentsMatrixTranspose, $meanVector);
|
|
|
|
|
|
|
|
$expPartMatrix = Matrix::scalarMultiply(-0.5, (Matrix::multiply(Matrix::multiply($start, $middleInverse), $end)));
|
|
|
|
$expPart = $expPartMatrix->getDeterminant();
|
|
|
|
|
|
|
|
$sqrtPartNumerator = $aTa->getDeterminant();
|
|
|
|
$sqrtPartDenominator = $middle->getDeterminant();
|
|
|
|
$sqrtPart = $sqrtPartNumerator / $sqrtPartDenominator;
|
|
|
|
|
|
|
|
$result = exp($expPart) * sqrt($sqrtPart);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2010-09-25 15:46:23 -04:00
|
|
|
private static function getPlayerMeansVector(array &$teamAssignmentsList)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
// A simple vector of all the player means.
|
2010-10-02 23:22:01 -04:00
|
|
|
return new Vector(self::getPlayerRatingValues($teamAssignmentsList,
|
|
|
|
function($rating)
|
|
|
|
{
|
|
|
|
return $rating->getMean();
|
|
|
|
}));
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2010-09-25 15:46:23 -04:00
|
|
|
private static function getPlayerCovarianceMatrix(array &$teamAssignmentsList)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
// This is a square matrix whose diagonal values represent the variance (square of standard deviation) of all
|
|
|
|
// players.
|
|
|
|
return new DiagonalMatrix(
|
2010-10-02 23:22:01 -04:00
|
|
|
self::getPlayerRatingValues($teamAssignmentsList,
|
|
|
|
function($rating)
|
|
|
|
{
|
|
|
|
return square($rating->getStandardDeviation());
|
|
|
|
}));
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function that gets a list of values for all player ratings
|
2010-09-25 15:46:23 -04:00
|
|
|
private static function getPlayerRatingValues(array &$teamAssignmentsList,
|
2010-09-18 11:11:44 -04:00
|
|
|
$playerRatingFunction)
|
|
|
|
{
|
|
|
|
$playerRatingValues = array();
|
|
|
|
|
|
|
|
foreach ($teamAssignmentsList as $currentTeam)
|
|
|
|
{
|
2010-10-02 23:22:01 -04:00
|
|
|
foreach ($currentTeam->getAllRatings() as $currentRating)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
$playerRatingValues[] = $playerRatingFunction($currentRating);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $playerRatingValues;
|
|
|
|
}
|
|
|
|
|
2010-10-02 23:22:01 -04:00
|
|
|
private static function createPlayerTeamAssignmentMatrix(&$teamAssignmentsList, $totalPlayers)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
// The team assignment matrix is often referred to as the "A" matrix. It's a matrix whose rows represent the players
|
|
|
|
// and the columns represent teams. At Matrix[row, column] represents that player[row] is on team[col]
|
|
|
|
// Positive values represent an assignment and a negative value means that we subtract the value of the next
|
|
|
|
// team since we're dealing with pairs. This means that this matrix always has teams - 1 columns.
|
|
|
|
// The only other tricky thing is that values represent the play percentage.
|
|
|
|
|
|
|
|
// For example, consider a 3 team game where team1 is just player1, team 2 is player 2 and player 3, and
|
|
|
|
// team3 is just player 4. Furthermore, player 2 and player 3 on team 2 played 25% and 75% of the time
|
|
|
|
// (e.g. partial play), the A matrix would be:
|
|
|
|
|
|
|
|
// A = this 4x2 matrix:
|
|
|
|
// | 1.00 0.00 |
|
|
|
|
// | -0.25 0.25 |
|
|
|
|
// | -0.75 0.75 |
|
|
|
|
// | 0.00 -1.00 |
|
|
|
|
|
|
|
|
$playerAssignments = array();
|
|
|
|
$totalPreviousPlayers = 0;
|
|
|
|
|
|
|
|
$teamAssignmentsListCount = count($teamAssignmentsList);
|
|
|
|
|
2010-10-03 17:42:31 -04:00
|
|
|
$currentColumn = 0;
|
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
for ($i = 0; $i < $teamAssignmentsListCount - 1; $i++)
|
|
|
|
{
|
|
|
|
$currentTeam = $teamAssignmentsList[$i];
|
|
|
|
|
|
|
|
// Need to add in 0's for all the previous players, since they're not
|
|
|
|
// on this team
|
2010-10-03 17:42:31 -04:00
|
|
|
$playerAssignments[$currentColumn] = ($totalPreviousPlayers > 0) ? \array_fill(0, $totalPreviousPlayers, 0) : array();
|
2010-09-18 11:11:44 -04:00
|
|
|
|
2010-10-02 23:22:01 -04:00
|
|
|
foreach ($currentTeam->getAllPlayers() as $currentPlayer)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
2010-10-03 17:42:31 -04:00
|
|
|
$playerAssignments[$currentColumn][] = PartialPlay::getPartialPlayPercentage($currentPlayer);
|
2010-09-18 11:11:44 -04:00
|
|
|
// indicates the player is on the team
|
|
|
|
$totalPreviousPlayers++;
|
|
|
|
}
|
|
|
|
|
2010-10-03 17:42:31 -04:00
|
|
|
$rowsRemaining = $totalPlayers - $totalPreviousPlayers;
|
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
$nextTeam = $teamAssignmentsList[$i + 1];
|
2010-10-02 23:22:01 -04:00
|
|
|
foreach ($nextTeam->getAllPlayers() as $nextTeamPlayer)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
// Add a -1 * playing time to represent the difference
|
2010-10-03 17:42:31 -04:00
|
|
|
$playerAssignments[$currentColumn][] = -1 * PartialPlay::getPartialPlayPercentage($nextTeamPlayer);
|
|
|
|
$rowsRemaining--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($ixAdditionalRow = 0; $ixAdditionalRow < $rowsRemaining; $ixAdditionalRow++)
|
|
|
|
{
|
|
|
|
// Pad with zeros
|
|
|
|
$playerAssignments[$currentColumn][] = 0;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
2010-10-02 23:22:01 -04:00
|
|
|
|
2010-10-03 17:42:31 -04:00
|
|
|
$currentColumn++;
|
2010-09-18 11:11:44 -04:00
|
|
|
}
|
|
|
|
|
2010-10-02 23:22:01 -04:00
|
|
|
$playerTeamAssignmentsMatrix = Matrix::fromColumnValues($totalPlayers, $teamAssignmentsListCount - 1, $playerAssignments);
|
2010-09-18 11:11:44 -04:00
|
|
|
|
|
|
|
return $playerTeamAssignmentsMatrix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|