How usail-hkust/llmtscs works
1. What this repository is
LLMLight is a traffic-signal-control framework centered on a shared simulation environment. It compares three kinds of controllers: heuristic baselines, RL/DNN baselines, and LLM-based agents. The repo also contains a separate LightGPT fine-tuning pipeline for adapting an LLM to traffic signal decisions.
2. Core runtime boundary: simulation vs. decision policy
The main architectural split is between the traffic simulation layer and the control policy layer. The simulation layer is built around CityFlow and the road-network/traffic scenario JSON files under data/. The policy layer contains all agents that observe the current traffic state and emit a phase/action for each intersection.
3. Environment and state construction
utils/cityflow_env.py is the central environment wrapper. Supporting utilities in utils/ handle sampling, replay construction, update loops, evaluation, and transfer testing. The data/<city>/<scenario>/ folders provide the road network definition plus anonymized real/synthetic traffic traces that drive the simulator.
4. Agent hierarchy
models/agent.py is the base abstraction for controllers. Everything else in models/ is a concrete policy implementation: fixed-time, random, max-pressure variants, MPLight/CoLight/PressLight/AttendLight, plus advanced and efficient variants. This makes the model layer a plug-in set of interchangeable controllers over the same environment API.
5. Heuristic and RL baselines
The root run_*.py scripts are the execution entrypoints for benchmark runs. Scripts such as run_fixedtime.py, run_maxpressure.py, run_mplight.py, run_colight.py, and their advanced_ / efficient_ counterparts wire a specific agent class to the environment, execute training or evaluation, and produce logs/results for comparison.
6. LLM-based inference path
run_chatgpt.py, run_open_LLM.py, and run_open_LLM_with_vllm.py provide the LLM control path. Here, the simulator state is transformed into a textual prompt, sent to either a hosted model (ChatGPT) or a local open-source model, and the generated response is mapped back to a traffic-signal action. models/chatgpt.py holds the external API setup for OpenAI-style calls.
7. Prompting and instruction design
The prompts/ directory stores prompt templates and domain-knowledge material used to encode traffic state into natural-language instructions. models/chatgpt.py and the utilities around it sit at the boundary between structured simulator state and the LLM’s text interface.
8. LightGPT fine-tuning pipeline
The finetune/ package is a separate training subsystem for adapting a base LLM into LightGPT. run_imitation_finetune.py performs supervised imitation learning from collected traffic decision data, merge_lora.py merges LoRA adapters back into a base model, and run_policy_refinement.py performs critic-guided refinement after data collection. state_action_2_instructions.py converts simulator state/action pairs into instruction-style training examples.
9. Data collection for refinement
run_policy_refinement_data_collection.py bridges RL and LLM training: it uses a trained RL controller as a teacher to generate policy-refinement data, which is then stored under data/cgpr/. This creates a two-stage flow: simulator rollout from an RL agent, then instruction-formatted training data for the LLM adapter.
10. Evaluation and analysis utilities
utils/model_test.py, utils/RL_transfer_test.py, and related helpers support benchmarking and cross-scenario transfer. utils/llm_aft_trainer.py and ranking/loss utilities suggest additional training logic for alignment or preference-style optimization, but they remain within the same simulator-to-policy pipeline.
11. Frontend replay/visualization
frontend/page/ is a static browser-based viewer used to replay and inspect traffic-control behavior. It is not the control runtime itself; it consumes replay artifacts so users can visualize intersection states and agent decisions after an experiment.
12. Repository-level artifacts
The data/ tree is the main scenario asset store, while media/ holds figures and demo materials. The README also references model checkpoints and logs as external outputs of the runs. Architecturally, those artifacts are downstream products of the training/evaluation scripts rather than first-class application services.
13. Important external dependencies
CityFlow is the simulation engine and requires Linux. TensorFlow appears to support older RL components, while Hugging Face transformers, peft, accelerate, datasets, and optionally vllm power the LLM side. wandb is used for experiment tracking. These dependencies matter because they define two distinct execution modes: classic RL/simulation and LLM inference/fine-tuning.
Main entry points: agent.py Agent base · cityflow_env.py CityFlow env · run_chatgpt.py Run ChatGPT · run_policy_refinement_data_collection.py Collect data
Components 26
Simulation runtime
CityFlow environment, data assets, and shared rollout/evaluation utilities.
- CityFlow env · Central simulator wrapper that exposes state, action, and step APIs.
utils/cityflow_env.py - Samples · Builds training and rollout samples from simulator traces.
utils/construct_sample.py - Updater · Coordinates repeated environment updates during an experiment.
utils/updater.py - Pipeline · Orchestrates data flow between environment, sampling, and evaluation stages.
utils/pipeline.py - Generator · Creates or prepares traffic scenarios and related artifacts.
utils/generator.py
Control policies
Heuristic, RL, and LLM-based controllers that plug into the same environment API.
- Agent base · Common controller abstraction implemented by all policy types.
models/agent.py - Fixed-time · Static signal timing baseline.
models/fixedtime_agent.py - Max-pressure · Pressure-based traffic-signal controller baseline.
models/maxpressure_agent.py - MPLight · Graph-based reinforcement-learning controller.
models/mplight_agent.py - CoLight · Cooperative multi-intersection learning policy.
models/colight_agent.py - PressLight · Pressure-aware deep controller variant.
models/presslight_one.py - ChatGPT API · External LLM call boundary and response handling.
models/chatgpt.py - Run fixed · Executes the fixed-time baseline in the shared simulator.
run_fixedtime.py - Run MPLight · Runs MPLight training or evaluation against CityFlow.
run_mplight.py - Run ChatGPT · Turns simulator state into prompts and maps LLM output back to actions.
run_chatgpt.py - Run open LLM · Local open-model control path for traffic-signal decisions.
run_open_LLM.py - Domain prompts · Traffic-domain knowledge and instruction templates for LLM prompting.
prompts/prompt_domain_knowledge.json
LightGPT training
Separate pipeline for instruction data creation, imitation tuning, and refinement.
- State-action · Converts simulator state/action pairs into instruction-format examples.
finetune/state_action_2_instructions.py - Collect data · Uses an RL teacher to gather policy-refinement training data.
finetune/run_policy_refinement_data_collection.py - Imitation tune · Supervised LightGPT adaptation from collected traffic decisions.
finetune/run_imitation_finetune.py - Policy refine · Post-collection refinement stage for LightGPT-style policies.
finetune/run_policy_refinement.py - Merge LoRA · Merges LoRA adapters into a base model after tuning.
finetune/merge_lora.py
Support tools
Transfer testing, ranking losses, and replay/visualization utilities.
- Transfer test · Cross-scenario transfer benchmarking for trained controllers.
utils/RL_transfer_test.py - Model test · Shared benchmarking and result-checking utility.
utils/model_test.py - Rank loss · Ranking and alignment loss helpers used in advanced training.
utils/aft_rank_loss_utils.py
Replay viewer
Browser-based inspection of traffic experiments and replays.
- Replay viewer · Static frontend for inspecting traffic experiment replays.
frontend/page/index.html
Connections 26
- Run fixed → Fixed-time: uses · Entry script wires a specific controller to the environment.
- Run fixed → CityFlow env: simulates · Runs the baseline inside the shared CityFlow wrapper.
- Run MPLight → MPLight: uses · Benchmark entrypoint for the RL controller.
- Run MPLight → CityFlow env: simulates · Shares the same environment API as other controllers.
- Run ChatGPT → ChatGPT API: calls · Uses the OpenAI-style API boundary for inference.
- Run ChatGPT → Domain prompts: prompts with · Loads domain knowledge and instruction templates.
- Run ChatGPT → CityFlow env: observes · Converts simulator state into text prompts and actions.
- Run open LLM → Domain prompts: prompts with · Uses the same prompt material for local model inference.
- Run open LLM → CityFlow env: observes · Maps simulator observations into LLM actions.
- CityFlow env → Samples: feeds · Produces samples from simulation traces and rollouts.
- Samples → Updater: drives · Sampling supports repeated rollout/update loops.
- Updater → Pipeline: orchestrated by · Higher-level pipeline coordinates execution stages.
- Pipeline → Generator: prepares · Pipeline can create or transform scenario artifacts.
- Collect data → MPLight: teacher · Uses a trained RL controller to generate refinement data.
- Collect data → State-action: formats · Converts rollouts into instruction-style training pairs.
- State-action → Imitation tune: trains · Feeds supervised LightGPT imitation learning.
- Imitation tune → Merge LoRA: exports · Adapters are merged back into a base model after tuning.
- Collect data → Policy refine: supplies · Collected data is reused for refinement.
- Policy refine → Rank loss: uses · Alignment/refinement can rely on ranking losses.
- Model test → Transfer test: supports · Shared evaluation utilities underpin transfer testing.
- Replay viewer → Model test: views · Replay viewer consumes experiment outputs from runs.
- Agent base → Fixed-time: base for · Common policy interface implemented by controllers.
- Agent base → Max-pressure: base for · Common policy interface implemented by controllers.
- Agent base → MPLight: base for · Common policy interface implemented by controllers.
- Agent base → CoLight: base for · Common policy interface implemented by controllers.
- Agent base → PressLight: base for · Common policy interface implemented by controllers.