mirror of
https://github.com/furyfire/trueskill.git
synced 2025-03-20 00:37:48 +00:00
First TwoPlayerTrueSkillCalculator unit test passed
This commit is contained in:
46
UnitTests/Elo/EloAssert.php
Normal file
46
UnitTests/Elo/EloAssert.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace Moserware\Skills\Elo;
|
||||
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/Elo/EloRating.php');
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/GameInfo.php');
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/PairwiseComparison.php');
|
||||
|
||||
use Moserware\Skills\GameInfo;
|
||||
use Moserware\Skills\PairwiseComparison;
|
||||
|
||||
class EloAssert
|
||||
{
|
||||
const ERROR_TOLERANCE = 0.1;
|
||||
|
||||
public static function assertChessRating(
|
||||
$testClass,
|
||||
$twoPlayerEloCalculator,
|
||||
$player1BeforeRating,
|
||||
$player2BeforeRating,
|
||||
$player1Result,
|
||||
$player1AfterRating,
|
||||
$player2AfterRating)
|
||||
{
|
||||
$player1 = "Player1";
|
||||
$player2 = "Player2";
|
||||
|
||||
$teams = array(
|
||||
array( $player1 => new EloRating($player1BeforeRating) ),
|
||||
array( $player2 => new EloRating($player2BeforeRating) )
|
||||
);
|
||||
|
||||
$chessGameInfo = new GameInfo(1200, 0, 200);
|
||||
|
||||
$ranks = PairwiseComparison::getRankFromComparison($player1Result);
|
||||
|
||||
$result = $twoPlayerEloCalculator->calculateNewRatings(
|
||||
$chessGameInfo,
|
||||
$teams,
|
||||
$ranks);
|
||||
|
||||
$testClass->assertEquals($player1AfterRating, $result[$player1]->getMean(), '', self::ERROR_TOLERANCE);
|
||||
$testClass->assertEquals($player2AfterRating, $result[$player2]->getMean(), '', self::ERROR_TOLERANCE);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
39
UnitTests/Elo/FideEloCalculatorTest.php
Normal file
39
UnitTests/Elo/FideEloCalculatorTest.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace Moserware\Skills\Elo;
|
||||
|
||||
require_once(dirname(__FILE__) . '/EloAssert.php');
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/PairwiseComparison.php');
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/Elo/FideEloCalculator.php');
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/Elo/FideKFactor.php');
|
||||
|
||||
use Moserware\Skills\PairwiseComparison;
|
||||
use \PHPUnit_Framework_TestCase;
|
||||
|
||||
class FideEloCalculatorTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testFideProvisionalEloCalculator()
|
||||
{
|
||||
// verified against http://ratings.fide.com/calculator_rtd.phtml
|
||||
$calc = new FideEloCalculator(new ProvisionalFideKFactor());
|
||||
|
||||
EloAssert::assertChessRating($this, $calc, 1200, 1500, PairwiseComparison::WIN, 1221.25, 1478.75);
|
||||
EloAssert::assertChessRating($this, $calc, 1200, 1500, PairwiseComparison::DRAW, 1208.75, 1491.25);
|
||||
EloAssert::assertChessRating($this, $calc, 1200, 1500, PairwiseComparison::LOSE, 1196.25, 1503.75);
|
||||
}
|
||||
|
||||
public function testFideNonProvisionalEloCalculator()
|
||||
{
|
||||
// verified against http://ratings.fide.com/calculator_rtd.phtml
|
||||
$calc = FideEloCalculator::createWithDefaultKFactor();
|
||||
|
||||
EloAssert::assertChessRating($this, $calc, 1200, 1200, PairwiseComparison::WIN, 1207.5, 1192.5);
|
||||
EloAssert::assertChessRating($this, $calc, 1200, 1200, PairwiseComparison::DRAW, 1200, 1200);
|
||||
EloAssert::assertChessRating($this, $calc, 1200, 1200, PairwiseComparison::LOSE, 1192.5, 1207.5);
|
||||
|
||||
EloAssert::assertChessRating($this, $calc, 2600, 2500, PairwiseComparison::WIN, 2603.6, 2496.4);
|
||||
EloAssert::assertChessRating($this, $calc, 2600, 2500, PairwiseComparison::DRAW, 2598.6, 2501.4);
|
||||
EloAssert::assertChessRating($this, $calc, 2600, 2500, PairwiseComparison::LOSE, 2593.6, 2506.4);
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
15
UnitTests/Numerics/BasicMathTest.php
Normal file
15
UnitTests/Numerics/BasicMathTest.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/Numerics/BasicMath.php');
|
||||
|
||||
|
||||
class BasicMathTest extends PHPUnit_Framework_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) );
|
||||
}
|
||||
}
|
||||
?>
|
106
UnitTests/Numerics/GaussianDistributionTest.php
Normal file
106
UnitTests/Numerics/GaussianDistributionTest.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
namespace Moserware\Numerics;
|
||||
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/Numerics/GaussianDistribution.php');
|
||||
|
||||
|
||||
use \PHPUnit_Framework_TestCase;
|
||||
|
||||
class GaussianDistributionTest extends PHPUnit_Framework_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);
|
||||
}
|
||||
|
||||
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);
|
||||
$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);
|
||||
|
||||
$m4s5 = new GaussianDistribution(4, 5);
|
||||
$m6s7 = new GaussianDistribution(6, 7);
|
||||
|
||||
$product2 = GaussianDistribution::multiply($m4s5, $m6s7);
|
||||
|
||||
$expectedMean = (4 * square(7) + 6 * square(5)) / (square(5) + square(7));
|
||||
$this->assertEquals($expectedMean, $product2->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
|
||||
|
||||
$expectedSigma = sqrt(((square(5) * square(7)) / (square(5) + 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
|
||||
$product = new GaussianDistribution(0.2, 3.0 / sqrt(10));
|
||||
$standardNormal = new GaussianDistribution(0, 1);
|
||||
|
||||
$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 * square(7) + 6 * square(5)) / (square(5) + square(7)), sqrt(((square(5) * square(7)) / (square(5) + 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
|
||||
$standardNormal = new GaussianDistribution(0, 1);
|
||||
$lpn = GaussianDistribution::logProductNormalization($standardNormal, $standardNormal);
|
||||
$this->assertEquals(-1.2655121234846454, $lpn, '', GaussianDistributionTest::ERROR_TOLERANCE);
|
||||
|
||||
$m1s2 = new GaussianDistribution(1, 2);
|
||||
$m3s4 = new GaussianDistribution(3, 4);
|
||||
$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);
|
||||
}
|
||||
|
||||
public function testAbsoluteDifference()
|
||||
{
|
||||
// Verified with Ralf Herbrich's F# implementation
|
||||
$standardNormal = new GaussianDistribution(0, 1);
|
||||
$absDiff = GaussianDistribution::absoluteDifference($standardNormal, $standardNormal);
|
||||
$this->assertEquals(0.0, $absDiff, '', GaussianDistributionTest::ERROR_TOLERANCE);
|
||||
|
||||
$m1s2 = new GaussianDistribution(1, 2);
|
||||
$m3s4 = new GaussianDistribution(3, 4);
|
||||
$absDiff2 = GaussianDistribution::absoluteDifference($m1s2, $m3s4);
|
||||
$this->assertEquals(0.4330127018922193, $absDiff2, '', GaussianDistributionTest::ERROR_TOLERANCE);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
33
UnitTests/RankSorterTest.php
Normal file
33
UnitTests/RankSorterTest.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Moserware\Skills;
|
||||
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||
|
||||
require_once(dirname(__FILE__) . '/../PHPSkills/RankSorter.php');
|
||||
|
||||
|
||||
use \PHPUnit_Framework_TestCase;
|
||||
|
||||
class RankSorterTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSort()
|
||||
{
|
||||
$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($team3, $sortedRanks[1]);
|
||||
$this->assertEquals($team1, $sortedRanks[2]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
36
UnitTests/TrueSkill/DrawMarginTest.php
Normal file
36
UnitTests/TrueSkill/DrawMarginTest.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace Moserware\Skills\TrueSkill;
|
||||
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/TrueSkill/DrawMargin.php');
|
||||
|
||||
use \PHPUnit_Framework_TestCase;
|
||||
|
||||
class DrawMarginTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
const ERROR_TOLERANCE = 0.000001;
|
||||
|
||||
public function testGetDrawMarginFromDrawProbability()
|
||||
{
|
||||
$beta = 25.0 / 6.0;
|
||||
// The expected values were compared against Ralf Herbrich's implementation in F#
|
||||
$this->assertDrawMargin(0.10, $beta, 0.74046637542690541);
|
||||
$this->assertDrawMargin(0.25, $beta, 1.87760059883033);
|
||||
$this->assertDrawMargin(0.33, $beta, 2.5111010132487492);
|
||||
}
|
||||
|
||||
private function assertDrawMargin($drawProbability, $beta, $expected)
|
||||
{
|
||||
$actual = DrawMargin::getDrawMarginFromDrawProbability($drawProbability, $beta);
|
||||
$this->assertEquals($expected, $actual, '', DrawMarginTest::ERROR_TOLERANCE);
|
||||
}
|
||||
}
|
||||
|
||||
$testSuite = new \PHPUnit_Framework_TestSuite();
|
||||
$testSuite->addTest( new DrawMarginTest( "testGetDrawMarginFromDrawProbability" ) );
|
||||
\PHPUnit_TextUI_TestRunner::run($testSuite);
|
||||
|
||||
?>
|
||||
|
76
UnitTests/TrueSkill/TrueSkillCalculatorTests.php
Normal file
76
UnitTests/TrueSkill/TrueSkillCalculatorTests.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . "/../../PHPSkills/GameInfo.php");
|
||||
require_once(dirname(__FILE__) . "/../../PHPSkills/Player.php");
|
||||
require_once(dirname(__FILE__) . "/../../PHPSkills/Team.php");
|
||||
require_once(dirname(__FILE__) . "/../../PHPSkills/Teams.php");
|
||||
require_once(dirname(__FILE__) . "/../../PHPSkills/SkillCalculator.php");
|
||||
|
||||
use Moserware\Skills\GameInfo;
|
||||
use Moserware\Skills\Player;
|
||||
use Moserware\Skills\Team;
|
||||
use Moserware\Skills\Teams;
|
||||
use Moserware\Skills\SkillCalculator;
|
||||
|
||||
class TrueSkillCalculatorTests
|
||||
{
|
||||
const ERROR_TOLERANCE_TRUESKILL = 0.085;
|
||||
const ERROR_TOLERANCE_MATCH_QUALITY = 0.0005;
|
||||
|
||||
// These are the roll-up ones
|
||||
|
||||
public static function testAllTwoPlayerScenarios($testClass, SkillCalculator $calculator)
|
||||
{
|
||||
self::twoPlayerTestNotDrawn($testClass, $calculator);
|
||||
//self::twoPlayerTestDrawn($testClass, $calculator);
|
||||
//self::oneOnOneMassiveUpsetDrawTest($testClass, $calculator);
|
||||
//self::twoPlayerChessTestNotDrawn($testClass, $calculator);
|
||||
}
|
||||
|
||||
//------------------- Actual Tests ---------------------------
|
||||
// If you see more than 3 digits of precision in the decimal point, then the expected values calculated from
|
||||
// F# RalfH's implementation with the same input. It didn't support teams, so team values all came from the
|
||||
// online calculator at http://atom.research.microsoft.com/trueskill/rankcalculator.aspx
|
||||
//
|
||||
// All match quality expected values came from the online calculator
|
||||
|
||||
// In both cases, there may be some discrepancy after the first decimal point. I think this is due to my implementation
|
||||
// using slightly higher precision in GaussianDistribution.
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Two Player Tests
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
private static function twoPlayerTestNotDrawn($testClass, SkillCalculator $calculator)
|
||||
{
|
||||
$player1 = new Player(1);
|
||||
$player2 = new Player(2);
|
||||
$gameInfo = new GameInfo();
|
||||
|
||||
$team1 = new Team($player1, $gameInfo->getDefaultRating());
|
||||
$team2 = new Team($player2, $gameInfo->getDefaultRating());;
|
||||
$teams = Teams::concat($team1, $team2);
|
||||
|
||||
$newRatings = $calculator->calculateNewRatings($gameInfo, $teams, array(1, 2));
|
||||
|
||||
$player1NewRating = $newRatings->getRating($player1);
|
||||
self::assertRating($testClass, 29.39583201999924, 7.171475587326186, $player1NewRating);
|
||||
|
||||
$player2NewRating = $newRatings->getRating($player2);
|
||||
self::assertRating($testClass, 20.60416798000076, 7.171475587326186, $player2NewRating);
|
||||
|
||||
self::assertMatchQuality($testClass, 0.447, $calculator->calculateMatchQuality($gameInfo, $teams));
|
||||
}
|
||||
|
||||
private static function assertRating($testClass, $expectedMean, $expectedStandardDeviation, $actual)
|
||||
{
|
||||
$testClass->assertEquals($expectedMean, $actual->getMean(), '', self::ERROR_TOLERANCE_TRUESKILL);
|
||||
$testClass->assertEquals($expectedStandardDeviation, $actual->getStandardDeviation(), '', self::ERROR_TOLERANCE_TRUESKILL);
|
||||
}
|
||||
|
||||
private static function assertMatchQuality($testClass, $expectedMatchQuality, $actualMatchQuality)
|
||||
{
|
||||
$testClass->assertEquals($expectedMatchQuality, $actualMatchQuality, '', self::ERROR_TOLERANCE_MATCH_QUALITY);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
26
UnitTests/TrueSkill/TwoPlayerTrueSkillCalculatorTest.php
Normal file
26
UnitTests/TrueSkill/TwoPlayerTrueSkillCalculatorTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||
|
||||
require_once(dirname(__FILE__) . '/../../PHPSkills/TrueSkill/TwoPlayerTrueSkillCalculator.php');
|
||||
require_once(dirname(__FILE__) . '/TrueSkillCalculatorTests.php');
|
||||
|
||||
use \PHPUnit_Framework_TestCase;
|
||||
use Moserware\Skills\TrueSkill\TwoPlayerTrueSkillCalculator;
|
||||
|
||||
class TwoPlayerTrueSkillCalculatorTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testTwoPlayerTrueSkillCalculator()
|
||||
{
|
||||
$calculator = new TwoPlayerTrueSkillCalculator();
|
||||
|
||||
// We only support two players
|
||||
TrueSkillCalculatorTests::testAllTwoPlayerScenarios($this, $calculator);
|
||||
}
|
||||
}
|
||||
|
||||
$testSuite = new \PHPUnit_Framework_TestSuite();
|
||||
$testSuite->addTest( new TwoPlayerTrueSkillCalculatorTest("testTwoPlayerTrueSkillCalculator"));
|
||||
|
||||
\PHPUnit_TextUI_TestRunner::run($testSuite);
|
||||
?>
|
33
UnitTests/runner_example.php
Normal file
33
UnitTests/runner_example.php
Normal file
@ -0,0 +1,33 @@
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
require_once 'PHPUnit/TextUI/TestRunner.php';
|
||||
|
||||
require_once(dirname(__FILE__) . '/../PHPSkills/RankSorter.php');
|
||||
|
||||
|
||||
use \PHPUnit_Framework_TestCase;
|
||||
|
||||
class RankSorterTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testSort()
|
||||
{
|
||||
$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($team3, $sortedRanks[1]);
|
||||
$this->assertEquals($team1, $sortedRanks[2]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
$testSuite = new \PHPUnit_Framework_TestSuite();
|
||||
$testSuite->addTest( new RankSorterTest("testSort"));
|
||||
|
||||
\PHPUnit_TextUI_TestRunner::run($testSuite);
|
Reference in New Issue
Block a user