Files
chess-engine/python/python/constants.py
KeshavAnandCode 3eccd97536 feat: implement HalfKAv2_hm feature extraction (352 features)
- Implement piece-square feature extraction
- 32 active features for 32 pieces on board
- Tests for feature extraction (7 tests)
- Fix: piece_sq * 6 + piece_type mapping
2026-04-14 18:11:15 -05:00

527 lines
6.0 KiB
Python

"""Stockfish NNUE Feature Constants"""
# Total feature count: 352 + 60,720 = 61,072
HALF_KA_V2_HM = 352
FULL_THREATS = 60_720
TOTAL_FEATURES = HALF_KA_V2_HM + FULL_THREATS
# Piece Unicode symbol to piece type mapping (0 = pawn, 1 = knight, etc.)
PIECE_TYPE_MAP = {
"\u265f": 0, # pawn ♙
"\u265e": 1, # knight ♘
"\u265d": 2, # bishop ♗
"\u265c": 3, # rook ♖
"\u265b": 4, # queen ♕
"\u265a": 5, # king ♔
"\u2659": 0, # pawn ♟
"\u2658": 1, # knight ♞
"\u2657": 2, # bishop ♝
"\u2656": 3, # rook ♜
"\u2655": 4, # queen ♛
"\u2654": 5, # king ♚
}
# Piece Unicode symbols (Black pieces)
BLACK_PIECES = {
0: "\u2659", # pawn ♟
1: "\u2658", # knight ♞
2: "\u2657", # bishop ♝
3: "\u2656", # rook ♜
4: "\u2655", # queen ♛
5: "\u2654", # king ♚
}
# Piece types (Black pieces)
BLACK_PIECES = {
0: "P",
1: "N",
2: "B",
3: "R",
4: "Q",
5: "K",
}
# Piece-square index tables
# Maps (perspective, piece_type) to square index
PIECE_SQUARE_INDEX = [
# White perspective
[
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
], # pawns
[
2,
1,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
], # knights
[
3,
2,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
], # bishops
[
5,
4,
3,
2,
1,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
], # rooks
[
4,
3,
2,
1,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
], # queens
[
5,
4,
3,
2,
1,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
], # kings
# Black perspective
[
24,
23,
22,
21,
20,
19,
18,
17,
16,
15,
14,
13,
12,
11,
10,
9,
8,
7,
6,
5,
4,
3,
2,
1,
0,
5,
], # pawns
[
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
], # knights
[
24,
23,
22,
21,
20,
19,
18,
17,
16,
15,
14,
13,
12,
11,
10,
9,
8,
7,
6,
5,
4,
3,
2,
1,
0,
5,
], # bishops
[
22,
21,
20,
19,
18,
17,
16,
15,
14,
13,
12,
11,
10,
9,
8,
7,
6,
5,
4,
3,
2,
1,
0,
5,
6,
], # rooks
[
23,
22,
21,
20,
19,
18,
17,
16,
15,
14,
13,
12,
11,
10,
9,
8,
7,
6,
5,
4,
3,
2,
1,
0,
5,
6,
], # queens
[
24,
23,
22,
21,
20,
19,
18,
17,
16,
15,
14,
13,
12,
11,
10,
9,
8,
7,
6,
5,
4,
3,
2,
1,
0,
5,
], # kings
]
# Orientation table for king square
# ORIENT_TBL[ksq] gives the orientation offset based on king position
ORIENT_TBL = [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
]