使用 Traci进行仿真

Traffic Control Interface(Traci)通过访问一个正在运行的道路交通仿真,可以检索仿真对象的值,并“在线”控制它们的行为。

交通信号灯检索与控制

  • 获取交通信号灯的 id
traci.trafficlight.getIDList()
  • 获取交通信号的控制程序
traci.trafficlight.getProgram()
  • 获取交通信号的当前相位
traci.trafficlight.getPhase()
  • 获取交通信号的当前状态
traci.trafficlight.getRedYellowGreenState()
  • 获取交通信号的当前相位的持续时间
traci.trafficlight.getPhaseDuration()
  • 设置交通信号的当前相位的持续时间
traci.trafficlight.setPhaseDuration()
  • 获取交通信号切换至下一个相位的时间
traci.trafficlight.getNextSwitch()

车辆信息检索

  • 获取所有车辆 id
traci.vehicle.getIDList()
  • 获取车辆的等待时间
traci.vehicle.getWaitingTime()

也可以用这种方法获得 edge 或 lane 上的所有车辆的等待时间

仿真实例

使用 Traci 控制仿真的 python 程序如下

import traci
import os
import sys
from sumolib import checkBinary
import time

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'")

if_show_gui = True

if not if_show_gui:
    sumoBinary = checkBinary('sumo')
else:
    sumoBinary = checkBinary('sumo-gui')

sumocfgfile = "D:\Sumo\mybs\mybs.sumocfg"
traci.start([sumoBinary, "-c", sumocfgfile])

for step in range(0, 200):
    time.sleep(1)
    traci.simulationStep(step + 1)
    simulation_current_time = traci.simulation.getTime()
    print("仿真时间是", simulation_current_time)

    # all_vehicle_id = traci.vehicle.getIDList()
    # all_vehicle_position = [(i, traci.vehicle.getPosition(i)) for i in all_vehicle_id]
    all_trafficLight_id = traci.trafficlight.getIDList()
    now_traffic_program = traci.trafficlight.getProgram(all_trafficLight_id[0])
    now_traffic_phase = traci.trafficlight.getPhase(all_trafficLight_id[0])
    now_state = traci.trafficlight.getRedYellowGreenState(all_trafficLight_id[0])
    now_trafficLightDuration = traci.trafficlight.getPhaseDuration(all_trafficLight_id[0])
    NextSwitch = traci.trafficlight.getNextSwitch(all_trafficLight_id[0])
    all_vehicle_id = traci.vehicle.getIDList()
    WaitingTime = traci.vehicle.getWaitingTime(all_vehicle_id[0])

    print("trafficLight_id" + " = " + all_trafficLight_id[0])
    print("now_traffic_program" + " = " + now_traffic_program)
    print("now_traffic_phase" + " = " + str(now_traffic_phase))
    print("now_traffic_state" + " = " + now_state)
    print("now_trafficLightDuration" + " = " + str(now_trafficLightDuration))
    print("NextSwitch" + " = " + str(NextSwitch))
    print("WaitingTime" + " = " + str(WaitingTime))

    if step == 1:
        # 对后续相位没有影响
        traci.trafficlight.setPhaseDuration(all_trafficLight_id[0], 5.0)
        changed_trafficLightDuration = traci.trafficlight.getPhaseDuration(all_trafficLight_id[0])
        print("changed_trafficLightDuration" + " = " + str(changed_trafficLightDuration))
traci.close()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LouHerGetUp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值