Skip to content

Commit

Permalink
Unnormalized signal
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Dec 21, 2024
1 parent dab89c2 commit 666660b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 43 deletions.
34 changes: 25 additions & 9 deletions src/SanSignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ class SanSignal extends SanPlay
use SanTrait;

/**
* The signal.
* Unormalized signal.
*
* @var array
*/
public array $signal = [];
public array $unnormalized = [];

/**
* Normalized signal.
*
* @var array
*/
public array $normalized = [];

/**
* Signal components.
*
* @var array
*/
public array $component = [];

/**
* @param \Chess\Function\AbstractFunction $function
Expand All @@ -37,7 +51,7 @@ public function __construct(
parent::__construct($movetext, $board);

$this->result[] = array_fill(0, count($function->names()), 0);
$component = [];
$items = [];

foreach ($this->sanMovetext->moves as $val) {
if ($val !== Move::ELLIPSIS) {
Expand All @@ -48,20 +62,22 @@ public function __construct(
$val,
$this->board
));
$component[] = $item[Color::W] - $item[Color::B];
$items[] = $item[Color::W] - $item[Color::B];
}
$this->result[] = $component;
$component = [];
$this->result[] = $items;
$items = [];
}
}
}

for ($i = 0; $i < count($this->result[0]); $i++) {
$this->balance[$i] = $this->normalize(-1, 1, array_column($this->result, $i));
$this->component[$i] = array_column($this->result, $i);
$this->balance[$i] = $this->normalize(-1, 1, $this->component[$i]);
}

for ($i = 0; $i < count($this->balance[0]); $i++) {
$this->signal[$i] = round(array_sum(array_column($this->balance, $i)), 2);
for ($i = 0; $i < count($this->component[0]); $i++) {
$this->unnormalized[$i] = round(array_sum(array_column($this->component, $i)), 2);
$this->normalized[$i] = round(array_sum(array_column($this->balance, $i)), 2);
}
}
}
2 changes: 1 addition & 1 deletion src/SanTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait SanTrait
public array $result = [];

/**
* The balanced normalized result.
* The balanced result.
*
* @var array
*/
Expand Down
96 changes: 63 additions & 33 deletions tests/unit/SanSignalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public static function setUpBeforeClass(): void
*/
public function e4_d5_exd5_Qxd5()
{
$expectedCenter = [ 0, 1.0, 0.08, 0.67, -1.0 ];
$expectedConnectivity = [ 0, -1.0, -1.0, -1.0, 1.0 ];
$expectedSpace = [ 0, 1.0, 0.25, 0.50, -1.0 ];
$expectedSignal = [ 0.0, 2.0, -1.67, -0.16, -5.0 ];
$expectedCenter = [ 0.0, 1.0, 0.08, 0.67, -1.0 ];
$expectedConnectivity = [ 0.0, -1.0, -1.0, -1.0, 1.0 ];
$expectedSpace = [ 0.0, 1.0, 0.25, 0.50, -1.0 ];
$expectedUnnormalized = [ 0.0, 20.0, 1.0, 10.0, -63.5 ];
$expectedNormalized = [ 0.0, 2.0, -1.67, -0.16, -5.0 ];

$movetext = '1.e4 d5 2.exd5 Qxd5';

Expand All @@ -34,202 +35,231 @@ public function e4_d5_exd5_Qxd5()
$this->assertEquals($expectedCenter, $sanSignal->balance[1]);
$this->assertEquals($expectedConnectivity, $sanSignal->balance[2]);
$this->assertEquals($expectedSpace, $sanSignal->balance[3]);
$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function A59()
{
$expectedSignal = [ 0.0, 2.5, 0.77, 1.01, 1.31, 1.4, 1.69, 3.52, 3.03, 2.77, 0.0, 1.85, 0.17, 0.88, -2.13, 3.42, 2.07, 2.24 ];
$expectedUnnormalized = [ 0.0, 18.0, 2.6, 11.6, 4.6, 5.6, 1.6, 6.6, 4.6, 2.6, -7.4, 3.0, -2.0, 7.0, -4.1, 12.9, 8.9, 9.9 ];
$expectedNormalized = [ 0.0, 2.5, 0.77, 1.01, 1.31, 1.4, 1.69, 3.52, 3.03, 2.77, 0.0, 1.85, 0.17, 0.88, -2.13, 3.42, 2.07, 2.24 ];

$A59 = file_get_contents(self::DATA_FOLDER.'/sample/A59.pgn');

$sanSignal = new SanSignal(self::$function, $A59, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function a3()
{
$expectedSignal = [ 0.0, 2.0 ];
$expectedUnnormalized = [ 0.0, 2.0 ];
$expectedNormalized = [ 0.0, 2.0 ];

$movetext = '1.a3';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function a4()
{
$expectedSignal = [ 0.0, 2.0 ];
$expectedUnnormalized = [ 0.0, 3.0 ];
$expectedNormalized = [ 0.0, 2.0 ];

$movetext = '1.a4';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function b4()
{
$expectedSignal = [ 0.0, 1.0 ];
$expectedUnnormalized = [ 0.0, 5.0 ];
$expectedNormalized = [ 0.0, 1.0 ];

$movetext = '1.b4';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function c4()
{
$expectedSignal = [ 0.0, 1.0 ];
$expectedUnnormalized = [ 0.0, 9.0 ];
$expectedNormalized = [ 0.0, 1.0 ];

$movetext = '1.c4';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function d4()
{
$expectedSignal = [ 0.0, 3.0 ];
$expectedUnnormalized = [ 0.0, 18.0 ];
$expectedNormalized = [ 0.0, 3.0 ];

$movetext = '1.d4';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function e4()
{
$expectedSignal = [ 0.0, 2.0 ];
$expectedUnnormalized = [ 0.0, 20.0 ];
$expectedNormalized = [ 0.0, 2.0 ];

$movetext = '1.e4';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function f4()
{
$expectedSignal = [ 0.0, 2.0 ];
$expectedUnnormalized = [ 0.0, 9.0 ];
$expectedNormalized = [ 0.0, 2.0 ];

$movetext = '1.f4';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function g4()
{
$expectedSignal = [ 0.0, 1.0 ];
$expectedUnnormalized = [ 0.0, 5.0 ];
$expectedNormalized = [ 0.0, 1.0 ];

$movetext = '1.g4';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function h4()
{
$expectedSignal = [ 0.0, 2.0 ];
$expectedUnnormalized = [ 0.0, 3.0 ];
$expectedNormalized = [ 0.0, 2.0 ];

$movetext = '1.h4';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function a4_h5()
{
$expectedSignal = [ 0.0, 2.0, 0.0 ];
$expectedUnnormalized = [ 0.0, 3.0, 0.0 ];
$expectedNormalized = [ 0.0, 2.0, 0.0 ];

$movetext = '1.a4 h5';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function a4_a5()
{
$expectedSignal = [ 0.0, 2.0, 0.0 ];
$expectedUnnormalized = [ 0.0, 3.0, 0.0 ];
$expectedNormalized = [ 0.0, 2.0, 0.0 ];

$movetext = '1.a4 a5';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function a4_e5()
{
$expectedSignal = [ 0.0, 2.0, -2.0 ];
$expectedUnnormalized = [ 0.0, 3.0, -17.0 ];
$expectedNormalized = [ 0.0, 2.0, -2.0 ];

$movetext = '1.a4 e5';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}

/**
* @test
*/
public function h4_e5()
{
$expectedSignal = [ 0.0, 2.0, -3.0 ];
$expectedUnnormalized = [ 0.0, 3.0, -17.0 ];
$expectedNormalized = [ 0.0, 2.0, -3.0 ];

$movetext = '1.h4 e5';

$sanSignal = new SanSignal(self::$function, $movetext, new Board());

$this->assertEquals($expectedSignal, $sanSignal->signal);
$this->assertEquals($expectedUnnormalized, $sanSignal->unnormalized);
$this->assertEquals($expectedNormalized, $sanSignal->normalized);
}
}

0 comments on commit 666660b

Please sign in to comment.