Compare commits

...

2 Commits

Author SHA1 Message Date
ec00087025 More pedantric types.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2024-02-29 10:42:31 +00:00
03d26de045 Tiny type work 2024-02-29 10:16:18 +00:00
14 changed files with 30 additions and 32 deletions

@ -34,10 +34,12 @@
],
"static": [
"@analyze-phpstan",
"@analyze-psalm"
"@analyze-psalm",
"@analyze-rector"
],
"analyze-phpstan":"vendor/bin/phpstan analyze --error-format=raw",
"analyze-psalm": "vendor/bin/psalm --no-cache",
"analyze-rector": "vendor/bin/rector --dry-run",
"html": [
"pandoc -s README.md -o output/README.html",
"pandoc -s docs/index.rst -o output/index.html"

@ -47,6 +47,6 @@ PHP Tools
---------
For development Composer and the following packages are used (Recommended as Phars installed via Phive)
* sudo phive install -g composer phpdocumentor infection phpcs phpcbf phploc phpbench overtrue/phplint
* sudo phive install -g composer phpdocumentor infection phpcs phpcbf phploc phpbench overtrue/phplint --force-accept-unsigned
* composer install
* composer all

@ -1,6 +1,6 @@
<?php
require_once(__DIR__ . "/vendor/autoload.php");
require_once(__DIR__ . "/../vendor/autoload.php");
use DNW\Skills\TrueSkill\FactorGraphTrueSkillCalculator;
use DNW\Skills\GameInfo;

@ -1,6 +1,6 @@
<?php
require_once(__DIR__ . "/vendor/autoload.php");
require_once(__DIR__ . "/../vendor/autoload.php");
use DNW\Skills\TrueSkill\TwoPlayerTrueSkillCalculator;
use DNW\Skills\GameInfo;

@ -4,10 +4,15 @@ declare(strict_types=1);
namespace DNW\Skills\FactorGraphs;
class FactorGraph
abstract class FactorGraph
{
private VariableFactory $variableFactory;
protected function __construct()
{
$this->variableFactory = new VariableFactory(static fn(): null => NULL);
}
public function getVariableFactory(): VariableFactory
{
return $this->variableFactory;

@ -15,12 +15,12 @@ abstract class FactorGraphLayer
private array $localFactors = [];
/**
* @var array<int,array<int,object>>
* @var array<int,array<int,Variable>>
*/
private array $outputVariablesGroups = [];
/**
* @var array<int,array<int,object>>
* @var array<int,array<int,Variable>>
*/
private array $inputVariablesGroups = [];
@ -29,7 +29,7 @@ abstract class FactorGraphLayer
}
/**
* @return array<int,array<int,object>>
* @return array<int,array<int,Variable>>
*/
protected function getInputVariablesGroups(): array
{
@ -44,7 +44,7 @@ abstract class FactorGraphLayer
/**
* This reference is still needed
*
* @return array<int,array<int,object>>
* @return array<int,array<int,Variable>>
*/
public function &getOutputVariablesGroups(): array
{
@ -60,7 +60,7 @@ abstract class FactorGraphLayer
}
/**
* @param array<int,array<int,object>> $value
* @param array<int,array<int,Variable>> $value
*/
public function setInputVariablesGroups(array $value): void
{

@ -13,13 +13,6 @@ use Exception;
*/
class Guard
{
public static function argumentNotNull(mixed $value, string $parameterName): void
{
if ($value == NULL) {
throw new Exception($parameterName . ' can not be null');
}
}
public static function argumentIsValidIndex(int $index, int $count, string $parameterName): void
{
if (($index < 0) || ($index >= $count)) {

@ -39,7 +39,6 @@ class FactorGraphTrueSkillCalculator extends SkillCalculator
array $teamRanks
): RatingContainer
{
Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams);
RankSorter::sort($teams, $teamRanks);

@ -18,6 +18,9 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
public function buildLayer(): void
{
$inputVariablesGroups = $this->getInputVariablesGroups();
/**
* @var KeyedVariable[] $currentTeam
*/
foreach ($inputVariablesGroups as $currentTeam) {
$localCurrentTeam = $currentTeam;
$teamPerformance = $this->createOutputVariable($localCurrentTeam);

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace DNW\Skills\TrueSkill\Layers;
use DNW\Skills\FactorGraphs\KeyedVariable;
use DNW\Skills\FactorGraphs\Variable;
use DNW\Skills\FactorGraphs\ScheduleStep;
use DNW\Skills\Numerics\BasicMath;
use DNW\Skills\TrueSkill\Factors\GaussianLikelihoodFactor;
@ -20,9 +21,12 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
foreach ($inputVariablesGroups as $currentTeam) {
$currentTeamPlayerPerformances = [];
/**
* @var Variable $playerSkillVariable
*/
foreach ($currentTeam as $playerSkillVariable) {
$localPlayerSkillVariable = $playerSkillVariable;
$currentPlayer = $localPlayerSkillVariable->getKey();
$currentPlayer = ($localPlayerSkillVariable instanceof KeyedVariable) ? $localPlayerSkillVariable->getKey() : "";
$playerPerformance = $this->createOutputVariable($currentPlayer);
$newLikelihoodFactor = $this->createLikelihood($localPlayerSkillVariable, $playerPerformance);
$this->addLayerFactor($newLikelihoodFactor);
@ -33,7 +37,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
}
}
private function createLikelihood(KeyedVariable $playerSkill, KeyedVariable $playerPerformance): GaussianLikelihoodFactor
private function createLikelihood(Variable $playerSkill, Variable $playerPerformance): GaussianLikelihoodFactor
{
return new GaussianLikelihoodFactor(
BasicMath::square($this->getParentFactorGraph()->getGameInfo()->getBeta()),

@ -12,8 +12,10 @@ use DNW\Skills\GameInfo;
use DNW\Skills\Numerics\GaussianDistribution;
use DNW\Skills\Rating;
use DNW\Skills\Team;
use DNW\Skills\Player;
use DNW\Skills\RatingContainer;
use DNW\Skills\FactorGraphs\FactorGraphLayer;
use DNW\Skills\FactorGraphs\KeyedVariable;
use DNW\Skills\TrueSkill\Layers\IteratedTeamDifferencesInnerLayer;
use DNW\Skills\TrueSkill\Layers\PlayerPerformancesToTeamPerformancesLayer;
use DNW\Skills\TrueSkill\Layers\PlayerPriorValuesToSkillsLayer;
@ -37,6 +39,7 @@ class TrueSkillFactorGraph extends FactorGraph
*/
public function __construct(private readonly GameInfo $gameInfo, array $teams, array $teamRanks)
{
parent::__construct();
$this->priorLayer = new PlayerPriorValuesToSkillsLayer($this, $teams);
$newFactory = new VariableFactory(
static fn(): GaussianDistribution => GaussianDistribution::fromPrecisionMean(0, 0)
@ -131,7 +134,7 @@ class TrueSkillFactorGraph extends FactorGraph
$priorLayerOutputVariablesGroups = $this->priorLayer->getOutputVariablesGroups();
foreach ($priorLayerOutputVariablesGroups as $currentTeam) {
foreach ($currentTeam as $currentPlayer) {
$localCurrentPlayer = $currentPlayer->getKey();
$localCurrentPlayer = ($currentPlayer instanceof KeyedVariable) ? $currentPlayer->getKey() : new Player("");
$newRating = new Rating(
$currentPlayer->getValue()->getMean(),
$currentPlayer->getValue()->getStandardDeviation()

@ -40,7 +40,6 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
): RatingContainer
{
// Basic argument checking
Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams);
// Make sure things are in order
@ -143,7 +142,6 @@ class TwoPlayerTrueSkillCalculator extends SkillCalculator
*/
public function calculateMatchQuality(GameInfo $gameInfo, array $teams): float
{
Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams);
$team1 = $teams[0];

@ -34,7 +34,6 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
*/
public function calculateNewRatings(GameInfo $gameInfo, array $teams, array $teamRanks): RatingContainer
{
Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams);
RankSorter::sort($teams, $teamRanks);
@ -150,7 +149,6 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
*/
public function calculateMatchQuality(GameInfo $gameInfo, array $teams): float
{
Guard::argumentNotNull($gameInfo, 'gameInfo');
$this->validateTeamCountAndPlayersCountPerTeam($teams);
// We've verified that there's just two teams

@ -10,13 +10,6 @@ use PHPUnit\Framework\TestCase;
class GuardTest extends TestCase
{
public function testArgumentNotNull(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('dummy can not be null');
Guard::argumentNotNull(NULL, "dummy");
}
public function testargumentIsValidIndex(): void
{
$this->expectException(Exception::class);