mctser
An incrediblely easy-to-use library for Monte Carlo Tree Search.
All you need to do is to implement four required traits in this library for corresponding types in your game.
Usage
Add the dependency to your Cargo.toml
To use this library, two traits mctser::GameState and mctser::Action, and two marking traits mctser::EndStatus and mctser::Action need to be implemented for corresponding types in your game. The definations of the four traits are as follows.
/// The trait for the end status of the game, like player1 wins, player2 wins, or tie
/// The trait for the action. For example, in tictactoe, the action is the coordinate of the next move
/// The trait for the player
/// The trait for the game state
Here is an example for tic tac toe. Some details of the example are hiden and click here to see the full example. Clone this repository and cargo run --example tictactoe to see the game playing between two mctser bots.
To use this crate, four types are needed:
- A type representing the status of the game. For tic tac toe, it would be the situation on the borad, the player of next move, if the game ends and who wins the game when it ends.
- A type representing the players. For tic tac toe, we can use an
enumto representing the two players. - A type representing possible actions. For tic tac toe, it would be the coordination of a move.
- A type representing the status of end of the game. For tic tac toe, it would be player1 win, player2 win, or tie.
For these types, we have four corresponding traits in this crate, namely GameState, Player, Action and EndStatus, which you need to implement for your types.
As a start, we can define the four needed types as follows:
;
To make the game playable, we need to implement a few necessary methods. Here to see the detailed implementation. Then we can implement the corresponding traits for the these types:
Then you can build a mcts::SearchTree and start to search. To make search records of preceding searches reused in the next move, using mcts::SearchTree::renew to step forward.
The usage of this library is quite easy, isn't it?
Todo
- Add test cases
- Support custom tree policy
- Add parallel search
Contribution
All kind of contributions are welcome. Feel free to open an issue or a pull request.