From ae5d2a8b7304b9410a4957ebabce58cb5469615e Mon Sep 17 00:00:00 2001 From: Jens True Date: Tue, 19 Mar 2024 15:09:13 +0000 Subject: [PATCH] String based "name" for Variable class removed for performance --- src/FactorGraphs/KeyedVariable.php | 4 +- src/FactorGraphs/Variable.php | 12 +----- src/FactorGraphs/VariableFactory.php | 8 ++-- src/TrueSkill/Factors/GaussianFactor.php | 3 +- .../Factors/GaussianGreaterThanFactor.php | 7 +--- .../Factors/GaussianLikelihoodFactor.php | 2 +- src/TrueSkill/Factors/GaussianPriorFactor.php | 4 +- .../Factors/GaussianWeightedSumFactor.php | 40 +------------------ .../Factors/GaussianWithinFactor.php | 7 +--- ...yerPerformancesToTeamPerformancesLayer.php | 12 ++---- .../Layers/PlayerPriorValuesToSkillsLayer.php | 2 +- .../PlayerSkillsToPerformancesLayer.php | 2 +- ...ancesToTeamPerformanceDifferencesLayer.php | 5 ++- tests/FactorGraphs/VariableTest.php | 3 +- 14 files changed, 28 insertions(+), 83 deletions(-) diff --git a/src/FactorGraphs/KeyedVariable.php b/src/FactorGraphs/KeyedVariable.php index 006e160..e6094fe 100644 --- a/src/FactorGraphs/KeyedVariable.php +++ b/src/FactorGraphs/KeyedVariable.php @@ -6,9 +6,9 @@ namespace DNW\Skills\FactorGraphs; class KeyedVariable extends Variable { - public function __construct(private readonly mixed $key, string $name, mixed $prior) + public function __construct(private readonly mixed $key, mixed $prior) { - parent::__construct($name, $prior); + parent::__construct($prior); } public function getKey(): mixed diff --git a/src/FactorGraphs/Variable.php b/src/FactorGraphs/Variable.php index 77cdfc0..1e33e3f 100644 --- a/src/FactorGraphs/Variable.php +++ b/src/FactorGraphs/Variable.php @@ -6,15 +6,12 @@ namespace DNW\Skills\FactorGraphs; use DNW\Skills\Numerics\GaussianDistribution; -class Variable implements \Stringable +class Variable { - private readonly string $name; - private mixed $value; - public function __construct(string $name, private GaussianDistribution $prior) + public function __construct(private GaussianDistribution $prior) { - $this->name = 'Variable[' . $name . ']'; $this->resetToPrior(); } @@ -32,9 +29,4 @@ class Variable implements \Stringable { $this->value = $this->prior; } - - public function __toString(): string - { - return $this->name; - } } diff --git a/src/FactorGraphs/VariableFactory.php b/src/FactorGraphs/VariableFactory.php index 1cc5f1f..b7ea2e9 100644 --- a/src/FactorGraphs/VariableFactory.php +++ b/src/FactorGraphs/VariableFactory.php @@ -10,17 +10,17 @@ class VariableFactory { } - public function createBasicVariable(string $name): Variable + public function createBasicVariable(): Variable { $initializer = $this->variablePriorInitializer; - return new Variable($name, $initializer()); + return new Variable($initializer()); } - public function createKeyedVariable(mixed $key, string $name): KeyedVariable + public function createKeyedVariable(mixed $key): KeyedVariable { $initializer = $this->variablePriorInitializer; - return new KeyedVariable($key, $name, $initializer()); + return new KeyedVariable($key, $initializer()); } } diff --git a/src/TrueSkill/Factors/GaussianFactor.php b/src/TrueSkill/Factors/GaussianFactor.php index c45b44f..b581cb3 100644 --- a/src/TrueSkill/Factors/GaussianFactor.php +++ b/src/TrueSkill/Factors/GaussianFactor.php @@ -31,8 +31,7 @@ abstract class GaussianFactor extends Factor return parent::createVariableToMessageBindingWithMessage( $variable, new Message( - $newDistribution, - sprintf('message from %s to %s', (string)$this, (string)$variable) + $newDistribution,'message from %s to %s' ) ); } diff --git a/src/TrueSkill/Factors/GaussianGreaterThanFactor.php b/src/TrueSkill/Factors/GaussianGreaterThanFactor.php index 19e41c5..2e23a14 100644 --- a/src/TrueSkill/Factors/GaussianGreaterThanFactor.php +++ b/src/TrueSkill/Factors/GaussianGreaterThanFactor.php @@ -16,12 +16,9 @@ use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions; */ class GaussianGreaterThanFactor extends GaussianFactor { - private readonly float $epsilon; - - public function __construct(float $epsilon, Variable $variable) + public function __construct(private readonly float $epsilon, Variable $variable) { - parent::__construct(\sprintf('%s > %.2f', (string)$variable, $epsilon)); - $this->epsilon = $epsilon; + parent::__construct('%s > %.2f'); $this->createVariableToMessageBinding($variable); } diff --git a/src/TrueSkill/Factors/GaussianLikelihoodFactor.php b/src/TrueSkill/Factors/GaussianLikelihoodFactor.php index 7e7f7f1..10902bb 100644 --- a/src/TrueSkill/Factors/GaussianLikelihoodFactor.php +++ b/src/TrueSkill/Factors/GaussianLikelihoodFactor.php @@ -21,7 +21,7 @@ class GaussianLikelihoodFactor extends GaussianFactor public function __construct(float $betaSquared, Variable $variable1, Variable $variable2) { - parent::__construct(sprintf('Likelihood of %s going to %s', (string)$variable2, (string)$variable1)); + parent::__construct('Likelihood of %s going to %s'); $this->precision = 1.0 / $betaSquared; $this->createVariableToMessageBinding($variable1); $this->createVariableToMessageBinding($variable2); diff --git a/src/TrueSkill/Factors/GaussianPriorFactor.php b/src/TrueSkill/Factors/GaussianPriorFactor.php index 60b2e4e..e703aba 100644 --- a/src/TrueSkill/Factors/GaussianPriorFactor.php +++ b/src/TrueSkill/Factors/GaussianPriorFactor.php @@ -19,11 +19,11 @@ class GaussianPriorFactor extends GaussianFactor public function __construct(float $mean, float $variance, Variable $variable) { - parent::__construct(sprintf('Prior value going to %s', (string)$variable)); + parent::__construct('Prior value going to %s'); $this->newMessage = new GaussianDistribution($mean, sqrt($variance)); $newMessage = new Message( GaussianDistribution::fromPrecisionMean(0, 0), - sprintf('message from %s to %s', (string)$this, (string)$variable) + 'message from %s to %s' ); $this->createVariableToMessageBindingWithMessage($variable, $newMessage); diff --git a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php index 3622b14..7e7b407 100644 --- a/src/TrueSkill/Factors/GaussianWeightedSumFactor.php +++ b/src/TrueSkill/Factors/GaussianWeightedSumFactor.php @@ -42,7 +42,7 @@ class GaussianWeightedSumFactor extends GaussianFactor */ public function __construct(Variable $sumVariable, array $variablesToSum, array $variableWeights) { - parent::__construct(self::createName((string)$sumVariable, $variablesToSum, $variableWeights)); + parent::__construct('$sumVariable, $variablesToSum, $variableWeights'); // The first weights are a straightforward copy // v_0 = a_1*v_1 + a_2*v_2 + ... + a_n * v_n @@ -235,42 +235,4 @@ class GaussianWeightedSumFactor extends GaussianFactor $updatedVariables ); } - - /** - * @param Variable[] $variablesToSum - * @param float[] $weights - */ - private static function createName(string $sumVariable, array $variablesToSum, array $weights): string - { - // TODO: Perf? Use PHP equivalent of StringBuilder? implode on arrays? - $result = $sumVariable; - $result .= ' = '; - - $totalVars = count($variablesToSum); - for ($i = 0; $i < $totalVars; ++$i) { - $isFirst = ($i == 0); - - if ($isFirst && ($weights[$i] < 0)) { - $result .= '-'; - } - - $absValue = sprintf('%.2f', \abs($weights[$i])); // 0.00? - $result .= $absValue; - $result .= '*['; - $result .= (string)$variablesToSum[$i]; - $result .= ']'; - - $isLast = ($i === $totalVars - 1); - - if (! $isLast) { - if ($weights[$i + 1] >= 0) { - $result .= ' + '; - } else { - $result .= ' - '; - } - } - } - - return $result; - } } diff --git a/src/TrueSkill/Factors/GaussianWithinFactor.php b/src/TrueSkill/Factors/GaussianWithinFactor.php index cf0970f..24cbbb2 100644 --- a/src/TrueSkill/Factors/GaussianWithinFactor.php +++ b/src/TrueSkill/Factors/GaussianWithinFactor.php @@ -16,12 +16,9 @@ use DNW\Skills\TrueSkill\TruncatedGaussianCorrectionFunctions; */ class GaussianWithinFactor extends GaussianFactor { - private readonly float $epsilon; - - public function __construct(float $epsilon, Variable $variable) + public function __construct(private readonly float $epsilon, Variable $variable) { - parent::__construct(sprintf('%s <= %.2f', (string)$variable, $epsilon)); - $this->epsilon = $epsilon; + parent::__construct('%s <= %.2f'); $this->createVariableToMessageBinding($variable); } diff --git a/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php b/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php index a14e01a..01a8891 100644 --- a/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php +++ b/src/TrueSkill/Layers/PlayerPerformancesToTeamPerformancesLayer.php @@ -21,7 +21,7 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye */ foreach ($inputVariablesGroups as $currentTeam) { $localCurrentTeam = $currentTeam; - $teamPerformance = $this->createOutputVariable($localCurrentTeam); + $teamPerformance = $this->createOutputVariable(); $newSumFactor = $this->createPlayerToTeamSumFactor($localCurrentTeam, $teamPerformance); $this->addLayerFactor($newSumFactor); @@ -85,14 +85,10 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye } /** - * @param KeyedVariable[] $team + * Team's performance */ - private function createOutputVariable(array $team): Variable + private function createOutputVariable(): Variable { - $memberNames = array_map(static fn($currentPlayer): string => (string)($currentPlayer->getKey()->getId()), $team); - - $teamMemberNames = \implode(', ', $memberNames); - - return $this->getParentFactorGraph()->getVariableFactory()->createBasicVariable('Team[' . $teamMemberNames . "]'s performance"); + return $this->getParentFactorGraph()->getVariableFactory()->createBasicVariable(); } } diff --git a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php index 36f9f52..af57ffe 100644 --- a/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php +++ b/src/TrueSkill/Layers/PlayerPriorValuesToSkillsLayer.php @@ -77,6 +77,6 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer $parentFactorGraph = $this->getParentFactorGraph(); $variableFactory = $parentFactorGraph->getVariableFactory(); - return $variableFactory->createKeyedVariable($key, $key->getId() . "'s skill"); + return $variableFactory->createKeyedVariable($key); } } diff --git a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php index bbe4e11..fbb4c14 100644 --- a/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php +++ b/src/TrueSkill/Layers/PlayerSkillsToPerformancesLayer.php @@ -48,7 +48,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer private function createOutputVariable(mixed $key): KeyedVariable { - return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, $key->getId() . "'s performance"); + return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key); } public function createPriorSchedule(): ?ScheduleSequence diff --git a/src/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php b/src/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php index dddb5b8..5199d20 100644 --- a/src/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php +++ b/src/TrueSkill/Layers/TeamPerformancesToTeamPerformanceDifferencesLayer.php @@ -40,8 +40,11 @@ class TeamPerformancesToTeamPerformanceDifferencesLayer extends TrueSkillFactorG return new GaussianWeightedSumFactor($output, $teams, $weights); } + /** + * Team performance difference + */ private function createOutputVariable(): Variable { - return $this->getParentFactorGraph()->getVariableFactory()->createBasicVariable('Team performance difference'); + return $this->getParentFactorGraph()->getVariableFactory()->createBasicVariable(); } } diff --git a/tests/FactorGraphs/VariableTest.php b/tests/FactorGraphs/VariableTest.php index 88e997b..835f6d0 100644 --- a/tests/FactorGraphs/VariableTest.php +++ b/tests/FactorGraphs/VariableTest.php @@ -13,13 +13,12 @@ class VariableTest extends TestCase public function testGetterSetter(): void { $gd_prior = new GaussianDistribution(); - $var = new Variable('dummy', $gd_prior); + $var = new Variable($gd_prior); $this->assertEquals($gd_prior, $var->getValue()); $gd_new = new GaussianDistribution(); $this->assertEquals($gd_new, $var->getValue()); $var->resetToPrior(); $this->assertEquals($gd_prior, $var->getValue()); - $this->assertEquals('Variable[dummy]', (string)$var); } }