diff --git a/src/mcts.cpp b/src/mcts.cpp index fd51f11..b2570e3 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -318,7 +318,32 @@ float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) { return eval::value(pos); } -bool Searcher::expand(u32 idx, const Position& pos) { +float Searcher::policy_temperature(int depth) const { + const float d = float(depth + 1); // Monty counts the root as depth 1 + return params.policy_temperature * (1.0f - params.pst_base + std::pow(d - params.pst_root, -params.pst_power)); +} + +void Searcher::priors(const Position& pos, const MoveList& moves, int depth, float* out) const { + // The butterfly history of each move shifts its logit. The priors are a + // snapshot: later history updates do not change them. + float bias[256]; + for (int i = 0; i < moves.size(); i++) + bias[i] = float(butterfly_.get(pos.stm, moves[i])) / ButterflyHistory::LOGIT_SCALE; + eval::policy(pos, moves, policy_temperature(depth), bias, out); +} + +void Searcher::relabel(u32 idx, const Position& pos, int depth) { + const Node& node = tree_[idx]; + if (node.state != EXPANDED) return; + MoveList moves; + for (u32 i = 0; i < node.num_children; i++) moves.add(Move::from_raw(tree_[node.first_child + i].move)); + float p[256]; + priors(pos, moves, depth, p); + for (u32 i = 0; i < node.num_children; i++) + tree_[node.first_child + i].policy = u16(std::lround(std::clamp(p[i], 0.0f, 1.0f) * 65535.0f)); +} + +bool Searcher::expand(u32 idx, const Position& pos, int depth) { Node& node = tree_[idx]; MoveList moves; @@ -332,20 +357,14 @@ bool Searcher::expand(u32 idx, const Position& pos) { return false; } - // The butterfly history of each move shifts its logit. The priors are a - // snapshot: later history updates do not change them. - float bias[256]; - for (int i = 0; i < moves.size(); i++) - bias[i] = float(butterfly_.get(pos.stm, moves[i])) / ButterflyHistory::LOGIT_SCALE; - - float priors[256]; - eval::policy(pos, moves, params.policy_temperature, bias, priors); + float p[256]; + priors(pos, moves, depth, p); for (int i = 0; i < moves.size(); i++) { Node& child = tree_[first + i]; child = Node{}; child.move = moves[i].raw(); - child.policy = u16(std::lround(std::clamp(priors[i], 0.0f, 1.0f) * 65535.0f)); + child.policy = u16(std::lround(std::clamp(p[i], 0.0f, 1.0f) * 65535.0f)); } node.first_child = first; @@ -418,7 +437,7 @@ void Searcher::playout() { value = evaluate_leaf(idx, pos, depth); break; } - if (!expand(idx, pos)) { + if (!expand(idx, pos, depth)) { value = eval::value(pos); break; } @@ -574,10 +593,22 @@ SearchResult Searcher::search(const Position& root, const std::vector& hist // The root needs its children before the first playout, so that even a // single playout leaves a move to play. - if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) { + if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root, 0)) { tree_.clear(); gc_pending_ = false; - expand(Tree::root(), root); + expand(Tree::root(), root, 0); + } + + // A reused root and its children were expanded deeper in the old tree, + // with a lower policy temperature: give them the one for their new depth. + if (reused) { + relabel(Tree::root(), root, 0); + const Node& r = tree_[Tree::root()]; + for (u32 i = 0; i < r.num_children; i++) { + Position child = root; + child.make_move(Move::from_raw(tree_[r.first_child + i].move)); + relabel(r.first_child + i, child, 1); + } } // Root move visits brought along by tree reuse, for smart pruning. The diff --git a/src/mcts.h b/src/mcts.h index 229b026..f722a26 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -103,7 +103,14 @@ struct SearchParams { // (Monty: 64 x 128). float cpuct_visit_scale = 8192.0f; float fpu_reduction = 0.30f; + // Multiplies the depth-dependent policy softmax temperature below. float policy_temperature = 1.00f; + // Policy softmax temperature by depth, Monty's base PST (root = depth 1): + // 1 - base + (depth - root)^-power, about 2.80 at the root, 1.35 one ply + // down, 1.12 at two plies and 0.90 deep in the tree. + float pst_base = 0.0960f; + float pst_root = 0.3349f; + float pst_power = 1.5777f; i64 move_overhead = 20; }; @@ -180,8 +187,16 @@ private: // First visit of a node: settles terminal states, otherwise evaluates. float evaluate_leaf(u32 idx, const Position& pos, int depth); // Second visit: creates the children with their priors. False when the - // arena is full (a collection is then scheduled). - bool expand(u32 idx, const Position& pos); + // arena is full (a collection is then scheduled). `depth` is the node's + // distance from the root and sets the policy temperature. + bool expand(u32 idx, const Position& pos, int depth); + // Recomputes the priors of an expanded node's children for its depth (and + // the current butterfly history), for nodes that tree reuse moved closer + // to the root. + void relabel(u32 idx, const Position& pos, int depth); + float policy_temperature(int depth) const; + // Writes the priors of `moves` at `pos` for a node at `depth`. + void priors(const Position& pos, const MoveList& moves, int depth, float* out) const; u32 select_child(u32 idx) const; bool is_repetition(const Position& pos) const; // Finds the node for `root` in the previous tree and makes it the root.