diff --git a/examples/basic.php b/examples/basic.php new file mode 100644 index 0000000..29b1b8a --- /dev/null +++ b/examples/basic.php @@ -0,0 +1,38 @@ +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; +} + + + diff --git a/src/FactorGraphs/Factor.php b/src/FactorGraphs/Factor.php index 61d52c4..b0fbce6 100644 --- a/src/FactorGraphs/Factor.php +++ b/src/FactorGraphs/Factor.php @@ -10,7 +10,7 @@ abstract class Factor implements \Stringable { private array $messages = []; - private $messageToVariableBinding; + private HashMap $messageToVariableBinding; private string $name; diff --git a/src/FactorGraphs/FactorGraph.php b/src/FactorGraphs/FactorGraph.php index 1669985..387c256 100644 --- a/src/FactorGraphs/FactorGraph.php +++ b/src/FactorGraphs/FactorGraph.php @@ -4,14 +4,14 @@ namespace DNW\Skills\FactorGraphs; class FactorGraph { - private $variableFactory; + private VariableFactory $variableFactory; - public function getVariableFactory() + public function getVariableFactory(): VariableFactory { return $this->variableFactory; } - public function setVariableFactory(VariableFactory $factory) + public function setVariableFactory(VariableFactory $factory): void { $this->variableFactory = $factory; } diff --git a/src/FactorGraphs/Schedule.php b/src/FactorGraphs/Schedule.php index 5877081..865690f 100644 --- a/src/FactorGraphs/Schedule.php +++ b/src/FactorGraphs/Schedule.php @@ -4,7 +4,7 @@ namespace DNW\Skills\FactorGraphs; 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 { - return (string) $this->_name; + return (string) $this->name; } } diff --git a/src/GameInfo.php b/src/GameInfo.php index 4d26989..1daf746 100644 --- a/src/GameInfo.php +++ b/src/GameInfo.php @@ -18,41 +18,41 @@ class GameInfo private const DEFAULT_INITIAL_STANDARD_DEVIATION = 8.3333333333333333333333333333333; public function __construct( - private $_initialMean = self::DEFAULT_INITIAL_MEAN, - private $_initialStandardDeviation = self::DEFAULT_INITIAL_STANDARD_DEVIATION, - private $_beta = self::DEFAULT_BETA, - private $_dynamicsFactor = self::DEFAULT_DYNAMICS_FACTOR, - private $_drawProbability = self::DEFAULT_DRAW_PROBABILITY + private float $initialMean = self::DEFAULT_INITIAL_MEAN, + private float $initialStandardDeviation = self::DEFAULT_INITIAL_STANDARD_DEVIATION, + private float $beta = self::DEFAULT_BETA, + private float $dynamicsFactor = self::DEFAULT_DYNAMICS_FACTOR, + private float $drawProbability = self::DEFAULT_DRAW_PROBABILITY ) { } public function getInitialMean() { - return $this->_initialMean; + return $this->initialMean; } public function getInitialStandardDeviation() { - return $this->_initialStandardDeviation; + return $this->initialStandardDeviation; } public function getBeta() { - return $this->_beta; + return $this->beta; } public function getDynamicsFactor() { - return $this->_dynamicsFactor; + return $this->dynamicsFactor; } public function getDrawProbability() { - return $this->_drawProbability; + return $this->drawProbability; } public function getDefaultRating() { - return new Rating($this->_initialMean, $this->_initialStandardDeviation); + return new Rating($this->initialMean, $this->initialStandardDeviation); } } diff --git a/src/Player.php b/src/Player.php index deb3ae2..70f620d 100644 --- a/src/Player.php +++ b/src/Player.php @@ -18,7 +18,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable /** * 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 $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. */ diff --git a/src/Rating.php b/src/Rating.php index fd07986..0ead0cd 100644 --- a/src/Rating.php +++ b/src/Rating.php @@ -16,14 +16,14 @@ class Rating implements \Stringable * @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. */ - 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 �). + * The statistical mean value of the rating (also known as mu). */ - public function getMean() + public function getMean(): float { 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. */ - public function getStandardDeviation() + public function getStandardDeviation(): float { return $this->_standardDeviation; } @@ -39,12 +39,12 @@ class Rating implements \Stringable /** * 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; } - public function getPartialUpdate(Rating $prior, Rating $fullPosterior, $updatePercentage) + public function getPartialUpdate(Rating $prior, Rating $fullPosterior, $updatePercentage): Rating { $priorGaussian = new GaussianDistribution($prior->getMean(), $prior->getStandardDeviation()); $posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior . getStandardDeviation()); @@ -56,7 +56,7 @@ class Rating implements \Stringable $precisionDifference = $posteriorGaussian->getPrecision() - $priorGaussian->getPrecision(); $partialPrecisionDifference = $updatePercentage * $precisionDifference; - $precisionMeanDifference = $posteriorGaussian->getPrecisionMean() - $priorGaussian . getPrecisionMean(); + $precisionMeanDifference = $posteriorGaussian->getPrecisionMean() - $priorGaussian->getPrecisionMean(); $partialPrecisionMeanDifference = $updatePercentage * $precisionMeanDifference; $partialPosteriorGaussion = GaussianDistribution::fromPrecisionMean( diff --git a/src/RatingContainer.php b/src/RatingContainer.php index f02b860..1281d59 100644 --- a/src/RatingContainer.php +++ b/src/RatingContainer.php @@ -11,7 +11,7 @@ class RatingContainer $this->playerToRating = new HashMap(); } - public function getRating(Player $player): mixed + public function getRating(Player $player): Rating { return $this->playerToRating->getValue($player); } diff --git a/src/TrueSkill/Factors/GaussianPriorFactor.php b/src/TrueSkill/Factors/GaussianPriorFactor.php index 0e5fa98..f6418d5 100644 --- a/src/TrueSkill/Factors/GaussianPriorFactor.php +++ b/src/TrueSkill/Factors/GaussianPriorFactor.php @@ -13,7 +13,7 @@ use DNW\Skills\Numerics\GaussianDistribution; */ class GaussianPriorFactor extends GaussianFactor { - private $newMessage; + private GaussianDistribution $newMessage; public function __construct(float $mean, float $variance, Variable $variable) { diff --git a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php index ea70262..447d87a 100644 --- a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php +++ b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php @@ -11,7 +11,7 @@ use DNW\Skills\Numerics\GaussianDistribution; /** * 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 { @@ -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? $result = (string) $sumVariable; diff --git a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php index cc26252..4819075 100644 --- a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php +++ b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php @@ -10,7 +10,7 @@ use DNW\Skills\FactorGraphs\ScheduleSequence; class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer { - public function buildLayer() + public function buildLayer(): void { $inputVariablesGroups = $this->getInputVariablesGroups(); $outputVariablesGroups = &$this->getOutputVariablesGroups(); diff --git a/src/TrueSkill/Layers/TeamDifferencesComparisonLayer.php b/src/TrueSkill/Layers/TeamDifferencesComparisonLayer.php index edb7048..3774843 100644 --- a/src/TrueSkill/Layers/TeamDifferencesComparisonLayer.php +++ b/src/TrueSkill/Layers/TeamDifferencesComparisonLayer.php @@ -9,7 +9,7 @@ use DNW\Skills\TrueSkill\TrueSkillFactorGraph; class TeamDifferencesComparisonLayer extends TrueSkillFactorGraphLayer { - private $epsilon; + private float $epsilon; 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()); } - public function buildLayer() + public function buildLayer(): void { $inputVarGroups = $this->getInputVariablesGroups(); $inputVarGroupsCount = is_countable($inputVarGroups) ? count($inputVarGroups) : 0; diff --git a/src/TrueSkill/TwoPlayerTrueSkillCalculator.php b/src/TrueSkill/TwoPlayerTrueSkillCalculator.php index 95f0891..e7ab5ca 100644 --- a/src/TrueSkill/TwoPlayerTrueSkillCalculator.php +++ b/src/TrueSkill/TwoPlayerTrueSkillCalculator.php @@ -78,7 +78,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator 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( $gameInfo->getDrawProbability(),