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>
impl<T: Board, K: RandomGenerator> MonteCarloTreeSearchBuilder<T, K>
Sourcepub fn with_random_generator(self, rg: K) -> Self
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}
Sourcepub fn with_alpha_beta_pruning(self, use_abp: bool) -> Self
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}
Sourcepub fn build(self) -> MonteCarloTreeSearch<T, K>
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>
impl<T, K> RefUnwindSafe for MonteCarloTreeSearchBuilder<T, K>where
T: RefUnwindSafe,
K: RefUnwindSafe,
impl<T, K> Send for MonteCarloTreeSearchBuilder<T, K>
impl<T, K> Sync for MonteCarloTreeSearchBuilder<T, K>
impl<T, K> Unpin for MonteCarloTreeSearchBuilder<T, K>
impl<T, K> UnwindSafe for MonteCarloTreeSearchBuilder<T, K>where
T: UnwindSafe,
K: UnwindSafe,
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