2016-07-18 3 views
0

Ich schrieb das Skript unten mit Python und ich implementierte es auf Sumo, um die Anzahl der Fahrzeuge zwischen zwei Induktionsschleife, alle 60 Sekunden, in einer Spur zu erhalten. Aber dieser gibt jede Sekunde.Anzahl der Fahrzeuge jede T Sekunden

#!/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() 

Vielen Dank für Hilfe im Voraus. alle 60 Sekunden Simulation nicht alle 60 Sekunden Wanduhr, so ein Timer sinnlos ist hier

Grüße,

+0

können Sie den Einzug in Ihrem Code beheben –

+0

es ist jetzt behoben – mk10

Antwort

0

Sie haben wahrscheinlich den Wert haben wollen. Fragen Sie einfach nach 60 Simulationsschritten nach dem Wert (vorausgesetzt, Sie verwenden die Standardschrittlänge von sumo von einer Sekunde). So könnten Sie etwas wie schreiben:

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

Dies wird den Wert für den letzten Schritt alle 60 Schritte drucken. Wenn Sie den Wert für die letzte Minute möchten, müssen Sie sich selbst aggregieren (summieren).

+0

Die out-Put-Datei enthalten nur eine Zeile, die einen Schritt sagen. – mk10