# omrode1/chess-engine-rl architecture

> Architecture diagram of the GitHub repository omrode1/chess-engine-rl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/omrode1/chess-engine-rl
- Repository: https://github.com/omrode1/chess-engine-rl

## Overview

##### Main Components
- Main Controller/Input: Represented by the entry point (main.py) as well as command-line interfaces provided by scripts (e.g., train_agent.py, test_agent.py). This is where training or testing is initiated.
- Custom Environment: The gym environment in src/chess_environment.py, which models the chess board (using a 12×8×8 tensor) and integrates opening book knowledge.
- Reinforcement Learning Agent: The DQN algorithm (with a PyTorch backend) that is responsible for decision making. Indicate sub-components such as policy network (Convolutional Neural Network), action masking logic, and state evaluation.
- Utilities:
- Opening Book (from utils/opening_book.py): Provides opening theory insights.
- Custom Callbacks (from utils/custom_callbacks.py): For metric logging and tracking training events.
- Logging and Evaluation:
- Training logs (in tactical_logs and training_*.log files) that capture performance metrics.
- PGN game storage (in pgn_games) for later chess game analysis.

##### Relationships and Interactions
- The Training Engine outlines the core loop of the RL process:
- It interacts with the Custom Chess Environment to retrieve states, legal moves, and rewards.
- It passes state information to the DQN agent, which uses the convolutional network to evaluate board positions.
- The agent sends back actions to the environment (through the gym interface).
- Training outputs (including model checkpoints, log files, and PGN game records) are output to dedicated directories.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Different modules clearly separate responsibilities — the environment, RL agent, and utilities manage their own domains.
- Modular Design: The project has distinct modules for training, environment setup, logging, and PGN game output.
- Feedback Loop: Emphasize the cyclical nature of training where the agent continually interacts with the environment, collects rewards, and updates its neural network.
- Extensibility: Note that the architecture is designed to incorporate additional improvements (such as MCTS or PPO in future iterations) and allow integration with chess analysis tools.

##### Relevant Technologies and Frameworks
- Python as the programming language.
- PyTorch backend for neural network implementations.
- Stable Baselines3 for the DQN algorithm.
- OpenAI Gym (via a custom environment) for interacting with the chess game.
- Utility scripts may include additional Python libraries (e.g., for logging or chess-specific processing).

##### Essential Elements
- Clear labels for each component and file/directory responsible for that function.
- From the Main Controller to the training engine loop.
- Between the environment and the DQN agent.
- From both the Environment and Agent to the logging and PGN output.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Controller Layer
    subgraph "Controller Layer"
        cli_main["main.py"]:::controller
        cli_train["train_agent.py"]:::controller
        cli_test["test_agent.py"]:::controller
    end

    %% Training Engine Subgraph
    subgraph "Training Engine"
        ce["Custom Chess Environment"]:::environment
        agent["DQN Agent (with ConvNet)"]:::agent
        ce -->|"stateupdate/reward"| agent
        agent -->|"actionselection"| ce
    end

    %% Connections from Controller to Training Engine
    cli_main --> ce
    cli_train --> ce
    cli_test --> ce

    %% Utilities Integration
    ce -->|"openingguidance"| op["Opening Book Utility"]:::utility
    agent <-->|"metriclogging"| cb["Custom Callbacks"]:::utility

    %% Logging & PGN Outputs
    agent -->|"outputsmodeldata"| to["Training_Output_Log"]:::output
    agent -->|"metricslogs"| tc["Training_Curriculum_Log"]:::output
    agent -->|"traininglogs"| tl["Tactical_Logs"]:::output
    ce -->|"gamedata"| pg["PGN_Games_Storage"]:::output

    %% Legacy Code (historical reference)
    legacy["Legacy Code Components"]:::legacy
    agent ---|"legacyref"| legacy

    %% Styles
    classDef controller fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef environment fill:#A3E4D7,stroke:#1ABC9C,stroke-width:2px;
    classDef agent fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
    classDef utility fill:#FAD7A0,stroke:#E67E22,stroke-width:2px;
    classDef output fill:#D7BDE2,stroke:#8E44AD,stroke-width:2px;
    classDef legacy fill:#D5DBDB,stroke:#566573,stroke-width:2px;

    %% Click Events
    click cli_main "https://github.com/omrode1/chess-engine-rl/blob/main/main.py"
    click cli_train "https://github.com/omrode1/chess-engine-rl/blob/main/train_agent.py"
    click cli_test "https://github.com/omrode1/chess-engine-rl/blob/main/test_agent.py"
    click ce "https://github.com/omrode1/chess-engine-rl/blob/main/src/chess_environment.py"
    click agent "https://github.com/omrode1/chess-engine-rl/blob/main/train_agent.py"
    click op "https://github.com/omrode1/chess-engine-rl/blob/main/utils/opening_book.py"
    click cb "https://github.com/omrode1/chess-engine-rl/blob/main/utils/custom_callbacks.py"
    click tl "https://github.com/omrode1/chess-engine-rl/tree/main/tactical_logs"
    click to "https://github.com/omrode1/chess-engine-rl/blob/main/training_output.log"
    click tc "https://github.com/omrode1/chess-engine-rl/blob/main/training_curriculum.log"
    click pg "https://github.com/omrode1/chess-engine-rl/tree/main/pgn_games"
    click legacy "https://github.com/omrode1/chess-engine-rl/tree/main/legacy"
```
