diff --git a/README.md b/README.md index 8285a2e..b1b1ef9 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ terminal). | Option | Default | Meaning | | --- | --- | --- | -| Hash | 64 | Tree memory in MB. A node takes 20 bytes. | +| Hash | 64 | Tree memory in MB. A node takes 24 bytes. | | Threads | 1 | Only 1 for now. | | MoveOverhead | 20 | Milliseconds kept back per move. | | CPuct | 160 | Exploration constant x100. | diff --git a/src/mcts.cpp b/src/mcts.cpp index fd51f11..070157e 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -373,7 +373,13 @@ u32 Searcher::select_child(u32 idx) const { const float n = float(std::max(node.visits, 1)); // The root, where the move is chosen, explores more. const float base_cpuct = idx == Tree::root() ? params.root_cpuct : params.cpuct; - const float cpuct = base_cpuct * (1.0f + std::log((n + params.cpuct_visit_scale) / params.cpuct_visit_scale)); + float cpuct = base_cpuct * (1.0f + std::log((n + params.cpuct_visit_scale) / params.cpuct_visit_scale)); + // A node whose results spread widely explores more, a settled one less. + if (node.visits > 1) { + float frac = std::sqrt(node.variance()) / params.cpuct_var_scale; + frac += (1.0f - frac) / (1.0f + params.cpuct_var_warmup * float(node.visits)); + cpuct *= 1.0f + params.cpuct_var_weight * (frac - 1.0f); + } const float explore = cpuct * std::sqrt(n); u32 best = node.first_child; @@ -443,6 +449,7 @@ void Searcher::playout() { Node& node = tree_[path_[i]]; node.visits++; node.q += (value - node.q) / float(node.visits); + node.sq_q += (value * value - node.sq_q) / float(node.visits); if (i) butterfly_.update(root_pos_.stm ^ int((i - 1) & 1), Move::from_raw(node.move), bonus); } diff --git a/src/mcts.h b/src/mcts.h index 229b026..18d0ee8 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -19,18 +19,20 @@ enum NodeState : u8 { TERMINAL_DRAW, }; -// 20 bytes. Children of a node sit next to each other in the arena, so a node +// 24 bytes. Children of a node sit next to each other in the arena, so a node // only needs the index of its first child. struct Node { u32 first_child = 0; u32 visits = 0; float q = 0.0f; // running mean, from the view of the side that played `move` + float sq_q = 0.0f; // running mean of the squared results, for the variance of Q u16 move = 0; u16 policy = 0; // prior scaled to 0..65535 u8 num_children = 0; u8 state = UNEXPANDED; float prior() const { return float(policy) * (1.0f / 65535.0f); } + float variance() const { return std::max(sq_q - q * q, 0.0f); } }; class Tree { @@ -102,6 +104,13 @@ struct SearchParams { // cpuct is multiplied by 1 + ln((N + scale) / scale) for a node with N visits // (Monty: 64 x 128). float cpuct_visit_scale = 8192.0f; + // cpuct grows with the standard deviation of a node's Q (Monty's + // cpuct_var_scale, _weight, _warmup): x(1 + weight * (frac - 1)) with + // frac = stddev / scale, pulled towards 1 while the node has few visits. + // Monty's scale 0.2710 is for Q in [0, 1]; doubled for Rigby's [-1, 1]. + float cpuct_var_scale = 0.5420f; + float cpuct_var_weight = 0.8462f; + float cpuct_var_warmup = 0.4988f; float fpu_reduction = 0.30f; float policy_temperature = 1.00f; i64 move_overhead = 20;