Tutorial 01: Running Sumo Simulations

这个教程讲了如何在Flow里建立一个无强化学习的交通流。建立的是一个单车道环形道路。

1、组成部分

所有的仿真都由两部分构成:网络和环境(network and environment)

Network:describe the features of the transportation network used in simulation. This includes the positions and properties of nodes and edges constituting the lanes and junctions, as well as properties of the vehicles, traffic lights, inflows, etc. in the network.

包括构成车道和路口的位置以及属性,车辆的特性,红绿灯等等

Environment:on the other hand, initialize, reset, and advance simulations, and act the primary interface between the reinforcement learning algorithm and the network. Moreover, custom environments may be used to modify the dynamical features of an network.

初始化,重置,预先 模拟,作为强化学习算法和路网之间的接口。自定义的环境可以修改路网的动态特性(?)

2. 创建路网

  Flow包含很多已经创建的路网(flow/networks),可以通过导入这些已有的路网来创建新的路网。

from flow.networks.ring import RingNetwork

  FLow中的路网,由以下几部分组成:

name
vehicles
net_params
initial_config
traffic_lights

2.1 name

  路网的名称,无其他作用

2.2 vehicles

  通过 VehicleParams 来定义, VehicleParams 是一个类,包含路网中所有车辆的状态信息。这个类被用来确定车辆的动态行为以及是否被强化学习算法控制,observations and reward function的信息可以通过这个类中的函数来获取。

  首先建立一个空的 VehicleParams 对象:

from flow.core.params import VehicleParams

vehicles = VehicleParams()

  之后需要通过这个类中的 add 函数来添加车辆。具体 VehicleParams() 这个类中由哪些函数以及函数需要哪些参数可以看  classflow.core.params.VehicleParams  。add函数需要acceleration_controller routing_controller(一些其他的参数没有设置)。例子中使用的acceleration_controller是 Intelligent Driver Model (IDM) , routing_controller是 ContinuousRouter ,所以先导入一下:

from flow.controllers.car_following_models import IDMController
from flow.controllers.routing_controllers import ContinuousRouter

  在之前定义的 vehicles 这个对象中添加22辆车:

vehicles.add("human",
             acceleration_controller=(IDMController, {}),
             routing_controller=(ContinuousRouter, {}),
             num_vehicles=22)

   human 为增加的车的ID,会根据车的数量来进行命名,如 human1、human2、human3…… 

2.3 NetParams

  一些路网的参数特性,不同的路网需要的参数是不同的,可以在每个路网文件的 ADDITIONAL_NET_PARAMS 内查看需要输入那些参数,一例子中的环形路网为例:

from flow.networks.ring import ADDITIONAL_NET_PARAMS

print(ADDITIONAL_NET_PARAMS)

  输出为:

{'length': 460, 'lanes': 1, 'speed_limit': 30, 'resolution': 40}

  可以看到例子中环形路网需要的参数有这么几个:

length: length of the ring road
lanes: number of lanes
speed: speed limit for all edges
resolution: resolution of the curves on the ring. Setting this value to 1 converts the ring to a diamond.

  例子中直接使用这个默认的路网参数,所以定义路网参数方法如下,先导入在定义:

from flow.core.params import NetParams

net_params = NetParams(additional_params=ADDITIONAL_NET_PARAMS)

2.4 InitialConfig

  初始化配置是来设置在开始时车辆在路网中的位置,例子中配置如下:

from flow.core.params import InitialConfig

initial_config = InitialConfig(spacing="uniform", perturbation=1)

  其中 InitialConfig() 这个函数的参数所代表的意义以及可以选择的值见官方文档 classflow.core.params.InitialConfig() 

2.5 TrafficLightParams

  红绿灯 TrafficLightParams() 这个函数用来描述红绿灯在路网中的位置和类型,例子中创建了一个空的红绿灯对象:

from flow.core.params import TrafficLightParams

traffic_lights = TrafficLightParams()

  具体红绿灯函数中有哪些参数及如何定义见官方文档 classflow.core.params.TrafficLightParams 

 

  例子中的路网的parameter就定义了以上几类,还有很多没有定义,具体有哪些parameter见官方文档 flow.core.params module,这个文档中的参数定义不只是路网的,还有环境的参数、sumo的参数等等。

 

3、创建环境

  环境可以从 flow.envs 文件夹下来找到, 例子中用的是 AccelEnv ,导入:

from flow.envs.ring.accel import AccelEnv

  Flow中的环境由一下几个参数构成:

EnvParams
SumoParams
Network

  具体参数定义和上面的路网的参数定义是类似的,代码如下:
  先定义 SumoParams :

from flow.core.params import SumoParams

sumo_params = SumoParams(sim_step=0.1, render=True, emission_path='data')

  然后定义 EnvParams :

from flow.envs.ring.accel import ADDITIONAL_ENV_PARAMS

print(ADDITIONAL_ENV_PARAMS)

from flow.core.params import EnvParams

env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

从下面的代码来看,环境定义是包含上面的路网的定义的,即路网定义是环境定义的一个部分:

 

4、运行测试

  需要用到 Experiment 这个类:

from flow.core.experiment import Experiment

  然后是运行代码:

# create the network object
network = RingNetwork(name="ring_example",
                      vehicles=vehicles,
                      net_params=net_params,
                      initial_config=initial_config,
                      traffic_lights=traffic_lights)

# create the environment object
env = AccelEnv(env_params, sumo_params, network)

# create the experiment object
exp = Experiment(env)

# run the experiment for a set number of rollouts / time steps
_ = exp.run(1, 3000, convert_to_csv=True)

 

  运行完之后会调出sumo的GUI,同时会生成一个文件包含车辆的信息,比如速度位置等等,位于 ~/flow-master/tutorials/data 

 

  教程的完整代码如下:

from flow.networks.ring import RingNetwork

name = "ring_example"

from flow.core.params import VehicleParams

vehicles = VehicleParams()

from flow.controllers.car_following_models import IDMController

from flow.controllers.routing_controllers import ContinuousRouter

vehicles.add("human",
             acceleration_controller=(IDMController, {}),
             routing_controller=(ContinuousRouter, {}),
             num_vehicles=22)

from flow.networks.ring import ADDITIONAL_NET_PARAMS

print(ADDITIONAL_NET_PARAMS)

from flow.core.params import NetParams

net_params = NetParams(additional_params=ADDITIONAL_NET_PARAMS)

from flow.core.params import InitialConfig

initial_config = InitialConfig(spacing="uniform", perturbation=1)

from flow.core.params import TrafficLightParams

traffic_lights = TrafficLightParams()

from flow.envs.ring.accel import AccelEnv

from flow.core.params import SumoParams

sumo_params = SumoParams(sim_step=0.1, render=True, emission_path='data')

from flow.envs.ring.accel import ADDITIONAL_ENV_PARAMS

print(ADDITIONAL_ENV_PARAMS)

from flow.core.params import EnvParams

env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

from flow.core.experiment import Experiment

# create the network object
network = RingNetwork(name="ring_example",
                      vehicles=vehicles,
                      net_params=net_params,
                      initial_config=initial_config,
                      traffic_lights=traffic_lights)

# create the environment object
env = AccelEnv(env_params, sumo_params, network)

# create the experiment object
exp = Experiment(env)

# run the experiment for a set number of rollouts / time steps
_ = exp.run(1, 3000, convert_to_csv=True)
View Code

 

  

 

posted @ 2019-10-29 21:17  QMark  阅读(426)  评论(0)    收藏  举报