mirror of
				https://github.com/furyfire/trueskill.git
				synced 2025-11-04 02:02:29 +01:00 
			
		
		
		
	Bunch of generic code standard items. Will need a cleanup.
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				ci/woodpecker/push/woodpecker Pipeline failed
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	ci/woodpecker/push/woodpecker Pipeline failed
				
			This commit is contained in:
		@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class FactorGraph
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class KeyedVariable extends Variable
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\Numerics\GaussianDistribution;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
abstract class Schedule implements \Stringable
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
interface ISupportPartialUpdate
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -13,7 +15,8 @@ class BasicMath
 | 
			
		||||
    /**
 | 
			
		||||
     * Squares the input (x^2 = x * x)
 | 
			
		||||
     *
 | 
			
		||||
     * @param  float $x Value to square (x)
 | 
			
		||||
     * @param float $x Value to square (x)
 | 
			
		||||
     *
 | 
			
		||||
     * @return float The squared value (x^2)
 | 
			
		||||
     */
 | 
			
		||||
    public static function square($x): float
 | 
			
		||||
@@ -24,8 +27,9 @@ class BasicMath
 | 
			
		||||
    /**
 | 
			
		||||
     * Sums the items in $itemsToSum
 | 
			
		||||
     *
 | 
			
		||||
     * @param  mixed[]    $itemsToSum The items to sum,
 | 
			
		||||
     * @param  \Closure $callback   The function to apply to each array element before summing.
 | 
			
		||||
     * @param mixed[]    $itemsToSum The items to sum,
 | 
			
		||||
     * @param \Closure $callback   The function to apply to each array element before summing.
 | 
			
		||||
     *
 | 
			
		||||
     * @return float The sum.
 | 
			
		||||
     */
 | 
			
		||||
    public static function sum(array $itemsToSum, \Closure $callback): float
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -198,7 +200,8 @@ class GaussianDistribution implements \Stringable
 | 
			
		||||
            -1.523e-15,
 | 
			
		||||
            -9.4e-17,
 | 
			
		||||
            1.21e-16,
 | 
			
		||||
            -2.8e-17, ];
 | 
			
		||||
            -2.8e-17,
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        $ncof = count($coefficients);
 | 
			
		||||
        $d = 0.0;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
class IdentityMatrix extends DiagonalMatrix
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
// The whole purpose of this class is to make the code for the SkillCalculator(s)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,14 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
class SquareMatrix extends Matrix
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(float|int ...$allValues)
 | 
			
		||||
    {
 | 
			
		||||
        $rows = (int) sqrt(count($allValues));
 | 
			
		||||
        $rows = (int)sqrt(count($allValues));
 | 
			
		||||
        $cols = $rows;
 | 
			
		||||
 | 
			
		||||
        $matrixData = [];
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\Numerics;
 | 
			
		||||
 | 
			
		||||
class Vector extends Matrix
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
class PartialPlay
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -26,7 +28,8 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
 | 
			
		||||
        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');
 | 
			
		||||
        Guard::argumentInRangeInclusive($partialUpdatePercentage, 0, 1.0, 'partialUpdatePercentage');
 | 
			
		||||
@@ -60,6 +63,6 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
 | 
			
		||||
 | 
			
		||||
    public function __toString(): string
 | 
			
		||||
    {
 | 
			
		||||
        return (string) $this->Id;
 | 
			
		||||
        return (string)$this->Id;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\Numerics\Range;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
// Container for a player's rating.
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
class RatingContainer
 | 
			
		||||
 
 | 
			
		||||
@@ -37,8 +37,8 @@ abstract class SkillCalculator
 | 
			
		||||
    /**
 | 
			
		||||
     * Calculates the match quality as the likelihood of all teams drawing.
 | 
			
		||||
     *
 | 
			
		||||
     * @param  GameInfo $gameInfo Parameters for the game.
 | 
			
		||||
     * @param  Team[]   $teams    A mapping of team players and their ratings.
 | 
			
		||||
     * @param GameInfo $gameInfo Parameters for the game.
 | 
			
		||||
     * @param Team[]   $teams    A mapping of team players and their ratings.
 | 
			
		||||
     *
 | 
			
		||||
     * @return float The quality of the match between the teams as a percentage (0% = bad, 100% = well matched).
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
class SkillCalculatorSupportedOptions
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills;
 | 
			
		||||
 | 
			
		||||
class Team extends RatingContainer
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\TrueSkill;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\GameInfo;
 | 
			
		||||
@@ -34,7 +36,8 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        GameInfo $gameInfo,
 | 
			
		||||
        array $teams,
 | 
			
		||||
        array $teamRanks
 | 
			
		||||
    ): RatingContainer {
 | 
			
		||||
    ): RatingContainer
 | 
			
		||||
    {
 | 
			
		||||
        Guard::argumentNotNull($gameInfo, 'gameInfo');
 | 
			
		||||
        $this->validateTeamCountAndPlayersCountPerTeam($teams);
 | 
			
		||||
 | 
			
		||||
@@ -124,7 +127,9 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Helper function that gets a list of values for all player ratings
 | 
			
		||||
     *
 | 
			
		||||
     * @param Team[] $teamAssignmentsList
 | 
			
		||||
     *
 | 
			
		||||
     * @return int[]
 | 
			
		||||
     */
 | 
			
		||||
    private static function getPlayerRatingValues(array $teamAssignmentsList, \Closure $playerRatingFunction): array
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\FactorGraphs\Factor;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\FactorGraphs\Message;
 | 
			
		||||
@@ -62,11 +64,9 @@ class GaussianGreaterThanFactor extends GaussianFactor
 | 
			
		||||
        $denom = 1.0 - TruncatedGaussianCorrectionFunctions::wExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC);
 | 
			
		||||
 | 
			
		||||
        $newPrecision = $c / $denom;
 | 
			
		||||
        $newPrecisionMean = (
 | 
			
		||||
                $d +
 | 
			
		||||
        $newPrecisionMean = ($d +
 | 
			
		||||
                $sqrtC *
 | 
			
		||||
                TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC)
 | 
			
		||||
            ) / $denom;
 | 
			
		||||
                TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC)) / $denom;
 | 
			
		||||
 | 
			
		||||
        $newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\FactorGraphs\KeyedVariable;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\FactorGraphs\Message;
 | 
			
		||||
 
 | 
			
		||||
@@ -26,6 +26,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
    /**
 | 
			
		||||
     * This following is used for convenience, for example, the first entry is [0, 1, 2]
 | 
			
		||||
     * corresponding to v[0] = a1*v[1] + a2*v[2]
 | 
			
		||||
     *
 | 
			
		||||
     * @var array<float[]> $weights
 | 
			
		||||
     */
 | 
			
		||||
    private array $weights = [];
 | 
			
		||||
@@ -249,7 +250,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
 | 
			
		||||
            $absValue = sprintf('%.2f', \abs($weights[$i])); // 0.00?
 | 
			
		||||
            $result .= $absValue;
 | 
			
		||||
            $result .= '*[';
 | 
			
		||||
            $result .= (string) $variablesToSum[$i];
 | 
			
		||||
            $result .= (string)$variablesToSum[$i];
 | 
			
		||||
            $result .= ']';
 | 
			
		||||
 | 
			
		||||
            $isLast = ($i === $totalVars - 1);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\TrueSkill\Factors;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\FactorGraphs\Message;
 | 
			
		||||
@@ -65,8 +67,7 @@ class GaussianWithinFactor extends GaussianFactor
 | 
			
		||||
        $newPrecision = $c / $denominator;
 | 
			
		||||
        $newPrecisionMean = ($d +
 | 
			
		||||
                                $sqrtC *
 | 
			
		||||
                                TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC)
 | 
			
		||||
                            ) / $denominator;
 | 
			
		||||
                                TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC)) / $denominator;
 | 
			
		||||
 | 
			
		||||
        $newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);
 | 
			
		||||
        $newMessage = GaussianDistribution::divide(
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
        TrueSkillFactorGraph $parentGraph,
 | 
			
		||||
        private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $TeamPerformancesToTeamPerformanceDifferencesLayer,
 | 
			
		||||
        private readonly TeamDifferencesComparisonLayer $TeamDifferencesComparisonLayer
 | 
			
		||||
    ) {
 | 
			
		||||
    )
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($parentGraph);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -32,7 +32,8 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG
 | 
			
		||||
        Variable $strongerTeam,
 | 
			
		||||
        Variable $weakerTeam,
 | 
			
		||||
        Variable $output
 | 
			
		||||
    ): GaussianWeightedSumFactor {
 | 
			
		||||
    ): GaussianWeightedSumFactor
 | 
			
		||||
    {
 | 
			
		||||
        $teams = [$strongerTeam, $weakerTeam];
 | 
			
		||||
        $weights = [1.0, -1.0];
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace DNW\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\FactorGraphs\FactorGraphLayer;
 | 
			
		||||
 
 | 
			
		||||
@@ -31,9 +31,9 @@ class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
    private PlayerPriorValuesToSkillsLayer $priorLayer;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param  GameInfo $gameInfo  Parameters for the game.
 | 
			
		||||
     * @param  Team[]   $teams     A mapping of team players and their ratings.
 | 
			
		||||
     * @param  int[]    $teamRanks The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2).
 | 
			
		||||
     * @param GameInfo $gameInfo  Parameters for the game.
 | 
			
		||||
     * @param Team[]   $teams     A mapping of team players and their ratings.
 | 
			
		||||
     * @param int[]    $teamRanks The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2).
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct(private readonly GameInfo $gameInfo, array $teams, array $teamRanks)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -122,8 +122,7 @@ class TruncatedGaussianCorrectionFunctions
 | 
			
		||||
        $vt = self::vWithinMargin($teamPerformanceDifferenceAbsoluteValue, $drawMargin);
 | 
			
		||||
 | 
			
		||||
        return $vt * $vt +
 | 
			
		||||
        (
 | 
			
		||||
            ($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
        (($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
 | 
			
		||||
            *
 | 
			
		||||
            GaussianDistribution::at(
 | 
			
		||||
                $drawMargin - $teamPerformanceDifferenceAbsoluteValue
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,8 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        GameInfo $gameInfo,
 | 
			
		||||
        array $teams,
 | 
			
		||||
        array $teamRanks
 | 
			
		||||
    ): RatingContainer {
 | 
			
		||||
    ): RatingContainer
 | 
			
		||||
    {
 | 
			
		||||
        // Basic argument checking
 | 
			
		||||
        Guard::argumentNotNull($gameInfo, 'gameInfo');
 | 
			
		||||
        $this->validateTeamCountAndPlayersCountPerTeam($teams);
 | 
			
		||||
 
 | 
			
		||||
@@ -70,7 +70,8 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        Team $selfTeam,
 | 
			
		||||
        Team $otherTeam,
 | 
			
		||||
        PairwiseComparison $selfToOtherTeamComparison
 | 
			
		||||
    ): void {
 | 
			
		||||
    ): void
 | 
			
		||||
    {
 | 
			
		||||
        $drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
 | 
			
		||||
            $gameInfo->getDrawProbability(),
 | 
			
		||||
            $gameInfo->getBeta()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user