Skip to content

Commit

Permalink
Issue/704 the equilibrium factor phi (#709)
Browse files Browse the repository at this point in the history
* Updated Chess\Eval\CenterEval

* Updated tests
  • Loading branch information
programarivm authored Dec 22, 2024
1 parent e7b2594 commit 9550689
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 32 deletions.
22 changes: 16 additions & 6 deletions src/Eval/CenterEval.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class CenterEval extends AbstractEval
*/
const NAME = 'Center';

/**
* The equilibrium factor φ.
*
* The king's side is slightly tilted to differentiate between moves made on
* the queen side and those made on the king side.
*
* @var string
*/
const PHI = 0.1;

/**
* Integer values are assigned to squares based on their proximity to the
* center. The closer a square is to the center, the higher its value.
Expand All @@ -30,12 +40,12 @@ class CenterEval extends AbstractEval
*/
private array $center = [
'a8' => 0, 'b8' => 0, 'c8' => 0, 'd8' => 0, 'e8' => 0, 'f8' => 0, 'g8' => 0, 'h8' => 0,
'a7' => 0, 'b7' => 1, 'c7' => 1, 'd7' => 1, 'e7' => 1, 'f7' => 1, 'g7' => 1, 'h7' => 0,
'a6' => 0, 'b6' => 1, 'c6' => 2, 'd6' => 2, 'e6' => 2, 'f6' => 2, 'g6' => 1, 'h6' => 0,
'a5' => 0, 'b5' => 1, 'c5' => 2, 'd5' => 3, 'e5' => 3, 'f5' => 2, 'g5' => 1, 'h5' => 0,
'a4' => 0, 'b4' => 1, 'c4' => 2, 'd4' => 3, 'e4' => 3, 'f4' => 2, 'g4' => 1, 'h4' => 0,
'a3' => 0, 'b3' => 1, 'c3' => 2, 'd3' => 2, 'e3' => 2, 'f3' => 2, 'g3' => 1, 'h3' => 0,
'a2' => 0, 'b2' => 1, 'c2' => 1, 'd2' => 1, 'e2' => 1, 'f2' => 1, 'g2' => 1, 'h2' => 0,
'a7' => 0, 'b7' => 1, 'c7' => 1, 'd7' => 1, 'e7' => 1 + self::PHI, 'f7' => 1 + self::PHI, 'g7' => 1 + self::PHI, 'h7' => 0,
'a6' => 0, 'b6' => 1, 'c6' => 2, 'd6' => 2, 'e6' => 2 + self::PHI, 'f6' => 2 + self::PHI, 'g6' => 1 + self::PHI, 'h6' => 0,
'a5' => 0, 'b5' => 1, 'c5' => 2, 'd5' => 3, 'e5' => 3 + self::PHI, 'f5' => 2 + self::PHI, 'g5' => 1 + self::PHI, 'h5' => 0,
'a4' => 0, 'b4' => 1, 'c4' => 2, 'd4' => 3, 'e4' => 3 + self::PHI, 'f4' => 2 + self::PHI, 'g4' => 1 + self::PHI, 'h4' => 0,
'a3' => 0, 'b3' => 1, 'c3' => 2, 'd3' => 2, 'e3' => 2 + self::PHI, 'f3' => 2 + self::PHI, 'g3' => 1 + self::PHI, 'h3' => 0,
'a2' => 0, 'b2' => 1, 'c2' => 1, 'd2' => 1, 'e2' => 1 + self::PHI, 'f2' => 1 + self::PHI, 'g2' => 1 + self::PHI, 'h2' => 0,
'a1' => 0, 'b1' => 0, 'c1' => 0, 'd1' => 0, 'e1' => 0, 'f1' => 0, 'g1' => 0, 'h1' => 0,
];

Expand Down
105 changes: 97 additions & 8 deletions tests/unit/Eval/CenterEvalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Chess\Tests\Unit\Eval;

use Chess\FenToBoardFactory;
use Chess\Eval\CenterEval;
use Chess\Play\SanPlay;
use Chess\Tests\AbstractUnitTestCase;
Expand All @@ -15,8 +16,8 @@ class CenterEvalTest extends AbstractUnitTestCase
public function A08()
{
$expectedResult = [
'w' => 29.4,
'b' => 33.0,
'w' => 30.52,
'b' => 33.9,
];

$expectedExplanation = [
Expand All @@ -37,8 +38,8 @@ public function A08()
public function B25()
{
$expectedResult = [
'w' => 37.73,
'b' => 34.73,
'w' => 39.06,
'b' => 35.86,
];

$expectedExplanation = [
Expand All @@ -59,8 +60,8 @@ public function B25()
public function B56()
{
$expectedResult = [
'w' => 47.0,
'b' => 36.8,
'w' => 48.2,
'b' => 37.92,
];

$expectedExplanation = [
Expand All @@ -81,8 +82,8 @@ public function B56()
public function C60()
{
$expectedResult = [
'w' => 37.73,
'b' => 34.73,
'w' => 38.85,
'b' => 35.86,
];

$expectedExplanation = [
Expand All @@ -96,4 +97,92 @@ public function C60()
$this->assertSame($expectedResult, $centerEval->result);
$this->assertSame($expectedExplanation, $centerEval->explain());
}

/**
* @test
*/
public function a6()
{
$expectedResult = [
'w' => 2.1,
'b' => 1.1,
];

$expectedExplanation = [
'White has a slightly better control of the center.',
];

$fen = '7k/8/P7/8/8/8/8/7K w - -';
$board = FenToBoardFactory::create($fen);
$centerEval = new CenterEval($board);

$this->assertSame($expectedResult, $centerEval->result);
$this->assertSame($expectedExplanation, $centerEval->explain());
}

/**
* @test
*/
public function a7()
{
$expectedResult = [
'w' => 1.1,
'b' => 1.1,
];

$expectedExplanation = [
];

$fen = '7k/8/P7/8/8/8/8/7K w - -';
$board = FenToBoardFactory::create($fen);
$board->play('w', 'a7');
$centerEval = new CenterEval($board);

$this->assertSame($expectedResult, $centerEval->result);
$this->assertSame($expectedExplanation, $centerEval->explain());
}

/**
* @test
*/
public function h6()
{
$expectedResult = [
'w' => 2.1,
'b' => 1.0,
];

$expectedExplanation = [
'White has a slightly better control of the center.',
];

$fen = 'k7/8/7P/8/8/8/8/K7 w - -';
$board = FenToBoardFactory::create($fen);
$centerEval = new CenterEval($board);

$this->assertSame($expectedResult, $centerEval->result);
$this->assertSame($expectedExplanation, $centerEval->explain());
}

/**
* @test
*/
public function h7()
{
$expectedResult = [
'w' => 1.0,
'b' => 1.0,
];

$expectedExplanation = [
];

$fen = 'k7/8/7P/8/8/8/8/K7 w - -';
$board = FenToBoardFactory::create($fen);
$board->play('w', 'h7');
$centerEval = new CenterEval($board);

$this->assertSame($expectedResult, $centerEval->result);
$this->assertSame($expectedExplanation, $centerEval->explain());
}
}
2 changes: 1 addition & 1 deletion tests/unit/Labeller/SumLabellerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function A08()

$label = (new SumLabeller())->label($balance);

$expected = 0.25;
$expected = 0.26;

$this->assertSame($expected, $label);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/SanHeuristicsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function e4_d5_exd5_Qxd5_center()

$balance = (new SanHeuristics(self::$function, $movetext, $name))->balance;

$expected = [ 0, 1.0, 0.08, 0.67, -1.0 ];
$expected = [ 0, 1.0, 0.09, 0.65, -1.0 ];

$this->assertSame($expected, $balance);
}
Expand Down
32 changes: 16 additions & 16 deletions tests/unit/SanSignalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public static function setUpBeforeClass(): void
*/
public function e4_d5_exd5_Qxd5()
{
$expectedCenter = [ 0.0, 1.0, 0.08, 0.67, -1.0 ];
$expectedCenter = [ 0, 1.0, 0.09, 0.65, -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 ];
$expectedUnnormalized = [ 0.0, 20.3, 1.1, 10.0, -64.0 ];
$expectedNormalized = [ 0.0, 2.0, -1.66, -0.18, -5.0 ];

$movetext = '1.e4 d5 2.exd5 Qxd5';
$sanSignal = new SanSignal(self::$function, $movetext, new Board());
Expand All @@ -44,8 +44,8 @@ public function e4_d5_exd5_Qxd5()
*/
public function A59()
{
$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 ];
$expectedUnnormalized = [ 0.0, 18.3, 2.48, 11.48, 4.48, 5.48, 1.48, 6.48, 4.48, 2.48, -7.52, 2.98, -2.12, 7.08, -4.02, 13.08, 8.98, 9.98 ];
$expectedNormalized = [ 0.0, 2.5, 0.75, 0.98, 1.3, 1.38, 1.67, 3.5, 3.01, 2.75, 0.0, 1.85, 0.16, 0.87, -2.13, 3.41, 2.05, 2.22 ];

$A59 = file_get_contents(self::DATA_FOLDER.'/sample/A59.pgn');
$sanSignal = new SanSignal(self::$function, $A59, new Board());
Expand Down Expand Up @@ -119,7 +119,7 @@ public function c4()
*/
public function d4()
{
$expectedUnnormalized = [ 0.0, 18.0 ];
$expectedUnnormalized = [ 0.0, 18.3 ];
$expectedNormalized = [ 0.0, 3.0 ];

$movetext = '1.d4';
Expand All @@ -134,7 +134,7 @@ public function d4()
*/
public function e4()
{
$expectedUnnormalized = [ 0.0, 20.0 ];
$expectedUnnormalized = [ 0.0, 20.3 ];
$expectedNormalized = [ 0.0, 2.0 ];

$movetext = '1.e4';
Expand All @@ -149,7 +149,7 @@ public function e4()
*/
public function f4()
{
$expectedUnnormalized = [ 0.0, 9.0 ];
$expectedUnnormalized = [ 0.0, 9.3 ];
$expectedNormalized = [ 0.0, 2.0 ];

$movetext = '1.f4';
Expand All @@ -164,7 +164,7 @@ public function f4()
*/
public function g4()
{
$expectedUnnormalized = [ 0.0, 5.0 ];
$expectedUnnormalized = [ 0.0, 5.2 ];
$expectedNormalized = [ 0.0, 1.0 ];

$movetext = '1.g4';
Expand All @@ -179,7 +179,7 @@ public function g4()
*/
public function h4()
{
$expectedUnnormalized = [ 0.0, 3.0 ];
$expectedUnnormalized = [ 0.0, 3.1 ];
$expectedNormalized = [ 0.0, 2.0 ];

$movetext = '1.h4';
Expand All @@ -194,8 +194,8 @@ public function h4()
*/
public function a4_h5()
{
$expectedUnnormalized = [ 0.0, 3.0, 0.0 ];
$expectedNormalized = [ 0.0, 2.0, 0.0 ];
$expectedUnnormalized = [ 0.0, 3.0, -0.1 ];
$expectedNormalized = [ 0.0, 2.0, -1.0 ];

$movetext = '1.a4 h5';
$sanSignal = new SanSignal(self::$function, $movetext, new Board());
Expand Down Expand Up @@ -224,7 +224,7 @@ public function a4_a5()
*/
public function a4_e5()
{
$expectedUnnormalized = [ 0.0, 3.0, -17.0 ];
$expectedUnnormalized = [ 0.0, 3.0, -17.3 ];
$expectedNormalized = [ 0.0, 2.0, -2.0 ];

$movetext = '1.a4 e5';
Expand All @@ -239,7 +239,7 @@ public function a4_e5()
*/
public function h4_e5()
{
$expectedUnnormalized = [ 0.0, 3.0, -17.0 ];
$expectedUnnormalized = [ 0.0, 3.1, -17.2 ];
$expectedNormalized = [ 0.0, 2.0, -3.0 ];

$movetext = '1.h4 e5';
Expand Down Expand Up @@ -291,7 +291,7 @@ public function a7()
$expectedUnnormalized = [ 0.0, 3.0 ];
$expectedNormalized = [ 0.0, 3.0 ];

$fen = '7k/8/P7/8/8/8/8/7K w - -';
$fen = '4k3/8/P7/8/8/8/8/4K3 w - -';
$movetext = '1.a7';
$board = FenToBoardFactory::create($fen);
$sanSignal = new SanSignal(self::$function, $movetext, $board);
Expand Down Expand Up @@ -325,7 +325,7 @@ public function h7()
$expectedUnnormalized = [ 0.0, 3.0 ];
$expectedNormalized = [ 0.0, 3.0 ];

$fen = 'k7/8/7P/8/8/8/8/K7 w - -';
$fen = '4k3/8/7P/8/8/8/8/4K3 w - -';
$movetext = '1.h7';
$board = FenToBoardFactory::create($fen);
$sanSignal = new SanSignal(self::$function, $movetext, $board);
Expand Down

0 comments on commit 9550689

Please sign in to comment.