Heuristics

✨ If you ask a chess pro why a chess move is good, they'll probably give you a bunch of reasons, many of them intuitive, about why they made that decision.

It is important to develop your pieces in the opening while trying to control the center of the board at the same time. Castling is an excellent move as long as the king gets safe. Then, in the middlegame space becomes an advantage. And if a complex position can be simplified when you have an advantage, then so much the better. The pawn structure could determine the endgame.

The list of reasons goes on and on.

The mathematician Claude Shannon came to the conclusion that there are more chess moves than atoms in the universe. The game is complex and you need to learn how to make decisions to play chess like a pro. Since no human can calculate more than, let's say 30 moves ahead, it's all about thinking in terms of heuristics.

Heuristics are quick, mental shortcuts that we humans use to make decisions and solve problems in our daily lives. While far from being perfect, heuristics are approximations that help manage cognitive load.

Listed below are the chess heuristics implemented in PHP Chess.

Heuristic Evaluation
Absolute fork Chess\Eval\AbsoluteForkEval
Absolute pin Chess\Eval\AbsolutePinEval
Absolute skewer Chess\Eval\AbsoluteSkewerEval
Advanced pawn Chess\Eval\AdvancedPawnEval
Attack Chess\Eval\AttackEval
Backward pawn Chess\Eval\BackwardPawnEval
Bad bishop Chess\Eval\BadBishopEval
Bishop outpost Chess\Eval\BishopOutpostEval
Bishop pair Chess\Eval\BishopPairEval
Center Chess\Eval\CenterEval
Connectivity Chess\Eval\ConnectivityEval
Defense Chess\Eval\DefenseEval
Diagonal opposition Chess\Eval\DiagonalOppositionEval
Direct opposition Chess\Eval\DirectOppositionEval
Discovered check Chess\Eval\DiscoveredCheckEval
Doubled pawn Chess\Eval\DoubledPawnEval
Far-advanced pawn Chess\Eval\FarAdvancedPawnEval
Isolated pawn Chess\Eval\IsolatedPawnEval
King safety Chess\Eval\KingSafetyEval
Knight outpost Chess\Eval\KnightOutpostEval
Material Chess\Eval\MaterialEval
Passed pawn Chess\Eval\PassedPawnEval
Pressure Chess\Eval\PressureEval
Protection Chess\Eval\ProtectionEval
Relative fork Chess\Eval\RelativeForkEval
Relative pin Chess\Eval\RelativePinEval
Space Chess\Eval\SpaceEval
Square outpost Chess\Eval\SqOutpostEval
Threat Chess\Eval\ThreatEval

The evaluation features are used in two heuristics classes: Chess\Heuristics\FenHeuristics and Chess\Heuristics\SanHeuristics. The former allows to transform a FEN position to numbers while the latter transforms an entire chess game in SAN format to numbers.

use Chess\FenToBoardFactory;
use Chess\Function\StandardFunction;
use Chess\Heuristics\FenHeuristics;

$fen = 'rnbqkb1r/p1pp1ppp/1p2pn2/8/2PP4/2N2N2/PP2PPPP/R1BQKB1R b KQkq -';

$board = FenToBoardFactory::create($fen);

$result = [
    'names' => (new StandardFunction())->names(),
    'balance' => (new FenHeuristics($board))->getBalance(),
];

print_r($result);
Array
(
    [names] => Array
        (
            [0] => Material
            [1] => Center
            [2] => Connectivity
            [3] => Space
            [4] => Pressure
            [5] => King safety
            [6] => Protection
            [7] => Threat
            [8] => Attack
            [9] => Discovered check
            [10] => Doubled pawn
            [11] => Passed pawn
            [12] => Advanced pawn
            [13] => Far-advanced pawn
            [14] => Isolated pawn
            [15] => Backward pawn
            [16] => Defense
            [17] => Absolute skewer
            [18] => Absolute pin
            [19] => Relative pin
            [20] => Absolute fork
            [21] => Relative fork
            [22] => Outpost square
            [23] => Knight outpost
            [24] => Bishop outpost
            [25] => Bishop pair
            [26] => Bad bishop
            [27] => Diagonal opposition
            [28] => Direct opposition
        )

    [balance] => Array
        (
            [0] => 0
            [1] => 12.4
            [2] => 0
            [3] => 3
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
            [13] => 0
            [14] => 0
            [15] => 0
            [16] => 0
            [17] => 0
            [18] => 0
            [19] => 0
            [20] => 0
            [21] => 0
            [22] => 0
            [23] => 0
            [24] => 0
            [25] => 0
            [26] => 0
            [27] => 0
            [28] => 0
        )

)

A chess game can be plotted in terms of balance. +1 is the best possible evaluation for White and -1 the best possible evaluation for Black. Both forces being set to 0 means they're balanced.

use Chess\Function\StandardFunction;
use Chess\Heuristics\SanHeuristics;

$movetext = '1.d4 Nf6 2.c4 e6 3.Nf3 b6 4.Nc3';

$result = [
    'names' => (new StandardFunction())->names(),
    'balance' => (new SanHeuristics($movetext))->getBalance(),
];

print_r($result);

Figure 1

Figure 1. Heuristics of 1.d4 Nf6 2.c4 e6 3.Nf3 b6 4.Nc3

🎉 There it is! Chess positions converted to numbers can be processed with machine learning techniques.