]> git.sesse.net Git - stockfish/blob - src/movepick.h
History code rewrite (#1122)
[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-2017 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
26 #include "movegen.h"
27 #include "position.h"
28 #include "types.h"
29
30 /// StatBoards is a generic 2-dimensional array used to store various statistics
31 template<int Size1, int Size2, typename T = int>
32 struct StatBoards : public std::array<std::array<T, Size2>, Size1> {
33
34   void fill(const T& v) {
35     T* p = &(*this)[0][0];
36     std::fill(p, p + sizeof(*this) / sizeof(*p), v);
37   }
38 };
39
40 /// ButterflyBoards are 2 tables (one for each color) indexed by the move's from
41 /// and to squares, see chessprogramming.wikispaces.com/Butterfly+Boards
42 typedef StatBoards<COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyBoards;
43
44 /// PieceToBoards are addressed by a move's [piece][to] information
45 typedef StatBoards<PIECE_NB, SQUARE_NB> PieceToBoards;
46
47 /// ButterflyHistory records how often quiet moves have been successful or
48 /// unsuccessful during the current search, and is used for reduction and move
49 /// ordering decisions. It uses ButterflyBoards as backing store.
50 struct ButterflyHistory : public ButterflyBoards {
51
52   void update(Color c, Move m, int v) {
53
54     const int D = 324;
55     int& entry = (*this)[c][from_to(m)];
56
57     assert(abs(v) <= D); // Consistency check for below formula
58
59     entry += v * 32 - entry * abs(v) / D;
60
61     assert(abs(entry) <= 32 * D);
62   }
63 };
64
65 /// PieceToHistory is like ButterflyHistory, but is based on PieceToBoards
66 struct PieceToHistory : public PieceToBoards {
67
68   void update(Piece pc, Square to, int v) {
69
70     const int D = 936;
71     int& entry = (*this)[pc][to];
72
73     assert(abs(v) <= D); // Consistency check for below formula
74
75     entry += v * 32 - entry * abs(v) / D;
76
77     assert(abs(entry) <= 32 * D);
78   }
79 };
80
81 /// CounterMoveStat stores counter moves indexed by [piece][to] of the previous
82 /// move, see chessprogramming.wikispaces.com/Countermove+Heuristic
83 typedef StatBoards<PIECE_NB, SQUARE_NB, Move> CounterMoveStat;
84
85 /// CounterMoveHistoryStat is like CounterMoveStat but instead of a move it
86 /// stores a full history (based on PieceTo boards instead of ButterflyBoards).
87 typedef StatBoards<PIECE_NB, SQUARE_NB, PieceToHistory> CounterMoveHistoryStat;
88
89
90 /// MovePicker class is used to pick one pseudo legal move at a time from the
91 /// current position. The most important method is next_move(), which returns a
92 /// new pseudo legal move each time it is called, until there are no moves left,
93 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
94 /// beta algorithm, MovePicker attempts to return the moves which are most likely
95 /// to get a cut-off first.
96 namespace Search { struct Stack; }
97
98 class MovePicker {
99 public:
100   MovePicker(const MovePicker&) = delete;
101   MovePicker& operator=(const MovePicker&) = delete;
102
103   MovePicker(const Position&, Move, Value);
104   MovePicker(const Position&, Move, Depth, Square);
105   MovePicker(const Position&, Move, Depth, Search::Stack*);
106
107   Move next_move(bool skipQuiets = false);
108
109 private:
110   template<GenType> void score();
111   ExtMove* begin() { return cur; }
112   ExtMove* end() { return endMoves; }
113
114   const Position& pos;
115   const Search::Stack* ss;
116   Move killers[2];
117   Move countermove;
118   Depth depth;
119   Move ttMove;
120   Square recaptureSquare;
121   Value threshold;
122   int stage;
123   ExtMove *cur, *endMoves, *endBadCaptures;
124   ExtMove moves[MAX_MOVES];
125 };
126
127 #endif // #ifndef MOVEPICK_H_INCLUDED