]> git.sesse.net Git - stockfish/blob - src/movepick.h
Simplification: remove pawn shelter/storm masks
[stockfish] / src / movepick.h
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
5   Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
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
24 #include <array>
25 #include <limits>
26 #include <type_traits>
27
28 #include "movegen.h"
29 #include "position.h"
30 #include "types.h"
31
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 W, int D>
37 class StatsEntry {
38
39   static const bool IsInt = std::is_integral<T>::value;
40   typedef typename std::conditional<IsInt, int, T>::type TT;
41
42   T entry;
43
44 public:
45   T* get() { return &entry; }
46   void operator=(const T& v) { entry = v; }
47   operator TT() const { return entry; }
48
49   void operator<<(int bonus) {
50
51     assert(abs(bonus) <= D); // Ensure range is [-W * D, W * D]
52     assert(W * D < std::numeric_limits<T>::max()); // Ensure we don't overflow
53
54     entry += bonus * W - entry * abs(bonus) / D;
55
56     assert(abs(entry) <= W * D);
57   }
58 };
59
60 /// Stats is a generic N-dimensional array used to store various statistics.
61 /// The first template T parameter is the base type of the array, the W parameter
62 /// is the weight applied to the bonuses when we update values with the << operator,
63 /// the D parameter limits the range of updates (range is [-W * D, W * D]), and
64 /// the last parameters (Size and Sizes) encode the dimensions of the array.
65 template <typename T, int W, int D, int Size, int... Sizes>
66 struct Stats : public std::array<Stats<T, W, D, Sizes...>, Size>
67 {
68   T* get() { return this->at(0).get(); }
69
70   void fill(const T& v) {
71     T* p = get();
72     std::fill(p, p + sizeof(*this) / sizeof(*p), v);
73   }
74 };
75
76 template <typename T, int W, int D, int Size>
77 struct Stats<T, W, D, Size> : public std::array<StatsEntry<T, W, D>, Size> {
78   T* get() { return this->at(0).get(); }
79 };
80
81 /// Different tables use different W/D parameter, name them to ease readibility
82 enum StatsParams { W2 = 2, W32 = 32, D324 = 324, D936 = 936, NOT_USED = 0 };
83
84 /// ButterflyHistory records how often quiet moves have been successful or
85 /// unsuccessful during the current search, and is used for reduction and move
86 /// ordering decisions. It uses 2 tables (one for each color) indexed by
87 /// the move's from and to squares, see chessprogramming.wikispaces.com/Butterfly+Boards
88 typedef Stats<int16_t, W32, D324, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyHistory;
89
90 /// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
91 /// move, see chessprogramming.wikispaces.com/Countermove+Heuristic
92 typedef Stats<Move, NOT_USED, NOT_USED, PIECE_NB, SQUARE_NB> CounterMoveHistory;
93
94 /// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
95 typedef Stats<int16_t, W2, D324, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB> CapturePieceToHistory;
96
97 /// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
98 typedef Stats<int16_t, W32, D936, PIECE_NB, SQUARE_NB> PieceToHistory;
99
100 /// ContinuationHistory is the combined history of a given pair of moves, usually
101 /// the current one given a previous one. The nested history table is based on
102 /// PieceToHistory instead of ButterflyBoards.
103 typedef Stats<PieceToHistory, W32, NOT_USED, PIECE_NB, SQUARE_NB> ContinuationHistory;
104
105
106 /// MovePicker class is used to pick one pseudo legal move at a time from the
107 /// current position. The most important method is next_move(), which returns a
108 /// new pseudo legal move each time it is called, until there are no moves left,
109 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
110 /// beta algorithm, MovePicker attempts to return the moves which are most likely
111 /// to get a cut-off first.
112 class MovePicker {
113
114   enum PickType { Next, Best };
115
116 public:
117   MovePicker(const MovePicker&) = delete;
118   MovePicker& operator=(const MovePicker&) = delete;
119   MovePicker(const Position&, Move, Value, const CapturePieceToHistory*);
120   MovePicker(const Position&, Move, Depth, const ButterflyHistory*,  const CapturePieceToHistory*, Square);
121   MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const CapturePieceToHistory*, const PieceToHistory**, Move, Move*);
122   Move next_move(bool skipQuiets = false);
123
124 private:
125   template<PickType T, typename Pred> Move select(Pred);
126   template<GenType> void score();
127   ExtMove* begin() { return cur; }
128   ExtMove* end() { return endMoves; }
129
130   const Position& pos;
131   const ButterflyHistory* mainHistory;
132   const CapturePieceToHistory* captureHistory;
133   const PieceToHistory** contHistory;
134   Move ttMove;
135   ExtMove refutations[3], *cur, *endMoves, *endBadCaptures;
136   int stage;
137   Move move;
138   Square recaptureSquare;
139   Value threshold;
140   Depth depth;
141   ExtMove moves[MAX_MOVES];
142 };
143
144 #endif // #ifndef MOVEPICK_H_INCLUDED