mirror of
https://github.com/furyfire/trueskill.git
synced 2025-04-19 20:34:28 +00:00
String based "name" for Variable class removed for performance
This commit is contained in:
@ -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'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user