Bunch of generic code standard items. Will need a cleanup.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Jens True 2024-02-02 15:16:11 +00:00
parent 3dddfc05db
commit c18ccd38e2
38 changed files with 117 additions and 26 deletions

@ -13,6 +13,31 @@
<rule ref="PSR2"></rule> <rule ref="PSR2"></rule>
<rule ref="PSR12"></rule> <rule ref="PSR12"></rule>
<rule ref="Generic">
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed"/>
<exclude name="Generic.Files.LowercasedFilename.NotFound"/>
<exclude name="Generic.PHP.ClosingPHPTag.NotFound"/>
<exclude name="Generic.Files.EndFileNoNewline.Found"/>
<exclude name="Generic.Files.EndFileNoNewline.Found"/>
<exclude name="Generic.Arrays.DisallowShortArraySyntax.Found"/>
<exclude name="Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine"/>
<exclude name="Generic.Classes.OpeningBraceSameLine.BraceOnNewLine"/>
<exclude name="Generic.PHP.LowerCaseConstant.Found"/>
<exclude name="Generic.Formatting.SpaceAfterCast"/>
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSameWarning"/>
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
<exclude name="Generic.NamingConventions.AbstractClassNamePrefix.Missing"/>
<exclude name="Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed"/>
<exclude name="Generic.NamingConventions.InterfaceNameSuffix.Missing"/>
<exclude name="Generic.Commenting.Todo.TaskFound"/>
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUse"/>
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed"/>
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed"/>
<exclude name="Generic.Formatting.SpaceBeforeCast.NoSpace"/>
<exclude name="Generic.CodeAnalysis.UselessOverridingMethod.Found"/>
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.NewlineBeforeOpenBrace"/>
</rule>
<!-- Ban some functions --> <!-- Ban some functions -->
<rule ref="Generic.PHP.ForbiddenFunctions"> <rule ref="Generic.PHP.ForbiddenFunctions">
<properties> <properties>

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\FactorGraphs; namespace DNW\Skills\FactorGraphs;
class FactorGraph class FactorGraph

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\FactorGraphs; namespace DNW\Skills\FactorGraphs;
class KeyedVariable extends Variable class KeyedVariable extends Variable

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\FactorGraphs; namespace DNW\Skills\FactorGraphs;
use DNW\Skills\Numerics\GaussianDistribution; use DNW\Skills\Numerics\GaussianDistribution;

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\FactorGraphs; namespace DNW\Skills\FactorGraphs;
abstract class Schedule implements \Stringable abstract class Schedule implements \Stringable

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
use Exception; use Exception;

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
/** /**

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
/** /**

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
interface ISupportPartialUpdate interface ISupportPartialUpdate

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\Numerics; namespace DNW\Skills\Numerics;
/** /**
@ -13,7 +15,8 @@ class BasicMath
/** /**
* Squares the input (x^2 = x * x) * 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) * @return float The squared value (x^2)
*/ */
public static function square($x): float public static function square($x): float
@ -24,8 +27,9 @@ class BasicMath
/** /**
* Sums the items in $itemsToSum * Sums the items in $itemsToSum
* *
* @param mixed[] $itemsToSum The items to sum, * @param mixed[] $itemsToSum The items to sum,
* @param \Closure $callback The function to apply to each array element before summing. * @param \Closure $callback The function to apply to each array element before summing.
*
* @return float The sum. * @return float The sum.
*/ */
public static function sum(array $itemsToSum, \Closure $callback): float public static function sum(array $itemsToSum, \Closure $callback): float

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\Numerics; namespace DNW\Skills\Numerics;
/** /**
@ -198,7 +200,8 @@ class GaussianDistribution implements \Stringable
-1.523e-15, -1.523e-15,
-9.4e-17, -9.4e-17,
1.21e-16, 1.21e-16,
-2.8e-17, ]; -2.8e-17,
];
$ncof = count($coefficients); $ncof = count($coefficients);
$d = 0.0; $d = 0.0;

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\Numerics; namespace DNW\Skills\Numerics;
class IdentityMatrix extends DiagonalMatrix class IdentityMatrix extends DiagonalMatrix

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\Numerics; namespace DNW\Skills\Numerics;
// The whole purpose of this class is to make the code for the SkillCalculator(s) // The whole purpose of this class is to make the code for the SkillCalculator(s)

@ -1,12 +1,14 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\Numerics; namespace DNW\Skills\Numerics;
class SquareMatrix extends Matrix class SquareMatrix extends Matrix
{ {
public function __construct(float|int ...$allValues) public function __construct(float|int ...$allValues)
{ {
$rows = (int) sqrt(count($allValues)); $rows = (int)sqrt(count($allValues));
$cols = $rows; $cols = $rows;
$matrixData = []; $matrixData = [];

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\Numerics; namespace DNW\Skills\Numerics;
class Vector extends Matrix class Vector extends Matrix

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
/** /**

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
class PartialPlay class PartialPlay

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
/** /**
@ -26,7 +28,8 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
private mixed $Id, private mixed $Id,
float $partialPlayPercentage = self::DEFAULT_PARTIAL_PLAY_PERCENTAGE, float $partialPlayPercentage = self::DEFAULT_PARTIAL_PLAY_PERCENTAGE,
float $partialUpdatePercentage = self::DEFAULT_PARTIAL_UPDATE_PERCENTAGE float $partialUpdatePercentage = self::DEFAULT_PARTIAL_UPDATE_PERCENTAGE
) { )
{
// If they don't want to give a player an id, that's ok... // If they don't want to give a player an id, that's ok...
Guard::argumentInRangeInclusive($partialPlayPercentage, 0.0, 1.0, 'partialPlayPercentage'); Guard::argumentInRangeInclusive($partialPlayPercentage, 0.0, 1.0, 'partialPlayPercentage');
Guard::argumentInRangeInclusive($partialUpdatePercentage, 0, 1.0, 'partialUpdatePercentage'); Guard::argumentInRangeInclusive($partialUpdatePercentage, 0, 1.0, 'partialUpdatePercentage');
@ -60,6 +63,6 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
public function __toString(): string public function __toString(): string
{ {
return (string) $this->Id; return (string)$this->Id;
} }
} }

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
use DNW\Skills\Numerics\Range; use DNW\Skills\Numerics\Range;

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
// Container for a player's rating. // Container for a player's rating.

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
class RatingContainer class RatingContainer

@ -37,8 +37,8 @@ abstract class SkillCalculator
/** /**
* Calculates the match quality as the likelihood of all teams drawing. * Calculates the match quality as the likelihood of all teams drawing.
* *
* @param GameInfo $gameInfo Parameters for the game. * @param GameInfo $gameInfo Parameters for the game.
* @param Team[] $teams A mapping of team players and their ratings. * @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). * @return float The quality of the match between the teams as a percentage (0% = bad, 100% = well matched).
*/ */

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
class SkillCalculatorSupportedOptions class SkillCalculatorSupportedOptions

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills; namespace DNW\Skills;
class Team extends RatingContainer class Team extends RatingContainer

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill; namespace DNW\Skills\TrueSkill;
use DNW\Skills\GameInfo; use DNW\Skills\GameInfo;
@ -34,7 +36,8 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
GameInfo $gameInfo, GameInfo $gameInfo,
array $teams, array $teams,
array $teamRanks array $teamRanks
): RatingContainer { ): RatingContainer
{
Guard::argumentNotNull($gameInfo, 'gameInfo'); Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams); $this->validateTeamCountAndPlayersCountPerTeam($teams);
@ -124,7 +127,9 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
/** /**
* Helper function that gets a list of values for all player ratings * Helper function that gets a list of values for all player ratings
*
* @param Team[] $teamAssignmentsList * @param Team[] $teamAssignmentsList
*
* @return int[] * @return int[]
*/ */
private static function getPlayerRatingValues(array $teamAssignmentsList, \Closure $playerRatingFunction): array private static function getPlayerRatingValues(array $teamAssignmentsList, \Closure $playerRatingFunction): array

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors; namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Factor; use DNW\Skills\FactorGraphs\Factor;

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors; namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Message; use DNW\Skills\FactorGraphs\Message;
@ -62,11 +64,9 @@ class GaussianGreaterThanFactor extends GaussianFactor
$denom = 1.0 - TruncatedGaussianCorrectionFunctions::wExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC); $denom = 1.0 - TruncatedGaussianCorrectionFunctions::wExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC);
$newPrecision = $c / $denom; $newPrecision = $c / $denom;
$newPrecisionMean = ( $newPrecisionMean = ($d +
$d +
$sqrtC * $sqrtC *
TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC) TruncatedGaussianCorrectionFunctions::vExceedsMargin($dOnSqrtC, $epsilsonTimesSqrtC)) / $denom;
) / $denom;
$newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision); $newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors; namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\KeyedVariable; use DNW\Skills\FactorGraphs\KeyedVariable;

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors; namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Message; 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] * 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] * corresponding to v[0] = a1*v[1] + a2*v[2]
*
* @var array<float[]> $weights * @var array<float[]> $weights
*/ */
private array $weights = []; private array $weights = [];
@ -249,7 +250,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
$absValue = sprintf('%.2f', \abs($weights[$i])); // 0.00? $absValue = sprintf('%.2f', \abs($weights[$i])); // 0.00?
$result .= $absValue; $result .= $absValue;
$result .= '*['; $result .= '*[';
$result .= (string) $variablesToSum[$i]; $result .= (string)$variablesToSum[$i];
$result .= ']'; $result .= ']';
$isLast = ($i === $totalVars - 1); $isLast = ($i === $totalVars - 1);

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Factors; namespace DNW\Skills\TrueSkill\Factors;
use DNW\Skills\FactorGraphs\Message; use DNW\Skills\FactorGraphs\Message;
@ -65,8 +67,7 @@ class GaussianWithinFactor extends GaussianFactor
$newPrecision = $c / $denominator; $newPrecision = $c / $denominator;
$newPrecisionMean = ($d + $newPrecisionMean = ($d +
$sqrtC * $sqrtC *
TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC) TruncatedGaussianCorrectionFunctions::vWithinMargin($dOnSqrtC, $epsilonTimesSqrtC)) / $denominator;
) / $denominator;
$newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision); $newMarginal = GaussianDistribution::fromPrecisionMean($newPrecisionMean, $newPrecision);
$newMessage = GaussianDistribution::divide( $newMessage = GaussianDistribution::divide(

@ -17,7 +17,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
TrueSkillFactorGraph $parentGraph, TrueSkillFactorGraph $parentGraph,
private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $TeamPerformancesToTeamPerformanceDifferencesLayer, private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $TeamPerformancesToTeamPerformanceDifferencesLayer,
private readonly TeamDifferencesComparisonLayer $TeamDifferencesComparisonLayer private readonly TeamDifferencesComparisonLayer $TeamDifferencesComparisonLayer
) { )
{
parent::__construct($parentGraph); parent::__construct($parentGraph);
} }

@ -32,7 +32,8 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG
Variable $strongerTeam, Variable $strongerTeam,
Variable $weakerTeam, Variable $weakerTeam,
Variable $output Variable $output
): GaussianWeightedSumFactor { ): GaussianWeightedSumFactor
{
$teams = [$strongerTeam, $weakerTeam]; $teams = [$strongerTeam, $weakerTeam];
$weights = [1.0, -1.0]; $weights = [1.0, -1.0];

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Layers; namespace DNW\Skills\TrueSkill\Layers;
use DNW\Skills\FactorGraphs\FactorGraphLayer; use DNW\Skills\FactorGraphs\FactorGraphLayer;

@ -31,9 +31,9 @@ class TrueSkillFactorGraph extends FactorGraph
private PlayerPriorValuesToSkillsLayer $priorLayer; private PlayerPriorValuesToSkillsLayer $priorLayer;
/** /**
* @param GameInfo $gameInfo Parameters for the game. * @param GameInfo $gameInfo Parameters for the game.
* @param Team[] $teams A mapping of team players and their ratings. * @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 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) public function __construct(private readonly GameInfo $gameInfo, array $teams, array $teamRanks)
{ {

@ -122,8 +122,7 @@ class TruncatedGaussianCorrectionFunctions
$vt = self::vWithinMargin($teamPerformanceDifferenceAbsoluteValue, $drawMargin); $vt = self::vWithinMargin($teamPerformanceDifferenceAbsoluteValue, $drawMargin);
return $vt * $vt + return $vt * $vt +
( (($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
($drawMargin - $teamPerformanceDifferenceAbsoluteValue)
* *
GaussianDistribution::at( GaussianDistribution::at(
$drawMargin - $teamPerformanceDifferenceAbsoluteValue $drawMargin - $teamPerformanceDifferenceAbsoluteValue

@ -37,7 +37,8 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
GameInfo $gameInfo, GameInfo $gameInfo,
array $teams, array $teams,
array $teamRanks array $teamRanks
): RatingContainer { ): RatingContainer
{
// Basic argument checking // Basic argument checking
Guard::argumentNotNull($gameInfo, 'gameInfo'); Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams); $this->validateTeamCountAndPlayersCountPerTeam($teams);

@ -70,7 +70,8 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
Team $selfTeam, Team $selfTeam,
Team $otherTeam, Team $otherTeam,
PairwiseComparison $selfToOtherTeamComparison PairwiseComparison $selfToOtherTeamComparison
): void { ): void
{
$drawMargin = DrawMargin::getDrawMarginFromDrawProbability( $drawMargin = DrawMargin::getDrawMarginFromDrawProbability(
$gameInfo->getDrawProbability(), $gameInfo->getDrawProbability(),
$gameInfo->getBeta() $gameInfo->getBeta()