pub struct MctsTreeNode<'a, T: Board>(pub NodeRef<'a, MctsNode<T>>);
Tuple Fields§
§0: NodeRef<'a, MctsNode<T>>
Implementations§
Source§impl<'a, T: Board> MctsTreeNode<'a, T>
impl<'a, T: Board> MctsTreeNode<'a, T>
Sourcepub fn get_best_child(&self) -> Option<MctsTreeNode<'a, T>>
pub fn get_best_child(&self) -> Option<MctsTreeNode<'a, T>>
Returns the child of the given node that is considered the most promising, based on win rate.
Examples found in repository?
examples/tic_tac_toe.rs (line 31)
7fn main() {
8 // Create a new Tic-Tac-Toe board
9 let board = TicTacToeBoard::default();
10
11 // Create a new MCTS search instance
12 let mut mcts = MonteCarloTreeSearch::builder(board)
13 .with_alpha_beta_pruning(false)
14 .with_random_generator(CustomNumberGenerator::default())
15 .build();
16
17 // Run the search for 20,000 iterations
18 mcts.iterate_n_times(20000);
19
20 // Print the chances
21 let root = mcts.get_root();
22 for node in root.children() {
23 println!(
24 "Move: {:?} = {:.2?}%",
25 node.value().prev_move,
26 node.value().wins_rate() * 100.0
27 );
28 }
29
30 // Get the most promising move
31 let best_move_node = root.get_best_child().unwrap();
32 let best_move = best_move_node.value().prev_move;
33
34 println!("The best move is: {:?}", best_move);
35 assert_eq!(best_move, Some(4));
36}
Methods from Deref<Target = NodeRef<'a, MctsNode<T>>>§
Sourcepub fn value(&self) -> &'a T
pub fn value(&self) -> &'a T
Returns the value of this node.
Examples found in repository?
examples/tic_tac_toe.rs (line 25)
7fn main() {
8 // Create a new Tic-Tac-Toe board
9 let board = TicTacToeBoard::default();
10
11 // Create a new MCTS search instance
12 let mut mcts = MonteCarloTreeSearch::builder(board)
13 .with_alpha_beta_pruning(false)
14 .with_random_generator(CustomNumberGenerator::default())
15 .build();
16
17 // Run the search for 20,000 iterations
18 mcts.iterate_n_times(20000);
19
20 // Print the chances
21 let root = mcts.get_root();
22 for node in root.children() {
23 println!(
24 "Move: {:?} = {:.2?}%",
25 node.value().prev_move,
26 node.value().wins_rate() * 100.0
27 );
28 }
29
30 // Get the most promising move
31 let best_move_node = root.get_best_child().unwrap();
32 let best_move = best_move_node.value().prev_move;
33
34 println!("The best move is: {:?}", best_move);
35 assert_eq!(best_move, Some(4));
36}
Sourcepub fn prev_sibling(&self) -> Option<NodeRef<'a, T>>
pub fn prev_sibling(&self) -> Option<NodeRef<'a, T>>
Returns the previous sibling of this node.
Sourcepub fn next_sibling(&self) -> Option<NodeRef<'a, T>>
pub fn next_sibling(&self) -> Option<NodeRef<'a, T>>
Returns the next sibling of this node.
Sourcepub fn first_child(&self) -> Option<NodeRef<'a, T>>
pub fn first_child(&self) -> Option<NodeRef<'a, T>>
Returns the first child of this node.
Sourcepub fn last_child(&self) -> Option<NodeRef<'a, T>>
pub fn last_child(&self) -> Option<NodeRef<'a, T>>
Returns the last child of this node.
Sourcepub fn has_siblings(&self) -> bool
pub fn has_siblings(&self) -> bool
Returns true if this node has siblings.
Sourcepub fn has_children(&self) -> bool
pub fn has_children(&self) -> bool
Returns true if this node has children.
Sourcepub fn prev_siblings(&self) -> PrevSiblings<'a, T>
pub fn prev_siblings(&self) -> PrevSiblings<'a, T>
Returns an iterator over previous siblings.
Sourcepub fn next_siblings(&self) -> NextSiblings<'a, T>
pub fn next_siblings(&self) -> NextSiblings<'a, T>
Returns an iterator over next siblings.
Sourcepub fn first_children(&self) -> FirstChildren<'a, T>
pub fn first_children(&self) -> FirstChildren<'a, T>
Returns an iterator over first children.
Sourcepub fn last_children(&self) -> LastChildren<'a, T>
pub fn last_children(&self) -> LastChildren<'a, T>
Returns an iterator over last children.
Sourcepub fn children(&self) -> Children<'a, T>
pub fn children(&self) -> Children<'a, T>
Returns an iterator over children.
Examples found in repository?
examples/tic_tac_toe.rs (line 22)
7fn main() {
8 // Create a new Tic-Tac-Toe board
9 let board = TicTacToeBoard::default();
10
11 // Create a new MCTS search instance
12 let mut mcts = MonteCarloTreeSearch::builder(board)
13 .with_alpha_beta_pruning(false)
14 .with_random_generator(CustomNumberGenerator::default())
15 .build();
16
17 // Run the search for 20,000 iterations
18 mcts.iterate_n_times(20000);
19
20 // Print the chances
21 let root = mcts.get_root();
22 for node in root.children() {
23 println!(
24 "Move: {:?} = {:.2?}%",
25 node.value().prev_move,
26 node.value().wins_rate() * 100.0
27 );
28 }
29
30 // Get the most promising move
31 let best_move_node = root.get_best_child().unwrap();
32 let best_move = best_move_node.value().prev_move;
33
34 println!("The best move is: {:?}", best_move);
35 assert_eq!(best_move, Some(4));
36}
Sourcepub fn traverse(&self) -> Traverse<'a, T>
pub fn traverse(&self) -> Traverse<'a, T>
Returns an iterator which traverses the subtree starting at this node.
Sourcepub fn descendants(&self) -> Descendants<'a, T>
pub fn descendants(&self) -> Descendants<'a, T>
Returns an iterator over this node and its descendants.
Trait Implementations§
Source§impl<'a, T: Board> Deref for MctsTreeNode<'a, T>
impl<'a, T: Board> Deref for MctsTreeNode<'a, T>
Source§impl<'a, T: Board> DerefMut for MctsTreeNode<'a, T>
impl<'a, T: Board> DerefMut for MctsTreeNode<'a, T>
Auto Trait Implementations§
impl<'a, T> Freeze for MctsTreeNode<'a, T>
impl<'a, T> RefUnwindSafe for MctsTreeNode<'a, T>
impl<'a, T> Send for MctsTreeNode<'a, T>
impl<'a, T> Sync for MctsTreeNode<'a, T>
impl<'a, T> Unpin for MctsTreeNode<'a, T>
impl<'a, T> UnwindSafe for MctsTreeNode<'a, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more