Struct MctsNode

Source
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: i32

A unique identifier for the node.

§height: i32

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.

Implementations§

Source§

impl<T: Board> MctsNode<T>

Source

pub fn new(id: i32, boxed_board: Box<T>) -> Self

Creates a new MctsNode with the given ID and board state.

Source

pub fn wins_rate(&self) -> f64

Calculates the win rate of this node.

Examples found in repository?
examples/tic_tac_toe.rs (line 28)
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 tree = mcts.get_tree();
22    let root = mcts.get_root();
23    for node_id in root.children() {
24        let node = tree.get(node_id).unwrap();
25        println!(
26            "Move: {:?} = {:.2?}%",
27            node.data().prev_move,
28            node.data().wins_rate() * 100.0
29        );
30    }
31
32    // Get the most promising move
33    let best_move_node = mcts.get_most_perspective_move();
34    let best_move = best_move_node.data().prev_move;
35
36    println!("The best move is: {:?}", best_move);
37    assert_eq!(best_move, Some(4));
38}
Source

pub fn draws_rate(&self) -> f64

Calculates the draw rate of this node.

Trait Implementations§

Source§

impl<T: Board> Default for MctsNode<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for MctsNode<T>
where <T as Board>::Move: Freeze,

§

impl<T> RefUnwindSafe for MctsNode<T>

§

impl<T> Send for MctsNode<T>
where <T as Board>::Move: Send, T: Send,

§

impl<T> Sync for MctsNode<T>
where <T as Board>::Move: Sync, T: Sync,

§

impl<T> Unpin for MctsNode<T>
where <T as Board>::Move: Unpin,

§

impl<T> UnwindSafe for MctsNode<T>
where <T as Board>::Move: UnwindSafe, T: 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