Metadata-Version: 2.1
Name: mctorch-mcts
Version: 0.1.1
Summary: McTorch — Monte Carlo Tree Search framework with PyTorch and C++ backends
License: MIT License
        
        Copyright (c) 2026 mcts-praktikum-ss2026
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0
Requires-Dist: numpy>=1.22
Provides-Extra: chess
Requires-Dist: chess; extra == "chess"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Provides-Extra: jax
Requires-Dist: mctx; extra == "jax"
Requires-Dist: jax; extra == "jax"
Requires-Dist: jaxlib; extra == "jax"
Provides-Extra: pgx
Requires-Dist: pgx; extra == "pgx"
Requires-Dist: mctx; extra == "pgx"
Requires-Dist: jax; extra == "pgx"
Requires-Dist: jaxlib; extra == "pgx"

# McTorch

A Monte Carlo Tree Search framework built on Python and PyTorch, with a C++ backend for performance-critical workloads.

---

## Overview

McTorch provides a clean, environment-agnostic MCTS implementation. The design separates the search engine, environment interface, and execution backend so that each piece can be developed and tested independently.

Two backends are supported:

- **TorchBackend** — pure Python reference implementation, correct by design.
- **CppBackend** — C++ accelerated backend, built via Cython bindings.

---

## Repository layout

```text
implement/            source code — package, tests, examples, benchmarks
docs/                 all documentation
management/           project management artifacts
scripts/              build and test helper scripts
assets/               diagrams and images
```

---

## Installation

Requires Python 3.10+, PyTorch 2.0+, and CMake 3.18+.

The C++ extension links against PyTorch at build time, so PyTorch must be
installed **before** building the package. Use `--no-build-isolation` so the
build step can find it:

```bash
# 1. Install PyTorch and build tools
pip install torch numpy wheel pybind11

# 2. Build and install mctorch (core only)
pip install -e . --no-build-isolation

# 3. Optional: include pgx game environments + JAX/mctx reference backend
pip install -e ".[pgx]" --no-build-isolation

# 4. Optional: development tools (pytest, ruff, mypy)
pip install -e ".[dev]" --no-build-isolation
```

> **Why `--no-build-isolation`?**  
> pip normally builds in a sandboxed environment that only has the packages
> listed under `[build-system]` in `pyproject.toml`. PyTorch is too large to
> list there, so the sandbox would never have it. `--no-build-isolation` tells
> pip to build in your current environment instead, where torch is already
> present.

---

## Quick start

```python
import torch
import torch.nn as nn
from mctorch import alphazero_policy, RootFnOutput, RecurrentFnOutput

# Minimal two-headed network (policy + value)
class Net(nn.Module):
    def forward(self, obs):
        ...  # return policy_logits [B, A], value [B]

net = Net()
board = torch.zeros(16, 9)          # 16 games, 9-cell TicTacToe boards

with torch.no_grad():
    logits, values = net(board)

root = RootFnOutput(prior_logits=logits, value=values, embedding=board)

def recurrent_fn(params, actions, embedding):
    new_board, reward, discount = your_step_fn(embedding, actions)
    with torch.no_grad():
        new_logits, new_values = params(new_board)
    return RecurrentFnOutput(reward=reward, discount=discount,
                             prior_logits=new_logits, value=new_values), new_board

out = alphazero_policy(net, root, recurrent_fn, num_simulations=200)
print(out.action)        # [B] best action per game
```

See [implement/examples/](implement/examples/) for complete runnable scripts.

---

## Running tests

```bash
bash scripts/run_tests.sh
```

or directly:

```bash
pytest implement/tests/ -v
```

---

## Running benchmarks

```bash
# mctorch C++ arena vs raw baseline
python implement/benchmarks/bench_mctorch_native.py

# mctx JAX reference backend
python implement/benchmarks/bench_mctx.py

# All backends side-by-side
python implement/benchmarks/bench_all_backends.py

# pgx game environments (requires .[pgx] extras)
python implement/benchmarks/bench_pgx.py
```

---

## Documentation

- [API reference](docs/api.md)
- [Architecture](docs/architecture.md)
- [Examples guide](docs/examples.md)
- [Development conventions](docs/conventions.md)
- [Contributing guide](CONTRIBUTING.md)
- [Execution plan](management/docs/Execution%20plan%20document.md)

---

## License

MIT
