| Crates.io | mcts-lib |
| lib.rs | mcts-lib |
| version | 0.3.2 |
| created_at | 2025-07-20 15:50:36.076631+00 |
| updated_at | 2025-08-23 16:15:35.876641+00 |
| description | A small and simple library for Monte Carlo tree search |
| homepage | |
| repository | https://github.com/darkcodi/mcts-lib |
| max_upload_size | |
| id | 1761204 |
| size | 49,312 |
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.
Board trait for easy integration with your own games.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:
Board trait: Implement the Board trait for your game state. This involves defining the logic for:
MonteCarloTreeSearch: Use the MonteCarloTreeSearch::builder() to create and configure an instance of the search algorithm.iterate_n_times to run the MCTS algorithm.get_most_perspective_move to get the best move found by the algorithm.The library includes a Tic-Tac-Toe implementation that you can use as a reference. See examples/tic_tac_toe.rs.
use mcts_lib::boards::tic_tac_toe::TicTacToeBoard;
use mcts_lib::mcts::MonteCarloTreeSearch;
use mcts_lib::random::CustomNumberGenerator;
// Create a new Tic-Tac-Toe board
let board = TicTacToeBoard::default();
// Create a new MCTS search instance
let mut mcts = MonteCarloTreeSearch::builder(board)
.with_alpha_beta_pruning(false)
.with_random_generator(CustomNumberGenerator::default())
.build();
// Run the search for 20,000 iterations
mcts.iterate_n_times(20000);
// Print the chances
let root = mcts.get_root();
for node in root.children() {
println!(
"Move: {:?} = {:.2?}%",
node.value().prev_move,
node.value().wins_rate() * 100.0
);
}
// Get the most promising move
let best_move_node = root.get_best_child().unwrap();
let best_move = best_move_node.value().prev_move;
println!("The best move is: {:?}", best_move);
cargo buildcargo testThis project is licensed under the MIT License - see the LICENSE file for details.