交通

  

#!/usr/bin/env python # Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
# Copyright (C) 2009-2018 German Aerospace Center (DLR) and others. # This program and the accompanying materials # are made available under the terms of the Eclipse Public License v2.0 # which accompanies this distribution, and is available at # http://www.eclipse.org/legal/epl-v20.html
# SPDX-License-Identifier: EPL-2.0 # @file runner.py # @author Lena Kalleske # @author Daniel Krajzewicz # @author Michael Behrisch # @author Jakob Erdmann # @date 2009-03-26 # @version $Id$ from __future__ import absolute_import from __future__ import print_function import os import sys import optparse import random # we need to import python modules from the $SUMO_HOME/tools directory 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 # noqa import traci # noqa def generate_routefile(): random.seed(42) # make tests reproducible N = 3600 # number of time steps,ʵ���ʱ�䳤��. # demand per second from different directions '''     加入方向pSN    1/n  表示1s來幾個車.1/10表示 1小時來360個車 '''     pWE = 1. / 10 pEW = 1. / 10 pNS = 1. / 10 pSN = 1. / 10 with open("data/cross.rou.xml", "w") as routes: #開始寫的是車輛信息,以後<route>這行寫的是車流信息,其中sigma參數表示車輛的不肯定性.通常0.5會波動一下模擬更真實.若是是0表示徹底定死的車輛運動,不包含任何隨機.其中參數length表示車頭到車尾,車自己的長度,minGap表示排隊時(standing in a jam)前車的屁股跟後車的車頭之間的距離. #下面路線裏面寫個字母o和i是什麼意思?是路線的名字.寫路線名字,不要寫node名字 # depart: # Determines the time at which the vehicle enters the network (for <flow the value of begin is used instead). If there is not enough space in the network, the actual depart time may be later. #下面print裏面參數寫的是file因此就不會在python終端輸出字符.而直接寫入文件. print("""<routes>
        <vType id="typeWE" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" \ guiShape="passenger"/>
        <vType id="typeNS" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" guiShape="passenger"/>



        <route id="right" edges="51o 1i 2o 52i" />
        <route id="left" edges="52o 2i 1o 51i" />
        <route id="down" edges="54o 4i 3o 53i" />
        <route id="up" edges="53o 3i 4o 54i" />""", file=routes)
        vehNr = 0
        for i in range(N): if random.uniform(0, 1) < pWE: print(' <vehicle id="right_%i" type="typeWE" route="right" depart="%i" />' % ( vehNr, i), file=routes) vehNr += 1
            if random.uniform(0, 1) < pEW: print(' <vehicle id="left_%i" type="typeWE" route="left" depart="%i" />' % ( vehNr, i), file=routes) vehNr += 1
            if random.uniform(0, 1) < pNS: print(' <vehicle id="down_%i" type="typeNS" route="down" depart="%i" color="1,0,0"/>' % ( vehNr, i), file=routes) vehNr += 1
            if random.uniform(0, 1) < pSN: print(' <vehicle id="up_%i" type="typeNS" route="up" depart="%i" color="1,0,0"/>' % ( vehNr, i), file=routes) vehNr += 1 print("</routes>", file=routes) # The program looks like this # <tlLogic id="0" type="static" programID="0" offset="0"> # the locations of the tls are NESW # <phase duration="31" state="GrGr"/> # <phase duration="6"  state="yryr"/> # <phase duration="31" state="rGrG"/> # <phase duration="6"  state="ryry"/> # </tlLogic> def run(): '''     這個模型交通燈的id是"0",traci.trafficlight.setPhase("0", 2) 裏面的參數是第一個是id這裏就鎖定是0了,第二個是index '''     """execute the TraCI control loop""" step = 0#表示的是時間 # we start with phase 2 where EW has green traci.trafficlight.setPhase("0", 2) print('開始') #print((traci.inductionloop.getIDList())) 只有一個元素'0' 類型tuple #print((traci.inductionloop.getPosition('0'))) '''     模型裏面中間的圓圈叫'0',是感應圈. '''  







    while traci.simulation.getMinExpectedNumber() > 0: #循環體寫這個while裏面. '''  traci.simulation.getMinExpectedNumber: Returns the number of vehicles which are in the net plus the ones still waiting to start. '''  traci.simulationStep() #print((traci.inductionloop.getPosition('0'))) #move 1s for stimulation #print(traci.trafficlight.getPhase("0"))#在python終端會輸出.0,1,2,3表示信號燈4種信號 '''         0狀態表示左右通行,--------上下綠 1狀態表示左右黃燈,---------上下變黃 2狀態表示上下通行, -------左右綠 3狀態表示上下黃燈, '''         print((traci.trafficlight.getPhase('0'))) if traci.trafficlight.getPhase("0") == 2:#if 信號燈狀態在2 # we are not already switching '''  getLastStepVehicleNumber: Returns the number of vehicles that were on the named induction loop within the last simulation step. 也就是返回傳遞參數這個id這個induction loop中上一個step中的汽車數量. 因此induction loop只識別 On the approach in the north we have an induction loop to recognize entering vehicles. 下行表示,若是 induction loop只識別從上到下的車輛. '''             #print(traci.inductionloop.getLastStepVehicleNumber("0")) 這行打印以後都是0和1的數字, #若是是1就表示當前時間這1s正好感應圈中有一個車. '''             
            '''             if traci.inductionloop.getLastStepVehicleNumber("0") > 0:#??????????why?表示當上一秒是否有車輛經過,若是有車輛經過,那麼就設置爲狀態3:上下黃燈. # there is a vehicle from the north, switch traci.trafficlight.setPhase("0", 3) else: # otherwise try to keep green for EW traci.trafficlight.setPhase("0", 2) step += 1 print(step) traci.close() sys.stdout.flush() def get_options(): optParser = optparse.OptionParser() optParser.add_option("--nogui", action="store_true", default=False, help="run the commandline version of sumo") options, args = optParser.parse_args() return options # this is the main entry point of this script if __name__ == "__main__": options = get_options() # this script has been called from the command line. It will start sumo as a # server, then connect and run if options.nogui: sumoBinary = checkBinary('sumo') else: sumoBinary = checkBinary('sumo-gui') # first, generate the route file for this simulation generate_routefile() # this is the normal way of using traci. sumo is started as a # subprocess and then the python script connects and runs traci.start([sumoBinary, "-c", "data/cross.sumocfg", "--tripinfo-output", "tripinfo.xml"]) run()
相關文章
相關標籤/搜索