MctsNode

Struct MctsNode 

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

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 26)
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}
Source

pub fn draws_rate(&self) -> f64

Calculates the draw rate of this node.

Trait Implementations§

Source§

impl<T: Clone + Board> Clone for MctsNode<T>
where T::Move: Clone,

Source§

fn clone(&self) -> MctsNode<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Board> Debug for MctsNode<T>
where T::Move: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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