Fixing failing tests and misc cleanup

This commit is contained in:
Alexander Liljengård
2016-05-24 15:12:29 +02:00
parent 5694a2fb30
commit 25b64d53f0
20 changed files with 132 additions and 125 deletions

View File

@ -1,15 +1,18 @@
<?php namespace Moserware\Skills\Tests\Elo;
use Moserware\Skills\Elo\EloRating;
use Moserware\Skills\Elo\FideEloCalculator;
use Moserware\Skills\GameInfo;
use Moserware\Skills\PairwiseComparison;
use Moserware\Skills\Tests\TestCase;
class EloAssert
{
const ERROR_TOLERANCE = 0.1;
public static function assertChessRating(
$testClass,
$twoPlayerEloCalculator,
TestCase $testClass,
FideEloCalculator $twoPlayerEloCalculator,
$player1BeforeRating,
$player2BeforeRating,
$player1Result,
@ -31,7 +34,8 @@ class EloAssert
$result = $twoPlayerEloCalculator->calculateNewRatings(
$chessGameInfo,
$teams,
$ranks);
$ranks
);
$testClass->assertEquals($player1AfterRating, $result[$player1]->getMean(), '', self::ERROR_TOLERANCE);
$testClass->assertEquals($player2AfterRating, $result[$player2]->getMean(), '', self::ERROR_TOLERANCE);

View File

@ -1,13 +1,14 @@
<?php namespace Moserware\Skills\Tests\Numerics;
use Moserware\Skills\Numerics\BasicMath;
use Moserware\Skills\Tests\TestCase;
class BasicMathTest extends TestCase
{
public function testSquare()
{
$this->assertEquals(1, Moserware\Numerics\square(1));
$this->assertEquals(1.44, Moserware\Numerics\square(1.2));
$this->assertEquals(4, Moserware\Numerics\square(2));
$this->assertEquals(1, BasicMath::square(1));
$this->assertEquals(1.44, BasicMath::square(1.2));
$this->assertEquals(4, BasicMath::square(2));
}
}

View File

@ -1,33 +1,34 @@
<?php namespace Moserware\Skills\Tests\Numerics;
use Moserware\Numerics\GaussianDistribution;
use Moserware\Skills\Numerics\BasicMath;
use Moserware\Skills\Numerics\GaussianDistribution;
use Moserware\Skills\Tests\TestCase;
class GaussianDistributionTest extends TestCase
{
{
const ERROR_TOLERANCE = 0.000001;
public function testCumulativeTo()
{
{
// Verified with WolframAlpha
// (e.g. http://www.wolframalpha.com/input/?i=CDF%5BNormalDistribution%5B0%2C1%5D%2C+0.5%5D )
$this->assertEquals( 0.691462, GaussianDistribution::cumulativeTo(0.5),'', GaussianDistributionTest::ERROR_TOLERANCE);
$this->assertEquals(0.691462, GaussianDistribution::cumulativeTo(0.5), '', GaussianDistributionTest::ERROR_TOLERANCE);
}
public function testAt()
{
// Verified with WolframAlpha
// (e.g. http://www.wolframalpha.com/input/?i=PDF%5BNormalDistribution%5B0%2C1%5D%2C+0.5%5D )
$this->assertEquals(0.352065, GaussianDistribution::at(0.5), '', GaussianDistributionTest::ERROR_TOLERANCE);
}
public function testMultiplication()
{
// I verified this against the formula at http://www.tina-vision.net/tina-knoppix/tina-memo/2003-003.pdf
$standardNormal = new GaussianDistribution(0, 1);
$standardNormal = new GaussianDistribution(0, 1);
$shiftedGaussian = new GaussianDistribution(2, 3);
$product = GaussianDistribution::multiply($standardNormal, $shiftedGaussian);
$this->assertEquals(0.2, $product->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
$this->assertEquals(3.0 / sqrt(10), $product->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE);
@ -35,14 +36,14 @@ class GaussianDistributionTest extends TestCase
$m6s7 = new GaussianDistribution(6, 7);
$product2 = GaussianDistribution::multiply($m4s5, $m6s7);
$expectedMean = (4 * BasicMatch::square(7) + 6 * BasicMatch::square(5)) / (BasicMatch::square(5) + BasicMatch::square(7));
$expectedMean = (4 * BasicMath::square(7) + 6 * BasicMath::square(5)) / (BasicMath::square(5) + BasicMath::square(7));
$this->assertEquals($expectedMean, $product2->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
$expectedSigma = sqrt(((BasicMatch::square(5) * BasicMatch::square(7)) / (BasicMatch::square(5) + BasicMatch::square(7))));
$expectedSigma = sqrt(((BasicMath::square(5) * BasicMath::square(7)) / (BasicMath::square(5) + BasicMath::square(7))));
$this->assertEquals($expectedSigma, $product2->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE);
}
public function testDivision()
{
// Since the multiplication was worked out by hand, we use the same numbers but work backwards
@ -51,15 +52,15 @@ class GaussianDistributionTest extends TestCase
$productDividedByStandardNormal = GaussianDistribution::divide($product, $standardNormal);
$this->assertEquals(2.0, $productDividedByStandardNormal->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
$this->assertEquals(3.0, $productDividedByStandardNormal->getStandardDeviation(),'', GaussianDistributionTest::ERROR_TOLERANCE);
$product2 = new GaussianDistribution((4 * BasicMatch::square(7) + 6 * BasicMatch::square(5)) / (BasicMatch::square(5) + BasicMatch::square(7)), sqrt(((BasicMatch::square(5) * BasicMatch::square(7)) / (BasicMatch::square(5) + BasicMatch::square(7)))));
$m4s5 = new GaussianDistribution(4,5);
$this->assertEquals(3.0, $productDividedByStandardNormal->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE);
$product2 = new GaussianDistribution((4 * BasicMath::square(7) + 6 * BasicMath::square(5)) / (BasicMath::square(5) + BasicMath::square(7)), sqrt(((BasicMath::square(5) * BasicMath::square(7)) / (BasicMath::square(5) + BasicMath::square(7)))));
$m4s5 = new GaussianDistribution(4, 5);
$product2DividedByM4S5 = GaussianDistribution::divide($product2, $m4s5);
$this->assertEquals(6.0, $product2DividedByM4S5->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
$this->assertEquals(7.0, $product2DividedByM4S5->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE);
}
public function testLogProductNormalization()
{
// Verified with Ralf Herbrich's F# implementation
@ -72,16 +73,16 @@ class GaussianDistributionTest extends TestCase
$lpn2 = GaussianDistribution::logProductNormalization($m1s2, $m3s4);
$this->assertEquals(-2.5168046699816684, $lpn2, '', GaussianDistributionTest::ERROR_TOLERANCE);
}
public function testLogRatioNormalization()
{
// Verified with Ralf Herbrich's F# implementation
$m1s2 = new GaussianDistribution(1, 2);
$m3s4 = new GaussianDistribution(3, 4);
$lrn = GaussianDistribution::logRatioNormalization($m1s2, $m3s4);
$this->assertEquals(2.6157405972171204, $lrn, '', GaussianDistributionTest::ERROR_TOLERANCE);
$this->assertEquals(2.6157405972171204, $lrn, '', GaussianDistributionTest::ERROR_TOLERANCE);
}
public function testAbsoluteDifference()
{
// Verified with Ralf Herbrich's F# implementation

View File

@ -1,8 +1,8 @@
<?php namespace Moserware\Skills\Tests\Numerics;
use Moserware\Numerics\Matrix;
use Moserware\Numerics\IdentityMatrix;
use Moserware\Numerics\SquareMatrix;
use Moserware\Skills\Numerics\IdentityMatrix;
use Moserware\Skills\Numerics\Matrix;
use Moserware\Skills\Numerics\SquareMatrix;
use Moserware\Skills\Tests\TestCase;
class MatrixTest extends TestCase

View File

@ -6,17 +6,17 @@ class RankSorterTest extends TestCase
{
public function testSort()
{
$team1 = array( "a" => 1, "b" => 2 );
$team2 = array( "c" => 3, "d" => 4 );
$team3 = array( "e" => 5, "f" => 6 );
$team1 = array("a" => 1, "b" => 2);
$team2 = array("c" => 3, "d" => 4);
$team3 = array("e" => 5, "f" => 6);
$teams = array($team1, $team2, $team3);
$teamRanks = array(3, 1, 2);
$sortedRanks = RankSorter::sort($teams, $teamRanks);
$this->assertEquals($team2, $sortedRanks[0]);
$this->assertEquals($team2, $sortedRanks[0]);
$this->assertEquals($team3, $sortedRanks[1]);
$this->assertEquals($team1, $sortedRanks[2]);
}

View File

@ -1,4 +1,5 @@
<?php namespace Moserware\Skills\Tests;
class TestCase extends \PHPUnit_Framework_TestCase {
class TestCase extends \PHPUnit_Framework_TestCase
{
}

View File

@ -4,9 +4,9 @@ use Moserware\Skills\Tests\TestCase;
use Moserware\Skills\TrueSkill\DrawMargin;
class DrawMarginTest extends TestCase
{
{
const ERROR_TOLERANCE = 0.000001;
public function testGetDrawMarginFromDrawProbability()
{
$beta = 25.0 / 6.0;
@ -20,5 +20,5 @@ class DrawMarginTest extends TestCase
{
$actual = DrawMargin::getDrawMarginFromDrawProbability($drawProbability, $beta);
$this->assertEquals($expected, $actual, '', DrawMarginTest::ERROR_TOLERANCE);
}
}
}

View File

@ -6,9 +6,9 @@ use Moserware\Skills\TrueSkill\FactorGraphTrueSkillCalculator;
class FactorGraphTrueSkillCalculatorTest extends TestCase
{
public function testFactorGraphTrueSkillCalculator()
{
{
$calculator = new FactorGraphTrueSkillCalculator();
TrueSkillCalculatorTests::testAllTwoPlayerScenarios($this, $calculator);
TrueSkillCalculatorTests::testAllTwoTeamScenarios($this, $calculator);
TrueSkillCalculatorTests::testAllMultipleTeamScenarios($this, $calculator);

View File

@ -17,14 +17,14 @@ class TrueSkillCalculatorTests
public static function testAllTwoPlayerScenarios($testClass, SkillCalculator $calculator)
{
self::twoPlayerTestNotDrawn($testClass, $calculator);
self::twoPlayerTestDrawn($testClass, $calculator);
self::twoPlayerTestDrawn($testClass, $calculator);
self::twoPlayerChessTestNotDrawn($testClass, $calculator);
self::oneOnOneMassiveUpsetDrawTest($testClass, $calculator);
}
public static function testAllTwoTeamScenarios($testClass, SkillCalculator $calculator)
{
self::oneOnTwoSimpleTest($testClass, $calculator);
self::oneOnTwoSimpleTest($testClass, $calculator);
self::oneOnTwoSomewhatBalanced($testClass, $calculator);
self::oneOnTwoDrawTest($testClass, $calculator);
self::oneOnThreeDrawTest($testClass, $calculator);
@ -75,7 +75,7 @@ class TrueSkillCalculatorTests
private static function twoPlayerTestNotDrawn($testClass, SkillCalculator $calculator)
{
$player1 = new Player(1);
$player2 = new Player(2);
$player2 = new Player(2);
$gameInfo = new GameInfo();
$team1 = new Team($player1, $gameInfo->getDefaultRating());
@ -621,7 +621,7 @@ class TrueSkillCalculatorTests
$team1 = new Team($player1, $gameInfo->getDefaultRating());
$team2 = new Team($player2, $gameInfo->getDefaultRating());
$team3 = new Team($player3, $gameInfo->getDefaultRating());
$teams = Teams::concat($team1, $team2, $team3);
$newRatings = $calculator->calculateNewRatings($gameInfo, $teams, array(1, 1, 1));
@ -847,14 +847,14 @@ class TrueSkillCalculatorTests
$team16 = new Team($player16, $gameInfo->getDefaultRating());
$teams = Teams::concat(
$team1, $team2, $team3, $team4, $team5,
$team6, $team7, $team8, $team9, $team10,
$team11, $team12, $team13, $team14, $team15,
$team16);
$team1, $team2, $team3, $team4, $team5,
$team6, $team7, $team8, $team9, $team10,
$team11, $team12, $team13, $team14, $team15,
$team16);
$newRatings = $calculator->calculateNewRatings(
$gameInfo, $teams,
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
$gameInfo, $teams,
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16));
$player1NewRating = $newRatings->getRating($player1);
self::assertRating($testClass, 40.53945776946920, 5.27581643889050, $player1NewRating);
@ -913,8 +913,8 @@ class TrueSkillCalculatorTests
$gameInfo = new GameInfo();
$team1 = new Team();
$team1->addPlayer($player1, new Rating(40,4));
$team1->addPlayer($player2, new Rating(45,3));
$team1->addPlayer($player1, new Rating(40, 4));
$team1->addPlayer($player2, new Rating(45, 3));
$player3 = new Player(3);
$player4 = new Player(4);
@ -931,8 +931,8 @@ class TrueSkillCalculatorTests
$player8 = new Player(8);
$team3 = new Team();
$team3->addPlayer($player7, new Rating(50,5));
$team3->addPlayer($player8, new Rating(30,2));
$team3->addPlayer($player7, new Rating(50, 5));
$team3->addPlayer($player8, new Rating(30, 2));
$teams = Teams::concat($team1, $team2, $team3);
$newRatingsWinLose = $calculator->calculateNewRatings($gameInfo, $teams, array(1, 2, 2));
@ -986,7 +986,7 @@ class TrueSkillCalculatorTests
private static function assertRating($testClass, $expectedMean, $expectedStandardDeviation, $actual)
{
$testClass->assertEquals($expectedMean, $actual->getMean(), '', self::ERROR_TOLERANCE_TRUESKILL);
$testClass->assertEquals($expectedMean, $actual->getMean(), '', self::ERROR_TOLERANCE_TRUESKILL);
$testClass->assertEquals($expectedStandardDeviation, $actual->getStandardDeviation(), '', self::ERROR_TOLERANCE_TRUESKILL);
}