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; $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() public static function fromRowsColumns()
{ {
$args = \func_get_args(); $args = \func_get_args();
@ -60,6 +78,7 @@ class Matrix
// Just flip everything // Just flip everything
$transposeMatrix = array(); $transposeMatrix = array();
$rowMatrixData = $this->_matrixRowData;
for ($currentRowTransposeMatrix = 0; for ($currentRowTransposeMatrix = 0;
$currentRowTransposeMatrix < $this->_columnCount; $currentRowTransposeMatrix < $this->_columnCount;
$currentRowTransposeMatrix++) $currentRowTransposeMatrix++)
@ -69,7 +88,7 @@ class Matrix
$currentColumnTransposeMatrix++) $currentColumnTransposeMatrix++)
{ {
$transposeMatrix[$currentRowTransposeMatrix][$currentColumnTransposeMatrix] = $transposeMatrix[$currentRowTransposeMatrix][$currentColumnTransposeMatrix] =
$this->_matrixRowData[$currentColumnTransposeMatrix][$currentRowTransposeMatrix]; $rowMatrixData[$currentColumnTransposeMatrix][$currentRowTransposeMatrix];
} }
} }
@ -92,7 +111,7 @@ class Matrix
if ($this->_rowCount == 1) if ($this->_rowCount == 1)
{ {
// Really happy path :) // Really happy path :)
return $this->_matrixRowValues[0][0]; return $this->_matrixRowData[0][0];
} }
if ($this->_rowCount == 2) if ($this->_rowCount == 2)
@ -356,7 +375,12 @@ class Vector extends Matrix
{ {
public function __construct(array $vectorValues) 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,11 +103,11 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
private static function getPlayerMeansVector(array &$teamAssignmentsList) private static function getPlayerMeansVector(array &$teamAssignmentsList)
{ {
// A simple vector of all the player means. // A simple vector of all the player means.
return new Vector($this->getPlayerRatingValues($teamAssignmentsList, return new Vector(self::getPlayerRatingValues($teamAssignmentsList,
function($rating) function($rating)
{ {
return $rating->getMean(); return $rating->getMean();
})); }));
} }
private static function getPlayerCovarianceMatrix(array &$teamAssignmentsList) private static function getPlayerCovarianceMatrix(array &$teamAssignmentsList)
@ -115,11 +115,11 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
// This is a square matrix whose diagonal values represent the variance (square of standard deviation) of all // This is a square matrix whose diagonal values represent the variance (square of standard deviation) of all
// players. // players.
return new DiagonalMatrix( return new DiagonalMatrix(
$this->getPlayerRatingValues($teamAssignmentsList, self::getPlayerRatingValues($teamAssignmentsList,
function($rating) function($rating)
{ {
return square($rating->getStandardDeviation()); return square($rating->getStandardDeviation());
})); }));
} }
// Helper function that gets a list of values for all player ratings // Helper function that gets a list of values for all player ratings
@ -130,7 +130,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
foreach ($teamAssignmentsList as $currentTeam) foreach ($teamAssignmentsList as $currentTeam)
{ {
foreach (currentTeam.Values as $currentRating) foreach ($currentTeam->getAllRatings() as $currentRating)
{ {
$playerRatingValues[] = $playerRatingFunction($currentRating); $playerRatingValues[] = $playerRatingFunction($currentRating);
} }
@ -139,7 +139,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
return $playerRatingValues; 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 // 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] // 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 // Need to add in 0's for all the previous players, since they're not
// on this team // on this team
$currentRowValues = array(); $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 // indicates the player is on the team
$totalPreviousPlayers++; $totalPreviousPlayers++;
} }
$nextTeam = $teamAssignmentsList[$i + 1]; $nextTeam = $teamAssignmentsList[$i + 1];
foreach ($nextTeam as $nextTeamPlayerPair) foreach ($nextTeam->getAllPlayers() as $nextTeamPlayer)
{ {
// Add a -1 * playing time to represent the difference // Add a -1 * playing time to represent the difference
$currentRowValues[] = -1 * PartialPlay::getPartialPlayPercentage($nextTeamPlayerPair->getKey()); $currentRowValues[] = -1 * PartialPlay::getPartialPlayPercentage($nextTeamPlayer);
} }
$playerAssignments[] = &$currentRowValues;
} }
$playerTeamAssignmentsMatrix = new Matrix($totalPlayers, $teamAssignmentsListCount - 1, $playerAssignments); $playerTeamAssignmentsMatrix = Matrix::fromColumnValues($totalPlayers, $teamAssignmentsListCount - 1, $playerAssignments);
return $playerTeamAssignmentsMatrix; return $playerTeamAssignmentsMatrix;
} }