]> git.sesse.net Git - stockfish/blob - src/nnue/features/half_kp.h
Cleanup and simplify NNUE code.
[stockfish] / src / nnue / features / half_kp.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_KP_H_INCLUDED
22 #define NNUE_FEATURES_HALF_KP_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 HalfKP: Combination of the position of own king
36   // and the position of pieces other than kings
37   class HalfKP {
38
39     // unique number for each piece type on each square
40     enum {
41       PS_NONE     =  0,
42       PS_W_PAWN   =  1,
43       PS_B_PAWN   =  1 * SQUARE_NB + 1,
44       PS_W_KNIGHT =  2 * SQUARE_NB + 1,
45       PS_B_KNIGHT =  3 * SQUARE_NB + 1,
46       PS_W_BISHOP =  4 * SQUARE_NB + 1,
47       PS_B_BISHOP =  5 * SQUARE_NB + 1,
48       PS_W_ROOK   =  6 * SQUARE_NB + 1,
49       PS_B_ROOK   =  7 * SQUARE_NB + 1,
50       PS_W_QUEEN  =  8 * SQUARE_NB + 1,
51       PS_B_QUEEN  =  9 * SQUARE_NB + 1,
52       PS_NB = 10 * SQUARE_NB + 1
53     };
54
55     static constexpr IndexType PieceSquareIndex[COLOR_NB][PIECE_NB] = {
56       // convention: W - us, B - them
57       // viewed from other side, W and B are reversed
58       { PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_NONE, PS_NONE,
59         PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_NONE, PS_NONE },
60       { PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_NONE, PS_NONE,
61         PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_NONE, PS_NONE }
62     };
63
64     // Orient a square according to perspective (rotates by 180 for black)
65     static Square orient(Color perspective, Square s);
66
67     // Index of a feature for a given king position and another piece on some square
68     static IndexType make_index(Color perspective, Square s, Piece pc, Square ksq);
69
70    public:
71     // Feature name
72     static constexpr const char* Name = "HalfKP(Friend)";
73
74     // Hash value embedded in the evaluation file
75     static constexpr std::uint32_t HashValue = 0x5D69D5B8u;
76
77     // Number of feature dimensions
78     static constexpr IndexType Dimensions =
79         static_cast<IndexType>(SQUARE_NB) * static_cast<IndexType>(PS_NB);
80
81     // Maximum number of simultaneously active features. 30 because kins are not included.
82     static constexpr IndexType MaxActiveDimensions = 30;
83
84     // Get a list of indices for active features
85     static void append_active_indices(
86       const Position& pos,
87       Color perspective,
88       ValueListInserter<IndexType> active);
89
90     // Get a list of indices for recently changed features
91     static void append_changed_indices(
92       Square ksq,
93       StateInfo* st,
94       Color perspective,
95       ValueListInserter<IndexType> removed,
96       ValueListInserter<IndexType> added);
97
98     // Returns the cost of updating one perspective, the most costly one.
99     // Assumes no refresh needed.
100     static int update_cost(StateInfo* st);
101     static int refresh_cost(const Position& pos);
102
103     // Returns whether the change stored in this StateInfo means that
104     // a full accumulator refresh is required.
105     static bool requires_refresh(StateInfo* st, Color perspective);
106   };
107
108 }  // namespace Stockfish::Eval::NNUE::Features
109
110 #endif // #ifndef NNUE_FEATURES_HALF_KP_H_INCLUDED