# traffic-alpha/illm-tsc architecture

> Architecture diagram of the GitHub repository traffic-alpha/illm-tsc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/traffic-alpha/illm-tsc
- Repository: https://github.com/traffic-alpha/illm-tsc

## Overview

##### Project Overview and Key Components
- Project Purpose: iLLM-TSC is built to refine RL agent decisions in traffic signal control scenarios using contextual reasoning from an LLM. It leverages simulation environments (based on SUMO via TransSimHub), RL training and evaluation procedures, and an LLM module that provides feedback, explanations, and recommendations.
- Primary Components:
- TSCAssistant: Contains files (tsc_agent_prompt.py and tsc_assistant.py) responsible for interfacing with the LLM. It likely formulates prompts and processes LLM outputs.
- TSCEnvironment: Provides the simulation environment wrappers and utilities (e.g., tsc_env.py, tsc_env_wrapper.py, tsc_rl_wrapper.py) that abstract and encapsulate the SUMO simulation configuration and interactions.
- TSCScenario: Holds configuration details, scenario files, and simulation metadata (e.g., SumoNets folder with XML configuration files for road networks, detectors, and signal programs), which define specific simulation cases and traffic setups.
- RL Training and Evaluation Scripts: Files such as sb3_ppo.py, eval_rl_agent.py, rule_based.py, and rl_llm_tsc.py indicate different modes of operation—training the RL model, evaluating its performance, and integrating RL decisions with LLM guidance.
- Utils: A collection of supporting modules (e.g., custom_models.py, make_tsc_env.py, readConfig.py, tsccn.py) and configuration files (config.yaml) that help manage model parameters, configurations, and environment setups.
- Assets and Logs: Visual assets (images and videos) used for result displays and logs to capture monitor CSV data during simulation runs.

##### Understanding the Interactions and Data Flow
- The Simulation Environment (TSCEnvironment and TSCScenario) provides state and setup information based on traffic configurations defined in XML files (SUMO networks, detectors, traffic light states).
- The RL Component (from scripts like sb3_ppo.py and eval_rl_agent.py) interacts with the simulated environment using the wrappers provided in TSCEnvironment and utils. It receives the current state and produces an action decision regarding traffic signal control.
- TSCAssistant (LLM integration module) then evaluates these decisions. When an RL action is potentially suboptimal (e.g., in emergency cases), TSCAssistant triggers requests to the LLM. The LLM provides explanations and corrective action recommendations, which are then fed back into the control loop.
- Configuration and external dependencies (e.g., OpenAI API keys in the utils/config.yaml) ensure that the LLM integration is properly authenticating and functioning.

##### Architectural Patterns and Design Principles
- Modularity: The separation into distinct modules (TSCAssistant, TSCEnvironment, TSCScenario, and utils) indicates a modular design, where each module handles a specific responsibility.
- Layered Architecture:
- Application Layer: Scripts and entry points (sb3_ppo.py, eval_rl_agent.py, rl_llm_tsc.py) that are executed to train and run simulations.
- Domain Layer: Environment and scenario definitions (TSCEnvironment, TSCScenario) capturing traffic simulation details.
- Integration Layer: TSCAssistant integrates LLM decision support with the RL model.
- Use of Wrappers: Wrapper modules (e.g., tsc_env_wrapper.py, tsc_rl_wrapper.py, tsccn.py) abstract low-level simulation details and provide cleaner interfaces for higher-level logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% iLLM-TSC System Overview
    System["iLLM-TSC System"]:::header

    %% Simulation Layer Subgraph
    subgraph "Simulation Layer (SUMO/TransSimHub)"
        direction TB
        %% TSCEnvironment Components
        subgraph "TSCEnvironment"
            direction TB
            TENV1["tsc_env.py"]:::simulation
            TENV2["tsc_env_wrapper.py"]:::simulation
            TENV3["tsc_rl_wrapper.py"]:::simulation
            TENV4["wrapper_utils.py"]:::simulation
        end
        TSC_SCEN["TSCScenario: Traffic Configurations"]:::simulation
    end

    %% RL Module Subgraph
    subgraph "RL Module (Training & Evaluation)"
        direction TB
        RL1["sb3_ppo.py"]:::rl
        RL2["eval_rl_agent.py"]:::rl
        RL3["rl_llm_tsc.py"]:::rl
        RL4["rule_based.py"]:::rl
    end

    %% LLM Integration Subgraph
    subgraph "LLM Integration (TSCAssistant)"
        direction TB
        LLM1["Agent Prompt"]:::llm
        LLM2["LLM Assistant"]:::llm
        EX["OpenAI API"]:::llm
    end

    %% Utils & Config Subgraph
    subgraph "Utils & Config"
        direction TB
        UTIL1["config.yaml"]:::utils
        UTIL2["custom_models.py"]:::utils
        UTIL3["make_tsc_env.py"]:::utils
        UTIL4["readConfig.py"]:::utils
        UTIL5["sb3_utils.py"]:::utils
        UTIL6["scnn.py"]:::utils
        UTIL7["tsc_env.py (utils)"]:::utils
        UTIL8["tsc_wrapper.py"]:::utils
    end

    %% Assets and Logs Subgraph
    subgraph "Assets & Logs"
        direction TB
        ASSET["Assets"]:::assets
        LOG1["Log Directory (log)"]:::assets
        LOG2["Log Directory (log_test)"]:::assets
    end

    %% Connections
    System -->|"Encapsulates"| TENV1
    System -->|"Encapsulates"| TSC_SCEN
    System -->|"Encapsulates"| RL1
    System -->|"Encapsulates"| LLM2
    System -->|"Encapsulates"| UTIL1
    System -->|"Encapsulates"| ASSET

    %% Data flow from Simulation to RL Module
    TSC_SCEN -->|"state/observations"| RL1

    %% RL Module sends actions to LLM Integration for evaluation
    RL3 -->|"action_evaluation"| LLM2
    LLM2 -->|"recommendations"| RL3

    %% LLM Integration influences Simulation Environment
    LLM2 -->|"control_update"| TENV1

    %% Utils & Config provide configuration to Simulation, RL and LLM layers
    UTIL1 ---|"configures"| TSC_SCEN
    UTIL1 ---|"configures"| RL1
    UTIL1 ---|"configures"| LLM2

    %% Outputs to Assets & Logs from Simulation and RL Module
    TSC_SCEN -->|"results/logs"| ASSET
    RL2 -->|"evaluation_data"| LOG1

    %% Styles
    classDef header fill:#f9f871,stroke:#b58900,stroke-width:2px;
    classDef simulation fill:#a2d2ff,stroke:#1c7ed6,stroke-width:2px;
    classDef rl fill:#c7f464,stroke:#74c69d,stroke-width:2px;
    classDef llm fill:#ffad5a,stroke:#ff793f,stroke-width:2px;
    classDef utils fill:#d6a4a4,stroke:#b02a37,stroke-width:2px;
    classDef assets fill:#ffeedb,stroke:#f08c00,stroke-width:2px;

    %% Click Events for TSCAssistant Components
    click LLM1 "https://github.com/traffic-alpha/illm-tsc/blob/main/TSCAssistant/tsc_agent_prompt.py"
    click LLM2 "https://github.com/traffic-alpha/illm-tsc/blob/main/TSCAssistant/tsc_assistant.py"

    %% Click Events for TSCEnvironment Components
    click TENV1 "https://github.com/traffic-alpha/illm-tsc/blob/main/TSCEnvironment/tsc_env.py"
    click TENV2 "https://github.com/traffic-alpha/illm-tsc/blob/main/TSCEnvironment/tsc_env_wrapper.py"
    click TENV3 "https://github.com/traffic-alpha/illm-tsc/blob/main/TSCEnvironment/tsc_rl_wrapper.py"
    click TENV4 "https://github.com/traffic-alpha/illm-tsc/blob/main/TSCEnvironment/wrapper_utils.py"
    
    %% Click Event for TSCScenario
    click TSC_SCEN "https://github.com/traffic-alpha/illm-tsc/tree/main/TSCScenario"

    %% Click Events for RL Module Scripts
    click RL1 "https://github.com/traffic-alpha/illm-tsc/blob/main/sb3_ppo.py"
    click RL2 "https://github.com/traffic-alpha/illm-tsc/blob/main/eval_rl_agent.py"
    click RL3 "https://github.com/traffic-alpha/illm-tsc/blob/main/rl_llm_tsc.py"
    click RL4 "https://github.com/traffic-alpha/illm-tsc/blob/main/rule_based.py"

    %% Click Events for Utils & Config
    click UTIL1 "https://github.com/traffic-alpha/illm-tsc/blob/main/utils/config.yaml"
    click UTIL2 "https://github.com/traffic-alpha/illm-tsc/blob/main/utils/custom_models.py"
    click UTIL3 "https://github.com/traffic-alpha/illm-tsc/blob/main/utils/make_tsc_env.py"
    click UTIL4 "https://github.com/traffic-alpha/illm-tsc/blob/main/utils/readConfig.py"
    click UTIL5 "https://github.com/traffic-alpha/illm-tsc/blob/main/utils/sb3_utils.py"
    click UTIL6 "https://github.com/traffic-alpha/illm-tsc/blob/main/utils/scnn.py"
    click UTIL7 "https://github.com/traffic-alpha/illm-tsc/blob/main/utils/tsc_env.py"
    click UTIL8 "https://github.com/traffic-alpha/illm-tsc/blob/main/utils/tsc_wrapper.py"

    %% Click Events for Assets & Logs
    click ASSET "https://github.com/traffic-alpha/illm-tsc/tree/main/assets"
    click LOG1 "https://github.com/traffic-alpha/illm-tsc/tree/main/log"
    click LOG2 "https://github.com/traffic-alpha/illm-tsc/tree/main/log_test"
```
