First test passes!

The second one... not so much ;-)
This commit is contained in:
Jeff Moser 2010-10-02 23:22:01 -04:00
parent 124d73fdbf
commit 2c3c35dbff
2 changed files with 48 additions and 27 deletions

@ -16,6 +16,24 @@ class Matrix
$this->_matrixRowData = $matrixData;
}
public static function fromColumnValues($rows, $columns, $columnValues)
{
$data = array();
$result = new Matrix($rows, $columns, $data);
for($currentColumn = 0; $currentColumn < $columns; $currentColumn++)
{
$currentColumnData = $columnValues[$currentColumn];
for($currentRow = 0; $currentRow < $rows; $currentRow++)
{
$result->setValue($currentRow, $currentColumn, $currentColumnData[$currentRow]);
}
}
return $result;
}
public static function fromRowsColumns()
{
$args = \func_get_args();
@ -60,6 +78,7 @@ class Matrix
// Just flip everything
$transposeMatrix = array();
$rowMatrixData = $this->_matrixRowData;
for ($currentRowTransposeMatrix = 0;
$currentRowTransposeMatrix < $this->_columnCount;
$currentRowTransposeMatrix++)
@ -69,7 +88,7 @@ class Matrix
$currentColumnTransposeMatrix++)
{
$transposeMatrix[$currentRowTransposeMatrix][$currentColumnTransposeMatrix] =
$this->_matrixRowData[$currentColumnTransposeMatrix][$currentRowTransposeMatrix];
$rowMatrixData[$currentColumnTransposeMatrix][$currentRowTransposeMatrix];
}
}
@ -92,7 +111,7 @@ class Matrix
if ($this->_rowCount == 1)
{
// Really happy path :)
return $this->_matrixRowValues[0][0];
return $this->_matrixRowData[0][0];
}
if ($this->_rowCount == 2)
@ -356,7 +375,12 @@ class Vector extends Matrix
{
public function __construct(array $vectorValues)
{
parent::__construct(count($vectorValues), 1, array($vectorValues));
$columnValues = array();
foreach($vectorValues as $currentVectorValue)
{
$columnValues[] = array($currentVectorValue);
}
parent::__construct(count($vectorValues), 1, $columnValues);
}
}
@ -407,10 +431,6 @@ class DiagonalMatrix extends Matrix
}
}
}
for($i = 0; $i < $diagonalCount; $i++)
{
$this->setValue($i, $i, $diagonalValues[$i]);
}
}
}

@ -103,7 +103,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
private static function getPlayerMeansVector(array &$teamAssignmentsList)
{
// A simple vector of all the player means.
return new Vector($this->getPlayerRatingValues($teamAssignmentsList,
return new Vector(self::getPlayerRatingValues($teamAssignmentsList,
function($rating)
{
return $rating->getMean();
@ -115,7 +115,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
// This is a square matrix whose diagonal values represent the variance (square of standard deviation) of all
// players.
return new DiagonalMatrix(
$this->getPlayerRatingValues($teamAssignmentsList,
self::getPlayerRatingValues($teamAssignmentsList,
function($rating)
{
return square($rating->getStandardDeviation());
@ -130,7 +130,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
foreach ($teamAssignmentsList as $currentTeam)
{
foreach (currentTeam.Values as $currentRating)
foreach ($currentTeam->getAllRatings() as $currentRating)
{
$playerRatingValues[] = $playerRatingFunction($currentRating);
}
@ -139,7 +139,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
return $playerRatingValues;
}
private static function createPlayerTeamAssignmentMatrix(&$teamAssignmentsList, &$totalPlayers)
private static function createPlayerTeamAssignmentMatrix(&$teamAssignmentsList, $totalPlayers)
{
// 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]
@ -169,24 +169,25 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
// Need to add in 0's for all the previous players, since they're not
// on this team
$currentRowValues = array();
$playerAssignments[] = &$currentRowValues;
foreach ($currentTeam as $currentRating)
foreach ($currentTeam->getAllPlayers() as $currentPlayer)
{
$currentRowValues[] = PartialPlay::getPartialPlayPercentage($currentRating->getKey());
$currentRowValues[] = PartialPlay::getPartialPlayPercentage($currentPlayer);
// indicates the player is on the team
$totalPreviousPlayers++;
}
$nextTeam = $teamAssignmentsList[$i + 1];
foreach ($nextTeam as $nextTeamPlayerPair)
foreach ($nextTeam->getAllPlayers() as $nextTeamPlayer)
{
// Add a -1 * playing time to represent the difference
$currentRowValues[] = -1 * PartialPlay::getPartialPlayPercentage($nextTeamPlayerPair->getKey());
}
$currentRowValues[] = -1 * PartialPlay::getPartialPlayPercentage($nextTeamPlayer);
}
$playerTeamAssignmentsMatrix = new Matrix($totalPlayers, $teamAssignmentsListCount - 1, $playerAssignments);
$playerAssignments[] = &$currentRowValues;
}
$playerTeamAssignmentsMatrix = Matrix::fromColumnValues($totalPlayers, $teamAssignmentsListCount - 1, $playerAssignments);
return $playerTeamAssignmentsMatrix;
}