sumo入门-保姆级教程

SUMO学习——入门篇

1.安装SUMO

在sumo官网(https://sumo.dlr.de/docs/Downloads.php)安装即可,为省略不必要的困扰,推荐安装在C盘,(注意:安装路径和后期学习文件都要使用非中文名称,否则路径搜索会产生错误。)

2.安装python

在python官网安装即可,由于sumo自带文件程序为python包,需提前安装python软件。

3.一种生成路网的方法

1)在openstreetmap 官网下载一张截选的地图文件(osm格式)。

2)将地图osm文件放在bin文件夹下,在bin文件夹路径处选中输入cmd,回车,进入windows窗口。输入以下代码:

netconvert --osm-files map.osm -o map.net.xml

,回车,提示Success,osm格式自动转换为net.xml格式。

  1. 把第1步生成的net文.xml件,剪切到tools文件下,生成trips.xml文件:在tools文件夹路径处选中输入cmd,回车,进入windows窗口。输入以下代码:
    python randomTrips.py -n map.net.xml -l -e 600 -o map.trips.xml
    ,回车,生成了trips.xml文件。

  1. 把trips.xml和net.xml文件剪切到bin文件夹下,在bin文件夹路径处选中输入cmd,回车,进入windows窗口。输入以下代码:
    duarouter -n map.net.xml -t map.trips.xml -o map.rou.xml --ignore-errors
    ,回车, 生成了rou文件。

5)创建sumocfg文件,可以创建一个记事本(推荐使用vscode编辑sumo的所有文件,各种库都很方便),编辑输入信息:(#中间行的每一行最后都是以"/"结尾,在后续学习过程中,是一个大概率会产生的运行错误)

<?xml version="1.0" encoding="utf-8"?>

<configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
 <input>
    <net-file value="map.net.xml" />
    <route-files value="map.rou.xml" />
  </input>

  <time>
    <begin value="0" />
    <end value="2000" />
  </time>

</configuration>

注意文件名对应,把记事本文件拖拽进sumo-gui中,即可执行。

方法二:

在sumo/tools文件夹下打开osmWebWizard(python文件),运行后自动转到openstreetmap官网,进行下载。select Area ,调整车辆类型和流量,点击Generate Scenario,即可运行sumogui进行仿真。

### 使用 SUMO 模拟高速公路交通流的 Python 示例 #### 高速公路场景配置文件准备 SUMO 的仿真依赖于 XML 文件定义道路网络和交通流。对于高速公路场景,通常需要创建 `net.xml` 和 `routes.xml`。 - **Net File (net.xml)** 描述了路网结构。 - **Route Files (routes.xml)** 定义了车辆及其行驶路径。 这些文件可以通过 NETEDIT 工具手动编辑或自动生成。 ```xml <!-- routes.xml --> <routes> <vType id="car" vClass="passenger"/> <!-- Define a route from edge "beg" to "end" --> <route id="route_0" edges="beg end"/> <!-- Insert vehicles into the simulation with defined frequency and type --> <flow id="flow_0" type="car" begin="0" end="3600" number="1000" route="route_0"/> </routes> ``` #### 启动 SUMO 并连接到 TraCI 接口 TRAFFIC CONTROL INTERFACE (TraCI) 是 SUMO 提供的一个接口,允许外部程序实时控制 SUMO 仿真的运行状态。下面是一个简单的例子展示如何启动 SUMO 进程并与之交互: ```python import os import sys if 'SUMO_HOME' in os.environ: tools = os.path.join(os.environ['SUMO_HOME'], 'tools') sys.path.append(tools) else: sys.exit("please declare environment variable 'SUMO_HOME'") from sumolib import checkBinary import traci def run_simulation(): step = 0 while step < 1000: # Run for 1000 steps traci.simulationStep() # Advance one time step # Collect data or control traffic lights here as needed. step += 1 traci.close() if __name__ == "__main__": sumo_binary = checkBinary('sumo-gui') if '--gui' in sys.argv else checkBinary('sumo') traci.start([sumo_binary, "-c", "path/to/your/highway.sumocfg", "--tripinfo-output", "tripinfo.xml"]) try: run_simulation() except Exception as e: print(f"An error occurred during simulation: {e}") ``` 此脚本会加载指定配置文件并开始模拟,在每一步都可以插入逻辑来监控或调整交通状况[^2]。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值