mirror of
https://github.com/furyfire/trueskill.git
synced 2025-01-15 17:37:39 +00:00
Even less strings
This commit is contained in:
@ -8,7 +8,7 @@ use DNW\Skills\Guard;
|
|||||||
use DNW\Skills\HashMap;
|
use DNW\Skills\HashMap;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
abstract class Factor implements \Stringable
|
abstract class Factor
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Message[] $messages
|
* @var Message[] $messages
|
||||||
@ -17,16 +17,13 @@ abstract class Factor implements \Stringable
|
|||||||
|
|
||||||
private readonly HashMap $messageToVariableBinding;
|
private readonly HashMap $messageToVariableBinding;
|
||||||
|
|
||||||
private readonly string $name;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Variable[] $variables
|
* @var Variable[] $variables
|
||||||
*/
|
*/
|
||||||
private array $variables = [];
|
private array $variables = [];
|
||||||
|
|
||||||
protected function __construct(string $name)
|
protected function __construct()
|
||||||
{
|
{
|
||||||
$this->name = 'Factor[' . $name . ']';
|
|
||||||
$this->messageToVariableBinding = new HashMap();
|
$this->messageToVariableBinding = new HashMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,9 +118,4 @@ abstract class Factor implements \Stringable
|
|||||||
|
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
|
||||||
{
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,9 @@ namespace DNW\Skills\FactorGraphs;
|
|||||||
|
|
||||||
use DNW\Skills\Numerics\GaussianDistribution;
|
use DNW\Skills\Numerics\GaussianDistribution;
|
||||||
|
|
||||||
class Message implements \Stringable
|
class Message
|
||||||
{
|
{
|
||||||
public function __construct(private GaussianDistribution $value, private readonly string $name)
|
public function __construct(private GaussianDistribution $value)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,9 +21,4 @@ class Message implements \Stringable
|
|||||||
{
|
{
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
|
||||||
{
|
|
||||||
return $this->name;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ abstract class GaussianFactor extends Factor
|
|||||||
return parent::createVariableToMessageBindingWithMessage(
|
return parent::createVariableToMessageBindingWithMessage(
|
||||||
$variable,
|
$variable,
|
||||||
new Message(
|
new Message(
|
||||||
$newDistribution,'message from %s to %s'
|
$newDistribution,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ class GaussianGreaterThanFactor extends GaussianFactor
|
|||||||
{
|
{
|
||||||
public function __construct(private readonly float $epsilon, Variable $variable)
|
public function __construct(private readonly float $epsilon, Variable $variable)
|
||||||
{
|
{
|
||||||
parent::__construct('%s > %.2f');
|
parent::__construct();
|
||||||
$this->createVariableToMessageBinding($variable);
|
$this->createVariableToMessageBinding($variable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,8 @@ class GaussianLikelihoodFactor extends GaussianFactor
|
|||||||
|
|
||||||
public function __construct(float $betaSquared, Variable $variable1, Variable $variable2)
|
public function __construct(float $betaSquared, Variable $variable1, Variable $variable2)
|
||||||
{
|
{
|
||||||
parent::__construct('Likelihood of %s going to %s');
|
//Likelihood of $variable1 going to $variable2
|
||||||
|
parent::__construct();
|
||||||
$this->precision = 1.0 / $betaSquared;
|
$this->precision = 1.0 / $betaSquared;
|
||||||
$this->createVariableToMessageBinding($variable1);
|
$this->createVariableToMessageBinding($variable1);
|
||||||
$this->createVariableToMessageBinding($variable2);
|
$this->createVariableToMessageBinding($variable2);
|
||||||
|
@ -19,11 +19,11 @@ class GaussianPriorFactor extends GaussianFactor
|
|||||||
|
|
||||||
public function __construct(float $mean, float $variance, Variable $variable)
|
public function __construct(float $mean, float $variance, Variable $variable)
|
||||||
{
|
{
|
||||||
parent::__construct('Prior value going to %s');
|
//Prior value going to $variable
|
||||||
|
parent::__construct();
|
||||||
$this->newMessage = new GaussianDistribution($mean, sqrt($variance));
|
$this->newMessage = new GaussianDistribution($mean, sqrt($variance));
|
||||||
$newMessage = new Message(
|
$newMessage = new Message(
|
||||||
GaussianDistribution::fromPrecisionMean(0, 0),
|
GaussianDistribution::fromPrecisionMean(0, 0)
|
||||||
'message from %s to %s'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->createVariableToMessageBindingWithMessage($variable, $newMessage);
|
$this->createVariableToMessageBindingWithMessage($variable, $newMessage);
|
||||||
|
@ -42,7 +42,7 @@ class GaussianWeightedSumFactor extends GaussianFactor
|
|||||||
*/
|
*/
|
||||||
public function __construct(Variable $sumVariable, array $variablesToSum, array $variableWeights)
|
public function __construct(Variable $sumVariable, array $variablesToSum, array $variableWeights)
|
||||||
{
|
{
|
||||||
parent::__construct('$sumVariable, $variablesToSum, $variableWeights');
|
parent::__construct();
|
||||||
|
|
||||||
// The first weights are a straightforward copy
|
// The first weights are a straightforward copy
|
||||||
// v_0 = a_1*v_1 + a_2*v_2 + ... + a_n * v_n
|
// v_0 = a_1*v_1 + a_2*v_2 + ... + a_n * v_n
|
||||||
|
@ -18,7 +18,8 @@ class GaussianWithinFactor extends GaussianFactor
|
|||||||
{
|
{
|
||||||
public function __construct(private readonly float $epsilon, Variable $variable)
|
public function __construct(private readonly float $epsilon, Variable $variable)
|
||||||
{
|
{
|
||||||
parent::__construct('%s <= %.2f');
|
//$epsilon <= $variable
|
||||||
|
parent::__construct();
|
||||||
$this->createVariableToMessageBinding($variable);
|
$this->createVariableToMessageBinding($variable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user