MctsTreeNode

Struct MctsTreeNode 

Source
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>

Source

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>>>§

Source

pub fn id(&self) -> NodeId

Returns the ID of this node.

Source

pub fn tree(&self) -> &'a Tree<T>

Returns the tree owning this node.

Source

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}
Source

pub fn parent(&self) -> Option<NodeRef<'a, T>>

Returns the parent of this node.

Source

pub fn prev_sibling(&self) -> Option<NodeRef<'a, T>>

Returns the previous sibling of this node.

Source

pub fn next_sibling(&self) -> Option<NodeRef<'a, T>>

Returns the next sibling of this node.

Source

pub fn first_child(&self) -> Option<NodeRef<'a, T>>

Returns the first child of this node.

Source

pub fn last_child(&self) -> Option<NodeRef<'a, T>>

Returns the last child of this node.

Source

pub fn has_siblings(&self) -> bool

Returns true if this node has siblings.

Source

pub fn has_children(&self) -> bool

Returns true if this node has children.

Source

pub fn ancestors(&self) -> Ancestors<'a, T>

Returns an iterator over ancestors.

Source

pub fn prev_siblings(&self) -> PrevSiblings<'a, T>

Returns an iterator over previous siblings.

Source

pub fn next_siblings(&self) -> NextSiblings<'a, T>

Returns an iterator over next siblings.

Source

pub fn first_children(&self) -> FirstChildren<'a, T>

Returns an iterator over first children.

Source

pub fn last_children(&self) -> LastChildren<'a, T>

Returns an iterator over last children.

Source

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}
Source

pub fn traverse(&self) -> Traverse<'a, T>

Returns an iterator which traverses the subtree starting at this node.

Source

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>

Source§

type Target = NodeRef<'a, MctsNode<T>>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T: Board> DerefMut for MctsTreeNode<'a, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, T: Board> From<NodeRef<'a, MctsNode<T>>> for MctsTreeNode<'a, T>

Source§

fn from(node: NodeRef<'a, MctsNode<T>>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: Board> Into<NodeRef<'a, MctsNode<T>>> for MctsTreeNode<'a, T>

Source§

fn into(self) -> NodeRef<'a, MctsNode<T>>

Converts this type into the (usually inferred) input type.

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>
where <T as Board>::Move: Sync, T: Sync,

§

impl<'a, T> Sync for MctsTreeNode<'a, T>
where <T as Board>::Move: Sync, T: Sync,

§

impl<'a, T> Unpin for MctsTreeNode<'a, T>

§

impl<'a, T> UnwindSafe for MctsTreeNode<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V