0

我用python编写了下面的脚本,并在sumo上实现了它,以获得两个inductionLoop之间的车辆数量,每60秒,在一条车道上。但这一个给每一秒。

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

提前感谢您的帮助。

问候,

4

1 回答 1

0

您可能希望每 60 个模拟秒而不是每 60 个挂钟秒获得一次值,因此计时器在这里毫无意义。只需在 60 个模拟步骤后询问该值(假设您使用相扑的默认步长一秒)。所以你可以写这样的东西:

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

这将每 60 步打印最后一步的值。如果您想要最后一分钟的值,您需要自己汇总(总结)。

于 2016-07-18T21:23:06.090 回答