pub struct MctsNode<T: Board> {
pub id: i32,
pub height: usize,
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,
pub board_hash: u128,
}
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: i32
A unique identifier for the node.
height: usize
The 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: Player
The player whose turn it is in this node’s game state.
outcome: GameOutcome
The outcome of the game at this node, if it is terminal.
visits: i32
The number of times this node has been visited during the search.
wins: i32
The number of times simulations from this node have resulted in a win for the current player.
draws: i32
The number of times simulations from this node have resulted in a draw.
bound: Bound
The bound of the node, used for alpha-beta pruning.
is_fully_calculated: bool
A flag indicating whether the outcome of this node is definitively known.
board_hash: u128
A hash value representing the board state, used for quick comparisons and lookups.
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.