Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79fd017348 |
No files matched your search
+64
-12
@@ -259,6 +259,46 @@ 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.
|
||||||
@@ -486,8 +526,17 @@ 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 (!std::isfinite(e.q) || std::abs(e.q) > 1.0f) fail(idx, pos, "edge q out of range");
|
if (idx == root && e.visits) {
|
||||||
if (!e.child && e.q != 0.0f) fail(idx, pos, "edge q without a child");
|
// 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;
|
||||||
@@ -622,7 +671,7 @@ u32 Searcher::select_edge(u32 idx) const {
|
|||||||
const u32 c = node.first_edge + i;
|
const u32 c = node.first_edge + i;
|
||||||
const Edge& e = tree_.edge(c);
|
const Edge& e = tree_.edge(c);
|
||||||
// The child's Q is from the view of the side to move here.
|
// The child's Q is from the view of the side to move here.
|
||||||
const float q = e.visits ? cached_edge_q(e) : fpu;
|
const float q = e.visits ? visited_edge_q(e) : fpu;
|
||||||
const float score = q + explore * e.prior() / float(1 + e.visits);
|
const float score = q + explore * e.prior() / float(1 + e.visits);
|
||||||
if (score > best_score) {
|
if (score > best_score) {
|
||||||
best_score = score;
|
best_score = score;
|
||||||
@@ -640,7 +689,7 @@ void Searcher::recompute_q(Node& node) const {
|
|||||||
for (u32 i = 0; i < node.num_edges; i++) {
|
for (u32 i = 0; i < node.num_edges; i++) {
|
||||||
const Edge& e = tree_.edge(node.first_edge + i);
|
const Edge& e = tree_.edge(node.first_edge + i);
|
||||||
const u32 w = e.visits - e.draws;
|
const u32 w = e.visits - e.draws;
|
||||||
if (w) sum -= double(w) * double(e.q);
|
if (w) sum -= double(w) * double(tree_.node(e.child).q);
|
||||||
}
|
}
|
||||||
node.q = float(sum / double(node.visits));
|
node.q = float(sum / double(node.visits));
|
||||||
}
|
}
|
||||||
@@ -746,8 +795,6 @@ void Searcher::playout() {
|
|||||||
Edge& e = tree_.edge(edge_path_[j]);
|
Edge& e = tree_.edge(edge_path_[j]);
|
||||||
e.visits++;
|
e.visits++;
|
||||||
if (leaf == PATH_DRAW && j + 1 == edge_path_.size()) e.draws++;
|
if (leaf == PATH_DRAW && j + 1 == edge_path_.size()) e.draws++;
|
||||||
// The child is final for this playout (updated in the step before).
|
|
||||||
if (e.child) e.q = tree_.node(e.child).q;
|
|
||||||
butterfly_.update(root_pos_.stm ^ int(j & 1), Move::from_raw(e.move), bonus);
|
butterfly_.update(root_pos_.stm ^ int(j & 1), Move::from_raw(e.move), bonus);
|
||||||
|
|
||||||
Node& node = tree_.node(path_[j]);
|
Node& node = tree_.node(path_[j]);
|
||||||
@@ -868,10 +915,13 @@ 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;
|
||||||
@@ -976,8 +1026,8 @@ struct NodeSnapshot {
|
|||||||
u32 base;
|
u32 base;
|
||||||
u8 state;
|
u8 state;
|
||||||
u16 check;
|
u16 check;
|
||||||
// (move, policy, visits, draws, child's key or 0, cached q) per edge
|
// (move, policy, visits, draws, child's key or 0) per edge
|
||||||
std::vector<std::tuple<u16, u16, u32, u32, u64, float>> edges;
|
std::vector<std::tuple<u16, u16, u32, u32, u64>> edges;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -1036,7 +1086,7 @@ void Searcher::self_test() {
|
|||||||
NodeSnapshot s{n.key, n.visits, n.q, n.u, n.base, n.state, n.check, {}};
|
NodeSnapshot s{n.key, n.visits, n.q, n.u, n.base, n.state, n.check, {}};
|
||||||
for (u32 i = 0; i < n.num_edges; i++) {
|
for (u32 i = 0; i < n.num_edges; i++) {
|
||||||
const Edge& e = tree_.edge(n.first_edge + i);
|
const Edge& e = tree_.edge(n.first_edge + i);
|
||||||
s.edges.emplace_back(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0, e.q);
|
s.edges.emplace_back(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0);
|
||||||
if (e.child && !seen[e.child]) {
|
if (e.child && !seen[e.child]) {
|
||||||
seen[e.child] = 1;
|
seen[e.child] = 1;
|
||||||
stack.push_back(e.child);
|
stack.push_back(e.child);
|
||||||
@@ -1081,7 +1131,7 @@ void Searcher::self_test() {
|
|||||||
if (n.num_edges && idx != tree_.root()) kept_min = std::min(kept_min, n.visits);
|
if (n.num_edges && idx != tree_.root()) kept_min = std::min(kept_min, n.visits);
|
||||||
for (u32 i = 0; i < n.num_edges; i++) {
|
for (u32 i = 0; i < n.num_edges; i++) {
|
||||||
const Edge& e = tree_.edge(n.first_edge + i);
|
const Edge& e = tree_.edge(n.first_edge + i);
|
||||||
if (std::make_tuple(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0, e.q) !=
|
if (std::make_tuple(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0) !=
|
||||||
s.edges[i])
|
s.edges[i])
|
||||||
self_test_failed("edge changed");
|
self_test_failed("edge changed");
|
||||||
if (e.child && !seen[e.child]) {
|
if (e.child && !seen[e.child]) {
|
||||||
@@ -1149,14 +1199,16 @@ 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 " +
|
||||||
|
|||||||
+11
-15
@@ -41,17 +41,12 @@ struct Edge {
|
|||||||
u32 child = 0; // node index, 0 = not linked yet
|
u32 child = 0; // node index, 0 = not linked yet
|
||||||
u32 visits = 0; // N(n,a): how often search chose this move here
|
u32 visits = 0; // N(n,a): how often search chose this move here
|
||||||
u32 draws = 0; // D(n,a): of those, playouts that ended in a draw of the path after this move
|
u32 draws = 0; // D(n,a): of those, playouts that ended in a draw of the path after this move
|
||||||
// The child's Q as of the last playout through this edge (plan variant 5b).
|
|
||||||
// Selection and the parent's recompute read it instead of the child, so
|
|
||||||
// they only touch the parent's contiguous edges. It goes stale when the
|
|
||||||
// child is updated through another parent, until a playout passes here.
|
|
||||||
float q = 0.0f;
|
|
||||||
u16 move = 0;
|
u16 move = 0;
|
||||||
u16 policy = 0; // prior scaled to 0..65535
|
u16 policy = 0; // prior scaled to 0..65535
|
||||||
|
|
||||||
float prior() const { return float(policy) * (1.0f / 65535.0f); }
|
float prior() const { return float(policy) * (1.0f / 65535.0f); }
|
||||||
};
|
};
|
||||||
static_assert(sizeof(Edge) == 20);
|
static_assert(sizeof(Edge) == 16);
|
||||||
|
|
||||||
// A position, created (and evaluated) when search first reaches it.
|
// A position, created (and evaluated) when search first reaches it.
|
||||||
struct Node {
|
struct Node {
|
||||||
@@ -76,9 +71,8 @@ u16 position_check(const Position& pos);
|
|||||||
// check, about 80 bits.
|
// check, about 80 bits.
|
||||||
class Tree {
|
class Tree {
|
||||||
public:
|
public:
|
||||||
// Edges per node the arenas are sized for: with 20-byte edges the edge
|
// Edges per node the arenas are sized for: the edge arena gets 128 of
|
||||||
// arena gets 160 of every 192 bytes, the node arena 32 (plus the table).
|
// every 160 bytes, the node arena 32. Measured in step 1 of the MCGS plan
|
||||||
// Measured in step 1 of the MCGS plan
|
|
||||||
// (docs/mcgs-log.md, edges per visited node): 13 on average in 5 s
|
// (docs/mcgs-log.md, edges per visited node): 13 on average in 5 s
|
||||||
// searches without collections, 19-21 in middlegames, 3.6-5.5 in simple
|
// searches without collections, 19-21 in middlegames, 3.6-5.5 in simple
|
||||||
// endgames, and 3-9 right after collections (pruned nodes keep their node
|
// endgames, and 3-9 right after collections (pruned nodes keep their node
|
||||||
@@ -348,6 +342,14 @@ 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
|
||||||
@@ -372,12 +374,6 @@ private:
|
|||||||
}
|
}
|
||||||
// The same, 0 when the edge is unvisited.
|
// The same, 0 when the edge is unvisited.
|
||||||
float edge_q(const Edge& e) const { return e.visits ? visited_edge_q(e) : 0.0f; }
|
float edge_q(const Edge& e) const { return e.visits ? visited_edge_q(e) : 0.0f; }
|
||||||
// The same from the Q the edge last saw (selection and recompute).
|
|
||||||
static float cached_edge_q(const Edge& e) {
|
|
||||||
if (!e.draws) return e.q;
|
|
||||||
const u32 w = e.visits - e.draws;
|
|
||||||
return w ? float(w) / float(e.visits) * e.q : 0.0f;
|
|
||||||
}
|
|
||||||
void print_info() const;
|
void print_info() const;
|
||||||
std::vector<Move> principal_variation() const;
|
std::vector<Move> principal_variation() const;
|
||||||
void draw_dashboard(bool done);
|
void draw_dashboard(bool done);
|
||||||
|
|||||||
Reference in new issue
Block a user