Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e7f2bfbf18 |
No files matched your search
+12
-27
@@ -486,6 +486,8 @@ 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");
|
||||
legal_seen[it - legal.begin()] = true;
|
||||
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 (!e.child && e.q != 0.0f) fail(idx, pos, "edge q without a child");
|
||||
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");
|
||||
edge_visits += e.visits;
|
||||
@@ -592,27 +594,9 @@ bool Searcher::expand(u32 idx, const Position& pos) {
|
||||
return true;
|
||||
}
|
||||
|
||||
u32 Searcher::select_edge(u32 idx, const Position& pos) const {
|
||||
u32 Searcher::select_edge(u32 idx) const {
|
||||
const Node& node = tree_.node(idx);
|
||||
|
||||
// Repetitions of this path: a child whose position already occurred on
|
||||
// this playout's path or in the game, with the same side to move and
|
||||
// within the reversible window, would be a draw if chosen now, whatever
|
||||
// its Q says about other paths. Such an edge scores Q = 0. The window
|
||||
// keys are those history_ holds 3, 5, ... plies before `pos` (4, 6, ...
|
||||
// before the child), up to pos.halfmove. A 64-bit filter skips the exact
|
||||
// search for most children.
|
||||
const int n_hist = int(history_.size());
|
||||
const int max_back = std::min(pos.halfmove, n_hist - 1);
|
||||
u64 window_filter = 0;
|
||||
for (int back = 3; back <= max_back; back += 2) window_filter |= 1ULL << (history_[n_hist - 1 - back] & 63);
|
||||
const auto repeats = [&](u64 key) {
|
||||
if (!(window_filter & (1ULL << (key & 63)))) return false;
|
||||
for (int back = 3; back <= max_back; back += 2)
|
||||
if (history_[n_hist - 1 - back] == key) return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
// node.q is from the view of the side that moved into this node, so the side
|
||||
// to move here sees -q. Visits are N(n), the node's own evaluation included.
|
||||
const float parent_q = node.visits ? -node.q : 0.0f;
|
||||
@@ -638,8 +622,7 @@ u32 Searcher::select_edge(u32 idx, const Position& pos) const {
|
||||
const u32 c = node.first_edge + i;
|
||||
const Edge& e = tree_.edge(c);
|
||||
// The child's Q is from the view of the side to move here.
|
||||
float q = e.visits ? visited_edge_q(e) : fpu;
|
||||
if (window_filter && e.child && repeats(tree_.node(e.child).key)) q = 0.0f;
|
||||
const float q = e.visits ? cached_edge_q(e) : fpu;
|
||||
const float score = q + explore * e.prior() / float(1 + e.visits);
|
||||
if (score > best_score) {
|
||||
best_score = score;
|
||||
@@ -657,7 +640,7 @@ void Searcher::recompute_q(Node& node) const {
|
||||
for (u32 i = 0; i < node.num_edges; i++) {
|
||||
const Edge& e = tree_.edge(node.first_edge + i);
|
||||
const u32 w = e.visits - e.draws;
|
||||
if (w) sum -= double(w) * double(tree_.node(e.child).q);
|
||||
if (w) sum -= double(w) * double(e.q);
|
||||
}
|
||||
node.q = float(sum / double(node.visits));
|
||||
}
|
||||
@@ -690,7 +673,7 @@ void Searcher::playout() {
|
||||
break;
|
||||
}
|
||||
|
||||
const u32 e = select_edge(idx, pos);
|
||||
const u32 e = select_edge(idx);
|
||||
Edge& edge = tree_.edge(e);
|
||||
pos.make_move(Move::from_raw(edge.move));
|
||||
history_.push_back(pos.key);
|
||||
@@ -763,6 +746,8 @@ void Searcher::playout() {
|
||||
Edge& e = tree_.edge(edge_path_[j]);
|
||||
e.visits++;
|
||||
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);
|
||||
|
||||
Node& node = tree_.node(path_[j]);
|
||||
@@ -991,8 +976,8 @@ struct NodeSnapshot {
|
||||
u32 base;
|
||||
u8 state;
|
||||
u16 check;
|
||||
// (move, policy, visits, draws, child's key or 0) per edge
|
||||
std::vector<std::tuple<u16, u16, u32, u32, u64>> edges;
|
||||
// (move, policy, visits, draws, child's key or 0, cached q) per edge
|
||||
std::vector<std::tuple<u16, u16, u32, u32, u64, float>> edges;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -1051,7 +1036,7 @@ void Searcher::self_test() {
|
||||
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++) {
|
||||
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);
|
||||
s.edges.emplace_back(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0, e.q);
|
||||
if (e.child && !seen[e.child]) {
|
||||
seen[e.child] = 1;
|
||||
stack.push_back(e.child);
|
||||
@@ -1096,7 +1081,7 @@ void Searcher::self_test() {
|
||||
if (n.num_edges && idx != tree_.root()) kept_min = std::min(kept_min, n.visits);
|
||||
for (u32 i = 0; i < n.num_edges; 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) !=
|
||||
if (std::make_tuple(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0, e.q) !=
|
||||
s.edges[i])
|
||||
self_test_failed("edge changed");
|
||||
if (e.child && !seen[e.child]) {
|
||||
|
||||
+17
-6
@@ -41,12 +41,17 @@ struct Edge {
|
||||
u32 child = 0; // node index, 0 = not linked yet
|
||||
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
|
||||
// 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 policy = 0; // prior scaled to 0..65535
|
||||
|
||||
float prior() const { return float(policy) * (1.0f / 65535.0f); }
|
||||
};
|
||||
static_assert(sizeof(Edge) == 16);
|
||||
static_assert(sizeof(Edge) == 20);
|
||||
|
||||
// A position, created (and evaluated) when search first reaches it.
|
||||
struct Node {
|
||||
@@ -71,8 +76,9 @@ u16 position_check(const Position& pos);
|
||||
// check, about 80 bits.
|
||||
class Tree {
|
||||
public:
|
||||
// Edges per node the arenas are sized for: the edge arena gets 128 of
|
||||
// every 160 bytes, the node arena 32. Measured in step 1 of the MCGS plan
|
||||
// Edges per node the arenas are sized for: with 20-byte edges the edge
|
||||
// arena gets 160 of every 192 bytes, the node arena 32 (plus the table).
|
||||
// Measured in step 1 of the MCGS plan
|
||||
// (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
|
||||
// endgames, and 3-9 right after collections (pruned nodes keep their node
|
||||
@@ -331,9 +337,8 @@ private:
|
||||
// Second visit: creates the edges with their priors. False when the
|
||||
// arena is full (a collection is then scheduled).
|
||||
bool expand(u32 idx, const Position& pos);
|
||||
// The edge PUCT picks at node `idx`, whose position is `pos` (the last
|
||||
// entry of history_).
|
||||
u32 select_edge(u32 idx, const Position& pos) const;
|
||||
// The edge PUCT picks at node `idx`.
|
||||
u32 select_edge(u32 idx) const;
|
||||
// A node's Q from its own value and its children's (see Edge).
|
||||
void recompute_q(Node& node) const;
|
||||
bool is_repetition(const Position& pos) const;
|
||||
@@ -367,6 +372,12 @@ private:
|
||||
}
|
||||
// The same, 0 when the edge is unvisited.
|
||||
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;
|
||||
std::vector<Move> principal_variation() const;
|
||||
void draw_dashboard(bool done);
|
||||
|
||||
Reference in new issue
Block a user