mcts-simple is a Python3 library that allows reinforcement learning problems to be solved easily with its implementations of Monte Carlo Tree Search.
str()
which can be reverted using Python's in-built eval()
.
delete_last_action()
method from game environment class._import()
and _export()
methods are now referred to as save()
and load()
respectively.MCTS attempts to identify the most promising moves at each state by choosing random actions at that state for every episode (playouts/rollouts). The final game result of each episode is then used to determine the weight of all nodes traversed during that episode so that the probability of choosing an action that yields higher current and potential rewards is increased.
There are 4 stages to the MCTS:
Note:
UCT, a variation of MCTS, is often used instead of vanilla MCTS for a few reasons, mainly:
UCT uses the UCB1 formula to evaluate actions at each state. The exploration parameter c in the UCB1 formula is theoretically equal to $\sqrt{2}$, but it can be changed depending on each situation.
Most of the time, a closed loop MCTS is sufficient in dealing with reinforcement learning problems. However, when it comes to games that have non-deterministic or non-discrete states, an open loop MCTS is required. Open loop MCTS would completely eliminate the need for chance nodes. Transpositions will also not be considered since we would ignore the game state entirely. Since the tree is now significantly smaller in an open loop MCTS, the branching factor is also a lot smaller and evaluations may be less accurate. This would also mean that results are more likely to converge at a faster rate. This variant of MCTS can be used for deterministic games as well.
Note:
mcts-simple only supports python 3.7 and above. If you want to use mcts-simple for other versions of Python, do note that it may not function as intended.
Module is tested only on Windows. Issues when setting up on Linux and macOS are still relatively unknown.
mcts-simple requires the following libraries, which is automatically installed together with it unless otherwise specified:
In command prompt on Windows,
pip install mcts-simple
In your python file,
from mcts_simple import *
Create a class for your game by inheriting the Game
class from mcts-simple, and define the following methods for your class:
Method | What it does |
---|---|
__init__() |
Initialises the object. |
render() |
Returns a visual representation of the current state of the game. |
get_state() |
Returns current state of the game. |
number_of_players() |
Returns number of players. |
current_player() |
Returns the player that is taking an action this turn. Note: Players are labelled from 0 to number of players - 1 . |
possible_actions() |
Returns the possible actions that can be taken by current player this turn. |
take_action(action: int) |
Player takes action. Note: Even if action leads to the end of the game, next player should still be chosen. |
has_outcome() |
Returns True if game has ended. Returns False if game is still ongoing. |
winner() |
Returns empty list if all players lose. Returns list of players if game ends in a draw. Returns list of winners if at least 1 player wins. |
After creating your game environment, you're basically done! You can train and export your MCTS with just 3 lines of code, noting that YourGame()
should be the game environment class you have created based on the above:
mcts = MCTS(YourGame(), training = True)
mcts.self_play(iterations = 50000)
mcts.save("game.mcts")
You can import your trained MCTS and see how it performs with another 3 lines of code:
mcts = MCTS(YourGame(), training = False)
mcts.load("game.mcts")
mcts.self_play()
If you have any issues in creating your environment, you can browse the source code or check out the examples provided here.
I appreciate if you are able to contribute to this project as I am the only contributor for now. This is also the first public Python package that I have written, so if you think that something is wrong with my code, you can open an issue and I'll try my best to resolve it!