Rev 169 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
96 | pmbaty | 1 | /* |
2 | Stockfish, a UCI chess playing engine derived from Glaurung 2.1 |
||
3 | Copyright (C) 2004-2008 Tord Romstad (Glaurung author) |
||
4 | Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad |
||
185 | pmbaty | 5 | Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad |
96 | pmbaty | 6 | |
7 | Stockfish is free software: you can redistribute it and/or modify |
||
8 | it under the terms of the GNU General Public License as published by |
||
9 | the Free Software Foundation, either version 3 of the License, or |
||
10 | (at your option) any later version. |
||
11 | |||
12 | Stockfish is distributed in the hope that it will be useful, |
||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | GNU General Public License for more details. |
||
16 | |||
17 | You should have received a copy of the GNU General Public License |
||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | */ |
||
20 | |||
21 | #ifndef MOVEPICK_H_INCLUDED |
||
22 | #define MOVEPICK_H_INCLUDED |
||
23 | |||
169 | pmbaty | 24 | #include <array> |
25 | #include <limits> |
||
185 | pmbaty | 26 | #include <type_traits> |
96 | pmbaty | 27 | |
28 | #include "movegen.h" |
||
29 | #include "position.h" |
||
30 | #include "types.h" |
||
31 | |||
185 | pmbaty | 32 | /// StatsEntry stores the stat table value. It is usually a number but could |
33 | /// be a move or even a nested history. We use a class instead of naked value |
||
34 | /// to directly call history update operator<<() on the entry so to use stats |
||
35 | /// tables at caller sites as simple multi-dim arrays. |
||
36 | template<typename T, int D> |
||
37 | class StatsEntry { |
||
96 | pmbaty | 38 | |
185 | pmbaty | 39 | T entry; |
96 | pmbaty | 40 | |
185 | pmbaty | 41 | public: |
42 | void operator=(const T& v) { entry = v; } |
||
43 | T* operator&() { return &entry; } |
||
44 | T* operator->() { return &entry; } |
||
45 | operator const T&() const { return entry; } |
||
96 | pmbaty | 46 | |
185 | pmbaty | 47 | void operator<<(int bonus) { |
48 | assert(abs(bonus) <= D); // Ensure range is [-D, D] |
||
49 | static_assert(D <= std::numeric_limits<T>::max(), "D overflows T"); |
||
96 | pmbaty | 50 | |
185 | pmbaty | 51 | entry += bonus - entry * abs(bonus) / D; |
96 | pmbaty | 52 | |
185 | pmbaty | 53 | assert(abs(entry) <= D); |
96 | pmbaty | 54 | } |
169 | pmbaty | 55 | }; |
96 | pmbaty | 56 | |
185 | pmbaty | 57 | /// Stats is a generic N-dimensional array used to store various statistics. |
58 | /// The first template parameter T is the base type of the array, the second |
||
59 | /// template parameter D limits the range of updates in [-D, D] when we update |
||
60 | /// values with the << operator, while the last parameters (Size and Sizes) |
||
61 | /// encode the dimensions of the array. |
||
62 | template <typename T, int D, int Size, int... Sizes> |
||
63 | struct Stats : public std::array<Stats<T, D, Sizes...>, Size> |
||
64 | { |
||
65 | typedef Stats<T, D, Size, Sizes...> stats; |
||
169 | pmbaty | 66 | |
67 | void fill(const T& v) { |
||
68 | |||
185 | pmbaty | 69 | // For standard-layout 'this' points to first struct member |
70 | assert(std::is_standard_layout<stats>::value); |
||
169 | pmbaty | 71 | |
185 | pmbaty | 72 | typedef StatsEntry<T, D> entry; |
73 | entry* p = reinterpret_cast<entry*>(this); |
||
74 | std::fill(p, p + sizeof(*this) / sizeof(entry), v); |
||
169 | pmbaty | 75 | } |
96 | pmbaty | 76 | }; |
77 | |||
185 | pmbaty | 78 | template <typename T, int D, int Size> |
79 | struct Stats<T, D, Size> : public std::array<StatsEntry<T, D>, Size> {}; |
||
96 | pmbaty | 80 | |
185 | pmbaty | 81 | /// In stats table, D=0 means that the template parameter is not used |
82 | enum StatsParams { NOT_USED = 0 }; |
||
96 | pmbaty | 83 | |
154 | pmbaty | 84 | |
169 | pmbaty | 85 | /// ButterflyHistory records how often quiet moves have been successful or |
86 | /// unsuccessful during the current search, and is used for reduction and move |
||
185 | pmbaty | 87 | /// ordering decisions. It uses 2 tables (one for each color) indexed by |
88 | /// the move's from and to squares, see chessprogramming.wikispaces.com/Butterfly+Boards |
||
89 | typedef Stats<int16_t, 10692, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyHistory; |
||
154 | pmbaty | 90 | |
185 | pmbaty | 91 | /// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous |
92 | /// move, see chessprogramming.wikispaces.com/Countermove+Heuristic |
||
93 | typedef Stats<Move, NOT_USED, PIECE_NB, SQUARE_NB> CounterMoveHistory; |
||
154 | pmbaty | 94 | |
185 | pmbaty | 95 | /// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type] |
96 | typedef Stats<int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB> CapturePieceToHistory; |
||
169 | pmbaty | 97 | |
185 | pmbaty | 98 | /// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to] |
99 | typedef Stats<int16_t, 29952, PIECE_NB, SQUARE_NB> PieceToHistory; |
||
154 | pmbaty | 100 | |
185 | pmbaty | 101 | /// ContinuationHistory is the combined history of a given pair of moves, usually |
102 | /// the current one given a previous one. The nested history table is based on |
||
103 | /// PieceToHistory instead of ButterflyBoards. |
||
104 | typedef Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB> ContinuationHistory; |
||
169 | pmbaty | 105 | |
154 | pmbaty | 106 | |
96 | pmbaty | 107 | /// MovePicker class is used to pick one pseudo legal move at a time from the |
108 | /// current position. The most important method is next_move(), which returns a |
||
109 | /// new pseudo legal move each time it is called, until there are no moves left, |
||
110 | /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha |
||
111 | /// beta algorithm, MovePicker attempts to return the moves which are most likely |
||
112 | /// to get a cut-off first. |
||
185 | pmbaty | 113 | class MovePicker { |
96 | pmbaty | 114 | |
185 | pmbaty | 115 | enum PickType { Next, Best }; |
116 | |||
96 | pmbaty | 117 | public: |
118 | MovePicker(const MovePicker&) = delete; |
||
119 | MovePicker& operator=(const MovePicker&) = delete; |
||
169 | pmbaty | 120 | MovePicker(const Position&, Move, Value, const CapturePieceToHistory*); |
185 | pmbaty | 121 | MovePicker(const Position&, Move, Depth, const ButterflyHistory*, |
122 | const CapturePieceToHistory*, |
||
123 | const PieceToHistory**, |
||
124 | Square); |
||
125 | MovePicker(const Position&, Move, Depth, const ButterflyHistory*, |
||
126 | const CapturePieceToHistory*, |
||
127 | const PieceToHistory**, |
||
128 | Move, |
||
129 | Move*); |
||
169 | pmbaty | 130 | Move next_move(bool skipQuiets = false); |
96 | pmbaty | 131 | |
132 | private: |
||
185 | pmbaty | 133 | template<PickType T, typename Pred> Move select(Pred); |
96 | pmbaty | 134 | template<GenType> void score(); |
154 | pmbaty | 135 | ExtMove* begin() { return cur; } |
96 | pmbaty | 136 | ExtMove* end() { return endMoves; } |
137 | |||
138 | const Position& pos; |
||
169 | pmbaty | 139 | const ButterflyHistory* mainHistory; |
140 | const CapturePieceToHistory* captureHistory; |
||
185 | pmbaty | 141 | const PieceToHistory** continuationHistory; |
142 | Move ttMove; |
||
143 | ExtMove refutations[3], *cur, *endMoves, *endBadCaptures; |
||
169 | pmbaty | 144 | int stage; |
185 | pmbaty | 145 | Move move; |
96 | pmbaty | 146 | Square recaptureSquare; |
147 | Value threshold; |
||
169 | pmbaty | 148 | Depth depth; |
154 | pmbaty | 149 | ExtMove moves[MAX_MOVES]; |
96 | pmbaty | 150 | }; |
151 | |||
152 | #endif // #ifndef MOVEPICK_H_INCLUDED |