# mehdirt/ai-pong-with-neat architecture

> Architecture diagram of the GitHub repository mehdirt/ai-pong-with-neat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mehdirt/ai-pong-with-neat
- Repository: https://github.com/mehdirt/ai-pong-with-neat

## Overview

##### Main Components

a) Game Engine Layer:
- Ball Component (from ball.py)
- Paddle Component (from paddle.py)
- Game Manager (from game.py)

b) AI/NEAT Layer:
- NEAT implementation with:
- Population Pool
- Genome Evolution Process
- Neural Network Structure

c) Storage Layer:
- Pickle Serialization (best.pickle)
- Configuration Management (config.txt)

##### Component Relationships

- Game Engine → NEAT (Input feeding)
- NEAT → Game Engine (Action outputs)
- NEAT ↔ Storage (Save/Load best genomes)
- Config → NEAT (Configuration parameters)

##### Process Flows

Two main operational flows:
a) Training Mode:
- Cycle of:
- Game State → Neural Network
- Network Output → Paddle Movement
- Fitness Evaluation
- Evolution Process

b) Testing Mode:
- Interaction between:
- Human Input
- Loaded AI Model
- Game Engine

##### Specific Implementation Details

Technical annotations for:
- Python/Pygame as the game engine
- NEAT-Python implementation
- Pickle serialization for model persistence
- Configuration parameters from config.txt

##### Additional Elements

Include:
- Version/checkpoint management
- Input/Output specifications
- Performance metrics (fitness calculation)

##### Architectural Patterns

Highlight:
- The event-driven nature of the game loop
- The genetic algorithm's evolutionary process
- The separation between training and testing modes

- The self-play training mechanism
- The evolution of neural networks
- The interaction between game physics and AI decision-making

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Game_Engine
        GM["Game Manager"]
        BC["Ball Component"]
        PC["Paddle Component"]
        GE["Game Engine Core"]
    end

    subgraph AI_NEAT
        PP["Population Pool"]
        GE_P["Genome Evolution"]
        NN["Neural Network"]
        subgraph Training_Flow
            GS["Game State"]
            NO["Network Output"]
            FE["Fitness Evaluation"]
            EP["Evolution Process"]
        end
    end

    subgraph Storage
        PS["Pickle Serialization"]
        CM["Configuration Management"]
    end

    subgraph Process_Modes
        TM["Training Mode"]
        TSM["Testing Mode"]
        HI["Human Input"]
    end

    %% Relationships
    GM --> BC
    GM --> PC
    GM --> GE
    GE --> PP
    PP --> NN
    NN --> NO
    NO --> PC
    GS --> NN
    FE --> EP
    EP --> PP
    PS <--> PP
    CM --> PP
    HI --> TSM
    TSM --> GE

    %% Click Events
    click GM "https://github.com/mehdirt/AI-Pong-with-NEAT/blob/main/src/pong/game.py"
    click BC "https://github.com/mehdirt/AI-Pong-with-NEAT/blob/main/src/pong/ball.py"
    click PC "https://github.com/mehdirt/AI-Pong-with-NEAT/blob/main/src/pong/paddle.py"
    click GE "https://github.com/mehdirt/AI-Pong-with-NEAT/blob/main/src/pong/__init__.py"
    click CM "https://github.com/mehdirt/AI-Pong-with-NEAT/blob/main/src/config/config.txt"
    click PS "https://github.com/mehdirt/AI-Pong-with-NEAT/blob/main/src/utils/best.pickle"

    %% Styling
    classDef gameEngine fill:#a8d1ff,stroke:#333,stroke-width:2px
    classDef ai fill:#90EE90,stroke:#333,stroke-width:2px
    classDef storage fill:#E8E8E8,stroke:#333,stroke-width:2px
    classDef process fill:#FFB6C1,stroke:#333,stroke-width:2px

    class GM,BC,PC,GE gameEngine
    class PP,GE_P,NN,GS,NO,FE,EP ai
    class PS,CM storage
    class TM,TSM,HI process
```
