mirror of
				https://github.com/furyfire/trueskill.git
				synced 2025-11-04 02:02:29 +01:00 
			
		
		
		
	More type work
This commit is contained in:
		@@ -7,9 +7,12 @@ namespace DNW\Skills\FactorGraphs;
 | 
			
		||||
 */
 | 
			
		||||
class FactorList
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var Factor[] $list
 | 
			
		||||
     */
 | 
			
		||||
    private array $list = [];
 | 
			
		||||
 | 
			
		||||
    public function getLogNormalization()
 | 
			
		||||
    public function getLogNormalization(): float
 | 
			
		||||
    {
 | 
			
		||||
        $list = $this->list;
 | 
			
		||||
        foreach ($list as &$currentFactor) {
 | 
			
		||||
@@ -39,12 +42,12 @@ class FactorList
 | 
			
		||||
        return $sumLogZ + $sumLogS;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function count()
 | 
			
		||||
    public function count(): int
 | 
			
		||||
    {
 | 
			
		||||
        return count($this->list);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function addFactor(Factor $factor)
 | 
			
		||||
    public function addFactor(Factor $factor): Factor
 | 
			
		||||
    {
 | 
			
		||||
        $this->list[] = $factor;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -8,10 +8,10 @@ abstract class Schedule implements \Stringable
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    abstract public function visit(int $depth = -1, int $maxDepth = 0);
 | 
			
		||||
    abstract public function visit(int $depth = -1, int $maxDepth = 0): float;
 | 
			
		||||
 | 
			
		||||
    public function __toString(): string
 | 
			
		||||
    {
 | 
			
		||||
        return (string) $this->name;
 | 
			
		||||
        return $this->name;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,7 @@ class ScheduleLoop extends Schedule
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit(int $depth = -1, int $maxDepth = 0)
 | 
			
		||||
    public function visit(int $depth = -1, int $maxDepth = 0): float
 | 
			
		||||
    {
 | 
			
		||||
        $totalIterations = 1;
 | 
			
		||||
        $delta = $this->scheduleToLoop->visit($depth + 1, $maxDepth);
 | 
			
		||||
 
 | 
			
		||||
@@ -4,12 +4,12 @@ namespace DNW\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class ScheduleSequence extends Schedule
 | 
			
		||||
{
 | 
			
		||||
    public function __construct($name, private readonly array $schedules)
 | 
			
		||||
    public function __construct(string $name, private readonly array $schedules)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit($depth = -1, $maxDepth = 0)
 | 
			
		||||
    public function visit(int $depth = -1, int $maxDepth = 0): float
 | 
			
		||||
    {
 | 
			
		||||
        $maxDelta = 0;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,12 +4,12 @@ namespace DNW\Skills\FactorGraphs;
 | 
			
		||||
 | 
			
		||||
class ScheduleStep extends Schedule
 | 
			
		||||
{
 | 
			
		||||
    public function __construct($name, private readonly Factor $factor, private $index)
 | 
			
		||||
    public function __construct(string $name, private readonly Factor $factor, private $index)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct($name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function visit(int $depth = -1, int $maxDepth = 0)
 | 
			
		||||
    public function visit(int $depth = -1, int $maxDepth = 0): float
 | 
			
		||||
    {
 | 
			
		||||
        $currentFactor = $this->factor;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -298,7 +298,7 @@ class Matrix
 | 
			
		||||
        return new Matrix($this->rowCount - 1, $this->columnCount - 1, $result);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getCofactor($rowToRemove, $columnToRemove)
 | 
			
		||||
    public function getCofactor(int $rowToRemove, int $columnToRemove): float
 | 
			
		||||
    {
 | 
			
		||||
        // See http://en.wikipedia.org/wiki/Cofactor_(linear_algebra) for details
 | 
			
		||||
        // REVIEW: should things be reversed since I'm 0 indexed?
 | 
			
		||||
@@ -312,7 +312,7 @@ class Matrix
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function equals($otherMatrix)
 | 
			
		||||
    public function equals(Matrix $otherMatrix): bool
 | 
			
		||||
    {
 | 
			
		||||
        // If one is null, but not both, return false.
 | 
			
		||||
        if ($otherMatrix == null) {
 | 
			
		||||
 
 | 
			
		||||
@@ -33,17 +33,17 @@ class Range
 | 
			
		||||
 | 
			
		||||
    // REVIEW: It's probably bad form to have access statics via a derived class, but the syntax looks better :-)
 | 
			
		||||
 | 
			
		||||
    public static function inclusive(int $min, int $max): self
 | 
			
		||||
    public static function inclusive(int $min, int $max): static
 | 
			
		||||
    {
 | 
			
		||||
        return static::create($min, $max);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function exactly(int $value): self
 | 
			
		||||
    public static function exactly(int $value): static
 | 
			
		||||
    {
 | 
			
		||||
        return static::create($value, $value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static function atLeast(int $minimumValue): self
 | 
			
		||||
    public static function atLeast(int $minimumValue): static
 | 
			
		||||
    {
 | 
			
		||||
        return static::create($minimumValue, PHP_INT_MAX);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -44,10 +44,10 @@ class Rating implements \Stringable
 | 
			
		||||
        return $this->mean - $this->conservativeStandardDeviationMultiplier * $this->standardDeviation;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getPartialUpdate(Rating $prior, Rating $fullPosterior, $updatePercentage): Rating
 | 
			
		||||
    public function getPartialUpdate(Rating $prior, Rating $fullPosterior, float $updatePercentage): Rating
 | 
			
		||||
    {
 | 
			
		||||
        $priorGaussian = new GaussianDistribution($prior->getMean(), $prior->getStandardDeviation());
 | 
			
		||||
        $posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior . getStandardDeviation());
 | 
			
		||||
        $posteriorGaussian = new GaussianDistribution($fullPosterior->getMean(), $fullPosterior->getStandardDeviation());
 | 
			
		||||
 | 
			
		||||
        // From a clarification email from Ralf Herbrich:
 | 
			
		||||
        // "the idea is to compute a linear interpolation between the prior and posterior skills of each player
 | 
			
		||||
 
 | 
			
		||||
@@ -15,9 +15,9 @@ use Exception;
 | 
			
		||||
 */
 | 
			
		||||
class GaussianLikelihoodFactor extends GaussianFactor
 | 
			
		||||
{
 | 
			
		||||
    private $precision;
 | 
			
		||||
    private float $precision;
 | 
			
		||||
 | 
			
		||||
    public function __construct($betaSquared, Variable $variable1, Variable $variable2)
 | 
			
		||||
    public function __construct(float $betaSquared, Variable $variable1, Variable $variable2)
 | 
			
		||||
    {
 | 
			
		||||
        parent::__construct(sprintf('Likelihood of %s going to %s', $variable2, $variable1));
 | 
			
		||||
        $this->precision = 1.0 / $betaSquared;
 | 
			
		||||
@@ -42,7 +42,7 @@ class GaussianLikelihoodFactor extends GaussianFactor
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function updateHelper(Message $message1, Message $message2, Variable $variable1, Variable $variable2)
 | 
			
		||||
    private function updateHelper(Message $message1, Message $message2, Variable $variable1, Variable $variable2): float
 | 
			
		||||
    {
 | 
			
		||||
        $message1Value = clone $message1->getValue();
 | 
			
		||||
        $message2Value = clone $message2->getValue();
 | 
			
		||||
@@ -70,7 +70,7 @@ class GaussianLikelihoodFactor extends GaussianFactor
 | 
			
		||||
        return GaussianDistribution::subtract($newMarginal, $marginal1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function updateMessageIndex($messageIndex)
 | 
			
		||||
    public function updateMessageIndex($messageIndex): float
 | 
			
		||||
    {
 | 
			
		||||
        $messages = $this->getMessages();
 | 
			
		||||
        $vars = $this->getVariables();
 | 
			
		||||
 
 | 
			
		||||
@@ -14,16 +14,16 @@ use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions;
 | 
			
		||||
 */
 | 
			
		||||
class GaussianWithinFactor 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[] $variables
 | 
			
		||||
@@ -46,7 +46,7 @@ class GaussianWithinFactor extends GaussianFactor
 | 
			
		||||
        return -GaussianDistribution::logProductNormalization($messageFromVariable, $message) + log($z);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function updateMessageVariable(Message $message, Variable $variable)
 | 
			
		||||
    protected function updateMessageVariable(Message $message, Variable $variable): float
 | 
			
		||||
    {
 | 
			
		||||
        $oldMarginal = clone $variable->getValue();
 | 
			
		||||
        $oldMessage = clone $message->getValue();
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ namespace DNW\Skills\TrueSkill\Layers;
 | 
			
		||||
 | 
			
		||||
use DNW\Skills\FactorGraphs\ScheduleStep;
 | 
			
		||||
use DNW\Skills\FactorGraphs\Variable;
 | 
			
		||||
use DNW\Skills\FactorGraphs\KeyedVariable;
 | 
			
		||||
use DNW\Skills\Numerics\BasicMath;
 | 
			
		||||
use DNW\Skills\Rating;
 | 
			
		||||
use DNW\Skills\TrueSkill\Factors\GaussianPriorFactor;
 | 
			
		||||
@@ -63,7 +64,7 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function createSkillOutputVariable($key)
 | 
			
		||||
    private function createSkillOutputVariable(mixed $key) : KeyedVariable
 | 
			
		||||
    {
 | 
			
		||||
        $parentFactorGraph = $this->getParentFactorGraph();
 | 
			
		||||
        $variableFactory = $parentFactorGraph->getVariableFactory();
 | 
			
		||||
 
 | 
			
		||||
@@ -40,7 +40,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function createOutputVariable($key): KeyedVariable
 | 
			
		||||
    private function createOutputVariable(mixed $key): KeyedVariable
 | 
			
		||||
    {
 | 
			
		||||
        return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, $key . "'s performance");
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -19,9 +19,12 @@ use DNW\Skills\TrueSkill\Layers\TeamPerformancesToTeamPerformanceDifferencesLaye
 | 
			
		||||
 | 
			
		||||
class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
{
 | 
			
		||||
    private $layers;
 | 
			
		||||
    /**
 | 
			
		||||
     * @var FactorGraphLayer[] $layers
 | 
			
		||||
     */
 | 
			
		||||
    private array $layers;
 | 
			
		||||
 | 
			
		||||
    private $priorLayer;
 | 
			
		||||
    private PlayerPriorValuesToSkillsLayer $priorLayer;
 | 
			
		||||
 | 
			
		||||
    public function __construct(private readonly GameInfo $gameInfo, array $teams, array $teamRanks)
 | 
			
		||||
    {
 | 
			
		||||
@@ -43,7 +46,7 @@ class TrueSkillFactorGraph extends FactorGraph
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getGameInfo()
 | 
			
		||||
    public function getGameInfo(): GameInfo
 | 
			
		||||
    {
 | 
			
		||||
        return $this->gameInfo;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -78,7 +78,7 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
 | 
			
		||||
        return $results;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, $comparison): Rating
 | 
			
		||||
    private static function calculateNewRating(GameInfo $gameInfo, Rating $selfRating, Rating $opponentRating, PairwiseComparison $comparison): Rating
 | 
			
		||||
    {
 | 
			
		||||
        $drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
 | 
			
		||||
            $gameInfo->getDrawProbability(),
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user