# TraCI

**TraCI**（交通控制接口，Traffic Control Interface）是一种基于 TCP 的开放客户端—服务器协议，用于观测和操控正在运行的交通仿真：客户端可以获取车辆、车道、信号灯等仿真对象的值，并在仿真运行过程中在线改变它们的行为<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。它最初是作为交通仿真器 SUMO 与网络仿真器 ns-2 之间的通用耦合接口而提出的，如今与 SUMO 关系最为密切，由 SUMO 充当 TraCI 服务器<sup>[2](https://dl.acm.org/doi/10.1145/1400713.1400740)</sup><sup> • </sup><sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。由于该协议开放且与语言无关，它已被广泛应用于交通仿真研究及其他领域，包括网络仿真器<sup>[3](https://www.cms-labs.org/bib/meng2023towards/meng2023towards.pdf)</sup>。

| 关键事实 | 详情 |
|---|---|
| 是什么 | 交通控制接口，一种 TCP 客户端—服务器协议；SUMO 为服务器端，启动时使用 `--remote-port` 选项<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup> |
| 同步机制 | 在所有已连接的客户端发出“仿真步进”命令（0x02）之前，仿真不会推进<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup><sup> • </sup><sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup> |
| 传输格式 | 每条 TCP 消息是一个容器：先是标明整条消息大小的头部，随后是一组命令，每条命令前都带有自身的长度和标识符；单条命令最长 255 字节，除非使用扩展长度字段<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup> |
| 状态响应 | 每条命令都会以一个结果码进行确认：0x00 表示成功，0xFF 表示失败，0x01 表示未实现<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup> |
| 吞吐量 | 一个基准测试脚本在普通取值模式下每秒处理约 25,000 辆车，使用订阅模式则约 50,000 辆<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup> |
| 最快访问方式 | libsumo 将 SUMO 作为库嵌入客户端进程，避免套接字开销，在性能敏感时被推荐使用<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup> |
| API 版本 | 当前 TraCI API 版本为 22，定义于 Python 客户端的常量模块中<sup>[5](https://github.com/eclipse-sumo/sumo/blob/main/tools/traci/constants.py)</sup> |
| 语言绑定 | 官方客户端包括 Python、C++/Java（libsumo、libtraci）、MATLAB（TraCI4Matlab）、Java（TraaS）和 .NET（TraCI.NET）<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup> |

## 协议与架构

SUMO 充当 TraCI 服务器。启动时使用命令行选项 `--remote-port`，客户端通过向该端口发起 TCP 连接来接入<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。TraCI 支持多客户端。每个客户端应在第一个仿真步之前发出 SetOrder 命令（0x03），来自不同客户端的命令按照该编号顺序执行<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup>。在多客户端配置下，SUMO 启动时必须已知客户端数量，且所有客户端必须在第一个仿真步之前完成连接<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup>。

**消息格式。** 每条 TCP 消息是一个容器，由一个小头部（给出整条消息的大小）和随后的一组命令组成；每条命令的长度和标识符置于命令本身之前<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup>。单条命令的最大长度为 255 字节。更长的命令使用扩展长度方案：将原来的无符号字节长度字段设为零，并追加一个整型长度字段<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup>。

**命令标识符。** 历史上 TraCI 每个域使用一个字节，其中较高的一半表示命令（get、set、subscribe 等），较低的一半表示域，例如感应线圈或车辆<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。自 SUMO 1.7.0 版起，最高位也用于表示域，从而允许 28 个域，因为四条通用命令占用了一些组合<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。

**步进与响应。** 客户端驱动仿真：每一步由命令 0x02（仿真步进）触发，所有客户端都发出该命令后仿真才推进<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup>。每条命令都会得到一个携带结果和描述的状态响应确认：0x00 表示成功，0xFF 表示所请求的命令失败，0x01 表示该命令在仿真器中未实现<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup>。目前所有客户端都会收到全部订阅结果，即使是其他客户端发出的订阅<sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup>。

## 能力与 API 域

TraCI 将访问组织为取值域和操控域，在支持的地方提供 get、set 和 subscribe 操作。文档记载的域涵盖车辆、行人、车辆类型、路线、感应线圈、车道区域检测器、多出入口检测器、校准器、信号灯、公交站、充电站、停车区、架空线、重路由器、仿真本身、图形界面、兴趣点和多边形<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。每个仿真对象，如路段、路口、信号灯或车辆，都包含可在线获取或修改的变量<sup>[6](https://www.mdpi.com/1424-8220/18/12/4399)</sup>。

步进命令是客户端控制仿真时间的主要手段：由于仿真会暂停直到所有客户端都调用步进命令，控制器可以检视状态、改变信号或重新规划车辆路线，然后才让时间推进<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup><sup> • </sup><sup>[4](https://sumo.dlr.de/docs/TraCI/Protocol.html)</sup>。

## 语言绑定与 Libsumo

官方客户端库包括 Python 模块 `traci`（可通过 `pip install traci` 安装，每日测试，支持所有 TraCI 命令）、带 C++、Java 和 Python 绑定的 libsumo、用于 C++ 和 Java 的 libtraci、TraCI4Matlab、用于 Java 的 TraaS，以及用于 .NET 的 TraCI.NET<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。Python 调用示例如 `traci.vehicle.getSpeed(vehID)` 和 `traci.vehicle.setRoute(vehID, edgeList)`<sup>[7](https://elib.dlr.de/96400/1/TRB-Circular-SUMO.pdf)</sup>。`traci` 包在 PyPI 上的版本为 1.27.1<sup>[8](https://pypi.org/project/traci/)</sup>。

**libsumo** 将 SUMO 作为库嵌入客户端进程，使用的方法签名与基于套接字的客户端相同，但避免了套接字通信开销；文档建议在性能敏感时使用它<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。这一区别在实践中很重要：在 Artery OMNeT++ 项目中，libsumo 在与 OMNeT++ 仿真相同的进程空间中运行交通仿真，而 libtraci 则将交通仿真保持为独立进程，通过协议交换数据<sup>[9](https://github.com/riebl/artery/issues/213)</sup>。性能并非在所有调用模式下都一致：仿真速度取决于每步函数调用的数量和类型、脚本内部的计算以及客户端语言，而且在 libsumo 中订阅甚至可能更慢，尽管它总体上具有速度优势<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。

第三方客户端存在，但完整程度参差不齐。Veins 项目是一个将 SUMO 与 OMNeT++ 耦合的中间件，提供 C++ 客户端库，其 API 完整度略逊于 Python 客户端<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。TraCI4Matlab 在较早的 Python（TraCI-Python）和 Java（TraCI4j）客户端之后发布，让 MATLAB 充当客户端、SUMO 充当服务器；[MathWorks](https://www.edgepedia.org/zh/mathworks) 页面记录其兼容 TraCI 版本 15，低于当前的 API 版本 22<sup>[10](https://link.springer.com/chapter/10.1007/978-3-319-15024-6_9)</sup><sup> • </sup><sup>[11](https://www.mathworks.com/matlabcentral/fileexchange/44805-traci4matlab)</sup><sup> • </sup><sup>[5](https://github.com/eclipse-sumo/sumo/blob/main/tools/traci/constants.py)</sup>。

## 数据一览

SUMO 文档直接对 TraCI 开销进行了基准测试。一个为每辆车取值的基准脚本每秒处理约 25,000 辆车；使用订阅可提升至每秒约 50,000 辆<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。在博洛尼亚（Bologna）场景中，9,000 辆车、5,000 个仿真步，无 TraCI 时运行时间为 8 秒，普通位置取值时为 90 秒，使用订阅时为 42 秒；C++ 客户端在同样的两种取值模式下分别达到 80 秒和 28 秒<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。8 秒与 90 秒之间的差距反映了 TraCI 基于步进的交互所要求的控制器与仿真之间频繁通信的成本<sup>[3](https://www.cms-labs.org/bib/meng2023towards/meng2023towards.pdf)</sup>。

两个结构性限制放大了这一成本。TraCI 基于步进的交互要求控制器在每个时间步暂停仿真以获取状态，产生频繁通信，在数据驱动应用中造成重大性能影响<sup>[3](https://www.cms-labs.org/bib/meng2023towards/meng2023towards.pdf)</sup>。此外，普通的 get 和 set 请求每次只按 ID 获取或修改一个对象的一个属性，这可能限制可扩展性，并在大规模仿真中造成显著网络开销<sup>[3](https://www.cms-labs.org/bib/meng2023towards/meng2023towards.pdf)</sup>。订阅缓解了逐请求成本，每秒 50,000 辆的数字和博洛尼亚的计时结果都表明了这一点<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>。

传输层本身也是一项可测量的成本。在一项研究中，使用 boost 进程间库以共享内存通信替代 TraCI 的 TCP/IP 通信，仅修改 SUMO 的 tcpip 套接字类中的 readExact 和 receiveExact 函数，在一个每个仿真步都采集污染物信息的场景中，将仿真时间缩短至基于 TCP 的集中式运行的平均 60%<sup>[12](https://www.easychair.org/publications/paper/Qwz1/download)</sup>。

## 与其他仿真接口的比较

SUMO 和 CityMoS 采用客户端—服务器架构，使用公开、标准化的 TraCI 协议，而 VISSIM 和 AIMSUN 使用进程内 API，缺乏语言无关性和水平可扩展性<sup>[3](https://www.cms-labs.org/bib/meng2023towards/meng2023towards.pdf)</sup>。一项 2024 年前后的对比基准并排展示了这些替代方案：通过内部 TraCI Python API 访问的 SUMO 1.22.0、由 Java 事件处理控制器驱动的 MATSim 2024.0，以及通过其 COM API 访问的 VISSIM 2024.00-04，均运行在配备 32 GB 内存的 [AMD Ryzen](https://www.edgepedia.org/zh/amd-ryzen) 7 3700X 上<sup>[13](https://www.mdpi.com/2673-7590/5/4/201)</sup>。

在车联网（V2X）工作中，Veins 中间件通过其 C++ TraCI 客户端将 SUMO 与 OMNeT++ 耦合<sup>[1](https://sumo.dlr.de/docs/TraCI/index.html)</sup>，而 TraCI 的初衷正是这类耦合：描述该架构的论文提供了将 SUMO 与网络仿真器 ns-2 连接的实现<sup>[2](https://dl.acm.org/doi/10.1145/1400713.1400740)</sup>。

## 应用

**自适应信号控制。** SUMO 自带默认的固定配时信号灯算法和基于检测到的车流间隙的自适应信号灯算法，并允许与外部进程耦合，通过 TraCI API 对信号灯切换进行细粒度控制；信号灯逻辑随后可用 Python、C++、MATLAB 或 Java 建模<sup>[14](https://elib.dlr.de/205944/1/High_fidelity_modelling_of_traffic_light_control_with_xml_logic_representation__Maik_Halbach__Jakob_Erdmann.pdf)</sup>。

**强化学习。** CISTAR（SUMO、TraCI 与 RLLab 的定制接口）以 TraCI 作为耦合机制，将 SUMO 与深度强化学习库 RLLab 集成，让用户能够为深度强化学习交通控制问题（如目标速度匹配和燃料最小化）实现控制器和异构实验<sup>[15](https://doi.org/10.1109/itsc.2017.8317694)</sup>。

**网联车辆与 V2X 建模。** 通过 TraCI 将 SUMO 与 Unity 3D 游戏引擎耦合，可将路段、路口、信号灯和车辆暴露给可视化环境，用于评估基于 V2X 通信的系统<sup>[6](https://www.mdpi.com/1424-8220/18/12/4399)</sup>。同样的耦合模式也延伸到网络仿真器，这正是 TraCI 最初设计的使用场景<sup>[2](https://dl.acm.org/doi/10.1145/1400713.1400740)</sup>。

## References

The authoritative protocol specification and domain documentation are maintained in the SUMO documentation at sumo.dlr.de; third-party clients such as Veins, TraCI4Matlab and Artery document their own coverage separately.

1. [TraCI – SUMO Documentation, DLR](https://sumo.dlr.de/docs/TraCI/index.html)
2. [Wegener et al., TraCI: an interface for coupling road traffic and network simulators, ACM](https://dl.acm.org/doi/10.1145/1400713.1400740)
3. [Meng et al. (2023), Towards Discrete-Event, Aggregating, and Relational Control Interfaces for Traffic Simulation](https://www.cms-labs.org/bib/meng2023towards/meng2023towards.pdf)
4. [Protocol – SUMO Documentation, DLR](https://sumo.dlr.de/docs/TraCI/Protocol.html)
5. [tools/traci/constants.py, eclipse-sumo/sumo, GitHub](https://github.com/eclipse-sumo/sumo/blob/main/tools/traci/constants.py)
6. [Connection of the SUMO Microscopic Traffic Simulator and the Unity 3D Game Engine, Sensors 18(12):4399](https://www.mdpi.com/1424-8220/18/12/4399)
7. [Evolution of SUMO's Simulation Model, DLR](https://elib.dlr.de/96400/1/TRB-Circular-SUMO.pdf)
8. [traci 1.27.1, PyPI](https://pypi.org/project/traci/)
9. [Artery issue 213: Switch from TraCIAPI to libsumo and/or libtraci](https://github.com/riebl/artery/issues/213)
10. [TraCI4Matlab: Enabling the Integration of the SUMO Road Traffic Simulator and Matlab, Springer](https://link.springer.com/chapter/10.1007/978-3-319-15024-6_9)
11. [TraCI4Matlab, MATLAB Central File Exchange](https://www.mathworks.com/matlabcentral/fileexchange/44805-traci4matlab)
12. [A new strategy for synchronizing traffic flow on a distributed simulation using SUMO, EasyChair](https://www.easychair.org/publications/paper/Qwz1/download)
13. [Quantitative Methodology for Comparing Microscopic Traffic Simulators, MDPI](https://www.mdpi.com/2673-7590/5/4/201)
14. [Halbach & Erdmann, High fidelity modelling of traffic light control with XML logic representation, DLR](https://elib.dlr.de/205944/1/High_fidelity_modelling_of_traffic_light_control_with_xml_logic_representation__Maik_Halbach__Jakob_Erdmann.pdf)
15. [Framework for control and deep reinforcement learning in traffic (CISTAR), ITSC 2017, IEEE](https://doi.org/10.1109/itsc.2017.8317694)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 交通运输与航天 › 道路运输 › Traffic engineering and operations › Traffic simulation software

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/traci

English original: https://www.edgepedia.org/traci
