MonteCarloTreeSearch

Struct MonteCarloTreeSearch 

Source
pub struct MonteCarloTreeSearch<T: Board, K: RandomGenerator> { /* private fields */ }
Expand description

The main struct for running the Monte Carlo Tree Search algorithm.

It holds the search tree, the random number generator, and the configuration for the search.

Implementations§

Source§

impl<T: Board, K: RandomGenerator> MonteCarloTreeSearch<T, K>

Source

pub fn builder(board: T) -> MonteCarloTreeSearchBuilder<T, K>

Returns a new builder for MonteCarloTreeSearch.

Examples found in repository?
examples/tic_tac_toe.rs (line 12)
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 new(board: T, rg: K, use_alpha_beta_pruning: bool) -> Self

Creates a new MonteCarloTreeSearch instance.

It is recommended to use the builder pattern via MonteCarloTreeSearch::builder() instead.

Source

pub fn get_tree(&self) -> &Tree<MctsNode<T>>

Returns an immutable reference to the underlying search tree.

Source

pub fn get_next_mcts_action(&self) -> &MctsAction

Returns the next MCTS action to be performed. Useful for debugging and visualization.

Source

pub fn execute_action(&mut self)

Executes a single step of the MCTS algorithm (Selection, Expansion, Simulation, or Backpropagation).

Source

pub fn do_iteration(&mut self) -> Vec<NodeId>

Performs one full iteration of the MCTS algorithm (Selection, Expansion, Simulation, Backpropagation). Returns the path of nodes that were updated during backpropagation.

Source

pub fn iterate_n_times(&mut self, n: u32)

Runs the MCTS search for a specified number of iterations.

Examples found in repository?
examples/tic_tac_toe.rs (line 18)
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 get_root(&self) -> MctsTreeNode<'_, T>

Returns a reference to the root node of the search tree.

Examples found in repository?
examples/tic_tac_toe.rs (line 21)
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§

impl<T: Board> MonteCarloTreeSearch<T, StandardRandomGenerator>

Source

pub fn from_board(board: T) -> Self

Auto Trait Implementations§

§

impl<T, K> Freeze for MonteCarloTreeSearch<T, K>
where K: Freeze,

§

impl<T, K> RefUnwindSafe for MonteCarloTreeSearch<T, K>

§

impl<T, K> Send for MonteCarloTreeSearch<T, K>
where K: Send, <T as Board>::Move: Send, T: Send,

§

impl<T, K> Sync for MonteCarloTreeSearch<T, K>
where K: Sync, <T as Board>::Move: Sync, T: Sync,

§

impl<T, K> Unpin for MonteCarloTreeSearch<T, K>
where K: Unpin, <T as Board>::Move: Unpin,

§

impl<T, K> UnwindSafe for MonteCarloTreeSearch<T, K>
where K: UnwindSafe, <T as Board>::Move: UnwindSafe, T: UnwindSafe,

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