Crates.io | mcts-rs |
lib.rs | mcts-rs |
version | 0.1.0 |
source | src |
created_at | 2024-10-22 07:28:20.886445 |
updated_at | 2024-10-22 07:28:20.886445 |
description | A Rust implementation of Monte Carlo Tree Search (MCTS) using an arena allocator. |
homepage | |
repository | https://github.com/PaytonWebber/mcts-rs |
max_upload_size | |
id | 1418299 |
size | 21,343 |
A Rust implementation of the Monte Carlo Tree Search (MCTS) algorithm using an arena allocator for efficient memory management. This project features a Tic-Tac-Toe game to showcase the MCTS algorithm in action.
Monte Carlo Tree Search (MCTS) is a search algorithm used for decision-making processes. This project provides a Rust implementation of MCTS that efficiently manages memory using an arena allocator. By storing all nodes in a central arena, we avoid the overhead of reference counting and interior mutability, resulting in a more performant and idiomatic Rust codebase.
The included Tic-Tac-Toe game serves as a practical example of how to use the MCTS library.
State
trait to allow MCTS to work with any game or decision process.Clone the repository:
git clone https://github.com/PaytonWebber/mcts-rs.git
cd mcts-rs
To run the Tic-Tac-Toe game where the MCTS algorithm plays against itself with a random starting move, use the following command:
cargo run --example tic_tac_toe
An arena allocator is used to efficiently manage memory for the nodes in the MCTS tree. Nodes are stored in a vector, and their relationships are represented by indices rather than pointers or references. This approach avoids the need for reference counting (Rc
) and interior mutability (RefCell
), leading to cleaner and more efficient code.
Benefits:
The MCTS algorithm consists of four main steps:
Key Components:
node.rs
): Represents a node in the search tree.arena.rs
): Stores all nodes and manages parent-child relationships between nodes.mod.rs
): Contains the logic for selection, expansion, simulation, and backpropagation.The State
trait abstracts the game logic, allowing the MCTS algorithm to work with any game or decision process that implements this trait. Here's the trait definition:
pub trait State {
/// Checks if the specified player has won the game.
fn player_has_won(&self, player: usize) -> bool;
/// Determines if the current state is a terminal state (no further moves possible).
fn is_terminal(&self) -> bool;
/// Returns a vector of legal actions available from the current state.
fn get_legal_actions(&self) -> Vec<(usize, usize)>;
/// Returns the index of the player whose turn it is to play.
fn to_play(&self) -> usize;
/// Returns a new state resulting from applying the given action to the current state.
fn step(&self, action: (usize, usize)) -> Self;
/// Calculates and returns the reward for the specified player in the current state.
fn reward(&self, player: usize) -> f32;
/// Renders or prints the current state (useful for debugging or display purposes).
fn render(&self);
}
By implementing this trait for your game or decision process, you can integrate it with the MCTS algorithm provided in this library. The tic_tac_toe.rs
file offers an example implementation of the State
trait for Tic-Tac-Toe.