mirror of
https://github.com/Noratrieb/monte-carlo-tree-search.git
synced 2026-01-16 08:15:05 +01:00
tree
This commit is contained in:
parent
bdd90a41c6
commit
3374c9ff35
1 changed files with 27 additions and 1 deletions
28
src/lib.rs
28
src/lib.rs
|
|
@ -1,3 +1,5 @@
|
||||||
|
//! https://www.baeldung.com/java-monte-carlo-tree-search
|
||||||
|
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use bumpalo::collections::Vec;
|
use bumpalo::collections::Vec;
|
||||||
|
|
@ -85,7 +87,7 @@ mod mcts {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_node<S>(node: &Node<S>) {
|
fn expand_node<S>(node: &Node<S>) {
|
||||||
todo!()
|
todo!("next")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn simulate_random_playout<S>(node: &Node<'_, S>) -> u64 {
|
fn simulate_random_playout<S>(node: &Node<'_, S>) -> u64 {
|
||||||
|
|
@ -95,6 +97,30 @@ mod mcts {
|
||||||
fn back_propagation<S>(node: &Node<'_, S>, playout_result: u64) {
|
fn back_propagation<S>(node: &Node<'_, S>, playout_result: u64) {
|
||||||
todo!()
|
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<S>(node: &Node<S>) -> Option<&Node<S>> {
|
||||||
|
let parent_visit_count = node.visited;
|
||||||
|
|
||||||
|
node.children
|
||||||
|
.iter()
|
||||||
|
.max_by_key(|n| uct(parent_visit_count, n.won, n.visited))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue