From 3374c9ff35651388d49db704f3f76e6f736d07d6 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 23 Jan 2022 22:03:24 +0100 Subject: [PATCH] tree --- src/lib.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 83d2737..0741f1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +//! https://www.baeldung.com/java-monte-carlo-tree-search + #![allow(dead_code)] use bumpalo::collections::Vec; @@ -85,7 +87,7 @@ mod mcts { } fn expand_node(node: &Node) { - todo!() + todo!("next") } fn simulate_random_playout(node: &Node<'_, S>) -> u64 { @@ -95,6 +97,30 @@ mod mcts { fn back_propagation(node: &Node<'_, S>, playout_result: u64) { todo!() } + + mod uct { + use crate::Node; + + pub fn uct(total_visit: u64, win_score: u64, node_visit: u64) -> u64 { + if node_visit == 0 { + return u64::MAX; + } + + let num = (win_score / node_visit) as f64 + + std::f64::consts::SQRT_2 + * f64::sqrt((total_visit as f64).ln() / node_visit as f64); + + num as u64 + } + + pub fn find_best_node_with_uct(node: &Node) -> Option<&Node> { + let parent_visit_count = node.visited; + + node.children + .iter() + .max_by_key(|n| uct(parent_visit_count, n.won, n.visited)) + } + } } #[cfg(test)]