First TwoPlayerTrueSkillCalculator unit test passed

This commit is contained in:
Jeff Moser
2010-08-28 22:05:41 -04:00
commit 12a02b8403
41 changed files with 1909 additions and 0 deletions

View 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);
?>

View 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);
}
}
?>

View 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);
?>