Struct SearchTree

Source
pub struct SearchTree<P, G, E, A>
where P: Player<E>, G: GameState<P, E, A>, E: EndStatus, A: Action,
{ /* private fields */ }
Expand description

SearchTree is the main struct to use for Monte Carlo Tree Search. Pass a GameState wrapped in a RC to SearchTree::new to create a new search tree. Then you can call SearchTree::search to get the best action and SearchTree::renew to move to the next state.

Implementations§

Source§

impl<P, G, E, A> SearchTree<P, G, E, A>
where P: Player<E>, G: GameState<P, E, A>, E: EndStatus, A: Action,

Source

pub fn new(game_state: Rc<G>) -> Self

Create a new search tree.

Examples found in repository?
examples/tictactoe.rs (line 51)
49fn main() {
50    let mut game = Rc::new(TictactoeGame::new());
51    let mut search_tree = mctser::SearchTree::new(game.clone());
52
53    while game.end_status.is_none() {
54        let selected = search_tree.search(1000).unwrap();
55        search_tree.renew(&selected).unwrap();
56        game = search_tree.get_game_state();
57        game.draw_board();
58    }
59}
Source

pub fn with_tree_policy( self, tree_policy: impl Fn(f32, f32, f32) -> f32 + 'static, ) -> Self

Set the tree policy function. By default, it is UCT tree policy.

Source

pub fn search(&self, n: u32) -> Option<A>

Search for the best action.

Examples found in repository?
examples/tictactoe.rs (line 54)
49fn main() {
50    let mut game = Rc::new(TictactoeGame::new());
51    let mut search_tree = mctser::SearchTree::new(game.clone());
52
53    while game.end_status.is_none() {
54        let selected = search_tree.search(1000).unwrap();
55        search_tree.renew(&selected).unwrap();
56        game = search_tree.get_game_state();
57        game.draw_board();
58    }
59}
Source

pub fn renew(&mut self, action: &A) -> Result<(), String>

Move to the next state and renew the root node with given action.

Examples found in repository?
examples/tictactoe.rs (line 55)
49fn main() {
50    let mut game = Rc::new(TictactoeGame::new());
51    let mut search_tree = mctser::SearchTree::new(game.clone());
52
53    while game.end_status.is_none() {
54        let selected = search_tree.search(1000).unwrap();
55        search_tree.renew(&selected).unwrap();
56        game = search_tree.get_game_state();
57        game.draw_board();
58    }
59}
Source

pub fn get_game_state(&self) -> Rc<G>

Get the current game state.

Examples found in repository?
examples/tictactoe.rs (line 56)
49fn main() {
50    let mut game = Rc::new(TictactoeGame::new());
51    let mut search_tree = mctser::SearchTree::new(game.clone());
52
53    while game.end_status.is_none() {
54        let selected = search_tree.search(1000).unwrap();
55        search_tree.renew(&selected).unwrap();
56        game = search_tree.get_game_state();
57        game.draw_board();
58    }
59}
Source

pub fn root_node(&self) -> Rc<RefCell<Node<P, G, E, A>>>

Get the root node.

Auto Trait Implementations§

§

impl<P, G, E, A> Freeze for SearchTree<P, G, E, A>

§

impl<P, G, E, A> !RefUnwindSafe for SearchTree<P, G, E, A>

§

impl<P, G, E, A> !Send for SearchTree<P, G, E, A>

§

impl<P, G, E, A> !Sync for SearchTree<P, G, E, A>

§

impl<P, G, E, A> Unpin for SearchTree<P, G, E, A>

§

impl<P, G, E, A> !UnwindSafe for SearchTree<P, G, E, A>

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.