2016-07-18 7 views
0

私はPythonで以下のスクリプトを書いて、車線内の2つのinductionLoop間の車両の数を60秒ごとに取得するために、sumoで実装しました。 しかし、これは毎秒を与えます。各車両の台数

#!/usr/bin/env python 
    # -*-coding:Latin-1 -* 
    import os, sys 
    import optparse 
    import subprocess 
    import random 
    import threading 
    import time 

    SUMO_HOME = "/home/khadija/Téléchargements/sumo-0.25.0" 

    try: 
     sys.path.append(os.path.join(SUMO_HOME, "tools")) 
     from sumolib import checkBinary 
    except ImportError: 
     sys.exit("please declare environment variable 'SUMO_HOME' as the root directory of your sumo installation (it should contain folders 'bin', 'tools' and 'docs')") 

    import traci 
    routeFile="data2/cross.rou.xml" 
    PORT = 8873 

    #SIGN CONFIGURATIONS : NESW 
    NSgreen = "GrGr" 
    EWgreen = "rGrG" 
    PROGRAM = (NSgreen,EWgreen) 


    def nbr_veh_entr_indloop(i,o): 
    # i et j se sont les inductions loop input et output 
     threading.Timer(60.0, nbr_veh_entr_indloop).start() 
     x = traci.inductionloop.getLastStepMeanLength(i) - traci.inductionloop.getLastStepMeanLength(o) 
     return x 
    def run(): 
     steps = open("data2/step.txt","w") 
     traci.init(int(PORT)) 
     step = 0 
     while step < 7200 : 
      a = nbr_veh_entr_indloop("4i","40") 
      k=str(a) 
      print >> steps , "nombre des veh : " + k #concaténation 
      traci.simulationStep() 
      step +=1 



     steps.close() 
     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') 


    # this is the normal way of using traci. sumo is started as a 
    # subprocess and then the python script connects and runs 
     sumoProcess = subprocess.Popen([sumoBinary, "-c", "data2/cross.sumocfg", "--tripinfo-output","tripinfo.xml", "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr) 
     run() 
     sumoProcess.wait() 

ありがとうございました。あなたはおそらくしたい

よろしく、

+0

コード内のインデントを修正することができます –

+0

今すぐ修正 – mk10

答えて

0

ので、タイマーがここに無意味です、値ごとに60秒シミュレーションではない、すべての60壁時計の秒を持っています。スムージングのデフォルトのステップ長を1秒と仮定して、60回のシミュレーションステップの後に値を求めるだけです。したがって、次のように書くことができます:

 if step % 60 == 0: 
     print >> steps , "nombre des veh : " + k 

これは60ステップごとに最後のステップの値を出力します。最後の分の値が必要な場合は、自分で集計する必要があります。

+0

アウトプットファイルには、1つのステップという1行だけが含まれています。 – mk10

関連する問題