]> git.sesse.net Git - stockfish/blob - src/history.cpp
Be sure book file is closed before we leave
[stockfish] / src / history.cpp
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 Marco Costalba
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
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26 #include <cstring>
27
28 #include "history.h"
29
30
31 ////
32 //// Functions
33 ////
34
35 /// Constructor
36
37 History::History() {
38   this->clear();
39 }
40
41
42 /// History::clear() clears the history tables.
43
44 void History::clear() {
45   memset(history, 0, 2 * 8 * 64 * sizeof(int));
46   memset(successCount, 0, 2 * 8 * 64 * sizeof(int));
47   memset(failureCount, 0, 2 * 8 * 64 * sizeof(int));
48 }
49
50
51 /// History::success() registers a move as being successful.  This is done
52 /// whenever a non-capturing move causes a beta cutoff in the main search.
53 /// The three parameters are the moving piece, the move itself, and the
54 /// search depth.
55
56 void History::success(Piece p, Move m, Depth d) {
57   assert(piece_is_ok(p));
58   assert(move_is_ok(m));
59
60   history[p][move_to(m)] += int(d) * int(d);
61   successCount[p][move_to(m)]++;
62
63   // Prevent history overflow:
64   if(history[p][move_to(m)] >= HistoryMax)
65     for(int i = 0; i < 16; i++)
66       for(int j = 0; j < 64; j++)
67         history[i][j] /= 2;
68 }
69
70
71 /// History::failure() registers a move as being unsuccessful.  The function is
72 /// called for each non-capturing move which failed to produce a beta cutoff
73 /// at a node where a beta cutoff was finally found.
74
75 void History::failure(Piece p, Move m) {
76   assert(piece_is_ok(p));
77   assert(move_is_ok(m));
78
79   failureCount[p][move_to(m)]++;
80 }
81
82
83 /// History::move_ordering_score() returns an integer value used to order the
84 /// non-capturing moves in the MovePicker class.
85
86 int History::move_ordering_score(Piece p, Move m) const {
87   assert(piece_is_ok(p));
88   assert(move_is_ok(m));
89
90   return history[p][move_to(m)];
91 }
92
93
94 /// History::ok_to_prune() decides whether a move has been sufficiently
95 /// unsuccessful that it makes sense to prune it entirely.
96
97 bool History::ok_to_prune(Piece p, Move m, Depth d) const {
98   assert(piece_is_ok(p));
99   assert(move_is_ok(m));
100
101   return (int(d) * successCount[p][move_to(m)] < failureCount[p][move_to(m)]);
102 }