fastmcts


Namefastmcts JSON
Version 0.3.0 PyPI version JSON
download
home_pagehttps://github.com/ttong-ai/fastmcts
SummaryFast Python implementation of Monte Carlo Tree Search with parallel capabilities
upload_time2024-09-22 06:08:08
maintainerNone
docs_urlNone
authorTony Tong
requires_python>=3.8
licenseMIT
keywords mcts monte carlo tree search parallel fast
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastMCTS: Fast Python Implementation of Monte Carlo Tree Search

FastMCTS is a high-performance Python library that implements the Monte Carlo Tree Search (MCTS) algorithm with parallel capabilities. Designed for efficiency, it's suitable for both small and large game trees. This implementation is inspired by the [Monte Carlo Tree Search Beginner's Guide](https://int8.io/monte-carlo-tree-search-beginners-guide) and enhanced for speed.

## Features

- Fast, parallel implementation of MCTS
- Easy-to-use interface
- Support for two-player zero-sum games
- Extensible for custom game implementations
- Includes examples for Tic-Tac-Toe and Connect Four
- Optimized for performance with parallel processing

## Installation

Install FastMCTS using pip:

```bash
pip install fastmcts
```

## Quick Start

Here's a simple example of how to use FastMCTS with Tic-Tac-Toe:

```python
import numpy as np
from fastmcts.tree.nodes import TwoPlayersGameMonteCarloTreeSearchNode
from fastmcts.tree.search import MonteCarloTreeSearch
from fastmcts.games.tictactoe import TicTacToeGameState

# Initialize game state
state = np.zeros((3, 3))
initial_board_state = TicTacToeGameState(state=state, next_to_move=1)

# Set up MCTS
root = TwoPlayersGameMonteCarloTreeSearchNode(state=initial_board_state)
mcts = MonteCarloTreeSearch(root)

# Find the best action (utilizing parallel processing)
best_node = mcts.best_action_parallel(total_simulation_seconds=1)
```

## Using FastMCTS for Your Own Games

To use FastMCTS for your own two-player zero-sum game:

1. Create a new game state class that inherits from `fastmcts.games.common.TwoPlayersGameState`.
2. Implement the required methods (see `fastmcts.games.tictactoe.TicTacToeGameState` for an example).
3. Use your custom game state with the MCTS algorithm as shown in the quick start example.

## Example: Connect Four Game Play

Here's an example of how to use FastMCTS to play Connect Four:

```python
import numpy as np
from fastmcts.tree.nodes import TwoPlayersGameMonteCarloTreeSearchNode
from fastmcts.tree.search import MonteCarloTreeSearch
from fastmcts.games.connect4 import Connect4GameState

# Initialize game state
state = np.zeros((7, 7))
board_state = Connect4GameState(
    state=state, next_to_move=np.random.choice([-1, 1]), win=4)

# Define piece representations
pieces = {0: " ", 1: "X", -1: "O"}

# Helper functions to display the board
def stringify(row):
    return " " + " | ".join(map(lambda x: pieces[int(x)], row)) + " "

def display(board):
    board = board.copy().T[::-1]
    for row in board[:-1]:
        print(stringify(row))
        print("-" * (len(row) * 4 - 1))
    print(stringify(board[-1]))
    print()

# Main game loop
display(board_state.board)
while board_state.game_result is None:
    # Calculate best move using parallel processing
    root = TwoPlayersGameMonteCarloTreeSearchNode(state=board_state)
    mcts = MonteCarloTreeSearch(root)
    best_node = mcts.best_action_parallel(total_simulation_seconds=1)

    # Update and display board
    board_state = best_node.state
    display(board_state.board)

# Print result
print(f"Winner: {pieces[board_state.game_result]}")
```

## Performance

FastMCTS leverages parallel processing to significantly speed up the Monte Carlo Tree Search. This makes it suitable for more complex games and larger search spaces compared to traditional sequential MCTS implementations.

## Contributing

Contributions to FastMCTS are welcome! Please feel free to submit pull requests, create issues or suggest improvements. We're particularly interested in optimizations and extensions that can further improve performance.

## License

This project is licensed under the MIT License. See the LICENSE file for details.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ttong-ai/fastmcts",
    "name": "fastmcts",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "mcts monte carlo tree search parallel fast",
    "author": "Tony Tong",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/89/78/001034f94e7cdca9947241bc7055d590ca1d041ad4cea70effd20bb8d140/fastmcts-0.3.0.tar.gz",
    "platform": null,
    "description": "# FastMCTS: Fast Python Implementation of Monte Carlo Tree Search\n\nFastMCTS is a high-performance Python library that implements the Monte Carlo Tree Search (MCTS) algorithm with parallel capabilities. Designed for efficiency, it's suitable for both small and large game trees. This implementation is inspired by the [Monte Carlo Tree Search Beginner's Guide](https://int8.io/monte-carlo-tree-search-beginners-guide) and enhanced for speed.\n\n## Features\n\n- Fast, parallel implementation of MCTS\n- Easy-to-use interface\n- Support for two-player zero-sum games\n- Extensible for custom game implementations\n- Includes examples for Tic-Tac-Toe and Connect Four\n- Optimized for performance with parallel processing\n\n## Installation\n\nInstall FastMCTS using pip:\n\n```bash\npip install fastmcts\n```\n\n## Quick Start\n\nHere's a simple example of how to use FastMCTS with Tic-Tac-Toe:\n\n```python\nimport numpy as np\nfrom fastmcts.tree.nodes import TwoPlayersGameMonteCarloTreeSearchNode\nfrom fastmcts.tree.search import MonteCarloTreeSearch\nfrom fastmcts.games.tictactoe import TicTacToeGameState\n\n# Initialize game state\nstate = np.zeros((3, 3))\ninitial_board_state = TicTacToeGameState(state=state, next_to_move=1)\n\n# Set up MCTS\nroot = TwoPlayersGameMonteCarloTreeSearchNode(state=initial_board_state)\nmcts = MonteCarloTreeSearch(root)\n\n# Find the best action (utilizing parallel processing)\nbest_node = mcts.best_action_parallel(total_simulation_seconds=1)\n```\n\n## Using FastMCTS for Your Own Games\n\nTo use FastMCTS for your own two-player zero-sum game:\n\n1. Create a new game state class that inherits from `fastmcts.games.common.TwoPlayersGameState`.\n2. Implement the required methods (see `fastmcts.games.tictactoe.TicTacToeGameState` for an example).\n3. Use your custom game state with the MCTS algorithm as shown in the quick start example.\n\n## Example: Connect Four Game Play\n\nHere's an example of how to use FastMCTS to play Connect Four:\n\n```python\nimport numpy as np\nfrom fastmcts.tree.nodes import TwoPlayersGameMonteCarloTreeSearchNode\nfrom fastmcts.tree.search import MonteCarloTreeSearch\nfrom fastmcts.games.connect4 import Connect4GameState\n\n# Initialize game state\nstate = np.zeros((7, 7))\nboard_state = Connect4GameState(\n    state=state, next_to_move=np.random.choice([-1, 1]), win=4)\n\n# Define piece representations\npieces = {0: \" \", 1: \"X\", -1: \"O\"}\n\n# Helper functions to display the board\ndef stringify(row):\n    return \" \" + \" | \".join(map(lambda x: pieces[int(x)], row)) + \" \"\n\ndef display(board):\n    board = board.copy().T[::-1]\n    for row in board[:-1]:\n        print(stringify(row))\n        print(\"-\" * (len(row) * 4 - 1))\n    print(stringify(board[-1]))\n    print()\n\n# Main game loop\ndisplay(board_state.board)\nwhile board_state.game_result is None:\n    # Calculate best move using parallel processing\n    root = TwoPlayersGameMonteCarloTreeSearchNode(state=board_state)\n    mcts = MonteCarloTreeSearch(root)\n    best_node = mcts.best_action_parallel(total_simulation_seconds=1)\n\n    # Update and display board\n    board_state = best_node.state\n    display(board_state.board)\n\n# Print result\nprint(f\"Winner: {pieces[board_state.game_result]}\")\n```\n\n## Performance\n\nFastMCTS leverages parallel processing to significantly speed up the Monte Carlo Tree Search. This makes it suitable for more complex games and larger search spaces compared to traditional sequential MCTS implementations.\n\n## Contributing\n\nContributions to FastMCTS are welcome! Please feel free to submit pull requests, create issues or suggest improvements. We're particularly interested in optimizations and extensions that can further improve performance.\n\n## License\n\nThis project is licensed under the MIT License. See the LICENSE file for details.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Fast Python implementation of Monte Carlo Tree Search with parallel capabilities",
    "version": "0.3.0",
    "project_urls": {
        "Homepage": "https://github.com/ttong-ai/fastmcts"
    },
    "split_keywords": [
        "mcts",
        "monte",
        "carlo",
        "tree",
        "search",
        "parallel",
        "fast"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a77e4c0bd053c5406c593877256f27c8de3737f5766614e75981226aefe9df1c",
                "md5": "f7e17fcae5c9b48a4fe99cc94e157b79",
                "sha256": "8fdf7b0d9cc18a5f8941e94e87e7d3a8edda9251c188ef0f28d11abe5e31e0fa"
            },
            "downloads": -1,
            "filename": "fastmcts-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f7e17fcae5c9b48a4fe99cc94e157b79",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 12505,
            "upload_time": "2024-09-22T06:08:06",
            "upload_time_iso_8601": "2024-09-22T06:08:06.460206Z",
            "url": "https://files.pythonhosted.org/packages/a7/7e/4c0bd053c5406c593877256f27c8de3737f5766614e75981226aefe9df1c/fastmcts-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8978001034f94e7cdca9947241bc7055d590ca1d041ad4cea70effd20bb8d140",
                "md5": "94b538f3815181a4b1c626564276921c",
                "sha256": "d17326bb4934dcf85098162589dc7f4dd53169f21bd101f9de52cfae5614d0a0"
            },
            "downloads": -1,
            "filename": "fastmcts-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "94b538f3815181a4b1c626564276921c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12108,
            "upload_time": "2024-09-22T06:08:08",
            "upload_time_iso_8601": "2024-09-22T06:08:08.349737Z",
            "url": "https://files.pythonhosted.org/packages/89/78/001034f94e7cdca9947241bc7055d590ca1d041ad4cea70effd20bb8d140/fastmcts-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-22 06:08:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ttong-ai",
    "github_project": "fastmcts",
    "github_not_found": true,
    "lcname": "fastmcts"
}
        
Elapsed time: 0.32149s