# aadi1011/traffic-light-optimization-with-reinforcement-learning architecture

> Architecture diagram of the GitHub repository aadi1011/traffic-light-optimization-with-reinforcement-learning, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aadi1011/traffic-light-optimization-with-reinforcement-learning
- Repository: https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning

## Overview

##### Main Components
- Traffic Simulation Environment: Represented by the SUMO configuration and route files in the “intersection” directory (e.g., sumocfg, environment.net.xml, episode_routes.rou.xml). This component simulates the intersection and generates traffic flow data.
- Deep Q-Learning Agent: Central to the system, this agent (“Model”, “Memory”, and related files like memory.py, model.py, training_settings.ini) is responsible for decision-making. It has a state processing module (using discretized traffic states), an action selection mechanism (choosing traffic light phases), and a reward evaluation process (reducing cumulative waiting time).
- Simulation Controllers: The training and testing loops are handled by distinct entry point files (training_main.py, testing_main.py, training_simulation.py, and testing_simulation.py). They orchestrate the simulation cycles, interacting with SUMO and passing simulation data to the agent.
- Traffic Generator: Indicated by generator.py, this component deals with vehicle and route definitions for each simulation episode.
- Visualization & Utilities: Using visualization.py and utils.py, these components support plotting performance data and provide file handling or other helper functions.
- Additional Modules: gpucheck.py ensures the proper GPU environment, and there are multiple model directories under “models” that may represent different experimental configurations or trained model versions.

##### Component Relationships and Data Flow
- Experience Replay Memory which stores state-action-reward transitions to be used for training the neural network.
- A separate “training loop” section where the model is updated based on batches from memory. Show that the “training_main.py” script, along with “training_simulation.py”, is responsible for driving this cycle.
- That “testing_main.py” and “testing_simulation.py” are used for evaluation purposes.
- Side components like the Traffic Generator that feeds the intersection simulation with vehicle routes, and the Visualization module which plots reward curves, queue lengths, or delay data as logged by the tests (as evidenced by the “plot_*” files in the models folders).

##### Architectural Patterns and Design Principles
- Separation of concerns: Clearly distinguish between simulation control, learning logic, utility and visualization modules.
- Real-time Interaction with External Services: Include SUMO as an external simulation engine interacting with the internal training/testing loops.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Simulation Environment
    subgraph "External Simulation Environment"
        S("SUMO Environment"):::external
    end

    %% Simulation Controllers Layer
    subgraph "Simulation Controllers"
        T("Training Loop Controller"):::controller
        E("Testing Loop Controller"):::controller
    end

    %% Deep Q-Learning Agent Layer
    subgraph "Deep Q-Learning Agent"
        N("Neural Network Model"):::agent
        M("Experience Replay Memory"):::agent
        TS("Training Settings"):::agentSmall
        TM2("Trained Model 2"):::agentSmall
        TM3("Trained Model 3"):::agentSmall
    end

    %% Support Modules
    subgraph "Support Modules"
        G("Traffic Generator"):::support
        V("Visualization Module"):::support
        U("Utilities"):::support
        GPU("GPU Environment Checker"):::support
    end

    %% Data Flow Connections
    %% Environment and Agent Interaction
    S -->|"state_data"| N
    N -->|"traffic_light_phase"| S

    %% Simulation Controllers to Environment
    T -->|"simulate_training"| S
    E -->|"simulate_testing"| S

    %% Simulation Controllers to Agent (Training and Evaluation)
    T -->|"train_update"| N
    T -->|"store_experience"| M
    E -->|"evaluate_model"| N

    %% Agent Configuration Connections
    TS -->|"configures"| N
    TM2 -->|"model_weights"| N
    TM3 -->|"model_weights"| N

    %% Support Module Interactions
    G -->|"vehicle_routes"| S
    E -->|"performance_data"| V
    U -.->|"helper_functions"| N
    GPU -.->|"check_GPU"| N

    %% Styling Definitions
    classDef external fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef controller fill:#FFB347,stroke:#333,stroke-width:2px;
    classDef agent fill:#87CEEB,stroke:#333,stroke-width:2px;
    classDef agentSmall fill:#B0E0E6,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
    classDef support fill:#90EE90,stroke:#333,stroke-width:2px;

    %% Click Events for Component Mapping
    click S "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/tree/main/intersection/"
    click N "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/model.py"
    click M "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/memory.py"
    click TS "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/training_settings.ini"
    click TM2 "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/models/model_2/trained_model.h5"
    click TM3 "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/models/model_3/trained_model.h5"
    click T "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/training_main.py"
    click E "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/testing_main.py"
    click G "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/generator.py"
    click V "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/visualization.py"
    click U "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/utils.py"
    click GPU "https://github.com/aadi1011/traffic-light-optimization-with-reinforcement-learning/blob/main/gpucheck.py"
```
