Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e7f2bfbf18 |
No files matched your search
+15
-18
@@ -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;
|
||||
@@ -620,7 +622,7 @@ u32 Searcher::select_edge(u32 idx) 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.
|
||||
const float q = e.visits ? visited_edge_q(e) : fpu;
|
||||
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;
|
||||
@@ -638,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));
|
||||
}
|
||||
@@ -650,7 +652,7 @@ void Searcher::playout() {
|
||||
edge_path_.clear();
|
||||
|
||||
// How the playout ended; it decides what happens to the last node.
|
||||
enum Leaf { NEW_NODE, TERMINAL, NOT_EXPANDED, PATH_DRAW, STOPPED } leaf;
|
||||
enum Leaf { NEW_NODE, TERMINAL, NOT_EXPANDED, PATH_DRAW, DEPTH_LIMIT } leaf;
|
||||
u32 idx = tree_.root();
|
||||
path_.push_back(idx);
|
||||
int depth = 0;
|
||||
@@ -712,24 +714,17 @@ void Searcher::playout() {
|
||||
}
|
||||
idx = edge.child;
|
||||
path_.push_back(idx);
|
||||
// Stop rule (KataGo, plan variant 5a): a child that already has more
|
||||
// visits than this edge (other move orders reached it) is ahead of
|
||||
// what this edge needs. The playout ends there: the edge counts the
|
||||
// visit with the child's current value, and the child is left as it
|
||||
// is. In a tree the two counts are equal and this never happens. The
|
||||
// depth limit ends a playout the same way.
|
||||
if (tree_.node(idx).visits > edge.visits || depth >= MAX_PLAYOUT_DEPTH) {
|
||||
if (depth >= MAX_PLAYOUT_DEPTH) {
|
||||
value = -tree_.node(idx).q;
|
||||
leaf = STOPPED;
|
||||
leaf = DEPTH_LIMIT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// The last node. A new node already has its one visit (N = base = 1); a
|
||||
// terminal node and a node that could not be expanded count one more visit
|
||||
// of their own value (q = u stays). A path draw has no node, and a node
|
||||
// where the playout stopped is left as it is: only the edge into it counts
|
||||
// the visit.
|
||||
// of their own value (q = u stays). A path draw has no node, and a node at
|
||||
// the depth limit is left as it is: only the edge into it counts the visit.
|
||||
if (leaf == TERMINAL || leaf == NOT_EXPANDED) {
|
||||
Node& node = tree_.node(path_.back());
|
||||
node.visits++;
|
||||
@@ -751,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]);
|
||||
@@ -979,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
|
||||
@@ -1039,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);
|
||||
@@ -1084,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]) {
|
||||
|
||||
+15
-3
@@ -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
|
||||
@@ -366,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