mirror of
https://github.com/furyfire/trueskill.git
synced 2025-01-16 01:47:39 +00:00
No more use of _ to mark private variables.
This commit is contained in:
@ -4,26 +4,26 @@ namespace DNW\Skills\FactorGraphs;
|
||||
|
||||
abstract class FactorGraphLayer
|
||||
{
|
||||
private array $_localFactors = [];
|
||||
private array $localFactors = [];
|
||||
|
||||
private array $_outputVariablesGroups = [];
|
||||
private array $outputVariablesGroups = [];
|
||||
|
||||
private $_inputVariablesGroups = [];
|
||||
private $inputVariablesGroups = [];
|
||||
|
||||
protected function __construct(private readonly FactorGraph $_parentFactorGraph)
|
||||
protected function __construct(private readonly FactorGraph $parentFactorGraph)
|
||||
{
|
||||
}
|
||||
|
||||
protected function getInputVariablesGroups()
|
||||
{
|
||||
return $this->_inputVariablesGroups;
|
||||
return $this->inputVariablesGroups;
|
||||
}
|
||||
|
||||
// HACK
|
||||
|
||||
public function getParentFactorGraph()
|
||||
{
|
||||
return $this->_parentFactorGraph;
|
||||
return $this->parentFactorGraph;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -31,17 +31,17 @@ abstract class FactorGraphLayer
|
||||
*/
|
||||
public function &getOutputVariablesGroups(): array
|
||||
{
|
||||
return $this->_outputVariablesGroups;
|
||||
return $this->outputVariablesGroups;
|
||||
}
|
||||
|
||||
public function getLocalFactors(): array
|
||||
{
|
||||
return $this->_localFactors;
|
||||
return $this->localFactors;
|
||||
}
|
||||
|
||||
public function setInputVariablesGroups(array $value): void
|
||||
{
|
||||
$this->_inputVariablesGroups = $value;
|
||||
$this->inputVariablesGroups = $value;
|
||||
}
|
||||
|
||||
protected function scheduleSequence(array $itemsToSequence, string $name): ScheduleSequence
|
||||
@ -51,7 +51,7 @@ abstract class FactorGraphLayer
|
||||
|
||||
protected function addLayerFactor(Factor $factor): void
|
||||
{
|
||||
$this->_localFactors[] = $factor;
|
||||
$this->localFactors[] = $factor;
|
||||
}
|
||||
|
||||
abstract public function buildLayer();
|
||||
|
@ -7,21 +7,21 @@ namespace DNW\Skills\FactorGraphs;
|
||||
*/
|
||||
class FactorList
|
||||
{
|
||||
private array $_list = [];
|
||||
private array $list = [];
|
||||
|
||||
public function getLogNormalization()
|
||||
{
|
||||
$list = $this->_list;
|
||||
$list = $this->list;
|
||||
foreach ($list as &$currentFactor) {
|
||||
$currentFactor->resetMarginals();
|
||||
}
|
||||
|
||||
$sumLogZ = 0.0;
|
||||
|
||||
$listCount = count($this->_list);
|
||||
$listCount = count($this->list);
|
||||
|
||||
for ($i = 0; $i < $listCount; $i++) {
|
||||
$f = $this->_list[$i];
|
||||
$f = $this->list[$i];
|
||||
|
||||
$numberOfMessages = $f->getNumberOfMessages();
|
||||
|
||||
@ -41,12 +41,12 @@ class FactorList
|
||||
|
||||
public function count()
|
||||
{
|
||||
return count($this->_list);
|
||||
return count($this->list);
|
||||
}
|
||||
|
||||
public function addFactor(Factor $factor)
|
||||
{
|
||||
$this->_list[] = $factor;
|
||||
$this->list[] = $factor;
|
||||
|
||||
return $factor;
|
||||
}
|
||||
|
@ -12,65 +12,65 @@ class GaussianDistribution implements \Stringable
|
||||
{
|
||||
// precision and precisionMean are used because they make multiplying and dividing simpler
|
||||
// (the the accompanying math paper for more details)
|
||||
private $_precision;
|
||||
private $precision;
|
||||
|
||||
private $_precisionMean;
|
||||
private $precisionMean;
|
||||
|
||||
private $_variance;
|
||||
private $variance;
|
||||
|
||||
public function __construct(private float $_mean = 0.0, private float $_standardDeviation = 1.0)
|
||||
public function __construct(private float $mean = 0.0, private float $standardDeviation = 1.0)
|
||||
{
|
||||
$this->_variance = BasicMath::square($_standardDeviation);
|
||||
$this->variance = BasicMath::square($standardDeviation);
|
||||
|
||||
if ($this->_variance != 0) {
|
||||
$this->_precision = 1.0 / $this->_variance;
|
||||
$this->_precisionMean = $this->_precision * $this->_mean;
|
||||
if ($this->variance != 0) {
|
||||
$this->precision = 1.0 / $this->variance;
|
||||
$this->precisionMean = $this->precision * $this->mean;
|
||||
} else {
|
||||
$this->_precision = \INF;
|
||||
$this->precision = \INF;
|
||||
|
||||
$this->_precisionMean = $this->_mean == 0 ? 0 : \INF;
|
||||
$this->precisionMean = $this->mean == 0 ? 0 : \INF;
|
||||
}
|
||||
}
|
||||
|
||||
public function getMean(): float
|
||||
{
|
||||
return $this->_mean;
|
||||
return $this->mean;
|
||||
}
|
||||
|
||||
public function getVariance(): float
|
||||
{
|
||||
return $this->_variance;
|
||||
return $this->variance;
|
||||
}
|
||||
|
||||
public function getStandardDeviation(): float
|
||||
{
|
||||
return $this->_standardDeviation;
|
||||
return $this->standardDeviation;
|
||||
}
|
||||
|
||||
public function getPrecision(): float
|
||||
{
|
||||
return $this->_precision;
|
||||
return $this->precision;
|
||||
}
|
||||
|
||||
public function getPrecisionMean(): float
|
||||
{
|
||||
return $this->_precisionMean;
|
||||
return $this->precisionMean;
|
||||
}
|
||||
|
||||
public function getNormalizationConstant(): float
|
||||
{
|
||||
// Great derivation of this is at http://www.astro.psu.edu/~mce/A451_2/A451/downloads/notes0.pdf
|
||||
return 1.0 / (sqrt(2 * M_PI) * $this->_standardDeviation);
|
||||
return 1.0 / (sqrt(2 * M_PI) * $this->standardDeviation);
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$result = new GaussianDistribution();
|
||||
$result->_mean = $this->_mean;
|
||||
$result->_standardDeviation = $this->_standardDeviation;
|
||||
$result->_variance = $this->_variance;
|
||||
$result->_precision = $this->_precision;
|
||||
$result->_precisionMean = $this->_precisionMean;
|
||||
$result->mean = $this->mean;
|
||||
$result->standardDeviation = $this->standardDeviation;
|
||||
$result->variance = $this->variance;
|
||||
$result->precision = $this->precision;
|
||||
$result->precisionMean = $this->precisionMean;
|
||||
|
||||
return $result;
|
||||
}
|
||||
@ -78,17 +78,17 @@ class GaussianDistribution implements \Stringable
|
||||
public static function fromPrecisionMean(float $precisionMean, float $precision): self
|
||||
{
|
||||
$result = new GaussianDistribution();
|
||||
$result->_precision = $precision;
|
||||
$result->_precisionMean = $precisionMean;
|
||||
$result->precision = $precision;
|
||||
$result->precisionMean = $precisionMean;
|
||||
|
||||
if ($precision != 0) {
|
||||
$result->_variance = 1.0 / $precision;
|
||||
$result->_standardDeviation = sqrt($result->_variance);
|
||||
$result->_mean = $result->_precisionMean / $result->_precision;
|
||||
$result->variance = 1.0 / $precision;
|
||||
$result->standardDeviation = sqrt($result->variance);
|
||||
$result->mean = $result->precisionMean / $result->precision;
|
||||
} else {
|
||||
$result->_variance = \INF;
|
||||
$result->_standardDeviation = \INF;
|
||||
$result->_mean = \NAN;
|
||||
$result->variance = \INF;
|
||||
$result->standardDeviation = \INF;
|
||||
$result->mean = \NAN;
|
||||
}
|
||||
|
||||
return $result;
|
||||
@ -98,15 +98,15 @@ class GaussianDistribution implements \Stringable
|
||||
// for multiplication, the precision mean ones are easier to write :)
|
||||
public static function multiply(GaussianDistribution $left, GaussianDistribution $right): self
|
||||
{
|
||||
return GaussianDistribution::fromPrecisionMean($left->_precisionMean + $right->_precisionMean, $left->_precision + $right->_precision);
|
||||
return GaussianDistribution::fromPrecisionMean($left->precisionMean + $right->precisionMean, $left->precision + $right->precision);
|
||||
}
|
||||
|
||||
// Computes the absolute difference between two Gaussians
|
||||
public static function absoluteDifference(GaussianDistribution $left, GaussianDistribution $right): float
|
||||
{
|
||||
return max(
|
||||
abs($left->_precisionMean - $right->_precisionMean),
|
||||
sqrt(abs($left->_precision - $right->_precision))
|
||||
abs($left->precisionMean - $right->precisionMean),
|
||||
sqrt(abs($left->precision - $right->precision))
|
||||
);
|
||||
}
|
||||
|
||||
@ -118,12 +118,12 @@ class GaussianDistribution implements \Stringable
|
||||
|
||||
public static function logProductNormalization(GaussianDistribution $left, GaussianDistribution $right): float
|
||||
{
|
||||
if (($left->_precision == 0) || ($right->_precision == 0)) {
|
||||
if (($left->precision == 0) || ($right->precision == 0)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$varianceSum = $left->_variance + $right->_variance;
|
||||
$meanDifference = $left->_mean - $right->_mean;
|
||||
$varianceSum = $left->variance + $right->variance;
|
||||
$meanDifference = $left->mean - $right->mean;
|
||||
|
||||
$logSqrt2Pi = log(sqrt(2 * M_PI));
|
||||
|
||||
@ -133,23 +133,23 @@ class GaussianDistribution implements \Stringable
|
||||
public static function divide(GaussianDistribution $numerator, GaussianDistribution $denominator): self
|
||||
{
|
||||
return GaussianDistribution::fromPrecisionMean(
|
||||
$numerator->_precisionMean - $denominator->_precisionMean,
|
||||
$numerator->_precision - $denominator->_precision
|
||||
$numerator->precisionMean - $denominator->precisionMean,
|
||||
$numerator->precision - $denominator->precision
|
||||
);
|
||||
}
|
||||
|
||||
public static function logRatioNormalization(GaussianDistribution $numerator, GaussianDistribution $denominator): float
|
||||
{
|
||||
if (($numerator->_precision == 0) || ($denominator->_precision == 0)) {
|
||||
if (($numerator->precision == 0) || ($denominator->precision == 0)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$varianceDifference = $denominator->_variance - $numerator->_variance;
|
||||
$meanDifference = $numerator->_mean - $denominator->_mean;
|
||||
$varianceDifference = $denominator->variance - $numerator->variance;
|
||||
$meanDifference = $numerator->mean - $denominator->mean;
|
||||
|
||||
$logSqrt2Pi = log(sqrt(2 * M_PI));
|
||||
|
||||
return log($denominator->_variance) + $logSqrt2Pi - log($varianceDifference) / 2.0 +
|
||||
return log($denominator->variance) + $logSqrt2Pi - log($varianceDifference) / 2.0 +
|
||||
BasicMath::square($meanDifference) / (2 * $varianceDifference);
|
||||
}
|
||||
|
||||
@ -258,6 +258,6 @@ class GaussianDistribution implements \Stringable
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('mean=%.4f standardDeviation=%.4f', $this->_mean, $this->_standardDeviation);
|
||||
return sprintf('mean=%.4f standardDeviation=%.4f', $this->mean, $this->standardDeviation);
|
||||
}
|
||||
}
|
||||
|
@ -9,28 +9,21 @@ use Exception;
|
||||
|
||||
class Range
|
||||
{
|
||||
private int $_min;
|
||||
|
||||
private int $_max;
|
||||
|
||||
public function __construct(int $min, int $max)
|
||||
public function __construct(private int $min, private int $max)
|
||||
{
|
||||
if ($min > $max) {
|
||||
throw new Exception('min > max');
|
||||
}
|
||||
|
||||
$this->_min = $min;
|
||||
$this->_max = $max;
|
||||
}
|
||||
|
||||
public function getMin(): int
|
||||
{
|
||||
return $this->_min;
|
||||
return $this->min;
|
||||
}
|
||||
|
||||
public function getMax(): int
|
||||
{
|
||||
return $this->_max;
|
||||
return $this->max;
|
||||
}
|
||||
|
||||
protected static function create(int $min, int $max): self
|
||||
@ -57,6 +50,6 @@ class Range
|
||||
|
||||
public function isInRange(int $value): bool
|
||||
{
|
||||
return ($this->_min <= $value) && ($value <= $this->_max);
|
||||
return ($this->min <= $value) && ($value <= $this->max);
|
||||
}
|
||||
}
|
||||
|
@ -7,31 +7,31 @@ namespace DNW\Skills;
|
||||
*/
|
||||
class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
|
||||
{
|
||||
final const DEFAULT_PARTIAL_PLAY_PERCENTAGE = 1.0; // = 100% play time
|
||||
private const DEFAULT_PARTIAL_PLAY_PERCENTAGE = 1.0; // = 100% play time
|
||||
|
||||
final const DEFAULT_PARTIAL_UPDATE_PERCENTAGE = 1.0;
|
||||
private const DEFAULT_PARTIAL_UPDATE_PERCENTAGE = 1.0;
|
||||
|
||||
private $_PartialPlayPercentage;
|
||||
private $PartialPlayPercentage;
|
||||
|
||||
private $_PartialUpdatePercentage;
|
||||
private $PartialUpdatePercentage;
|
||||
|
||||
/**
|
||||
* Constructs a player.
|
||||
*
|
||||
* @param mixed $_Id The identifier for the player, such as a name.
|
||||
* @param mixed $Id The identifier for the player, such as a name.
|
||||
* @param number $partialPlayPercentage The weight percentage to give this player when calculating a new rank.
|
||||
* @param number $partialUpdatePercentage Indicated how much of a skill update a player should receive where 0 represents no update and 1.0 represents 100% of the update.
|
||||
*/
|
||||
public function __construct(
|
||||
private $_Id,
|
||||
private $Id,
|
||||
$partialPlayPercentage = self::DEFAULT_PARTIAL_PLAY_PERCENTAGE,
|
||||
$partialUpdatePercentage = self::DEFAULT_PARTIAL_UPDATE_PERCENTAGE
|
||||
) {
|
||||
// If they don't want to give a player an id, that's ok...
|
||||
Guard::argumentInRangeInclusive($partialPlayPercentage, 0.0, 1.0, 'partialPlayPercentage');
|
||||
Guard::argumentInRangeInclusive($partialUpdatePercentage, 0, 1.0, 'partialUpdatePercentage');
|
||||
$this->_PartialPlayPercentage = $partialPlayPercentage;
|
||||
$this->_PartialUpdatePercentage = $partialUpdatePercentage;
|
||||
$this->PartialPlayPercentage = $partialPlayPercentage;
|
||||
$this->PartialUpdatePercentage = $partialUpdatePercentage;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,7 +39,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->_Id;
|
||||
return $this->Id;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -47,7 +47,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
|
||||
*/
|
||||
public function getPartialPlayPercentage()
|
||||
{
|
||||
return $this->_PartialPlayPercentage;
|
||||
return $this->PartialPlayPercentage;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,11 +55,11 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
|
||||
*/
|
||||
public function getPartialUpdatePercentage()
|
||||
{
|
||||
return $this->_PartialUpdatePercentage;
|
||||
return $this->PartialUpdatePercentage;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return (string) $this->_Id;
|
||||
return (string) $this->Id;
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use DNW\Skills\Numerics\GaussianDistribution;
|
||||
|
||||
class Rating implements \Stringable
|
||||
{
|
||||
final const CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER = 3;
|
||||
private const CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER = 3;
|
||||
|
||||
/**
|
||||
* Constructs a rating.
|
||||
|
@ -4,35 +4,35 @@ namespace DNW\Skills;
|
||||
|
||||
class RatingContainer
|
||||
{
|
||||
private HashMap $_playerToRating;
|
||||
private HashMap $playerToRating;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->_playerToRating = new HashMap();
|
||||
$this->playerToRating = new HashMap();
|
||||
}
|
||||
|
||||
public function getRating(Player $player): mixed
|
||||
{
|
||||
return $this->_playerToRating->getValue($player);
|
||||
return $this->playerToRating->getValue($player);
|
||||
}
|
||||
|
||||
public function setRating(Player $player, Rating $rating): HashMap
|
||||
{
|
||||
return $this->_playerToRating->setValue($player, $rating);
|
||||
return $this->playerToRating->setValue($player, $rating);
|
||||
}
|
||||
|
||||
public function getAllPlayers(): array
|
||||
{
|
||||
return $this->_playerToRating->getAllKeys();
|
||||
return $this->playerToRating->getAllKeys();
|
||||
}
|
||||
|
||||
public function getAllRatings(): array
|
||||
{
|
||||
return $this->_playerToRating->getAllValues();
|
||||
return $this->playerToRating->getAllValues();
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return $this->_playerToRating->count();
|
||||
return $this->playerToRating->count();
|
||||
}
|
||||
}
|
||||
|
@ -13,12 +13,12 @@ use DNW\Skills\Numerics\GaussianDistribution;
|
||||
*/
|
||||
class GaussianPriorFactor extends GaussianFactor
|
||||
{
|
||||
private $_newMessage;
|
||||
private $newMessage;
|
||||
|
||||
public function __construct(float $mean, float $variance, Variable $variable)
|
||||
{
|
||||
parent::__construct(sprintf('Prior value going to %s', $variable));
|
||||
$this->_newMessage = new GaussianDistribution($mean, sqrt($variance));
|
||||
$this->newMessage = new GaussianDistribution($mean, sqrt($variance));
|
||||
$newMessage = new Message(
|
||||
GaussianDistribution::fromPrecisionMean(0, 0),
|
||||
sprintf('message from %s to %s', $this, $variable)
|
||||
@ -32,12 +32,12 @@ class GaussianPriorFactor extends GaussianFactor
|
||||
$oldMarginal = clone $variable->getValue();
|
||||
$oldMessage = $message;
|
||||
$newMarginal = GaussianDistribution::fromPrecisionMean(
|
||||
$oldMarginal->getPrecisionMean() + $this->_newMessage->getPrecisionMean() - $oldMessage->getValue()->getPrecisionMean(),
|
||||
$oldMarginal->getPrecision() + $this->_newMessage->getPrecision() - $oldMessage->getValue()->getPrecision()
|
||||
$oldMarginal->getPrecisionMean() + $this->newMessage->getPrecisionMean() - $oldMessage->getValue()->getPrecisionMean(),
|
||||
$oldMarginal->getPrecision() + $this->newMessage->getPrecision() - $oldMessage->getValue()->getPrecision()
|
||||
);
|
||||
|
||||
$variable->setValue($newMarginal);
|
||||
$message->setValue($this->_newMessage);
|
||||
$message->setValue($this->newMessage);
|
||||
|
||||
return GaussianDistribution::subtract($oldMarginal, $newMarginal);
|
||||
}
|
||||
|
Reference in New Issue
Block a user