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>
impl<T: Board, K: RandomGenerator> MonteCarloTreeSearch<T, K>
Sourcepub fn builder(board: T) -> MonteCarloTreeSearchBuilder<T, K>
pub fn builder(board: T) -> MonteCarloTreeSearchBuilder<T, K>
Returns a new builder for MonteCarloTreeSearch
.
Examples found in repository?
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 new(board: T, rg: K, use_alpha_beta_pruning: bool) -> Self
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.
Sourcepub fn get_tree(&self) -> &Tree<MctsNode<T>>
pub fn get_tree(&self) -> &Tree<MctsNode<T>>
Returns an immutable reference to the underlying search tree.
Sourcepub fn get_next_mcts_action(&self) -> &MctsAction
pub fn get_next_mcts_action(&self) -> &MctsAction
Returns the next MCTS action to be performed. Useful for debugging and visualization.
Sourcepub fn execute_action(&mut self)
pub fn execute_action(&mut self)
Executes a single step of the MCTS algorithm (Selection, Expansion, Simulation, or Backpropagation).
Sourcepub fn do_iteration(&mut self) -> Vec<NodeId>
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.
Sourcepub fn iterate_n_times(&mut self, n: u32)
pub fn iterate_n_times(&mut self, n: u32)
Runs the MCTS search for a specified number of iterations.
Examples found in repository?
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 get_root(&self) -> MctsTreeNode<'_, T>
pub fn get_root(&self) -> MctsTreeNode<'_, T>
Returns a reference to the root node of the search tree.
Examples found in repository?
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}