pub struct MctsNode<T: Board> {
pub id: i32,
pub height: i32,
pub board: Box<T>,
pub prev_move: Option<T::Move>,
pub current_player: Player,
pub outcome: GameOutcome,
pub visits: i32,
pub wins: i32,
pub draws: i32,
pub bound: Bound,
pub is_fully_calculated: bool,
}Expand description
Represents a single node in the Monte Carlo search tree.
Each node stores the state of the game, statistics about the outcomes of simulations, and information about the move that led to this state.
Fields§
§id: i32A unique identifier for the node.
height: i32The depth of the node in the tree.
board: Box<T>The game state that this node represents.
prev_move: Option<T::Move>The move that led to this node’s state from its parent. None for the root node.
current_player: PlayerThe player whose turn it is in this node’s game state.
outcome: GameOutcomeThe outcome of the game at this node, if it is terminal.
visits: i32The number of times this node has been visited during the search.
wins: i32The number of times simulations from this node have resulted in a win for the current player.
draws: i32The number of times simulations from this node have resulted in a draw.
bound: BoundThe bound of the node, used for alpha-beta pruning.
is_fully_calculated: boolA flag indicating whether the outcome of this node is definitively known.
Implementations§
Source§impl<T: Board> MctsNode<T>
impl<T: Board> MctsNode<T>
Sourcepub fn new(id: i32, boxed_board: Box<T>) -> Self
pub fn new(id: i32, boxed_board: Box<T>) -> Self
Creates a new MctsNode with the given ID and board state.
Sourcepub fn wins_rate(&self) -> f64
pub fn wins_rate(&self) -> f64
Calculates the win rate of this node.
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 draws_rate(&self) -> f64
pub fn draws_rate(&self) -> f64
Calculates the draw rate of this node.