]> git.sesse.net Git - stockfish/blob - src/movepick.h
Introduce pawn structure based history
[stockfish] / src / movepick.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 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 #ifndef MOVEPICK_H_INCLUDED
20 #define MOVEPICK_H_INCLUDED
21
22 #include <array>
23 #include <cassert>
24 #include <cstdint>
25 #include <cstdlib>
26 #include <limits>
27 #include <type_traits>  // IWYU pragma: keep
28
29 #include "movegen.h"
30 #include "types.h"
31 #include "position.h"
32
33 namespace Stockfish {
34
35 constexpr int PAWN_HISTORY_SIZE = 512;
36
37 inline int pawn_structure(const Position& pos) { return pos.pawn_key() & (PAWN_HISTORY_SIZE - 1); }
38
39 // StatsEntry stores the stat table value. It is usually a number but could
40 // be a move or even a nested history. We use a class instead of a naked value
41 // to directly call history update operator<<() on the entry so to use stats
42 // tables at caller sites as simple multi-dim arrays.
43 template<typename T, int D>
44 class StatsEntry {
45
46     T entry;
47
48    public:
49     void operator=(const T& v) { entry = v; }
50     T*   operator&() { return &entry; }
51     T*   operator->() { return &entry; }
52     operator const T&() const { return entry; }
53
54     void operator<<(int bonus) {
55         assert(abs(bonus) <= D);  // Ensure range is [-D, D]
56         static_assert(D <= std::numeric_limits<T>::max(), "D overflows T");
57
58         entry += (bonus * D - entry * abs(bonus)) / (D * 5 / 4);
59
60         assert(abs(entry) <= D);
61     }
62 };
63
64 // Stats is a generic N-dimensional array used to store various statistics.
65 // The first template parameter T is the base type of the array, and the second
66 // template parameter D limits the range of updates in [-D, D] when we update
67 // values with the << operator, while the last parameters (Size and Sizes)
68 // encode the dimensions of the array.
69 template<typename T, int D, int Size, int... Sizes>
70 struct Stats: public std::array<Stats<T, D, Sizes...>, Size> {
71     using stats = Stats<T, D, Size, Sizes...>;
72
73     void fill(const T& v) {
74
75         // For standard-layout 'this' points to the first struct member
76         assert(std::is_standard_layout_v<stats>);
77
78         using entry = StatsEntry<T, D>;
79         entry* p    = reinterpret_cast<entry*>(this);
80         std::fill(p, p + sizeof(*this) / sizeof(entry), v);
81     }
82 };
83
84 template<typename T, int D, int Size>
85 struct Stats<T, D, Size>: public std::array<StatsEntry<T, D>, Size> {};
86
87 // In stats table, D=0 means that the template parameter is not used
88 enum StatsParams {
89     NOT_USED = 0
90 };
91 enum StatsType {
92     NoCaptures,
93     Captures
94 };
95
96 // ButterflyHistory records how often quiet moves have been successful or
97 // unsuccessful during the current search, and is used for reduction and move
98 // ordering decisions. It uses 2 tables (one for each color) indexed by
99 // the move's from and to squares, see www.chessprogramming.org/Butterfly_Boards
100 // (~11 elo)
101 using ButterflyHistory = Stats<int16_t, 7183, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)>;
102
103 // CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
104 // move, see www.chessprogramming.org/Countermove_Heuristic
105 using CounterMoveHistory = Stats<Move, NOT_USED, PIECE_NB, SQUARE_NB>;
106
107 // CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
108 using CapturePieceToHistory = Stats<int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;
109
110 // PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
111 using PieceToHistory = Stats<int16_t, 29952, PIECE_NB, SQUARE_NB>;
112
113 // ContinuationHistory is the combined history of a given pair of moves, usually
114 // the current one given a previous one. The nested history table is based on
115 // PieceToHistory instead of ButterflyBoards.
116 // (~63 elo)
117 using ContinuationHistory = Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB>;
118
119 // PawnStructureHistory is addressed by the pawn structure and a move's [piece][to]
120 using PawnHistory = Stats<int16_t, 8192, PAWN_HISTORY_SIZE, PIECE_NB, SQUARE_NB>;
121
122 // MovePicker class is used to pick one pseudo-legal move at a time from the
123 // current position. The most important method is next_move(), which returns a
124 // new pseudo-legal move each time it is called, until there are no moves left,
125 // when MOVE_NONE is returned. In order to improve the efficiency of the
126 // alpha-beta algorithm, MovePicker attempts to return the moves which are most
127 // likely to get a cut-off first.
128 class MovePicker {
129
130     enum PickType {
131         Next,
132         Best
133     };
134
135    public:
136     MovePicker(const MovePicker&)            = delete;
137     MovePicker& operator=(const MovePicker&) = delete;
138     MovePicker(const Position&,
139                Move,
140                Depth,
141                const ButterflyHistory*,
142                const CapturePieceToHistory*,
143                const PieceToHistory**,
144                const PawnHistory&,
145                Move,
146                const Move*);
147     MovePicker(const Position&,
148                Move,
149                Depth,
150                const ButterflyHistory*,
151                const CapturePieceToHistory*,
152                const PieceToHistory**,
153                const PawnHistory&,
154                Square);
155     MovePicker(const Position&, Move, Value, const CapturePieceToHistory*, const PawnHistory&);
156     Move next_move(bool skipQuiets = false);
157
158    private:
159     template<PickType T, typename Pred>
160     Move select(Pred);
161     template<GenType>
162     void     score();
163     ExtMove* begin() { return cur; }
164     ExtMove* end() { return endMoves; }
165
166     const Position&              pos;
167     const ButterflyHistory*      mainHistory;
168     const CapturePieceToHistory* captureHistory;
169     const PieceToHistory**       continuationHistory;
170     const PawnHistory&           pawnHistory;
171     Move                         ttMove;
172     ExtMove                      refutations[3], *cur, *endMoves, *endBadCaptures;
173     int                          stage;
174     Square                       recaptureSquare;
175     Value                        threshold;
176     Depth                        depth;
177     ExtMove                      moves[MAX_MOVES];
178 };
179
180 }  // namespace Stockfish
181
182 #endif  // #ifndef MOVEPICK_H_INCLUDED