]> git.sesse.net Git - stockfish/blob - src/nnue/features/half_ka_v2_hm.h
c7b1a68df71be28e4e5df19a6ef330f6b7a73176
[stockfish] / src / nnue / features / half_ka_v2_hm.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 //Definition of input features HalfKP of NNUE evaluation function
20
21 #ifndef NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED
22 #define NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED
23
24 #include "../nnue_common.h"
25
26 #include "../../evaluate.h"
27 #include "../../misc.h"
28
29 namespace Stockfish {
30   struct StateInfo;
31 }
32
33 namespace Stockfish::Eval::NNUE::Features {
34
35   // Feature HalfKAv2_hm: Combination of the position of own king
36   // and the position of pieces. Position mirrored such that king always on e..h files.
37   class HalfKAv2_hm {
38
39     // unique number for each piece type on each square
40     enum {
41       PS_NONE     =  0,
42       PS_W_PAWN   =  0,
43       PS_B_PAWN   =  1 * SQUARE_NB,
44       PS_W_KNIGHT =  2 * SQUARE_NB,
45       PS_B_KNIGHT =  3 * SQUARE_NB,
46       PS_W_BISHOP =  4 * SQUARE_NB,
47       PS_B_BISHOP =  5 * SQUARE_NB,
48       PS_W_ROOK   =  6 * SQUARE_NB,
49       PS_B_ROOK   =  7 * SQUARE_NB,
50       PS_W_QUEEN  =  8 * SQUARE_NB,
51       PS_B_QUEEN  =  9 * SQUARE_NB,
52       PS_KING     =  10 * SQUARE_NB,
53       PS_NB       =  11 * SQUARE_NB
54     };
55
56     static constexpr IndexType PieceSquareIndex[COLOR_NB][PIECE_NB] = {
57       // convention: W - us, B - them
58       // viewed from other side, W and B are reversed
59       { PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_KING, PS_NONE,
60         PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_KING, PS_NONE },
61       { PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_KING, PS_NONE,
62         PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_KING, PS_NONE }
63     };
64
65     // Orient a square according to perspective (rotates by 180 for black)
66     static Square orient(Color perspective, Square s, Square ksq);
67
68     // Index of a feature for a given king position and another piece on some square
69     static IndexType make_index(Color perspective, Square s, Piece pc, Square ksq);
70
71    public:
72     // Feature name
73     static constexpr const char* Name = "HalfKAv2_hm(Friend)";
74
75     // Hash value embedded in the evaluation file
76     static constexpr std::uint32_t HashValue = 0x7f234cb8u;
77
78     // Number of feature dimensions
79     static constexpr IndexType Dimensions =
80         static_cast<IndexType>(SQUARE_NB) * static_cast<IndexType>(PS_NB) / 2;
81
82     static constexpr int KingBuckets[64] = {
83       -1, -1, -1, -1, 31, 30, 29, 28,
84       -1, -1, -1, -1, 27, 26, 25, 24,
85       -1, -1, -1, -1, 23, 22, 21, 20,
86       -1, -1, -1, -1, 19, 18, 17, 16,
87       -1, -1, -1, -1, 15, 14, 13, 12,
88       -1, -1, -1, -1, 11, 10,  9,  8,
89       -1, -1, -1, -1,  7,  6,  5,  4,
90       -1, -1, -1, -1,  3,  2,  1,  0
91     };
92
93     // Maximum number of simultaneously active features.
94     static constexpr IndexType MaxActiveDimensions = 32;
95     using IndexList = ValueList<IndexType, MaxActiveDimensions>;
96
97     // Get a list of indices for active features
98     static void append_active_indices(
99       const Position& pos,
100       Color perspective,
101       IndexList& active);
102
103     // Get a list of indices for recently changed features
104     static void append_changed_indices(
105       Square ksq,
106       const DirtyPiece& dp,
107       Color perspective,
108       IndexList& removed,
109       IndexList& added
110     );
111
112     // Returns the cost of updating one perspective, the most costly one.
113     // Assumes no refresh needed.
114     static int update_cost(const StateInfo* st);
115     static int refresh_cost(const Position& pos);
116
117     // Returns whether the change stored in this StateInfo means that
118     // a full accumulator refresh is required.
119     static bool requires_refresh(const StateInfo* st, Color perspective);
120   };
121
122 }  // namespace Stockfish::Eval::NNUE::Features
123
124 #endif // #ifndef NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED