]> git.sesse.net Git - stockfish/blob - src/tt.cpp
Fixed a bug in PV extraction from the transposition table: The
[stockfish] / src / tt.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-2009 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 <cmath>
27 #include <cstring>
28
29 #include "movegen.h"
30 #include "tt.h"
31
32
33 ////
34 //// Functions
35 ////
36
37 TranspositionTable::TranspositionTable() {
38
39   size = writes = 0;
40   entries = 0;
41   generation = 0;
42 }
43
44 TranspositionTable::~TranspositionTable() {
45
46   delete [] entries;
47 }
48
49
50 /// TranspositionTable::set_size sets the size of the transposition table,
51 /// measured in megabytes.
52
53 void TranspositionTable::set_size(unsigned mbSize) {
54
55   assert(mbSize >= 4 && mbSize <= 4096);
56
57   unsigned newSize = 1024;
58
59   // We store a cluster of 4 TTEntry for each position and newSize is
60   // the maximum number of storable positions
61   while ((2 * newSize) * 4 * (sizeof(TTEntry)) <= (mbSize << 20))
62       newSize *= 2;
63
64   if (newSize != size)
65   {
66       size = newSize;
67       delete [] entries;
68       entries = new TTEntry[size * 4];
69       if (!entries)
70       {
71           std::cerr << "Failed to allocate " << mbSize
72                     << " MB for transposition table." << std::endl;
73           Application::exit_with_failure();
74       }
75       clear();
76   }
77 }
78
79
80 /// TranspositionTable::clear overwrites the entire transposition table
81 /// with zeroes. It is called whenever the table is resized, or when the
82 /// user asks the program to clear the table (from the UCI interface).
83 /// Perhaps we should also clear it when the "ucinewgame" command is recieved?
84
85 void TranspositionTable::clear() {
86
87   memset(entries, 0, size * 4 * sizeof(TTEntry));
88 }
89
90
91 /// TranspositionTable::store writes a new entry containing a position,
92 /// a value, a value type, a search depth, and a best move to the
93 /// transposition table. Transposition table is organized in clusters of
94 /// four TTEntry objects, and when a new entry is written, it replaces
95 /// the least valuable of the four entries in a cluster. A TTEntry t1 is
96 /// considered to be more valuable than a TTEntry t2 if t1 is from the
97 /// current search and t2 is from a previous search, or if the depth of t1
98 /// is bigger than the depth of t2. A TTEntry of type VALUE_TYPE_EVAL
99 /// never replaces another entry for the same position.
100
101 void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m) {
102
103   TTEntry *tte, *replace;
104
105   tte = replace = first_entry(posKey);
106   for (int i = 0; i < 4; i++, tte++)
107   {
108       if (!tte->key() || tte->key() == posKey) // empty or overwrite old
109       {
110           // Do not overwrite when new type is VALUE_TYPE_EVAL
111           if (tte->key() && t == VALUE_TYPE_EVAL)
112               return;
113
114           if (m == MOVE_NONE)
115               m = tte->move();
116
117           *tte = TTEntry(posKey, v, t, d, m, generation);
118           return;
119       }
120       else if (i == 0)  // replace would be a no-op in this common case
121           continue;
122
123       int c1 = (replace->generation() == generation ?  2 : 0);
124       int c2 = (tte->generation() == generation ? -2 : 0);
125       int c3 = (tte->depth() < replace->depth() ?  1 : 0);
126
127       if (c1 + c2 + c3 > 0)
128           replace = tte;
129   }
130   *replace = TTEntry(posKey, v, t, d, m, generation);
131   writes++;
132 }
133
134
135 /// TranspositionTable::retrieve looks up the current position in the
136 /// transposition table. Returns a pointer to the TTEntry or NULL
137 /// if position is not found.
138
139 TTEntry* TranspositionTable::retrieve(const Key posKey) const {
140
141   TTEntry *tte = first_entry(posKey);
142
143   for (int i = 0; i < 4; i++, tte++)
144       if (tte->key() == posKey)
145           return tte;
146
147   return NULL;
148 }
149
150
151 /// TranspositionTable::first_entry returns a pointer to the first
152 /// entry of a cluster given a position.
153
154 inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
155
156   return entries + (int(posKey & (size - 1)) << 2);
157 }
158
159 /// TranspositionTable::new_search() is called at the beginning of every new
160 /// search. It increments the "generation" variable, which is used to
161 /// distinguish transposition table entries from previous searches from
162 /// entries from the current search.
163
164 void TranspositionTable::new_search() {
165
166   generation++;
167   writes = 0;
168 }
169
170
171 /// TranspositionTable::insert_pv() is called at the end of a search
172 /// iteration, and inserts the PV back into the PV. This makes sure
173 /// the old PV moves are searched first, even if the old TT entries
174 /// have been overwritten.
175
176 void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
177
178   StateInfo st;
179   Position p(pos);
180
181   for (int i = 0; pv[i] != MOVE_NONE; i++)
182   {
183       store(p.get_key(), VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
184       p.do_move(pv[i], st);
185   }
186 }
187
188
189 /// TranspositionTable::extract_pv() extends a PV by adding moves from the
190 /// transposition table at the end. This should ensure that the PV is almost
191 /// always at least two plies long, which is important, because otherwise we
192 /// will often get single-move PVs when the search stops while failing high,
193 /// and a single-move PV means that we don't have a ponder move.
194
195 void TranspositionTable::extract_pv(const Position& pos, Move pv[]) {
196
197   int ply;
198   Position p(pos);
199   StateInfo st[100];
200
201   for (ply = 0; pv[ply] != MOVE_NONE; ply++)
202       p.do_move(pv[ply], st[ply]);
203
204   bool stop;
205   const TTEntry* tte;
206   for (stop = false, tte = retrieve(p.get_key());
207        tte && tte->move() != MOVE_NONE && !stop;
208        tte = retrieve(p.get_key()), ply++)
209   {
210       if (!move_is_legal(p, tte->move()))
211           break;
212       pv[ply] = tte->move();
213       p.do_move(pv[ply], st[ply]);
214       for (int j = 0; j < ply; j++)
215           if (st[j].key == p.get_key()) stop = true;
216   }
217   pv[ply] = MOVE_NONE;
218 }
219
220
221 /// TranspositionTable::full() returns the permill of all transposition table
222 /// entries which have received at least one write during the current search.
223 /// It is used to display the "info hashfull ..." information in UCI.
224
225 int TranspositionTable::full() const {
226
227   double N = double(size) * 4.0;
228   return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
229 }