【深度学习】Traffic-light-management-system 交通灯管理系统

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

这里主要是对这篇文章的学习
我将从头翻译代码并且进行改动以满足需求(造轮子)。其主要目的复现我学习的过程


提示:以下是本篇文章正文内容,下面案例可供参考

一、使用强化学习的交通灯管理系统

示例:这个交通灯管理系统是基于强化学习 的一个系统,使用到了torch numpy matplotlib sumo,这里面除sumo其他都是老常客了,这里给一个sumo(相扑)的介绍链接sumo
建议是搜索相关博主的介绍文

二、模型的训练思路

在这里插入图片描述
假设我们有一个如上所示的城市交通网络,其中有4个交通灯节点。
N1,N2,N3,N4。
这里不是传统的十字路口信号灯而是这样
在这里插入图片描述

在这里插入图片描述
只选通一个边,当然不能无限制的一直开绿灯所以设置了一个最小时间(30s)在这个限制下选择那一条边为绿灯,我们的目的是为了减少车辆等信号灯的时间。
so,定义等待交通灯时间=出现在信号灯的总车辆x等待时间
所以每个交通灯有着4个计时器
在这里插入图片描述
这图我看着都蒙所以我们来解析一下,要理解上图,得了解一下深度学习的一个算法 Deep Q-Network,DQN,我这里就不说理论的东西了,先说这个算法的框架,

代码如下(示例):

# 经验记忆库
class Memory(object):
    def __init__(self, batch_size, max_size):

    def store_transition(self, s, a, r, s_, done):

    def get_mini_batches(self):

class DQN(object):
    def __init__(self, sess, s_dim, a_dim, batch_size, gamma, lr, epsilon, replace_target_iter):
    # 利用 epsilon-greedy 来选取下一个行为
    def choose_action(self, s):
    # 生成模型
    def _generate_model(self):
    # 生成模型过程中建立两个神经网络
    def _build_net(self, s, scope, trainable):
    # 存储一个episode并且利用经验回放进行学习
    def store_transition_and_learn(self, s, a, r, s_, done):

    def _learn(self):

这个是最基本的框架
DQN伪代码
第【1】行,初始化回放记忆D,可容纳的数据条数为N
第【2】行,利用随机权值来初始化动作-行为值函数Q
第【3】行,令初始化用来计算TD目标的动作行为值Q
第【4】行,循环每次事件
第【5】行,初始化事件的第一个状态s1 ,预处理得到状态对应的特征输入
第【6】行,循环每个事件的每一步
第【7】行,利用概率选一个随机动作
第【8】行,若果小概率事件没发生,则用贪婪策略选择当前值函数最大的那个动作。注意,这里选最大动作时用到的值函数网络与逼近值函数所用的网络是一个网络,都对应着。注意:第【7】行和第【8】行是行动策略,即策略。
第【9】行,在仿真器中执行动作,观测回报以及图像.
第【10】行,设置,预处理第【11】行,将转换储存在回放记忆D中
第【12】行,从回放记忆D中均匀随机采样一个转换样本数据,用来表示。
第【13】行,判断是否是一个事件的终止状态,若是终止状态则TD目标为,否则利用TD目标网络计算TD目标。
第【14】行,执行一次梯度下降算法.
第【15】行,更新动作值函数逼近的网络参数
第【16】行,每隔C步更新一次TD目标网络权值即令
第【17】行,结束每次事件内循环
第【18】行,结束事件间的循环我们可以看到,在第【12】行,利用了经验回放;在【13】行利用了独立的目标网络;第【15】行,更新动作值函数逼近网络参数;第【17】行更新目标网络参数.

对于我们的模型肯定不是完全随机的,因为车不可能随机的出现在街道的任何一个随机的地方而是从一个边界随机的出现,然后从边界消失这是最简单的情况当然现实不会是这么一个情况,比如还有自行车,行人,从建筑中(停车场)出现车辆,所以我们要编辑多个事件,事件被定义成车辆以固定的方式(伪随机)通过节点的固定运动,我们会用类似的事件来训练模型

用sumo来模拟和训练

这里作者专门调用sumo的一个脚本文件来生成事件和模拟

这里啊需要画一张道路的图,但是呢我自然偷一波懒,,用sumo自带的工具获得一张,
cd到你sumo的安装目录,然后运行python osmWebWizard. py
在这里插入图片描述
在这里插入图片描述
这里可以调整车辆在场景中的数量,

环岛车流仿真


好了把创建好的图放到map目录下然后cd到map目录运行下面的的命令

python randomTrips.py -n network.net.xml -r routes.rou.xml -e 500

这里我解释一下这里面的命令后缀都有啥用,解释的不对的请指正毕竟还在学习阶段

optParser.add_option("-n", "--net-file", dest="netfile",
                         help="define the net file (mandatory)")#定义路网文件强制性的
    optParser.add_option("-a", "--additional-files", dest="additional",
                         help="define additional files to be loaded by the router")#定义路网文件的附属文件
    optParser.add_option("-o", "--output-trip-file", dest="tripfile",
                         default="trips.trips.xml", help="define the output trip filename")#输出的文件名
    optParser.add_option("-r", "--route-file", dest="routefile",
                         help="generates route file with duarouter")#创建最短路径
    optParser.add_option("--vtype-output", dest="vtypeout",
                         help="Store generated vehicle types in a separate file")#将生成的车辆类型存储在单独的文件中
    optParser.add_option("--weights-prefix", dest="weightsprefix",
                         help="loads probabilities for being source, destination and via-edge from the files named " +
                         "<prefix>.src.xml, <prefix>.sink.xml and <prefix>.via.xml")#从命名的文件中加载源、目的和via-edge的概率
    optParser.add_option("--weights-output-prefix", dest="weights_outprefix",
                         help="generates weights files for visualisation")#为可视化生成权重文件
    optParser.add_option("--pedestrians", action="store_true",
                         default=False, help="create a person file with pedestrian trips instead of vehicle trips")#创建一个人的文件与步行旅行,而不是车辆旅行
    optParser.add_option("--persontrips", action="store_true",
                         default=False, help="create a person file with person trips instead of vehicle trips")#创建一个人的行程而不是车辆行程的个人文件
    optParser.add_option("--personrides", help="create a person file with rides using STR as lines attribute")#创建一个开str的人作为其路线的属性
    optParser.add_option("--persontrip.transfer.car-walk", dest="carWalkMode",
                         help="Where are mode changes from car to walking allowed " +
                         "(possible values: 'ptStops', 'allJunctions' and combinations)")#在什么地方可以从汽车模式切换到步行模式,(几率,公交站点,所有的交叉路口,排列组合)
    optParser.add_option("--persontrip.walkfactor", dest="walkfactor",
                         help="Use FLOAT as a factor on pedestrian maximum speed during intermodal routing")#在多式联运路线中,使用浮动作为行人最大速度的一个因素
    optParser.add_option("--prefix", dest="tripprefix",
                         default="", help="prefix for the trip ids")#trip id的前缀
    optParser.add_option("-t", "--trip-attributes", dest="tripattrs",
                         default="", help="additional trip attributes. When generating pedestrians, attributes for " +
                         "<person> and <walk> are supported.")#额外的属性。在生成行人时,支持属性为走路和人
    optParser.add_option("--fringe-start-attributes", dest="fringeattrs",
                         default="", help="additional trip attributes when starting on a fringe.")#额外的属性。在生成行人时,属性为边缘
    optParser.add_option("-b", "--begin", default=0, help="begin time")#开始时间
    optParser.add_option("-e", "--end", default=3600, help="end time (default 3600)")#结束时间
    optParser.add_option(
        "-p", "--period", type="float", default=1, help="Generate vehicles with equidistant departure times and " +
        "period=FLOAT (default 1.0). If option --binomial is used, the expected arrival rate is set to 1/period.")#生成等距出发时间的车辆(周期为浮动)如果使用-二项式,则预期到达率设置为1/period
    optParser.add_option("-s", "--seed", type="int", default=42, help="random seed")
    optParser.add_option("--random", action="store_true",
                         default=False, help="use a random seed to initialize the random number generator")#使用随机种子初始化随机数生成器
    optParser.add_option("-l", "--length", action="store_true",
                         default=False, help="weight edge probability by length")#按长度加权边缘概率
    optParser.add_option("-L", "--lanes", action="store_true",
                         default=False, help="weight edge probability by number of lanes")#根据车道数加权边缘概率
    optParser.add_option("--edge-param", dest="edgeParam",
                         help="use the given edge parameter as factor for edge")#使用给定的边参数作为边的因子
    optParser.add_option("--speed-exponent", type="float", dest="speed_exponent",
                         default=0.0, help="weight edge probability by speed^<FLOAT> (default 0)")#根据速度加权边缘概率(浮动)
    optParser.add_option("--angle", type="float", dest="angle",
                         default=90.0, help="weight edge probability by angle [0-360] relative to the network center")#相对于网络中心角度[0-360]的权值边缘概率
    optParser.add_option("--angle-factor", type="float", dest="angle_weight",
                         default=1.0, help="maximum weight factor for angle")#角度的最大权重因子
    optParser.add_option("--fringe-factor", type="float", dest="fringe_factor",
                         default=1.0, help="multiply weight of fringe edges by <FLOAT> (default 1")#将边缘的权重乘<FLOAT>(默认为1
    optParser.add_option("--fringe-threshold", type="float", dest="fringe_threshold",
                         default=0.0, help="only consider edges with speed above <FLOAT> as fringe edges (default 0)")#只考虑速度高于<FLOAT>的边缘为边缘(默认为0)
    optParser.add_option("--allow-fringe", dest="allow_fringe", action="store_true",
                         default=False, help="Allow departing on edges that leave the network and arriving on edges " +
                         "that enter the network (via turnarounds or as 1-edge trips")#“允许在离开网络的边缘离开,并在边缘到达,它们进入网络(通过周转或单边旅行)
    optParser.add_option("--allow-fringe.min-length", type="float", dest="allow_fringe_min_length",
                         help="Allow departing on edges that leave the network and arriving on edges " +
                         "that enter the network, if they have at least the given length")#允许在离开网络的边缘离开,并在边缘到达,它们进入网络,如果它们至少有给定的长度
    optParser.add_option("--min-distance", type="float", dest="min_distance",
                         default=0.0, help="require start and end edges for each trip to be at least <FLOAT> m apart")#要求每次行程的起始边和结束边之间的距离至少为<FLOAT> m
    optParser.add_option("--max-distance", type="float", dest="max_distance",
                         default=None, help="require start and end edges for each trip to be at most <FLOAT> m " +
                         "apart (default 0 which disables any checks)")#要求每次行程的起始边和结束边不大于<FLOAT>
    optParser.add_option("-i", "--intermediate", type="int",
                         default=0, help="generates the given number of intermediate way points")#生成给定数量的中间路径点
    optParser.add_option("--flows", type="int",
                         default=0, help="generates INT flows that together output vehicles with the specified period")#生成INT流,一起输出车辆与指定的时期
    optParser.add_option("--jtrrouter", action="store_true",
                         default=False, help="Create flows without destination as input for jtrrouter")#创建没有目的地的流作为jtrrouter的输入
    optParser.add_option("--maxtries", type="int",
                         default=100, help="number of attemps for finding a trip which meets the distance constraints")#寻找满足距离约束的旅程的尝试次数
    optParser.add_option("--binomial", type="int", metavar="N",
                         help="If this is set, the number of departures per seconds will be drawn from a binomial " +
                         "distribution with n=N and p=PERIOD/N where PERIOD is the argument given to " +
                         "option --period. Tnumber of attemps for finding a trip which meets the distance constraints")#如果设置了这一项,那么每秒钟的偏离次数将从二项式中得出 +
                                                                                                                       # n= n, p=PERIOD/ n的分布,其中PERIOD是给出的参数 + 选项——时间。寻找满足距离约束的旅程的尝试次数
    optParser.add_option(
        "-c", "--vclass", "--edge-permission", default="passenger",
        help="only from and to edges which permit the given vehicle class")#只有在边缘,这允许车辆类
    optParser.add_option(
        "--vehicle-class", help="The vehicle class assigned to the generated trips (adds a standard vType definition " +
        "to the output file).")#分配给生成的行程的车辆类别添加一个标准的vType定义++输出文件
    optParser.add_option("--remove-loops", dest="remove_loops", action="store_true",
                         default=False, help="Remove loops at route start and end")#在路由开始和结束时移除循环
    optParser.add_option("--junction-taz", dest="junctionTaz", action="store_true",
                         default=False, help="Write trips with fromJunction and toJunction")#写入路线 fromJunction和toJunction
    optParser.add_option("--via-edge-types", dest="viaEdgeTypes",
                         help="Set list of edge types that cannot be used for departure or arrival " +
                         "(unless being on the fringe)")#置离开或到达时不能使用的边缘类型列表,除非处在边缘
    optParser.add_option("--validate", default=False, action="store_true",
                         help="Whether to produce trip output that is already checked for connectivity")#是否产生已检查连接性的跳闸输出
    optParser.add_option("-v", "--verbose", action="store_true",
                         default=False, help="tell me what you are doing")#tell me what you are doing

这样上面命令是什么意思就显而易见了
之后我们在开始解释主代码

总结

这里感谢莫烦python,和bilbil

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值