Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e7c3ef64a |
No files matched your search
+2
-68
@@ -259,46 +259,6 @@ size_t Tree::compact(u32 new_root, u32 min_visits) {
|
|||||||
|
|
||||||
Searcher::Searcher() { set_hash(64); }
|
Searcher::Searcher() { set_hash(64); }
|
||||||
|
|
||||||
bool Searcher::root_child_drawn(const Position& child, const std::vector<u64>& keys) {
|
|
||||||
const int n = int(keys.size());
|
|
||||||
for (int back = 4; back <= child.halfmove && back < n; back += 2)
|
|
||||||
if (keys[n - 1 - back] == child.key) return true;
|
|
||||||
return child.halfmove >= 100 && !(child.in_check() && !has_legal_move(child));
|
|
||||||
}
|
|
||||||
|
|
||||||
int Searcher::reconcile_root_edges(const Position& root) {
|
|
||||||
// Draw counts on the root's edges were collected under other game
|
|
||||||
// histories (the root was an inner node then). Now the history is common
|
|
||||||
// to every path: a move into a repetition (or past the 50-move rule)
|
|
||||||
// draws on every path, any other move on none. A drawn edge keeps its
|
|
||||||
// visits, all of them draws; an edge that is no longer drawn loses the
|
|
||||||
// draw visits, and so does the root.
|
|
||||||
Node& r = tree_.node(tree_.root());
|
|
||||||
if (r.state != EXPANDED) return 0;
|
|
||||||
int changed = 0;
|
|
||||||
std::vector<u64> keys = history_;
|
|
||||||
for (u32 i = 0; i < r.num_edges; i++) {
|
|
||||||
Edge& e = tree_.edge(r.first_edge + i);
|
|
||||||
if (!e.visits) continue;
|
|
||||||
Position child = root;
|
|
||||||
child.make_move(Move::from_raw(e.move));
|
|
||||||
keys.push_back(child.key);
|
|
||||||
const bool drawn = root_child_drawn(child, keys);
|
|
||||||
keys.pop_back();
|
|
||||||
if (drawn && e.draws != e.visits) {
|
|
||||||
e.draws = e.visits;
|
|
||||||
changed++;
|
|
||||||
} else if (!drawn && e.draws) {
|
|
||||||
r.visits -= e.draws;
|
|
||||||
e.visits -= e.draws;
|
|
||||||
e.draws = 0;
|
|
||||||
changed++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (changed) recompute_q(r);
|
|
||||||
return changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Searcher::clear_tree(const Position& root) {
|
void Searcher::clear_tree(const Position& root) {
|
||||||
// The root is created like any node and evaluated once; its own value
|
// The root is created like any node and evaluated once; its own value
|
||||||
// takes part in its Q with weight 1, as at every other node.
|
// takes part in its Q with weight 1, as at every other node.
|
||||||
@@ -526,17 +486,6 @@ void Searcher::verify_tree(const std::vector<u64>& history, bool collected) cons
|
|||||||
if (it == legal.end() || legal_seen[it - legal.begin()]) fail(idx, pos, "illegal or repeated edge move");
|
if (it == legal.end() || legal_seen[it - legal.begin()]) fail(idx, pos, "illegal or repeated edge move");
|
||||||
legal_seen[it - legal.begin()] = true;
|
legal_seen[it - legal.begin()] = true;
|
||||||
if (e.draws > e.visits) fail(idx, pos, "edge with more draws than visits");
|
if (e.draws > e.visits) fail(idx, pos, "edge with more draws than visits");
|
||||||
if (idx == root && e.visits) {
|
|
||||||
// The game history is common to every path from the root, so a
|
|
||||||
// root edge is all draws or none.
|
|
||||||
Position child = pos;
|
|
||||||
child.make_move(Move::from_raw(e.move));
|
|
||||||
std::vector<u64> keys = history;
|
|
||||||
keys.push_back(child.key);
|
|
||||||
if (e.draws != (root_child_drawn(child, keys) ? e.visits : 0))
|
|
||||||
fail(idx, pos, "root edge " + pos.uci(Move::from_raw(e.move)) + " has " +
|
|
||||||
std::to_string(e.draws) + " draws of " + std::to_string(e.visits));
|
|
||||||
}
|
|
||||||
if (e.child >= tree_.nodes_used()) fail(idx, pos, "edge child out of range");
|
if (e.child >= tree_.nodes_used()) fail(idx, pos, "edge child out of range");
|
||||||
if (e.visits > e.draws && !e.child) fail(idx, pos, "edge with non-draw visits and no child");
|
if (e.visits > e.draws && !e.child) fail(idx, pos, "edge with non-draw visits and no child");
|
||||||
edge_visits += e.visits;
|
edge_visits += e.visits;
|
||||||
@@ -594,10 +543,6 @@ u32 Searcher::create_node(const Position& pos, bool is_root, float& value) {
|
|||||||
} else if (!is_root && pos.insufficient_material()) {
|
} else if (!is_root && pos.insufficient_material()) {
|
||||||
node.state = TERMINAL_DRAW;
|
node.state = TERMINAL_DRAW;
|
||||||
value = 0.0f;
|
value = 0.0f;
|
||||||
} else if (float q; value_hash_.probe(pos.key, q)) {
|
|
||||||
// A position searched before (dropped by a collection, or from an
|
|
||||||
// earlier search) brings its Q along.
|
|
||||||
value = q;
|
|
||||||
} else {
|
} else {
|
||||||
value = eval::value(pos);
|
value = eval::value(pos);
|
||||||
}
|
}
|
||||||
@@ -779,10 +724,6 @@ void Searcher::playout() {
|
|||||||
node.visits++;
|
node.visits++;
|
||||||
node.base++;
|
node.base++;
|
||||||
}
|
}
|
||||||
if (leaf == NEW_NODE || leaf == TERMINAL || leaf == NOT_EXPANDED) {
|
|
||||||
const Node& node = tree_.node(path_.back());
|
|
||||||
value_hash_.store(node.key, -node.q, node.visits);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Up the path: each edge counts the visit (and the draw), and its parent
|
// Up the path: each edge counts the visit (and the draw), and its parent
|
||||||
// node recomputes its Q from its children. `value` is from the view of
|
// node recomputes its Q from its children. `value` is from the view of
|
||||||
@@ -800,8 +741,6 @@ void Searcher::playout() {
|
|||||||
Node& node = tree_.node(path_[j]);
|
Node& node = tree_.node(path_[j]);
|
||||||
node.visits++;
|
node.visits++;
|
||||||
recompute_q(node);
|
recompute_q(node);
|
||||||
// The Q from the view of the side to move at the node.
|
|
||||||
value_hash_.store(node.key, -node.q, node.visits);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
iterations_++;
|
iterations_++;
|
||||||
@@ -915,13 +854,10 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
|||||||
// rests on the 80-bit key and check; the root is checked in full.
|
// rests on the 80-bit key and check; the root is checked in full.
|
||||||
if (reused && !root_edges_valid(root)) reused = 0;
|
if (reused && !root_edges_valid(root)) reused = 0;
|
||||||
if (!reused) clear_tree(root);
|
if (!reused) clear_tree(root);
|
||||||
const int reconciled = reused ? reconcile_root_edges(root) : 0;
|
|
||||||
#ifdef RIGBY_VERIFY_TREE
|
#ifdef RIGBY_VERIFY_TREE
|
||||||
if (reused) verify_tree(history_);
|
if (reused) verify_tree(history_);
|
||||||
#endif
|
#endif
|
||||||
if (print && !pretty && reused) uci_io::send("info string reused " + std::to_string(reused) + " nodes");
|
if (print && !pretty && reused) uci_io::send("info string reused " + std::to_string(reused) + " nodes");
|
||||||
if (print && !pretty && reconciled)
|
|
||||||
uci_io::send("info string reconciled " + std::to_string(reconciled) + " root edges");
|
|
||||||
iterations_ = 0;
|
iterations_ = 0;
|
||||||
depth_sum_ = 0;
|
depth_sum_ = 0;
|
||||||
seldepth_ = 0;
|
seldepth_ = 0;
|
||||||
@@ -1199,16 +1135,14 @@ void Searcher::self_test() {
|
|||||||
const size_t count = compare(before, false, pruned_max, kept_min);
|
const size_t count = compare(before, false, pruned_max, kept_min);
|
||||||
if (count != before.size() || tree_.nodes_used() != nodes_before || tree_.edges_used() != edges_before)
|
if (count != before.size() || tree_.nodes_used() != nodes_before || tree_.edges_used() != edges_before)
|
||||||
self_test_failed("reroot changed the graph");
|
self_test_failed("reroot changed the graph");
|
||||||
|
root_pos_ = pos;
|
||||||
|
verify_tree(history);
|
||||||
// The next collection (with room for everything) keeps exactly what the
|
// The next collection (with room for everything) keeps exactly what the
|
||||||
// new root reaches, unchanged.
|
// new root reaches, unchanged.
|
||||||
tree_.collect_garbage(tree_.node_capacity(), tree_.edge_capacity());
|
tree_.collect_garbage(tree_.node_capacity(), tree_.edge_capacity());
|
||||||
const size_t kept = compare(before, false, pruned_max, kept_min);
|
const size_t kept = compare(before, false, pruned_max, kept_min);
|
||||||
if (kept != before.size() || kept != tree_.nodes_used() - 1)
|
if (kept != before.size() || kept != tree_.nodes_used() - 1)
|
||||||
self_test_failed("collection after a reroot kept the wrong nodes");
|
self_test_failed("collection after a reroot kept the wrong nodes");
|
||||||
// As search does after reuse: the root's edges follow the game history.
|
|
||||||
root_pos_ = pos;
|
|
||||||
history_ = history;
|
|
||||||
reconcile_root_edges(pos);
|
|
||||||
verify_tree(history, true);
|
verify_tree(history, true);
|
||||||
uci_io::send("info string verify (b) reroot " + std::string(fen) + ": " + std::to_string(nodes_before - 1) +
|
uci_io::send("info string verify (b) reroot " + std::string(fen) + ": " + std::to_string(nodes_before - 1) +
|
||||||
" nodes, " + std::to_string(count) + " reachable from the new root, collected to " +
|
" nodes, " + std::to_string(count) + " reachable from the new root, collected to " +
|
||||||
|
|||||||
+5
-51
@@ -175,42 +175,6 @@ private:
|
|||||||
std::array<std::array<std::array<i16, 64>, 64>, 2> table_{};
|
std::array<std::array<std::array<i16, 64>, 64>, 2> table_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Q of positions searched before, from the view of the side to move there
|
|
||||||
// (as in Monty and Vine). Every node of a playout's path stores its Q here;
|
|
||||||
// a leaf whose position is found takes that value instead of a fresh
|
|
||||||
// evaluation. Transpositions are common: 37-85% of leaf evaluations in a
|
|
||||||
// 5-second search repeat a position already evaluated in the same search.
|
|
||||||
class ValueHash {
|
|
||||||
public:
|
|
||||||
void resize(size_t entries) {
|
|
||||||
table_.assign(std::max<size_t>(entries, 1), Entry{});
|
|
||||||
table_.shrink_to_fit();
|
|
||||||
}
|
|
||||||
void clear() { std::fill(table_.begin(), table_.end(), Entry{}); }
|
|
||||||
bool probe(u64 key, float& q) const {
|
|
||||||
const Entry& e = table_[key % table_.size()];
|
|
||||||
if (!e.visits || e.check != u32(key >> 32)) return false;
|
|
||||||
q = e.q;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// A different position always replaces the entry; the same one only with
|
|
||||||
// at least as many visits.
|
|
||||||
void store(u64 key, float q, u32 visits) {
|
|
||||||
Entry& e = table_[key % table_.size()];
|
|
||||||
if (e.check == u32(key >> 32) && visits < e.visits) return;
|
|
||||||
e = Entry{u32(key >> 32), visits, q};
|
|
||||||
}
|
|
||||||
static constexpr size_t ENTRY_BYTES = 12;
|
|
||||||
|
|
||||||
private:
|
|
||||||
struct Entry {
|
|
||||||
u32 check = 0;
|
|
||||||
u32 visits = 0;
|
|
||||||
float q = 0.0f;
|
|
||||||
};
|
|
||||||
std::vector<Entry> table_;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct SearchLimits {
|
struct SearchLimits {
|
||||||
u64 nodes = 0;
|
u64 nodes = 0;
|
||||||
i64 movetime = 0;
|
i64 movetime = 0;
|
||||||
@@ -250,11 +214,11 @@ public:
|
|||||||
Searcher();
|
Searcher();
|
||||||
|
|
||||||
void set_hash(size_t mb) {
|
void set_hash(size_t mb) {
|
||||||
// A 32nd of the memory goes to the value hash, the rest to the graph
|
// All of it goes to the graph (its arenas, split by
|
||||||
// (its arenas, split by Tree::EDGES_PER_NODE, and its table).
|
// Tree::EDGES_PER_NODE, and its table). The graph shares a position's
|
||||||
const size_t bytes = mb * 1024 * 1024;
|
// value between move orders, which a separate value hash did in the
|
||||||
value_hash_.resize(bytes / 32 / ValueHash::ENTRY_BYTES);
|
// tree (plan variant 5c removed it).
|
||||||
tree_.resize_bytes(bytes - bytes / 32);
|
tree_.resize_bytes(mb * 1024 * 1024);
|
||||||
tree_valid_ = false;
|
tree_valid_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,7 +226,6 @@ public:
|
|||||||
void new_game() {
|
void new_game() {
|
||||||
tree_valid_ = false;
|
tree_valid_ = false;
|
||||||
butterfly_.clear();
|
butterfly_.clear();
|
||||||
value_hash_.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Synchronous search. `history` holds the keys of every position that led to
|
// Synchronous search. `history` holds the keys of every position that led to
|
||||||
@@ -283,7 +246,6 @@ private:
|
|||||||
// Kept between moves of a game, like the tree.
|
// Kept between moves of a game, like the tree.
|
||||||
ButterflyHistory butterfly_;
|
ButterflyHistory butterfly_;
|
||||||
Position root_pos_;
|
Position root_pos_;
|
||||||
ValueHash value_hash_;
|
|
||||||
std::vector<u64> history_;
|
std::vector<u64> history_;
|
||||||
size_t root_history_size_ = 0;
|
size_t root_history_size_ = 0;
|
||||||
// The nodes of a playout, from the root, and the edges it took
|
// The nodes of a playout, from the root, and the edges it took
|
||||||
@@ -342,14 +304,6 @@ private:
|
|||||||
size_t reuse_tree(const Position& root);
|
size_t reuse_tree(const Position& root);
|
||||||
// True when the root node's edges are exactly the legal moves of `root`.
|
// True when the root node's edges are exactly the legal moves of `root`.
|
||||||
bool root_edges_valid(const Position& root) const;
|
bool root_edges_valid(const Position& root) const;
|
||||||
// After reuse the game history is common to every path, so whether a
|
|
||||||
// root move is a draw by repetition or the 50-move rule is exact. Makes
|
|
||||||
// each root edge all draws or no draws accordingly (plan 3.6.7). Returns
|
|
||||||
// the number of edges changed.
|
|
||||||
int reconcile_root_edges(const Position& root);
|
|
||||||
// Whether `child`, reached from the root, is a draw of the game history
|
|
||||||
// `keys` (which ends with the child's key).
|
|
||||||
static bool root_child_drawn(const Position& child, const std::vector<u64>& keys);
|
|
||||||
// Empties the graph, leaving an evaluated, unexpanded root for `root`.
|
// Empties the graph, leaving an evaluated, unexpanded root for `root`.
|
||||||
void clear_tree(const Position& root);
|
void clear_tree(const Position& root);
|
||||||
#ifdef RIGBY_VERIFY_TREE
|
#ifdef RIGBY_VERIFY_TREE
|
||||||
|
|||||||
Reference in new issue
Block a user