mirror of
https://github.com/furyfire/trueskill.git
synced 2025-01-16 01:47:39 +00:00
More return types. Basic example
This commit is contained in:
38
examples/basic.php
Normal file
38
examples/basic.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once("vendor/autoload.php");
|
||||||
|
|
||||||
|
use DNW\Skills\TrueSkill\TwoPlayerTrueSkillCalculator;
|
||||||
|
use DNW\Skills\GameInfo;
|
||||||
|
use DNW\Skills\Player;
|
||||||
|
use DNW\Skills\Team;
|
||||||
|
use DNW\Skills\Teams;
|
||||||
|
|
||||||
|
$gameInfo = new GameInfo();
|
||||||
|
|
||||||
|
$p1 = new Player("Winner");
|
||||||
|
$p2 = new Player("Average");
|
||||||
|
$p3 = new Player("Looser");
|
||||||
|
|
||||||
|
|
||||||
|
$team1 = new Team($p1, $gameInfo->getDefaultRating());
|
||||||
|
|
||||||
|
$team2 = new Team($p2, $gameInfo->getDefaultRating());
|
||||||
|
|
||||||
|
for($i = 0; $i < 5; $i++) {
|
||||||
|
echo "Iteration: $i\n";
|
||||||
|
$teams = Teams::concat($team1, $team2);
|
||||||
|
|
||||||
|
$calculator = new TwoPlayerTrueSkillCalculator();
|
||||||
|
|
||||||
|
$newRatings = $calculator->calculateNewRatings($gameInfo, $teams, [1, 2]);
|
||||||
|
|
||||||
|
$team1 = new Team($p1, $newRatings->getRating($p1));
|
||||||
|
$team2 = new Team($p2, $newRatings->getRating($p2));
|
||||||
|
|
||||||
|
echo "P1: ". $newRatings->getRating($p1)->getConservativeRating() . PHP_EOL;
|
||||||
|
echo "P2: ". $newRatings->getRating($p2)->getConservativeRating() . PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -10,7 +10,7 @@ abstract class Factor implements \Stringable
|
|||||||
{
|
{
|
||||||
private array $messages = [];
|
private array $messages = [];
|
||||||
|
|
||||||
private $messageToVariableBinding;
|
private HashMap $messageToVariableBinding;
|
||||||
|
|
||||||
private string $name;
|
private string $name;
|
||||||
|
|
||||||
|
@ -4,14 +4,14 @@ namespace DNW\Skills\FactorGraphs;
|
|||||||
|
|
||||||
class FactorGraph
|
class FactorGraph
|
||||||
{
|
{
|
||||||
private $variableFactory;
|
private VariableFactory $variableFactory;
|
||||||
|
|
||||||
public function getVariableFactory()
|
public function getVariableFactory(): VariableFactory
|
||||||
{
|
{
|
||||||
return $this->variableFactory;
|
return $this->variableFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setVariableFactory(VariableFactory $factory)
|
public function setVariableFactory(VariableFactory $factory): void
|
||||||
{
|
{
|
||||||
$this->variableFactory = $factory;
|
$this->variableFactory = $factory;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ namespace DNW\Skills\FactorGraphs;
|
|||||||
|
|
||||||
abstract class Schedule implements \Stringable
|
abstract class Schedule implements \Stringable
|
||||||
{
|
{
|
||||||
protected function __construct(private $_name)
|
protected function __construct(private string $name)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -12,6 +12,6 @@ abstract class Schedule implements \Stringable
|
|||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
return (string) $this->_name;
|
return (string) $this->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,41 +18,41 @@ class GameInfo
|
|||||||
private const DEFAULT_INITIAL_STANDARD_DEVIATION = 8.3333333333333333333333333333333;
|
private const DEFAULT_INITIAL_STANDARD_DEVIATION = 8.3333333333333333333333333333333;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private $_initialMean = self::DEFAULT_INITIAL_MEAN,
|
private float $initialMean = self::DEFAULT_INITIAL_MEAN,
|
||||||
private $_initialStandardDeviation = self::DEFAULT_INITIAL_STANDARD_DEVIATION,
|
private float $initialStandardDeviation = self::DEFAULT_INITIAL_STANDARD_DEVIATION,
|
||||||
private $_beta = self::DEFAULT_BETA,
|
private float $beta = self::DEFAULT_BETA,
|
||||||
private $_dynamicsFactor = self::DEFAULT_DYNAMICS_FACTOR,
|
private float $dynamicsFactor = self::DEFAULT_DYNAMICS_FACTOR,
|
||||||
private $_drawProbability = self::DEFAULT_DRAW_PROBABILITY
|
private float $drawProbability = self::DEFAULT_DRAW_PROBABILITY
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInitialMean()
|
public function getInitialMean()
|
||||||
{
|
{
|
||||||
return $this->_initialMean;
|
return $this->initialMean;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInitialStandardDeviation()
|
public function getInitialStandardDeviation()
|
||||||
{
|
{
|
||||||
return $this->_initialStandardDeviation;
|
return $this->initialStandardDeviation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBeta()
|
public function getBeta()
|
||||||
{
|
{
|
||||||
return $this->_beta;
|
return $this->beta;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDynamicsFactor()
|
public function getDynamicsFactor()
|
||||||
{
|
{
|
||||||
return $this->_dynamicsFactor;
|
return $this->dynamicsFactor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDrawProbability()
|
public function getDrawProbability()
|
||||||
{
|
{
|
||||||
return $this->_drawProbability;
|
return $this->drawProbability;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefaultRating()
|
public function getDefaultRating()
|
||||||
{
|
{
|
||||||
return new Rating($this->_initialMean, $this->_initialStandardDeviation);
|
return new Rating($this->initialMean, $this->initialStandardDeviation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
|
|||||||
/**
|
/**
|
||||||
* Constructs a player.
|
* Constructs a player.
|
||||||
*
|
*
|
||||||
* @param mixed $Id The identifier for the player, such as a name.
|
* @param mixed $Id The identifier for the player, such as a name.
|
||||||
* @param number $partialPlayPercentage The weight percentage to give this player when calculating a new rank.
|
* @param number $partialPlayPercentage The weight percentage to give this player when calculating a new rank.
|
||||||
* @param number $partialUpdatePercentage Indicated how much of a skill update a player should receive where 0 represents no update and 1.0 represents 100% of the update.
|
* @param number $partialUpdatePercentage Indicated how much of a skill update a player should receive where 0 represents no update and 1.0 represents 100% of the update.
|
||||||
*/
|
*/
|
||||||
|
@ -16,14 +16,14 @@ class Rating implements \Stringable
|
|||||||
* @param float $_standardDeviation The standard deviation of the rating (also known as s).
|
* @param float $_standardDeviation The standard deviation of the rating (also known as s).
|
||||||
* @param float|int $_conservativeStandardDeviationMultiplier optional The number of standardDeviations to subtract from the mean to achieve a conservative rating.
|
* @param float|int $_conservativeStandardDeviationMultiplier optional The number of standardDeviations to subtract from the mean to achieve a conservative rating.
|
||||||
*/
|
*/
|
||||||
public function __construct(private $_mean, private $_standardDeviation, private $_conservativeStandardDeviationMultiplier = self::CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER)
|
public function __construct(private float $_mean, private float $_standardDeviation, private float|int $_conservativeStandardDeviationMultiplier = self::CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The statistical mean value of the rating (also known as <EFBFBD>).
|
* The statistical mean value of the rating (also known as mu).
|
||||||
*/
|
*/
|
||||||
public function getMean()
|
public function getMean(): float
|
||||||
{
|
{
|
||||||
return $this->_mean;
|
return $this->_mean;
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ class Rating implements \Stringable
|
|||||||
/**
|
/**
|
||||||
* The standard deviation (the spread) of the rating. This is also known as s.
|
* The standard deviation (the spread) of the rating. This is also known as s.
|
||||||
*/
|
*/
|
||||||
public function getStandardDeviation()
|
public function getStandardDeviation(): float
|
||||||
{
|
{
|
||||||
return $this->_standardDeviation;
|
return $this->_standardDeviation;
|
||||||
}
|
}
|
||||||
@ -39,12 +39,12 @@ class Rating implements \Stringable
|
|||||||
/**
|
/**
|
||||||
* A conservative estimate of skill based on the mean and standard deviation.
|
* A conservative estimate of skill based on the mean and standard deviation.
|
||||||
*/
|
*/
|
||||||
public function getConservativeRating()
|
public function getConservativeRating(): float
|
||||||
{
|
{
|
||||||
return $this->_mean - $this->_conservativeStandardDeviationMultiplier * $this->_standardDeviation;
|
return $this->_mean - $this->_conservativeStandardDeviationMultiplier * $this->_standardDeviation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPartialUpdate(Rating $prior, Rating $fullPosterior, $updatePercentage)
|
public function getPartialUpdate(Rating $prior, Rating $fullPosterior, $updatePercentage): Rating
|
||||||
{
|
{
|
||||||
$priorGaussian = new GaussianDistribution($prior->getMean(), $prior->getStandardDeviation());
|
$priorGaussian = new GaussianDistribution($prior->getMean(), $prior->getStandardDeviation());
|
||||||
$posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior . getStandardDeviation());
|
$posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior . getStandardDeviation());
|
||||||
@ -56,7 +56,7 @@ class Rating implements \Stringable
|
|||||||
$precisionDifference = $posteriorGaussian->getPrecision() - $priorGaussian->getPrecision();
|
$precisionDifference = $posteriorGaussian->getPrecision() - $priorGaussian->getPrecision();
|
||||||
$partialPrecisionDifference = $updatePercentage * $precisionDifference;
|
$partialPrecisionDifference = $updatePercentage * $precisionDifference;
|
||||||
|
|
||||||
$precisionMeanDifference = $posteriorGaussian->getPrecisionMean() - $priorGaussian . getPrecisionMean();
|
$precisionMeanDifference = $posteriorGaussian->getPrecisionMean() - $priorGaussian->getPrecisionMean();
|
||||||
$partialPrecisionMeanDifference = $updatePercentage * $precisionMeanDifference;
|
$partialPrecisionMeanDifference = $updatePercentage * $precisionMeanDifference;
|
||||||
|
|
||||||
$partialPosteriorGaussion = GaussianDistribution::fromPrecisionMean(
|
$partialPosteriorGaussion = GaussianDistribution::fromPrecisionMean(
|
||||||
|
@ -11,7 +11,7 @@ class RatingContainer
|
|||||||
$this->playerToRating = new HashMap();
|
$this->playerToRating = new HashMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRating(Player $player): mixed
|
public function getRating(Player $player): Rating
|
||||||
{
|
{
|
||||||
return $this->playerToRating->getValue($player);
|
return $this->playerToRating->getValue($player);
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ use DNW\Skills\Numerics\GaussianDistribution;
|
|||||||
*/
|
*/
|
||||||
class GaussianPriorFactor extends GaussianFactor
|
class GaussianPriorFactor extends GaussianFactor
|
||||||
{
|
{
|
||||||
private $newMessage;
|
private GaussianDistribution $newMessage;
|
||||||
|
|
||||||
public function __construct(float $mean, float $variance, Variable $variable)
|
public function __construct(float $mean, float $variance, Variable $variable)
|
||||||
{
|
{
|
||||||
|
@ -11,7 +11,7 @@ use DNW\Skills\Numerics\GaussianDistribution;
|
|||||||
/**
|
/**
|
||||||
* Factor that sums together multiple Gaussians.
|
* Factor that sums together multiple Gaussians.
|
||||||
*
|
*
|
||||||
* See the accompanying math paper for more details.s
|
* See the accompanying math paper for more details.
|
||||||
*/
|
*/
|
||||||
class GaussianWeightedSumFactor extends GaussianFactor
|
class GaussianWeightedSumFactor extends GaussianFactor
|
||||||
{
|
{
|
||||||
@ -213,7 +213,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function createName($sumVariable, $variablesToSum, $weights)
|
private static function createName(string $sumVariable, array $variablesToSum, array $weights): string
|
||||||
{
|
{
|
||||||
// TODO: Perf? Use PHP equivalent of StringBuilder? implode on arrays?
|
// TODO: Perf? Use PHP equivalent of StringBuilder? implode on arrays?
|
||||||
$result = (string) $sumVariable;
|
$result = (string) $sumVariable;
|
||||||
|
@ -10,7 +10,7 @@ use DNW\Skills\FactorGraphs\ScheduleSequence;
|
|||||||
|
|
||||||
class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
|
class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
|
||||||
{
|
{
|
||||||
public function buildLayer()
|
public function buildLayer(): void
|
||||||
{
|
{
|
||||||
$inputVariablesGroups = $this->getInputVariablesGroups();
|
$inputVariablesGroups = $this->getInputVariablesGroups();
|
||||||
$outputVariablesGroups = &$this->getOutputVariablesGroups();
|
$outputVariablesGroups = &$this->getOutputVariablesGroups();
|
||||||
|
@ -9,7 +9,7 @@ use DNW\Skills\TrueSkill\TrueSkillFactorGraph;
|
|||||||
|
|
||||||
class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer
|
class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer
|
||||||
{
|
{
|
||||||
private $epsilon;
|
private float $epsilon;
|
||||||
|
|
||||||
public function __construct(TrueSkillFactorGraph $parentGraph, private readonly array $teamRanks)
|
public function __construct(TrueSkillFactorGraph $parentGraph, private readonly array $teamRanks)
|
||||||
{
|
{
|
||||||
@ -18,7 +18,7 @@ class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer
|
|||||||
$this->epsilon = DrawMargin::getDrawMarginFromDrawProbability($gameInfo->getDrawProbability(), $gameInfo->getBeta());
|
$this->epsilon = DrawMargin::getDrawMarginFromDrawProbability($gameInfo->getDrawProbability(), $gameInfo->getBeta());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildLayer()
|
public function buildLayer(): void
|
||||||
{
|
{
|
||||||
$inputVarGroups = $this->getInputVariablesGroups();
|
$inputVarGroups = $this->getInputVariablesGroups();
|
||||||
$inputVarGroupsCount = is_countable($inputVarGroups) ? count($inputVarGroups) : 0;
|
$inputVarGroupsCount = is_countable($inputVarGroups) ? count($inputVarGroups) : 0;
|
||||||
|
@ -78,7 +78,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
|
|||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, $comparison)
|
private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, $comparison): Rating
|
||||||
{
|
{
|
||||||
$drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
|
$drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
|
||||||
$gameInfo->getDrawProbability(),
|
$gameInfo->getDrawProbability(),
|
||||||
|
Reference in New Issue
Block a user