From 5c7471963c743102ca34da723c166452e08229fc Mon Sep 17 00:00:00 2001 From: Jens True Date: Wed, 2 Aug 2023 10:10:57 +0000 Subject: [PATCH] More type work. --- src/FactorGraphs/FactorGraphLayer.php | 2 +- src/FactorGraphs/Message.php | 3 ++- src/Numerics/IdentityMatrix.php | 2 +- src/Numerics/Matrix.php | 8 +++----- src/Player.php | 20 +++++++++---------- src/SkillCalculator.php | 6 +++--- src/Teams.php | 3 +++ .../FactorGraphTrueSkillCalculator.php | 6 +++--- .../Factors/GaussianGreaterThanFactor.php | 8 ++++---- .../Factors/GaussianLikelihoodFactor.php | 2 +- ...yerPerformancesToTeamPerformancesLayer.php | 7 ++++--- .../Layers/PlayerPriorValuesToSkillsLayer.php | 7 ++++--- ...ancesToTeamPerformanceDifferencesLayer.php | 6 +++--- src/TrueSkill/TrueSkillFactorGraph.php | 1 + 14 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/FactorGraphs/FactorGraphLayer.php b/src/FactorGraphs/FactorGraphLayer.php index f239e6c..cf2561c 100644 --- a/src/FactorGraphs/FactorGraphLayer.php +++ b/src/FactorGraphs/FactorGraphLayer.php @@ -21,7 +21,7 @@ abstract class FactorGraphLayer // HACK - public function getParentFactorGraph() + public function getParentFactorGraph(): FactorGraph { return $this->parentFactorGraph; } diff --git a/src/FactorGraphs/Message.php b/src/FactorGraphs/Message.php index 600e424..1dbe7c4 100644 --- a/src/FactorGraphs/Message.php +++ b/src/FactorGraphs/Message.php @@ -4,8 +4,9 @@ namespace DNW\Skills\FactorGraphs; class Message implements \Stringable { - public function __construct(private $value = null, private $name = null) + public function __construct(private ?object $value = null, private ?string $name = null) { + } public function getValue() diff --git a/src/Numerics/IdentityMatrix.php b/src/Numerics/IdentityMatrix.php index 0f4d4e4..5f5e183 100644 --- a/src/Numerics/IdentityMatrix.php +++ b/src/Numerics/IdentityMatrix.php @@ -4,7 +4,7 @@ namespace DNW\Skills\Numerics; class IdentityMatrix extends DiagonalMatrix { - public function __construct($rows) + public function __construct(int $rows) { parent::__construct(array_fill(0, $rows, 1)); } diff --git a/src/Numerics/Matrix.php b/src/Numerics/Matrix.php index 1d85b22..63edae5 100644 --- a/src/Numerics/Matrix.php +++ b/src/Numerics/Matrix.php @@ -8,7 +8,7 @@ class Matrix { public const ERROR_TOLERANCE = 0.0000000001; - public function __construct(private int $rowCount = 0, private int $columnCount = 0, private $matrixRowData = null) + public function __construct(private int $rowCount = 0, private int $columnCount = 0, private array $matrixRowData = array()) { } @@ -28,12 +28,10 @@ class Matrix return $result; } - public static function fromRowsColumns(...$args): Matrix + public static function fromRowsColumns(int $rows, int $cols, float|int ...$args): Matrix { - $rows = $args[0]; - $cols = $args[1]; $result = new Matrix($rows, $cols); - $currentIndex = 2; + $currentIndex = 0; for ($currentRow = 0; $currentRow < $rows; $currentRow++) { for ($currentCol = 0; $currentCol < $cols; $currentCol++) { diff --git a/src/Player.php b/src/Player.php index 70f620d..ce61228 100644 --- a/src/Player.php +++ b/src/Player.php @@ -11,21 +11,21 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable private const DEFAULT_PARTIAL_UPDATE_PERCENTAGE = 1.0; - private $PartialPlayPercentage; + private float $PartialPlayPercentage; - private $PartialUpdatePercentage; + private float $PartialUpdatePercentage; /** * Constructs a player. * * @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. + * @param float $partialPlayPercentage The weight percentage to give this player when calculating a new rank. + * @param float $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. */ public function __construct( - private $Id, - $partialPlayPercentage = self::DEFAULT_PARTIAL_PLAY_PERCENTAGE, - $partialUpdatePercentage = self::DEFAULT_PARTIAL_UPDATE_PERCENTAGE + private mixed $Id, + float $partialPlayPercentage = self::DEFAULT_PARTIAL_PLAY_PERCENTAGE, + float $partialUpdatePercentage = self::DEFAULT_PARTIAL_UPDATE_PERCENTAGE ) { // If they don't want to give a player an id, that's ok... Guard::argumentInRangeInclusive($partialPlayPercentage, 0.0, 1.0, 'partialPlayPercentage'); @@ -37,7 +37,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable /** * The identifier for the player, such as a name. */ - public function getId() + public function getId(): mixed { return $this->Id; } @@ -45,7 +45,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable /** * Indicates the percent of the time the player should be weighted where 0.0 indicates the player didn't play and 1.0 indicates the player played 100% of the time. */ - public function getPartialPlayPercentage() + public function getPartialPlayPercentage(): float { return $this->PartialPlayPercentage; } @@ -53,7 +53,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable /** * Indicated how much of a skill update a player should receive where 0.0 represents no update and 1.0 represents 100% of the update. */ - public function getPartialUpdatePercentage() + public function getPartialUpdatePercentage(): float { return $this->PartialUpdatePercentage; } diff --git a/src/SkillCalculator.php b/src/SkillCalculator.php index 5adc859..43362a9 100644 --- a/src/SkillCalculator.php +++ b/src/SkillCalculator.php @@ -10,7 +10,7 @@ use Exception; abstract class SkillCalculator { protected function __construct( - private $supportedOptions, + private int $supportedOptions, private readonly TeamsRange $totalTeamsAllowed, private readonly PlayersRange $playersPerTeamAllowed ) { @@ -22,13 +22,13 @@ abstract class SkillCalculator * @param GameInfo $gameInfo Parameters for the game. * @param array $teamsOfPlayerToRatings A mapping of team players and their ratings. * @param array $teamRanks The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2). - * @return All the players and their new ratings. + * @return RatingContainer All the players and their new ratings. */ abstract public function calculateNewRatings( GameInfo $gameInfo, array $teamsOfPlayerToRatings, array $teamRanks - ); + ): RatingContainer; /** * Calculates the match quality as the likelihood of all teams drawing. diff --git a/src/Teams.php b/src/Teams.php index 4502702..0e90919 100644 --- a/src/Teams.php +++ b/src/Teams.php @@ -4,6 +4,9 @@ namespace DNW\Skills; class Teams { + /** + * @return Team[] + */ public static function concat(Team ...$args/*variable arguments*/): array { $result = []; diff --git a/src/TrueSkill/FactorGraphTrueSkillCalculator.php b/src/TrueSkill/FactorGraphTrueSkillCalculator.php index f1db001..06ccead 100644 --- a/src/TrueSkill/FactorGraphTrueSkillCalculator.php +++ b/src/TrueSkill/FactorGraphTrueSkillCalculator.php @@ -86,7 +86,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator return exp($expPart) * sqrt($sqrtPart); } - private static function getPlayerMeansVector(array $teamAssignmentsList) + private static function getPlayerMeansVector(array $teamAssignmentsList): Vector { // A simple vector of all the player means. return new Vector( @@ -97,7 +97,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator ); } - private static function getPlayerCovarianceMatrix(array $teamAssignmentsList) + private static function getPlayerCovarianceMatrix(array $teamAssignmentsList): DiagonalMatrix { // This is a square matrix whose diagonal values represent the variance (square of standard deviation) of all // players. @@ -110,7 +110,7 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator } // Helper function that gets a list of values for all player ratings - private static function getPlayerRatingValues(array $teamAssignmentsList, $playerRatingFunction) + private static function getPlayerRatingValues(array $teamAssignmentsList, \Closure $playerRatingFunction): array { $playerRatingValues = []; diff --git a/src/TrueSkill/Factors/GaussianGreaterThanFactor.php b/src/TrueSkill/Factors/GaussianGreaterThanFactor.php index 36adb6a..249152f 100644 --- a/src/TrueSkill/Factors/GaussianGreaterThanFactor.php +++ b/src/TrueSkill/Factors/GaussianGreaterThanFactor.php @@ -14,16 +14,16 @@ use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions; */ class GaussianGreaterThanFactor extends GaussianFactor { - private $epsilon; + private float $epsilon; - public function __construct($epsilon, Variable $variable) + public function __construct(float $epsilon, Variable $variable) { parent::__construct(\sprintf('%s > %.2f', $variable, $epsilon)); $this->epsilon = $epsilon; $this->createVariableToMessageBinding($variable); } - public function getLogNormalization() + public function getLogNormalization(): float { /** * @var Variable[] $vars @@ -48,7 +48,7 @@ class GaussianGreaterThanFactor extends GaussianFactor ); } - protected function updateMessageVariable(Message $message, Variable $variable) + protected function updateMessageVariable(Message $message, Variable $variable): float { $oldMarginal = clone $variable->getValue(); $oldMessage = clone $message->getValue(); diff --git a/src/TrueSkill/Factors/GaussianLikelihoodFactor.php b/src/TrueSkill/Factors/GaussianLikelihoodFactor.php index ab88f49..114ae04 100644 --- a/src/TrueSkill/Factors/GaussianLikelihoodFactor.php +++ b/src/TrueSkill/Factors/GaussianLikelihoodFactor.php @@ -70,7 +70,7 @@ class GaussianLikelihoodFactor extends GaussianFactor return GaussianDistribution::subtract($newMarginal, $marginal1); } - public function updateMessageIndex($messageIndex): float + public function updateMessageIndex(int $messageIndex): float { $messages = $this->getMessages(); $vars = $this->getVariables(); diff --git a/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php b/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php index cb589fb..6ea086f 100644 --- a/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php +++ b/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php @@ -6,10 +6,11 @@ use DNW\Skills\FactorGraphs\ScheduleStep; use DNW\Skills\FactorGraphs\ScheduleSequence; use DNW\Skills\PartialPlay; use DNW\Skills\TrueSkill\Factors\GaussianWeightedSumFactor; +use DNW\Skills\FactorGraphs\Variable; class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLayer { - public function buildLayer() + public function buildLayer(): void { $inputVariablesGroups = $this->getInputVariablesGroups(); foreach ($inputVariablesGroups as $currentTeam) { @@ -38,7 +39,7 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye ); } - protected function createPlayerToTeamSumFactor($teamMembers, $sumVariable): GaussianWeightedSumFactor + protected function createPlayerToTeamSumFactor(array $teamMembers, Variable $sumVariable): GaussianWeightedSumFactor { $weights = array_map( function ($v) { @@ -75,7 +76,7 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye return $this->scheduleSequence($allFactors, "all of the team's sum iterations"); } - private function createOutputVariable($team) + private function createOutputVariable(array $team): Variable { $memberNames = array_map(fn ($currentPlayer) => (string) ($currentPlayer->getKey()), $team); diff --git a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php index 225e44d..f98aa62 100644 --- a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php +++ b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php @@ -9,6 +9,7 @@ use DNW\Skills\Numerics\BasicMath; use DNW\Skills\Rating; use DNW\Skills\TrueSkill\Factors\GaussianPriorFactor; use DNW\Skills\TrueSkill\TrueSkillFactorGraph; +use DNW\Skills\FactorGraphs\ScheduleSequence; // We intentionally have no Posterior schedule since the only purpose here is to // start the process. @@ -19,7 +20,7 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer parent::__construct($parentGraph); } - public function buildLayer() + public function buildLayer(): void { $teams = $this->teams; foreach ($teams as $currentTeam) { @@ -41,7 +42,7 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer } } - public function createPriorSchedule() + public function createPriorSchedule(): ScheduleSequence { $localFactors = $this->getLocalFactors(); @@ -54,7 +55,7 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer ); } - private function createPriorFactor(Rating $priorRating, Variable $skillsVariable) + private function createPriorFactor(Rating $priorRating, Variable $skillsVariable): GaussianPriorFactor { return new GaussianPriorFactor( $priorRating->getMean(), diff --git a/src/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php b/src/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php index 76469fe..b63f8f3 100644 --- a/src/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php +++ b/src/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php @@ -7,7 +7,7 @@ use DNW\Skills\TrueSkill\Factors\GaussianWeightedSumFactor; class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorGraphLayer { - public function buildLayer() + public function buildLayer(): void { $inputVariablesGroups = $this->getInputVariablesGroups(); $inputVariablesGroupsCount = is_countable($inputVariablesGroups) ? count($inputVariablesGroups) : 0; @@ -30,14 +30,14 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG Variable $strongerTeam, Variable $weakerTeam, Variable $output - ) { + ): GaussianWeightedSumFactor { $teams = [$strongerTeam, $weakerTeam]; $weights = [1.0, -1.0]; return new GaussianWeightedSumFactor($output, $teams, $weights); } - private function createOutputVariable() + private function createOutputVariable(): Variable { return $this->getParentFactorGraph()->getVariableFactory()->createBasicVariable('Team performance difference'); } diff --git a/src/TrueSkill/TrueSkillFactorGraph.php b/src/TrueSkill/TrueSkillFactorGraph.php index 98c7da5..888de4f 100644 --- a/src/TrueSkill/TrueSkillFactorGraph.php +++ b/src/TrueSkill/TrueSkillFactorGraph.php @@ -10,6 +10,7 @@ use DNW\Skills\GameInfo; use DNW\Skills\Numerics\GaussianDistribution; use DNW\Skills\Rating; use DNW\Skills\RatingContainer; +use DNW\Skills\FactorGraphs\FactorGraphLayer; use DNW\Skills\TrueSkill\Layers\IteratedTeamDifferencesInnerLayer; use DNW\Skills\TrueSkill\Layers\PlayerPerformancesToTeamPerformancesLayer; use DNW\Skills\TrueSkill\Layers\PlayerPriorValuesToSkillsLayer;