mcts-lib
A small and simple library for Monte Carlo tree search.
This library provides a generic implementation of the Monte Carlo Tree Search (MCTS) algorithm in Rust. MCTS is a powerful heuristic search algorithm for decision-making processes, particularly in games. This library is designed to be flexible and easy to integrate with various turn-based games.
Features
- Generic implementation of the MCTS algorithm.
- Flexible
Board
trait for easy integration with your own games. - Includes an example implementation for Tic-Tac-Toe.
- Alpha-beta pruning for optimization.
Getting Started
Usage
To use this library, you need to implement the Board
trait for your game's state representation. Here's a high-level overview of the steps:
- Define your game state: Create a struct or enum to represent your game's state.
- Implement the
Board
trait: Implement theBoard
trait for your game state. This involves defining the logic for:- Getting the current player.
- Determining the game's outcome (win, lose, draw, in-progress).
- Listing available moves.
- Applying a move to the board.
- Configure
MonteCarloTreeSearch
: Use theMonteCarloTreeSearch::builder()
to create and configure an instance of the search algorithm. - Run the search: Use
iterate_n_times
to run the MCTS algorithm. - Get the best move: Use
get_most_perspective_move
to get the best move found by the algorithm.
Example: Tic-Tac-Toe
The library includes a Tic-Tac-Toe implementation that you can use as a reference. See examples/tic_tac_toe.rs
.
use TicTacToeBoard;
use MonteCarloTreeSearch;
use CustomNumberGenerator;
// Create a new Tic-Tac-Toe board
let board = default;
// Create a new MCTS search instance
let mut mcts = builder
.with_alpha_beta_pruning
.with_random_generator
.build;
// Run the search for 20,000 iterations
mcts.iterate_n_times;
// Print the chances
let root = mcts.get_root;
for node in root.children
// Get the most promising move
let best_move_node = root.get_best_child.unwrap;
let best_move = best_move_node.value.prev_move;
println!;
Building and Testing
- Build:
cargo build
- Test:
cargo test
License
This project is licensed under the MIT License - see the LICENSE file for details.