Skip to main content

AlphaZero implementation for a triangle puzzle game.

Project description

CI/CD Status - codecov - PyPI versionLicense: MIT - Python Version

AlphaTriangle

AlphaTriangle Logo

Overview

AlphaTriangle is a project implementing an artificial intelligence agent based on AlphaZero principles to learn and play a custom puzzle game involving placing triangular shapes onto a grid. The agent learns through self-play reinforcement learning, guided by Monte Carlo Tree Search (MCTS) and a deep neural network (PyTorch).

The project includes:

  • A playable version of the triangle puzzle game using Pygame.
  • An implementation of the MCTS algorithm tailored for the game.
  • A deep neural network (policy and value heads) implemented in PyTorch, featuring convolutional layers and optional Transformer Encoder layers.
  • A reinforcement learning pipeline coordinating parallel self-play (using Ray), data storage, and network training, managed by the alphatriangle.training module.
  • Visualization tools for interactive play, debugging, and monitoring training progress (with near real-time plot updates).
  • Experiment tracking using MLflow.
  • Unit tests for core components.
  • A command-line interface for easy execution.

Core Technologies

  • Python 3.10+
  • Pygame: For game visualization and interactive modes.
  • PyTorch: For the deep learning model (CNNs, optional Transformers, Distributional Value Head) and training, with CUDA/MPS support.
  • NumPy: For numerical operations, especially state representation.
  • Ray: For parallelizing self-play data generation and statistics collection across multiple CPU cores/processes.
  • Numba: (Optional, used in features.grid_features) For performance optimization of specific grid calculations.
  • Cloudpickle: For serializing the experience replay buffer and training checkpoints.
  • MLflow: For logging parameters, metrics, and artifacts (checkpoints, buffers) during training runs.
  • Pydantic: For configuration management and data validation.
  • Typer: For the command-line interface.
  • Pytest: For running unit tests.

Project Structure

.
├── .github/workflows/      # GitHub Actions CI/CD
│   └── ci_cd.yml
├── .alphatriangle_data/    # Root directory for ALL persistent data (GITIGNORED)
│   ├── mlruns/             # MLflow tracking data
│   └── runs/               # Stores temporary/local artifacts per run
│       └── <run_name>/
│           ├── checkpoints/
│           ├── buffers/
│           ├── logs/
│           └── configs.json
├── alphatriangle/          # Source code for the project package
│   ├── __init__.py
│   ├── app.py
│   ├── cli.py              # CLI logic
│   ├── config/             # Pydantic configuration models
│   │   └── README.md
│   ├── data/               # Data saving/loading logic
│   │   └── README.md
│   ├── environment/        # Game rules, state, actions
│   │   └── README.md
│   ├── features/           # Feature extraction logic
│   │   └── README.md
│   ├── interaction/        # User input handling
│   │   └── README.md
│   ├── mcts/               # Monte Carlo Tree Search
│   │   └── README.md
│   ├── nn/                 # Neural network definition and wrapper
│   │   └── README.md
│   ├── rl/                 # RL components (Trainer, Buffer, Worker)
│   │   └── README.md
│   ├── stats/              # Statistics collection and plotting
│   │   └── README.md
│   ├── structs/            # Core data structures (Triangle, Shape)
│   │   └── README.md
│   ├── training/           # Training orchestration (Loop, Setup, Runners)
│   │   └── README.md
│   ├── utils/              # Shared utilities and types
│   │   └── README.md
│   └── visualization/      # Pygame rendering components
│       └── README.md
├── tests/                  # Unit tests
│   ├── ...
├── .gitignore
├── .python-version
├── LICENSE                 # License file (MIT)
├── MANIFEST.in             # Specifies files for source distribution
├── pyproject.toml          # Build system & package configuration
├── README.md               # This file
├── requirements.txt        # List of dependencies (also in pyproject.toml)
├── run_interactive.py      # Legacy script to run interactive modes
├── run_shape_editor.py     # Script to run the interactive shape definition tool
├── run_training_headless.py # Legacy script for headless training
└── run_training_visual.py  # Legacy script for visual training

Key Modules (alphatriangle)

Setup

  1. Clone the repository (for development):
    git clone https://github.com/lguibr/alphatriangle.git
    cd alphatriangle
    
  2. Create a virtual environment (recommended):
    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    
  3. Install the package:
    • For users:
      pip install alphatriangle # Or pip install git+https://github.com/lguibr/alphatriangle.git
      
    • For developers (editable install):
      pip install -e .
      # Install dev dependencies (optional, for running tests/linting)
      pip install pytest pytest-cov pytest-mock ruff mypy codecov twine build
      
    Note: Ensure you have the correct PyTorch version installed for your system (CPU/CUDA/MPS). See pytorch.org. Ray may have specific system requirements.
  4. (Optional) Add data directory to .gitignore: Create or edit the .gitignore file in your project root and add the line:
    .alphatriangle_data/
    

Running the Code (CLI)

Use the alphatriangle command:

  • Show Help:
    alphatriangle --help
    
  • Interactive Play Mode:
    alphatriangle play [--seed 42] [--log-level INFO]
    
  • Interactive Debug Mode:
    alphatriangle debug [--seed 42] [--log-level DEBUG]
    
  • Run Training (Visual Mode):
    alphatriangle train [--seed 42] [--log-level INFO]
    
  • Run Training (Headless Mode):
    alphatriangle train --headless [--seed 42] [--log-level INFO]
    # or
    alphatriangle train -H [--seed 42] [--log-level INFO]
    
  • Shape Editor (Run directly):
    python run_shape_editor.py
    
  • Monitoring Training (MLflow UI): While training (headless or visual), or after runs have completed, open a separate terminal in the project root and run:
    mlflow ui --backend-store-uri file:./.alphatriangle_data/mlruns
    
    Then navigate to http://localhost:5000 (or the specified port) in your browser.
  • Running Unit Tests (Development):
    pytest tests/
    

Configuration

All major parameters are defined in the Pydantic classes within the alphatriangle/config/ directory. Modify these files to experiment with different settings. The alphatriangle/config/validation.py script performs basic checks on startup.

Data Storage

All persistent data, including MLflow tracking data and run-specific artifacts, is stored within the .alphatriangle_data/ directory in the project root, managed by the DataManager and MLflow.

Maintainability

This project includes README files within each major alphatriangle submodule. Please keep these READMEs updated when making changes to the code's structure, interfaces, or core logic.

Project details


Download files

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

Source Distribution

alphatriangle-0.4.0.tar.gz (179.5 kB view details)

Uploaded Source

Built Distribution

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

alphatriangle-0.4.0-py3-none-any.whl (240.5 kB view details)

Uploaded Python 3

File details

Details for the file alphatriangle-0.4.0.tar.gz.

File metadata

  • Download URL: alphatriangle-0.4.0.tar.gz
  • Upload date:
  • Size: 179.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for alphatriangle-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7138d48c09d85c55825a72a53fed7bebafb6d6afe412978aa443857ffb37edd5
MD5 d795eceecb032474584a2a372dfb98b6
BLAKE2b-256 c7e5f8f295a05b5eff736e024c85315c990f8906ccc2b5d9451e7271c60f18e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for alphatriangle-0.4.0.tar.gz:

Publisher: ci_cd.yml on lguibr/alphatriangle

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

File details

Details for the file alphatriangle-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: alphatriangle-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 240.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for alphatriangle-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a635ad67a185319ccc3e961cfca4453feeb82eea855c91cb0bd9ae9119de5fc4
MD5 c25c900faac7fe7c1c88942044c115ad
BLAKE2b-256 3afee085d935d5e5c965bacfcda87c398bb029b6e4b11b4b6479ff8024700f52

See more details on using hashes here.

Provenance

The following attestation bundles were made for alphatriangle-0.4.0-py3-none-any.whl:

Publisher: ci_cd.yml on lguibr/alphatriangle

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