More stringable removal for performance.

This commit is contained in:
Jens True 2024-03-19 15:48:29 +00:00
parent ae5d2a8b73
commit bbff1fbbbc
11 changed files with 34 additions and 61 deletions

@ -72,7 +72,7 @@ abstract class FactorGraphLayer
*/ */
protected function scheduleSequence(array $itemsToSequence, string $name): ScheduleSequence protected function scheduleSequence(array $itemsToSequence, string $name): ScheduleSequence
{ {
return new ScheduleSequence($name, $itemsToSequence); return new ScheduleSequence($itemsToSequence);
} }
protected function addLayerFactor(Factor $factor): void protected function addLayerFactor(Factor $factor): void

@ -4,16 +4,7 @@ declare(strict_types=1);
namespace DNW\Skills\FactorGraphs; namespace DNW\Skills\FactorGraphs;
abstract class Schedule implements \Stringable abstract class Schedule
{ {
protected function __construct(private readonly string $name)
{
}
abstract public function visit(int $depth = -1, int $maxDepth = 0): float; abstract public function visit(int $depth = -1, int $maxDepth = 0): float;
public function __toString(): string
{
return $this->name;
}
} }

@ -6,9 +6,8 @@ namespace DNW\Skills\FactorGraphs;
class ScheduleLoop extends Schedule class ScheduleLoop extends Schedule
{ {
public function __construct(string $name, private readonly Schedule $scheduleToLoop, private readonly float $maxDelta) public function __construct(private readonly Schedule $scheduleToLoop, private readonly float $maxDelta)
{ {
parent::__construct($name);
} }
public function visit(int $depth = -1, int $maxDepth = 0): float public function visit(int $depth = -1, int $maxDepth = 0): float

@ -9,9 +9,8 @@ class ScheduleSequence extends Schedule
/** /**
* @param Schedule[] $schedules * @param Schedule[] $schedules
*/ */
public function __construct(string $name, private readonly array $schedules) public function __construct(private readonly array $schedules)
{ {
parent::__construct($name);
} }
public function visit(int $depth = -1, int $maxDepth = 0): float public function visit(int $depth = -1, int $maxDepth = 0): float

@ -6,15 +6,12 @@ namespace DNW\Skills\FactorGraphs;
class ScheduleStep extends Schedule class ScheduleStep extends Schedule
{ {
public function __construct(string $name, private readonly Factor $factor, private readonly int $index) public function __construct(private readonly Factor $factor, private readonly int $index)
{ {
parent::__construct($name);
} }
public function visit(int $depth = -1, int $maxDepth = 0): float public function visit(int $depth = -1, int $maxDepth = 0): float
{ {
$currentFactor = $this->factor; return $this->factor->updateMessageIndex($this->index);
return $currentFactor->updateMessageIndex($this->index);
} }
} }

@ -63,17 +63,17 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$firstDifferencesFactor = $localFactors[0]; $firstDifferencesFactor = $localFactors[0];
$lastDifferencesFactor = $localFactors[$totalTeamDifferences - 1]; $lastDifferencesFactor = $localFactors[$totalTeamDifferences - 1];
//inner schedule
return new ScheduleSequence( return new ScheduleSequence(
'inner schedule',
[ [
$loop, $loop,
//teamPerformanceToPerformanceDifferenceFactors[0] @ 1
new ScheduleStep( new ScheduleStep(
'teamPerformanceToPerformanceDifferenceFactors[0] @ 1',
$firstDifferencesFactor, $firstDifferencesFactor,
1 1
), ),
//teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2
new ScheduleStep( new ScheduleStep(
sprintf('teamPerformanceToPerformanceDifferenceFactors[teamTeamDifferences = %d - 1] @ 2', $totalTeamDifferences),
$lastDifferencesFactor, $lastDifferencesFactor,
2 2
), ),
@ -89,13 +89,13 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$firstPerfToTeamDiff = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0]; $firstPerfToTeamDiff = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[0];
$firstTeamDiffComparison = $teamDifferencesComparisonLayerLocalFactors[0]; $firstTeamDiffComparison = $teamDifferencesComparisonLayerLocalFactors[0];
$itemsToSequence = [ $itemsToSequence = [
//send team perf to perf differences
new ScheduleStep( new ScheduleStep(
'send team perf to perf differences',
$firstPerfToTeamDiff, $firstPerfToTeamDiff,
0 0
), ),
//send to greater than or within factor
new ScheduleStep( new ScheduleStep(
'send to greater than or within factor',
$firstTeamDiffComparison, $firstTeamDiffComparison,
0 0
), ),
@ -123,18 +123,18 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$currentForwardSchedulePiece = $currentForwardSchedulePiece =
$this->scheduleSequence( $this->scheduleSequence(
[ [
//team perf to perf diff
new ScheduleStep( new ScheduleStep(
sprintf('team perf to perf diff %d', $i),
$currentTeamPerfToTeamPerfDiff, $currentTeamPerfToTeamPerfDiff,
0 0
), ),
//greater than or within result factor
new ScheduleStep( new ScheduleStep(
sprintf('greater than or within result factor %d', $i),
$currentTeamDiffComparison, $currentTeamDiffComparison,
0 0
), ),
//'team perf to perf diff factors
new ScheduleStep( new ScheduleStep(
sprintf('team perf to perf diff factors [%d], 2', $i),
$currentTeamPerfToTeamPerfDiff, $currentTeamPerfToTeamPerfDiff,
2 2
), ),
@ -145,7 +145,8 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$forwardScheduleList[] = $currentForwardSchedulePiece; $forwardScheduleList[] = $currentForwardSchedulePiece;
} }
$forwardSchedule = new ScheduleSequence('forward schedule', $forwardScheduleList); //forward schedule
$forwardSchedule = new ScheduleSequence($forwardScheduleList);
$backwardScheduleList = []; $backwardScheduleList = [];
@ -157,21 +158,21 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$comparisonFactor = $teamDifferencesComparisonLayerLocalFactors[$totalTeamDifferences - 1 - $i]; $comparisonFactor = $teamDifferencesComparisonLayerLocalFactors[$totalTeamDifferences - 1 - $i];
$performancesToDifferencesFactor = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i]; $performancesToDifferencesFactor = $teamPerformancesToTeamPerformanceDifferencesLayerLocalFactors[$totalTeamDifferences - 1 - $i];
//current backward schedule piece
$currentBackwardSchedulePiece = new ScheduleSequence( $currentBackwardSchedulePiece = new ScheduleSequence(
'current backward schedule piece',
[ [
//teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0
new ScheduleStep( new ScheduleStep(
sprintf('teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0', $i),
$differencesFactor, $differencesFactor,
0 0
), ),
//greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0
new ScheduleStep( new ScheduleStep(
sprintf('greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0', $i),
$comparisonFactor, $comparisonFactor,
0 0
), ),
//teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1
new ScheduleStep( new ScheduleStep(
sprintf('teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1', $i),
$performancesToDifferencesFactor, $performancesToDifferencesFactor,
1 1
), ),
@ -180,18 +181,19 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$backwardScheduleList[] = $currentBackwardSchedulePiece; $backwardScheduleList[] = $currentBackwardSchedulePiece;
} }
$backwardSchedule = new ScheduleSequence('backward schedule', $backwardScheduleList); //backward schedule
$backwardSchedule = new ScheduleSequence($backwardScheduleList);
$forwardBackwardScheduleToLoop = $forwardBackwardScheduleToLoop =
//forward Backward Schedule To Loop
new ScheduleSequence( new ScheduleSequence(
'forward Backward Schedule To Loop',
[$forwardSchedule, $backwardSchedule] [$forwardSchedule, $backwardSchedule]
); );
$initialMaxDelta = 0.0001; $initialMaxDelta = 0.0001;
//loop with max delta
return new ScheduleLoop( return new ScheduleLoop(
sprintf('loop with max delta of %f', $initialMaxDelta),
$forwardBackwardScheduleToLoop, $forwardBackwardScheduleToLoop,
$initialMaxDelta $initialMaxDelta
); );

@ -38,7 +38,8 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
return $this->scheduleSequence( return $this->scheduleSequence(
array_map( array_map(
static fn($weightedSumFactor): ScheduleStep => new ScheduleStep('Perf to Team Perf Step', $weightedSumFactor, 0), //Perf to Team Perf Step
static fn($weightedSumFactor): ScheduleStep => new ScheduleStep($weightedSumFactor, 0),
$localFactors $localFactors
), ),
'all player perf to team perf schedule' 'all player perf to team perf schedule'
@ -73,8 +74,8 @@ class PlayerPerformancesToTeamPerformancesLayer extends TrueSkillFactorGraphLaye
$localCurrentFactor = $currentFactor; $localCurrentFactor = $currentFactor;
$numberOfMessages = $localCurrentFactor->getNumberOfMessages(); $numberOfMessages = $localCurrentFactor->getNumberOfMessages();
for ($currentIteration = 1; $currentIteration < $numberOfMessages; ++$currentIteration) { for ($currentIteration = 1; $currentIteration < $numberOfMessages; ++$currentIteration) {
//team sum perf
$allFactors[] = new ScheduleStep( $allFactors[] = new ScheduleStep(
'team sum perf @' . $currentIteration,
$localCurrentFactor, $localCurrentFactor,
$currentIteration $currentIteration
); );

@ -55,7 +55,8 @@ class PlayerPriorValuesToSkillsLayer extends TrueSkillFactorGraphLayer
return $this->scheduleSequence( return $this->scheduleSequence(
array_map( array_map(
static fn($prior): ScheduleStep => new ScheduleStep('Prior to Skill Step', $prior, 0), //Prior to Skill Step
static fn($prior): ScheduleStep => new ScheduleStep($prior, 0),
$localFactors $localFactors
), ),
'All priors' 'All priors'

@ -57,7 +57,8 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
return $this->scheduleSequence( return $this->scheduleSequence(
array_map( array_map(
static fn($likelihood): ScheduleStep => new ScheduleStep('Skill to Perf step', $likelihood, 0), //Skill to Perf step
static fn($likelihood): ScheduleStep => new ScheduleStep($likelihood, 0),
$localFactors $localFactors
), ),
'All skill to performance sending' 'All skill to performance sending'
@ -70,7 +71,7 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
return $this->scheduleSequence( return $this->scheduleSequence(
array_map( array_map(
static fn($likelihood): ScheduleStep => new ScheduleStep('name', $likelihood, 1), static fn($likelihood): ScheduleStep => new ScheduleStep($likelihood, 1),
$localFactors $localFactors
), ),
'All skill to performance sending' 'All skill to performance sending'

@ -124,7 +124,8 @@ class TrueSkillFactorGraph extends FactorGraph
} }
} }
return new ScheduleSequence('Full schedule', $fullSchedule); //Full schedule
return new ScheduleSequence($fullSchedule);
} }
public function getUpdatedRatings(): RatingContainer public function getUpdatedRatings(): RatingContainer

@ -1,19 +0,0 @@
<?php
declare(strict_types=1);
namespace DNW\Skills\Tests\FactorGraphs;
use DNW\Skills\FactorGraphs\ScheduleStep;
use DNW\Skills\FactorGraphs\Factor;
use PHPUnit\Framework\TestCase;
class ScheduleStepTest extends TestCase
{
public function testtoStringInterface(): void
{
$stub = $this->createStub(Factor::class);
$ss = new ScheduleStep('dummy', $stub, 0);
$this->assertEquals('dummy', (string)$ss);
}
}