Skip to main content

High‑performance C++ MCTS (AlphaZero & MuZero) for triangular games

Project description

CI PyPI Coverage Status License: MIT Python Version

TriMCTS

TriMCTS Logo

TriMCTS is an installable Python package providing C++ bindings for Monte Carlo Tree Search, supporting both AlphaZero and MuZero paradigms, optimized for triangular grid games like the one in trianglengin.

🔑 Key Features

  • High-performance C++ core implementation.
  • Seamless Python integration via Pybind11.
  • Supports AlphaZero-style evaluation (policy/value from state).
  • (Planned) Supports MuZero-style evaluation (initial inference + recurrent inference).
  • Configurable search parameters (simulation count, PUCT, discount factor, Dirichlet noise).
  • Designed for use with external Python game state objects and network evaluators.
  • Type-hinted Python API (py.typed compliant).

🚀 Installation

# From PyPI (once published)
pip install trimcts

# For development (from cloned repo root)
# Ensure you clean previous builds if you encounter issues:
# rm -rf build/ src/trimcts.egg-info/ dist/ src/trimcts/trimcts_cpp.*.so
pip install -e .[dev]

💡 Usage Example (AlphaZero Style)

import time
import numpy as np
import torch # Added import
from trianglengin.core.environment import GameState # Example state object
from trianglengin.config import EnvConfig
# Assuming alphatriangle is installed and provides these:
# from alphatriangle.nn import NeuralNetwork # Example network wrapper
# from alphatriangle.config import ModelConfig, TrainConfig

from trimcts import run_mcts, SearchConfiguration, AlphaZeroNetworkInterface

# --- Mock Neural Network for demonstration ---
# Replace with your actual network implementation
class MockNeuralNetwork:
    def __init__(self, *args, **kwargs):
        self.model = torch.nn.Module() # Dummy model
        print("MockNeuralNetwork initialized.")

    def evaluate(self, state: GameState) -> tuple[dict[int, float], float]:
        # Mock evaluation: uniform policy over valid actions, fixed value
        valid_actions = state.valid_actions()
        if not valid_actions:
            return {}, 0.0 # Terminal or no valid actions
        policy = {action: 1.0 / len(valid_actions) for action in valid_actions}
        value = 0.5 # Fixed mock value
        return policy, value

    def evaluate_batch(self, states: list[GameState]) -> list[tuple[dict[int, float], float]]:
        return [self.evaluate(s) for s in states]

    def load_weights(self, path):
        print(f"Mock: Pretending to load weights from {path}")

    def to(self, device):
        print(f"Mock: Pretending to move model to {device}")
        return self
# --- End Mock Neural Network ---


# 1. Define your AlphaZero network wrapper conforming to the interface
class MyAlphaZeroWrapper(AlphaZeroNetworkInterface):
    def __init__(self, model_path: str | None = None):
        # Load your PyTorch/TensorFlow/etc. model here
        # Example using a Mock NeuralNetwork
        env_cfg = EnvConfig()
        # model_cfg = ModelConfig() # Assuming these exist if using alphatriangle
        # train_cfg = TrainConfig(DEVICE="cpu")
        self.network = MockNeuralNetwork() # Using Mock for this example
        # Load weights if model_path is provided
        if model_path:
             self.network.load_weights(model_path)
        # self.network.to(torch.device("cpu")) # Ensure model is on correct device if using real NN
        self.network.model.eval() # Set to evaluation mode
        print("MyAlphaZeroWrapper initialized.")

    def evaluate_state(self, state: GameState) -> tuple[dict[int, float], float]:
        """
        Evaluates a single game state.

        Args:
            state: The GameState object (passed from C++).

        Returns:
            A tuple containing:
                - Policy dict: {action_index: probability}
                - Value estimate: float
        """
        print(f"Python: Evaluating state step {state.current_step}")
        # Use the evaluate method of your network wrapper
        # Add necessary state transformations if your network expects specific input format
        # e.g., state_tensor = self.transform_state(state)
        # policy_logits, value_logit = self.network.model(state_tensor)
        # policy_map = self.process_policy(policy_logits, state.valid_actions())
        # value = torch.tanh(value_logit).item() # Example processing
        policy_map, value = self.network.evaluate(state) # Using mock evaluate directly
        print(f"Python: Evaluation result - Policy keys: {len(policy_map)}, Value: {value:.4f}")
        return policy_map, value

    def evaluate_batch(self, states: list[GameState]) -> list[tuple[dict[int, float], float]]:
        """
        Evaluates a batch of game states.

        Args:
            states: A list of GameState objects.

        Returns:
            A list of tuples, each containing (policy_dict, value_estimate).
        """
        print(f"Python: Evaluating batch of {len(states)} states.")
        # Use the evaluate_batch method of your network wrapper
        # Add necessary state transformations and batching if needed
        # e.g., batch_tensor = self.transform_batch(states)
        # policy_logits_batch, value_logit_batch = self.network.model(batch_tensor)
        # results = self.process_batch_output(policy_logits_batch, value_logit_batch, states)
        results = self.network.evaluate_batch(states) # Using mock evaluate_batch directly
        print(f"Python: Batch evaluation returned {len(results)} results.")
        return results

# 2. Instantiate your game state and network wrapper
env_config = EnvConfig()
# Ensure the config creates a playable state for the example
env_config.ROWS = 3
env_config.COLS = 3
env_config.NUM_SHAPE_SLOTS = 1
env_config.PLAYABLE_RANGE_PER_ROW = [(0,3), (0,3), (0,3)] # Example playable range

root_state = GameState(config=env_config, initial_seed=42)
network_wrapper = MyAlphaZeroWrapper() # Add path to your trained model if needed

# 3. Configure MCTS parameters
mcts_config = SearchConfiguration()
mcts_config.max_simulations = 50
mcts_config.max_depth = 10
mcts_config.cpuct = 1.25
mcts_config.dirichlet_alpha = 0.3
mcts_config.dirichlet_epsilon = 0.25
mcts_config.discount = 1.0 # AlphaZero typically uses no discount during search

# 4. Run MCTS
# The C++ run_mcts function will call network_wrapper.evaluate_batch() or evaluate_state()
print("Running MCTS...")
# Ensure root_state is not terminal before running
if not root_state.is_over():
    # run_mcts returns a dictionary: {action: visit_count}
    start_time = time.time()
    visit_counts = run_mcts(root_state, network_wrapper, mcts_config)
    end_time = time.time()
    print(f"\nMCTS Result (Visit Counts) after {end_time - start_time:.2f} seconds:")
    print(visit_counts)

    # Example: Select best action based on visits
    if visit_counts:
        best_action = max(visit_counts, key=visit_counts.get)
        print(f"\nBest action based on visits: {best_action}")
    else:
        print("\nNo actions explored or MCTS failed.")
else:
    print("Root state is already terminal. Cannot run MCTS.")

(MuZero example will be added later)

📂 Project Structure

trimcts/
├── .github/workflows/      # CI configuration (e.g., ci_cd.yml)
├── src/trimcts/            # Python package source ([src/trimcts/README.md](src/trimcts/README.md))
│   ├── cpp/                # C++ source code ([src/trimcts/cpp/README.md](src/trimcts/cpp/README.md))
│   │   ├── CMakeLists.txt  # CMake build script for C++ part
│   │   ├── bindings.cpp    # Pybind11 bindings
│   │   ├── config.h        # C++ configuration struct
│   │   ├── mcts.cpp        # C++ MCTS implementation
│   │   ├── mcts.h          # C++ MCTS header
│   │   └── python_interface.h # C++ helpers for Python interaction
│   ├── __init__.py         # Exposes public API (run_mcts, configs, etc.)
│   ├── config.py           # Python SearchConfiguration (Pydantic)
│   ├── mcts_wrapper.py     # Python network interface definition
│   └── py.typed            # Marker file for type checkers (PEP 561)
├── tests/                  # Python tests ([tests/README.md](tests/README.md))
│   ├── conftest.py
│   └── test_alpha_wrapper.py # Tests for AlphaZero functionality
├── .gitignore
├── LICENSE
├── MANIFEST.in             # Specifies files for source distribution
├── pyproject.toml          # Build system & package configuration
├── README.md               # This file
└── setup.py                # Setup script for C++ extension building

🛠️ Building from Source

  1. Clone the repository: git clone https://github.com/lguibr/trimcts.git
  2. Navigate to the directory: cd trimcts
  3. Recommended: Create and activate a virtual environment:
    python -m venv .venv
    source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
    
  4. Install build dependencies: pip install pybind11>=2.10 cmake wheel
  5. Clean previous builds (important if switching Python versions or encountering issues):
    rm -rf build/ src/trimcts.egg-info/ dist/ src/trimcts/trimcts_cpp.*.so
    
  6. Install the package in editable mode: pip install -e .

🧪 Running Tests

# Make sure you have installed dev dependencies
pip install -e .[dev]
pytest

🤝 Contributing

Contributions are welcome! Please follow standard fork-and-pull-request workflow. Ensure tests pass and code adheres to formatting/linting standards (Ruff, MyPy).

📜 License

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

trimcts-1.0.5-cp312-cp312-win_amd64.whl (159.4 kB view details)

Uploaded CPython 3.12Windows x86-64

trimcts-1.0.5-cp312-cp312-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

trimcts-1.0.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (193.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

trimcts-1.0.5-cp312-cp312-macosx_14_0_universal2.whl (76.3 kB view details)

Uploaded CPython 3.12macOS 14.0+ universal2 (ARM64, x86-64)

trimcts-1.0.5-cp311-cp311-win_amd64.whl (158.1 kB view details)

Uploaded CPython 3.11Windows x86-64

trimcts-1.0.5-cp311-cp311-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

trimcts-1.0.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (193.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

trimcts-1.0.5-cp311-cp311-macosx_14_0_universal2.whl (76.9 kB view details)

Uploaded CPython 3.11macOS 14.0+ universal2 (ARM64, x86-64)

trimcts-1.0.5-cp310-cp310-win_amd64.whl (155.4 kB view details)

Uploaded CPython 3.10Windows x86-64

trimcts-1.0.5-cp310-cp310-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

trimcts-1.0.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (193.1 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

trimcts-1.0.5-cp310-cp310-macosx_14_0_universal2.whl (75.3 kB view details)

Uploaded CPython 3.10macOS 14.0+ universal2 (ARM64, x86-64)

File details

Details for the file trimcts-1.0.5-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: trimcts-1.0.5-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 159.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for trimcts-1.0.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9d2a1ecf1f690027f7a4769d8edc763d60a01bf9c5073c1571ec743a6643aadc
MD5 0b4fb68cb87b3884cb1e898a31a2204f
BLAKE2b-256 28d06451df6c9bac2adad8e04e7e623b60bf09ec8c8c41c9b18b8b4bc22f2cc1

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp312-cp312-win_amd64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f756d82a3d7d4e02f608651a41ca37b95158305a9e26d823d54d27d69ff9c364
MD5 4f6216ec66a411138d64e55c6b87ba80
BLAKE2b-256 6142b0096593bbd14792ce36a3ce92bb0d503ec985df422a3f8ad1ca16cf76b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 09a301cc433a4f1534cfb17d5633b27153d2413b95e45f9638a9364a0a0229e9
MD5 b07762454a07824a4356414802a7bb98
BLAKE2b-256 d2b66333b7fae09a392edcd4d70a8131e53cebb1c66b553b3ed8639427f82a41

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp312-cp312-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp312-cp312-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 6e10251adcf512667d0061f1add956a25cb0b78fce008181efda68ae814c5b56
MD5 c345a4b25e4d420b7d111db2bbce4be5
BLAKE2b-256 a50ea8ad3e17adcfbb2dfe6a68c83ec297681ca7ff9845433464695604e16b2f

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp312-cp312-macosx_14_0_universal2.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: trimcts-1.0.5-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 158.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for trimcts-1.0.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c5ae508c7017f261b4f9997376882efbf507d1427fb434588890e77502b4aeb9
MD5 3164b8692ce3f14a3bc45de1a868f0a4
BLAKE2b-256 e24b55f2154598e71221ccfa7c9ab32e8056da4e8073853332f9d9ebaee784f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp311-cp311-win_amd64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 32b4d866ef23cf8387e0b9f53d88afddb9a665210087d84625f321df6b01e3ac
MD5 f3b27530a556fd81fb0c52ba0f80bd52
BLAKE2b-256 ce7c81a96a7950ee521322244c1cc5e10d6e95127c64402bcf5d38c0b3178bff

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp311-cp311-musllinux_1_2_x86_64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4192032bbe87d0bb274826cc9e2ad06c27a2269d4e6bc9402a1d1048dbd189fe
MD5 c68d31316a26b587c8b8ec5fa66dc201
BLAKE2b-256 773700dfe107992bc3615b3b86dedf5c9aadf83c464eeb3406546ca741938009

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp311-cp311-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp311-cp311-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 29b306100c97da0fe1f8dd8419762cba408944e98af52cbd18e88c49bcbf448d
MD5 026f9184900281622b9bf063e598b5ea
BLAKE2b-256 44c4c14ddf0f032d107dac03362e7b98530ac52c651217b249938d3c8cd37600

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp311-cp311-macosx_14_0_universal2.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: trimcts-1.0.5-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 155.4 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for trimcts-1.0.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2b1a9384a288d9fd2a5dca9b2b696a2e902c717c7e2f6accd6ae10904b9277a4
MD5 8dca5f0c1a079af4bbffccaa7544fe95
BLAKE2b-256 7975ff82976bebd016b4a4794ad826599a25225b1f3ba93421021541a1096312

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp310-cp310-win_amd64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d3d53433fe33378331870e9768da1ed9d33a9447039c2cf739eef6b7f2b0d0b6
MD5 8eee5ff82178d55fd8cc221b32588ac1
BLAKE2b-256 29b19a643f50fbc418d58d1e9bb1f1046cc583851c2afc090d5e8f7a546861c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp310-cp310-musllinux_1_2_x86_64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 cef5ace0842519784571a92ae0ba79cf2eb3fc6ed7931518d4a25888ef291e15
MD5 6e783ef8dcddc758f2cd810827355ad4
BLAKE2b-256 710318ddc8fe2d21617a32c9e79e6a52cd9679766d89f75206d30f327490979c

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trimcts-1.0.5-cp310-cp310-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for trimcts-1.0.5-cp310-cp310-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 a55cbe4b2d0d19381a84e66064a192190dd7de1dece1e11c347b7d6749edba89
MD5 baa3ce6c756fed3ce6353926754ff2cb
BLAKE2b-256 c59e0c502a0320c5b0cedefab65fd45abdb29cf1de713467cb2b929a8bb7a1c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for trimcts-1.0.5-cp310-cp310-macosx_14_0_universal2.whl:

Publisher: ci_cd.yml on lguibr/trimcts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page