No more use of _ to mark private variables.

This commit is contained in:
Jens True 2023-08-01 14:02:12 +00:00
parent c8c126962d
commit f0aa9413e1
8 changed files with 87 additions and 94 deletions

@ -4,26 +4,26 @@ namespace DNW\Skills\FactorGraphs;
abstract class FactorGraphLayer 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() protected function getInputVariablesGroups()
{ {
return $this->_inputVariablesGroups; return $this->inputVariablesGroups;
} }
// HACK // HACK
public function getParentFactorGraph() public function getParentFactorGraph()
{ {
return $this->_parentFactorGraph; return $this->parentFactorGraph;
} }
/** /**
@ -31,17 +31,17 @@ abstract class FactorGraphLayer
*/ */
public function &getOutputVariablesGroups(): array public function &getOutputVariablesGroups(): array
{ {
return $this->_outputVariablesGroups; return $this->outputVariablesGroups;
} }
public function getLocalFactors(): array public function getLocalFactors(): array
{ {
return $this->_localFactors; return $this->localFactors;
} }
public function setInputVariablesGroups(array $value): void public function setInputVariablesGroups(array $value): void
{ {
$this->_inputVariablesGroups = $value; $this->inputVariablesGroups = $value;
} }
protected function scheduleSequence(array $itemsToSequence, string $name): ScheduleSequence protected function scheduleSequence(array $itemsToSequence, string $name): ScheduleSequence
@ -51,7 +51,7 @@ abstract class FactorGraphLayer
protected function addLayerFactor(Factor $factor): void protected function addLayerFactor(Factor $factor): void
{ {
$this->_localFactors[] = $factor; $this->localFactors[] = $factor;
} }
abstract public function buildLayer(); abstract public function buildLayer();

@ -7,21 +7,21 @@ namespace DNW\Skills\FactorGraphs;
*/ */
class FactorList class FactorList
{ {
private array $_list = []; private array $list = [];
public function getLogNormalization() public function getLogNormalization()
{ {
$list = $this->_list; $list = $this->list;
foreach ($list as &$currentFactor) { foreach ($list as &$currentFactor) {
$currentFactor->resetMarginals(); $currentFactor->resetMarginals();
} }
$sumLogZ = 0.0; $sumLogZ = 0.0;
$listCount = count($this->_list); $listCount = count($this->list);
for ($i = 0; $i < $listCount; $i++) { for ($i = 0; $i < $listCount; $i++) {
$f = $this->_list[$i]; $f = $this->list[$i];
$numberOfMessages = $f->getNumberOfMessages(); $numberOfMessages = $f->getNumberOfMessages();
@ -41,12 +41,12 @@ class FactorList
public function count() public function count()
{ {
return count($this->_list); return count($this->list);
} }
public function addFactor(Factor $factor) public function addFactor(Factor $factor)
{ {
$this->_list[] = $factor; $this->list[] = $factor;
return $factor; return $factor;
} }

@ -12,65 +12,65 @@ class GaussianDistribution implements \Stringable
{ {
// precision and precisionMean are used because they make multiplying and dividing simpler // precision and precisionMean are used because they make multiplying and dividing simpler
// (the the accompanying math paper for more details) // (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) { if ($this->variance != 0) {
$this->_precision = 1.0 / $this->_variance; $this->precision = 1.0 / $this->variance;
$this->_precisionMean = $this->_precision * $this->_mean; $this->precisionMean = $this->precision * $this->mean;
} else { } 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 public function getMean(): float
{ {
return $this->_mean; return $this->mean;
} }
public function getVariance(): float public function getVariance(): float
{ {
return $this->_variance; return $this->variance;
} }
public function getStandardDeviation(): float public function getStandardDeviation(): float
{ {
return $this->_standardDeviation; return $this->standardDeviation;
} }
public function getPrecision(): float public function getPrecision(): float
{ {
return $this->_precision; return $this->precision;
} }
public function getPrecisionMean(): float public function getPrecisionMean(): float
{ {
return $this->_precisionMean; return $this->precisionMean;
} }
public function getNormalizationConstant(): float public function getNormalizationConstant(): float
{ {
// Great derivation of this is at http://www.astro.psu.edu/~mce/A451_2/A451/downloads/notes0.pdf // 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() public function __clone()
{ {
$result = new GaussianDistribution(); $result = new GaussianDistribution();
$result->_mean = $this->_mean; $result->mean = $this->mean;
$result->_standardDeviation = $this->_standardDeviation; $result->standardDeviation = $this->standardDeviation;
$result->_variance = $this->_variance; $result->variance = $this->variance;
$result->_precision = $this->_precision; $result->precision = $this->precision;
$result->_precisionMean = $this->_precisionMean; $result->precisionMean = $this->precisionMean;
return $result; return $result;
} }
@ -78,17 +78,17 @@ class GaussianDistribution implements \Stringable
public static function fromPrecisionMean(float $precisionMean, float $precision): self public static function fromPrecisionMean(float $precisionMean, float $precision): self
{ {
$result = new GaussianDistribution(); $result = new GaussianDistribution();
$result->_precision = $precision; $result->precision = $precision;
$result->_precisionMean = $precisionMean; $result->precisionMean = $precisionMean;
if ($precision != 0) { if ($precision != 0) {
$result->_variance = 1.0 / $precision; $result->variance = 1.0 / $precision;
$result->_standardDeviation = sqrt($result->_variance); $result->standardDeviation = sqrt($result->variance);
$result->_mean = $result->_precisionMean / $result->_precision; $result->mean = $result->precisionMean / $result->precision;
} else { } else {
$result->_variance = \INF; $result->variance = \INF;
$result->_standardDeviation = \INF; $result->standardDeviation = \INF;
$result->_mean = \NAN; $result->mean = \NAN;
} }
return $result; return $result;
@ -98,15 +98,15 @@ class GaussianDistribution implements \Stringable
// for multiplication, the precision mean ones are easier to write :) // for multiplication, the precision mean ones are easier to write :)
public static function multiply(GaussianDistribution $left, GaussianDistribution $right): self 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 // Computes the absolute difference between two Gaussians
public static function absoluteDifference(GaussianDistribution $left, GaussianDistribution $right): float public static function absoluteDifference(GaussianDistribution $left, GaussianDistribution $right): float
{ {
return max( return max(
abs($left->_precisionMean - $right->_precisionMean), abs($left->precisionMean - $right->precisionMean),
sqrt(abs($left->_precision - $right->_precision)) sqrt(abs($left->precision - $right->precision))
); );
} }
@ -118,12 +118,12 @@ class GaussianDistribution implements \Stringable
public static function logProductNormalization(GaussianDistribution $left, GaussianDistribution $right): float 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; return 0;
} }
$varianceSum = $left->_variance + $right->_variance; $varianceSum = $left->variance + $right->variance;
$meanDifference = $left->_mean - $right->_mean; $meanDifference = $left->mean - $right->mean;
$logSqrt2Pi = log(sqrt(2 * M_PI)); $logSqrt2Pi = log(sqrt(2 * M_PI));
@ -133,23 +133,23 @@ class GaussianDistribution implements \Stringable
public static function divide(GaussianDistribution $numerator, GaussianDistribution $denominator): self public static function divide(GaussianDistribution $numerator, GaussianDistribution $denominator): self
{ {
return GaussianDistribution::fromPrecisionMean( return GaussianDistribution::fromPrecisionMean(
$numerator->_precisionMean - $denominator->_precisionMean, $numerator->precisionMean - $denominator->precisionMean,
$numerator->_precision - $denominator->_precision $numerator->precision - $denominator->precision
); );
} }
public static function logRatioNormalization(GaussianDistribution $numerator, GaussianDistribution $denominator): float 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; return 0;
} }
$varianceDifference = $denominator->_variance - $numerator->_variance; $varianceDifference = $denominator->variance - $numerator->variance;
$meanDifference = $numerator->_mean - $denominator->_mean; $meanDifference = $numerator->mean - $denominator->mean;
$logSqrt2Pi = log(sqrt(2 * M_PI)); $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); BasicMath::square($meanDifference) / (2 * $varianceDifference);
} }
@ -258,6 +258,6 @@ class GaussianDistribution implements \Stringable
public function __toString(): string 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 class Range
{ {
private int $_min; public function __construct(private int $min, private int $max)
private int $_max;
public function __construct(int $min, int $max)
{ {
if ($min > $max) { if ($min > $max) {
throw new Exception('min > max'); throw new Exception('min > max');
} }
$this->_min = $min;
$this->_max = $max;
} }
public function getMin(): int public function getMin(): int
{ {
return $this->_min; return $this->min;
} }
public function getMax(): int public function getMax(): int
{ {
return $this->_max; return $this->max;
} }
protected static function create(int $min, int $max): self protected static function create(int $min, int $max): self
@ -57,6 +50,6 @@ class Range
public function isInRange(int $value): bool 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 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. * 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 $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. * @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( public function __construct(
private $_Id, private $Id,
$partialPlayPercentage = self::DEFAULT_PARTIAL_PLAY_PERCENTAGE, $partialPlayPercentage = self::DEFAULT_PARTIAL_PLAY_PERCENTAGE,
$partialUpdatePercentage = self::DEFAULT_PARTIAL_UPDATE_PERCENTAGE $partialUpdatePercentage = self::DEFAULT_PARTIAL_UPDATE_PERCENTAGE
) { ) {
// If they don't want to give a player an id, that's ok... // If they don't want to give a player an id, that's ok...
Guard::argumentInRangeInclusive($partialPlayPercentage, 0.0, 1.0, 'partialPlayPercentage'); Guard::argumentInRangeInclusive($partialPlayPercentage, 0.0, 1.0, 'partialPlayPercentage');
Guard::argumentInRangeInclusive($partialUpdatePercentage, 0, 1.0, 'partialUpdatePercentage'); Guard::argumentInRangeInclusive($partialUpdatePercentage, 0, 1.0, 'partialUpdatePercentage');
$this->_PartialPlayPercentage = $partialPlayPercentage; $this->PartialPlayPercentage = $partialPlayPercentage;
$this->_PartialUpdatePercentage = $partialUpdatePercentage; $this->PartialUpdatePercentage = $partialUpdatePercentage;
} }
/** /**
@ -39,7 +39,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
*/ */
public function getId() public function getId()
{ {
return $this->_Id; return $this->Id;
} }
/** /**
@ -47,7 +47,7 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
*/ */
public function getPartialPlayPercentage() public function getPartialPlayPercentage()
{ {
return $this->_PartialPlayPercentage; return $this->PartialPlayPercentage;
} }
/** /**
@ -55,11 +55,11 @@ class Player implements ISupportPartialPlay, ISupportPartialUpdate, \Stringable
*/ */
public function getPartialUpdatePercentage() public function getPartialUpdatePercentage()
{ {
return $this->_PartialUpdatePercentage; return $this->PartialUpdatePercentage;
} }
public function __toString(): string 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 class Rating implements \Stringable
{ {
final const CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER = 3; private const CONSERVATIVE_STANDARD_DEVIATION_MULTIPLIER = 3;
/** /**
* Constructs a rating. * Constructs a rating.

@ -4,35 +4,35 @@ namespace DNW\Skills;
class RatingContainer class RatingContainer
{ {
private HashMap $_playerToRating; private HashMap $playerToRating;
public function __construct() public function __construct()
{ {
$this->_playerToRating = new HashMap(); $this->playerToRating = new HashMap();
} }
public function getRating(Player $player): mixed public function getRating(Player $player): mixed
{ {
return $this->_playerToRating->getValue($player); return $this->playerToRating->getValue($player);
} }
public function setRating(Player $player, Rating $rating): HashMap public function setRating(Player $player, Rating $rating): HashMap
{ {
return $this->_playerToRating->setValue($player, $rating); return $this->playerToRating->setValue($player, $rating);
} }
public function getAllPlayers(): array public function getAllPlayers(): array
{ {
return $this->_playerToRating->getAllKeys(); return $this->playerToRating->getAllKeys();
} }
public function getAllRatings(): array public function getAllRatings(): array
{ {
return $this->_playerToRating->getAllValues(); return $this->playerToRating->getAllValues();
} }
public function count(): int 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 class GaussianPriorFactor extends GaussianFactor
{ {
private $_newMessage; private $newMessage;
public function __construct(float $mean, float $variance, Variable $variable) public function __construct(float $mean, float $variance, Variable $variable)
{ {
parent::__construct(sprintf('Prior value going to %s', $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( $newMessage = new Message(
GaussianDistribution::fromPrecisionMean(0, 0), GaussianDistribution::fromPrecisionMean(0, 0),
sprintf('message from %s to %s', $this, $variable) sprintf('message from %s to %s', $this, $variable)
@ -32,12 +32,12 @@ class GaussianPriorFactor extends GaussianFactor
$oldMarginal = clone $variable->getValue(); $oldMarginal = clone $variable->getValue();
$oldMessage = $message; $oldMessage = $message;
$newMarginal = GaussianDistribution::fromPrecisionMean( $newMarginal = GaussianDistribution::fromPrecisionMean(
$oldMarginal->getPrecisionMean() + $this->_newMessage->getPrecisionMean() - $oldMessage->getValue()->getPrecisionMean(), $oldMarginal->getPrecisionMean() + $this->newMessage->getPrecisionMean() - $oldMessage->getValue()->getPrecisionMean(),
$oldMarginal->getPrecision() + $this->_newMessage->getPrecision() - $oldMessage->getValue()->getPrecision() $oldMarginal->getPrecision() + $this->newMessage->getPrecision() - $oldMessage->getValue()->getPrecision()
); );
$variable->setValue($newMarginal); $variable->setValue($newMarginal);
$message->setValue($this->_newMessage); $message->setValue($this->newMessage);
return GaussianDistribution::subtract($oldMarginal, $newMarginal); return GaussianDistribution::subtract($oldMarginal, $newMarginal);
} }