]> git.sesse.net Git - stockfish/blob - src/movepick.h
Unify History and Gains under a single Stats class
[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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #if !defined MOVEPICK_H_INCLUDED
21 #define MOVEPICK_H_INCLUDED
22
23 #include <algorithm> // For std::max
24 #include <cstring>   // For memset
25
26 #include "position.h"
27 #include "search.h"
28 #include "types.h"
29
30
31 /// The Stats struct stores moves statistics. According to the template parameter
32 /// the class can store both History and Gains type statistics. History records
33 /// how often different moves have been successful or unsuccessful during the
34 /// current search and is used for reduction and move ordering decisions. Gains
35 /// records the move's best evaluation gain from one ply to the next and is used
36 /// for pruning decisions. Entries are stored according only to moving piece and
37 /// destination square, in particular two moves with different origin but same
38 /// destination and same piece will be considered identical.
39 template<bool Gain>
40 struct Stats {
41
42   static const Value Max = Value(2000);
43
44   const Value* operator[](Piece p) const { return &table[p][0]; }
45   void clear() { memset(table, 0, sizeof(table)); }
46
47   void update(Piece p, Square to, Value v) {
48
49     if (Gain)
50         table[p][to] = std::max(v, table[p][to] - 1);
51
52     else if (abs(table[p][to] + v) < Max)
53         table[p][to] +=  v;
54   }
55
56 private:
57   Value table[PIECE_NB][SQUARE_NB];
58 };
59
60 typedef Stats<false> History;
61 typedef Stats<true> Gains;
62
63
64 /// MovePicker class is used to pick one pseudo legal move at a time from the
65 /// current position. The most important method is next_move(), which returns a
66 /// new pseudo legal move each time it is called, until there are no moves left,
67 /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
68 /// beta algorithm, MovePicker attempts to return the moves which are most likely
69 /// to get a cut-off first.
70
71 class MovePicker {
72
73   MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
74
75 public:
76   MovePicker(const Position&, Move, Depth, const History&, Search::Stack*, Value);
77   MovePicker(const Position&, Move, Depth, const History&, Square);
78   MovePicker(const Position&, Move, const History&, PieceType);
79   template<bool SpNode> Move next_move();
80
81 private:
82   void score_captures();
83   void score_noncaptures();
84   void score_evasions();
85   void generate_next();
86
87   const Position& pos;
88   const History& Hist;
89   Search::Stack* ss;
90   Depth depth;
91   Move ttMove;
92   MoveStack killers[2];
93   Square recaptureSquare;
94   int captureThreshold, phase;
95   MoveStack *cur, *end, *endQuiets, *endBadCaptures;
96   MoveStack moves[MAX_MOVES];
97 };
98
99 #endif // !defined(MOVEPICK_H_INCLUDED)