{"id":17946552,"url":"https://github.com/paytonwebber/mcts-rs","last_synced_at":"2025-08-09T05:26:49.916Z","repository":{"id":258975407,"uuid":"876007798","full_name":"PaytonWebber/mcts-rs","owner":"PaytonWebber","description":"A Rust implementation of the Monte Carlo Tree Search (MCTS) algorithm, utilizing an arena allocator for efficient memory management.","archived":false,"fork":false,"pushed_at":"2024-10-22T23:15:05.000Z","size":30,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-23T13:37:29.319Z","etag":null,"topics":["arena-allocator","mcts","monte-carlo-tree-search","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PaytonWebber.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-21T08:45:04.000Z","updated_at":"2024-10-22T23:18:28.000Z","dependencies_parsed_at":"2024-10-25T09:14:36.256Z","dependency_job_id":null,"html_url":"https://github.com/PaytonWebber/mcts-rs","commit_stats":null,"previous_names":["paytonwebber/mcts-rs"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaytonWebber%2Fmcts-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaytonWebber%2Fmcts-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaytonWebber%2Fmcts-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaytonWebber%2Fmcts-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PaytonWebber","download_url":"https://codeload.github.com/PaytonWebber/mcts-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245348292,"owners_count":20600622,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arena-allocator","mcts","monte-carlo-tree-search","rust"],"created_at":"2024-10-29T07:06:12.354Z","updated_at":"2025-03-24T20:32:44.838Z","avatar_url":"https://github.com/PaytonWebber.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rust Monte Carlo Tree Search (MCTS) with Arena Allocator\n\n[![Crates.io](https://img.shields.io/crates/v/mcts-rs.svg)](https://crates.io/crates/mcts-rs)\n[![Documentation](https://docs.rs/mcts-rs/badge.svg)](https://docs.rs/mcts-rs)\n![License](https://img.shields.io/badge/license-MIT-blue.svg)\n![Rust](https://img.shields.io/badge/rust-1.56%2B-orange.svg)\n\nA 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.\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Features](#features)\n- [Getting Started](#getting-started)\n  - [Prerequisites](#prerequisites)\n  - [Adding to Your Project](#adding-to-your-project)\n  - [Running the Tic-Tac-Toe Example](#running-the-tic-tac-toe-example)\n- [Implementation Details](#implementation-details)\n  - [Arena Allocator](#arena-allocator)\n  - [MCTS Algorithm](#mcts-algorithm)\n  - [State Trait](#state-trait)\n- [License](#license)\n\n## Introduction\n\nMonte 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.\n\nThe `mcts-rs` crate is now available on [crates.io](https://crates.io/crates/mcts-rs), making it easy to include in your Rust projects.\n\nThe included Tic-Tac-Toe game serves as a practical example of how to use the MCTS library.\n\n## Features\n\n- **Efficient Memory Management**: Utilizes an arena allocator to store nodes, reducing allocation overhead.\n- **Generic State Management**: Defines a `State` trait to allow MCTS to work with any game or decision process.\n\n## Getting Started\n\n### Prerequisites\n\n- **Rust**: Ensure you have Rust and Cargo installed. You can install Rust using [rustup](https://rustup.rs/).\n\n### Adding to Your Project\n\nTo include `mcts-rs` in your project, add the following to your `Cargo.toml`:\n\n```toml\n[dependencies]\nmcts-rs = \"0.1.0\"\n```\n\nReplace `\"0.1.0\"` with the latest version available on [crates.io](https://crates.io/crates/mcts-rs).\n\n### Running the Tic-Tac-Toe Example\n\nTo run the Tic-Tac-Toe game where the MCTS algorithm plays against itself with a random starting move, clone the repository and use the following commands:\n\n```bash\ngit clone https://github.com/PaytonWebber/mcts-rs.git\ncd mcts-rs\ncargo run --example tic_tac_toe\n```\n\n## Implementation Details\n\n### Arena Allocator\n\nAn 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.\n\n**Benefits:**\n\n- **Performance**: Reduced allocation overhead and improved cache locality.\n- **Simplicity**: Simplifies ownership and borrowing by avoiding complex lifetime issues.\n- **Safety**: Leverages Rust's safety guarantees without resorting to unsafe code.\n\n### MCTS Algorithm\n\nThe MCTS algorithm consists of four main steps:\n\n1. **Selection**: Starting from the root node, select child nodes based on the Upper Confidence Bound (UCB) until a leaf node is reached.\n2. **Expansion**: If the leaf node is not a terminal state, expand it by adding all possible child nodes.\n3. **Simulation**: Run a simulation from the expanded node to a terminal state by making random moves.\n4. **Backpropagation**: Update the nodes along the path with the simulation result.\n\n**Key Components:**\n\n- **Node Struct** (`node.rs`): Represents a node in the search tree.\n- **Arena Struct** (`arena.rs`): Stores all nodes and manages parent-child relationships between nodes.\n- **MCTS Implementation** (`mod.rs`): Contains the logic for selection, expansion, simulation, and backpropagation.\n\n### State Trait\n\nThe `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:\n\n```rust\npub trait State {\n    /// The type of action that can be taken in the state (e.g., tuple of coordinates). \n    type Action: Copy;\n    \n    /// Returns the default action for the state (used for root node).\n    fn default_action() -\u003e Self::Action;\n\n    /// Checks if the specified player has won the game.\n    fn player_has_won(\u0026self, player: usize) -\u003e bool;\n\n    /// Determines if the current state is a terminal state (no further moves possible).\n    fn is_terminal(\u0026self) -\u003e bool;\n\n    /// Returns a vector of legal actions available from the current state.\n    fn get_legal_actions(\u0026self) -\u003e Vec\u003cSelf::Action\u003e;\n\n    /// Returns the index of the player whose turn it is to play.\n    fn to_play(\u0026self) -\u003e usize;\n\n    /// Returns a new state resulting from applying the given action to the current state.\n    fn step(\u0026self, action: Self::Action) -\u003e Self;\n    \n    /// Calculates and returns the reward for the specified player in the current state.\n    fn reward(\u0026self, player: usize) -\u003e f32;\n\n    /// Renders or prints the current state (useful for debugging or display purposes).\n    fn render(\u0026self);\n}\n```\n\nBy 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.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaytonwebber%2Fmcts-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaytonwebber%2Fmcts-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaytonwebber%2Fmcts-rs/lists"}