MonteCarloTreeSearchBuilder

Struct MonteCarloTreeSearchBuilder 

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

A builder for creating instances of MonteCarloTreeSearch.

This provides a convenient way to configure the MCTS search with different parameters.

Implementations§

Source§

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

Source

pub fn new(board: T) -> Self

Creates a new builder with the given initial board state.

Source

pub fn with_random_generator(self, rg: K) -> Self

Sets the random number generator for the MCTS search.

Examples found in repository?
examples/tic_tac_toe.rs (line 14)
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 with_alpha_beta_pruning(self, use_abp: bool) -> Self

Enables or disables alpha-beta pruning.

Examples found in repository?
examples/tic_tac_toe.rs (line 13)
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 build(self) -> MonteCarloTreeSearch<T, K>

Builds the MonteCarloTreeSearch instance with the configured parameters.

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

Auto Trait Implementations§

§

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

§

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

§

impl<T, K> Send for MonteCarloTreeSearchBuilder<T, K>
where T: Send, K: Send,

§

impl<T, K> Sync for MonteCarloTreeSearchBuilder<T, K>
where T: Sync, K: Sync,

§

impl<T, K> Unpin for MonteCarloTreeSearchBuilder<T, K>
where T: Unpin, K: Unpin,

§

impl<T, K> UnwindSafe for MonteCarloTreeSearchBuilder<T, K>
where T: UnwindSafe, K: 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