# nt505-p21-kltn/trafficcontrolmodel architecture

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

- Interactive diagram: https://gitdiagram.com/nt505-p21-kltn/trafficcontrolmodel
- Repository: https://github.com/nt505-p21-kltn/trafficcontrolmodel

## Overview

##### Project type and purpose
- This is a full-stack traffic simulation and control system built around SUMO (Simulation of Urban MObility).
- agent_new: Python agents that interact with SUMO to train and test traffic-signal control models.
- central_server: A Flask-based server providing a dashboard, data storage, and coordination for multiple agents.

##### File structure
- Top level
- agent_new/ – the client-side agent code, training & testing scripts, model files, SUMO configuration and route files.
- central_server/ – the server-side application, with static assets (JS/CSS), HTML templates, data storage under server_data/.
- server_data/, static/, templates/ – mirror or deployment copies of central_server’s data, UI, and views.
- agent_new substructure
- agent_communicator.py – handles HTTP or socket comms with central_server.
- training_main.py, testing_main.py – orchestration scripts for training and evaluation loops.
- intersection/ – SUMO network topology & route definitions (.net.xml, .rou.xml, .sumocfg).
- model.py, memory.py, generator.py, utils.py – core RL/DL components.
- models/ – saved model binaries (h5), training/test logs.
- central_server substructure
- central_server.py – Flask app entrypoint.
- server_data/agent_data.json – persistent JSON data store.
- static/ – dashboard.js, CSS, map HTML.
- templates/ – Jinja2 HTML views for index, map.

##### README
- Minimal README (# TrafficControlModel) – confirms project focus is on traffic control modeling with a custom agent and server.

##### Detail
- Training vs. testing scripts as separate subprocesses of the Agent Service.
- Model storage separately to reflect where trained_model.h5 lives.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External System
    SUMO{"SUMO Simulator"}:::external

    %% Agent Service
    subgraph "Agent Service" 
        direction TB
        Intersection["SUMO Config (intersection/)"]:::storage
        AgentCommunicator["Agent Communicator"]:::api
        TrainingMain["Training Orchestration"]:::compute
        TestingMain["Testing Orchestration"]:::compute
        ModelCore["RL/DL Core Modules"]:::compute
        ModelStorage["Model Storage (models/)"]:::storage
    end

    %% Central Server
    subgraph "Central Server"
        direction TB
        FlaskAPI["Flask API"]:::api
        DataStore["Data Store (JSON)"]:::storage
        Figures["Figures Store"]:::storage
        StaticAssets["Static Assets"]:::frontend
        Templates["Templates"]:::frontend
    end

    %% Frontend Dashboard
    Dashboard["Frontend Dashboard"]:::frontend

    %% Data Flows
    SUMO -->|runs simulation| Intersection
    Intersection -->|provides network state| AgentCommunicator
    AgentCommunicator -->|HTTP POST /state| FlaskAPI
    FlaskAPI -->|store data| DataStore
    FlaskAPI -->|store figures| Figures
    FlaskAPI -->|HTTP GET /metrics| Dashboard
    FlaskAPI -->|HTTP GET /map data| Dashboard
    Dashboard -->|loads JS/CSS| StaticAssets
    Dashboard -->|renders views| Templates
    FlaskAPI -->|HTTP response with actions| AgentCommunicator

    %% Training & Testing flows
    TrainingMain -->|uses communicator| AgentCommunicator
    TrainingMain -->|uses core modules| ModelCore
    TrainingMain -->|saves models| ModelStorage
    TestingMain -->|uses communicator| AgentCommunicator
    TestingMain -->|uses core modules| ModelCore
    TestingMain -->|reads models| ModelStorage

    %% Click Events for Agent Service
    click Intersection "https://github.com/nt505-p21-kltn/trafficcontrolmodel/tree/main/agent_new/intersection"
    click AgentCommunicator "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/agent_communicator.py"
    click AgentCommunicator "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/test_with_server.py"
    click AgentCommunicator "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/train_with_server.py"
    click TrainingMain "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/training_main.py"
    click TrainingMain "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/training_simulation.py"
    click TrainingMain "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/training_settings.ini"
    click TestingMain "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/testing_main.py"
    click TestingMain "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/testing_simulation.py"
    click TestingMain "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/testing_settings.ini"
    click ModelCore "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/model.py"
    click ModelCore "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/memory.py"
    click ModelCore "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/generator.py"
    click ModelCore "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/utils.py"
    click ModelCore "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/open_model.py"
    click ModelCore "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/visualization.py"
    click ModelStorage "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/models/model_5/trained_model.h5"
    click ModelStorage "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/agent_new/models/model_7/trained_model.h5"
    click ModelStorage "https://github.com/nt505-p21-kltn/trafficcontrolmodel/tree/main/agent_new/models/model_7/"
    
    %% Click Events for Central Server
    click FlaskAPI "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/central_server/central_server.py"
    click DataStore "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/central_server/server_data/agent_data.json"
    click Figures "https://github.com/nt505-p21-kltn/trafficcontrolmodel/tree/main/central_server/server_data/figures/"
    click StaticAssets "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/central_server/static/dashboard.js"
    click StaticAssets "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/central_server/static/style.css"
    click StaticAssets "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/central_server/static/intersection_map.html"
    click Templates "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/central_server/templates/index.html"
    click Templates "https://github.com/nt505-p21-kltn/trafficcontrolmodel/blob/main/central_server/templates/map.html"

    %% Styles
    classDef compute fill:#cfe2f3,stroke:#0b5394,stroke-width:1px
    classDef storage fill:#d9ead3,stroke:#38761d,stroke-width:1px
    classDef api fill:#ece0f8,stroke:#5b0f84,stroke-width:1px
    classDef frontend fill:#fff2cc,stroke:#b45f06,stroke-width:1px
    classDef external fill:#e7e6e6,stroke:#666666,stroke-width:1px,stroke-dasharray: 5 5
```
