def doStep(): setting.step += 1 if setting.verbose: print "step", setting.step moveNodes = cmdSimulationStep(setting.step) for vehicleID, edge, pos in moveNodes: _checkInitialPositions(vehicleID, edge, pos) vehicle = vehicleStatus[vehicleID] if edge == vehicle.target and not vehicle.parking: if edge.startswith("footmain"): vehicle.parking = True target = "footmainout" if edge == "footmainout": row = random.randrange(0, DOUBLE_ROWS) target = "footmain%sto%s" % (row, row + 1) statistics.personArrived(vehicleID, edge, target, setting.step) setting.manager.personArrived(vehicleID, edge, target) if edge.startswith("cyber"): if vehicle.delay: vehicle.delay -= 1 elif setting.breakstep and setting.step >= setting.breakstep and edge != "cyberin": if setting.verbose: print "broken", vehicleID, edge setting.breakstep = None setting.manager.cyberCarBroken(vehicleID, edge) vehicle.delay = BREAK_DELAY else: if setting.verbose: print "arrived", vehicleID, edge vehicle.parking = True setting.manager.cyberCarArrived(vehicleID, edge) setting.manager.setNewTargets()
def doStep(): setting.step += 1 if setting.verbose: print "step", setting.step moveNodes = cmdSimulationStep(setting.step) for vehicleID, edge, pos in moveNodes: _checkInitialPositions(vehicleID, edge, pos) vehicle = vehicleStatus[vehicleID] if edge == vehicle.target and not vehicle.parking: if edge.startswith("footmain"): vehicle.parking = True target = "footmainout" if edge == "footmainout": row = random.randrange(0, DOUBLE_ROWS) target = "footmain%sto%s" % (row, row+1) statistics.personArrived(vehicleID, edge, target, setting.step) setting.manager.personArrived(vehicleID, edge, target) if edge.startswith("cyber"): if vehicle.delay: vehicle.delay -= 1 elif setting.breakstep and setting.step >= setting.breakstep and edge != "cyberin": if setting.verbose: print "broken", vehicleID, edge setting.breakstep = None setting.manager.cyberCarBroken(vehicleID, edge) vehicle.delay = BREAK_DELAY else: if setting.verbose: print "arrived", vehicleID, edge vehicle.parking = True setting.manager.cyberCarArrived(vehicleID, edge) setting.manager.setNewTargets()
def runSimulation(self): print "ESTA AKI!!!!!!!!!!!!!!!!!!!!!!" f = open('output.txt', 'a') f.write( 'Network = ' + sys.argv[1] + '\nnumpy.average(durations) numpy.average(routeLengths) avgOccupancyAvgs avgDevOccupancyStdDevs)\n' ) # create set of plans # os.system('python ../simulations/randomTrips2.py -n ' + self.__networkFile + ' -o trips.xml -r ' + self.__routesFile + ' -e ' + sys.argv[2] + ' -p ' + sys.argv[3] + ' -B ' + sys.argv[4]) print "ESTA AKI2!!!!!!!!!!!!!!!!!!!!!!" # calls for the parsing of the network file self.__population = Population(self.__routesFile, self.__network) if not self.__population.isValid(): sys.exit('Can\'t parse the file ' + self.__routesFile + '. Please recheck the file\'s name and syntax') GRAPHICAL_INTERFACE = 1 # starts sumo and opens communication with traci (-N para iniciar o sumo-gui em pausa) if (GRAPHICAL_INTERFACE): subprocess.Popen('%s -c %s -N' % (os.path.join( '..', '..', 'sumo-0.12.2', 'bin', 'sumo-gui'), sys.argv[1]), shell=True, stdout=sys.stdout) else: subprocess.Popen('%s -c %s' % (os.path.join( '..', '..', 'sumo-0.12.2', 'bin', 'sumo'), sys.argv[1]), shell=True, stdout=sys.stdout) initTraCI(self.__port) activeAgents = [] stoppedAgents = [] # runs until simulation time is over and there is no more active agents simulationStep = self.__simulationFirstStep testess = 0 while not (simulationStep >= self.__simulationFinalStep and activeAgents == []): # advances 1s in the simulation and checks for active agents simulationStep += 1 activeAgents = cmdSimulationStep(1000) if len(activeAgents) == 0: continue # updates status of stopped agents self.__accidentTimer(stoppedAgents) # updates the network weights and statistics and checks agent decisions, providing departed and arrived agents for the simulated step self.__updateEdgeWeights() #print self.__network #self.__network.updateOccupancyStats() self.__population.agentActions( simulationStep, cmdGetSimulationVariable_departedIDList(), cmdGetSimulationVariable_arrivedIDList(), testess, self) testess = (testess + 1) % 2 #print testess # simulates accidents #if random.random() < ACCIDENT_PROBABILITY: # self.__simulateAccident(stoppedAgents,simulationStep) # closes the communication with traci cmdClose() # adds simulation results to output file and decreases number of scheduled simulations self.__printSimulationOutput(f)
roots.append(root) for root in sorted(roots): print "-- Test: %s" % root[len(srcRoot)+1:] prefix = os.path.join(root, "input_") sys.stdout.flush() subprocess.call([netconvertBinary, "-n", prefix+"nodes.nod.xml", "-e", prefix+"edges.edg.xml", "-x", prefix+"connections.con.xml", "-o", "./input_net.net.xml"], stdout=sys.stdout) sys.stdout.flush() shutil.copy(prefix + "routes.rou.xml", "./input_routes.rou.xml") shutil.copy(prefix + "additional.add.xml", "./input_additional.add.xml") sumoProcess = subprocess.Popen("%s -c sumo.sumo.cfg" % (sumoBinary), shell=True, stdout=sys.stdout) traciControl.initTraCI(8813) step = 0 traciControl.cmdSimulationStep(DELTA_T) step += 1 lanes = traciControl.cmdGetVehicleVariable_bestLanes("0") sys.stdout.flush() for l in lanes: print "lane %s:" % (l[0]) print " length: %s" % (l[1]) print " offset: %s" % (l[3]) print " allowsContinuation: %s" % (l[4]) print " over: %s" % (l[5]) traciControl.cmdClose() sys.stdout.flush() fdi = open(root + "/expected.txt") for i,l in enumerate(lanes): vals = fdi.readline().strip().split()
vehNr, i) vehNr += 1 lastVeh = i print >> routes, "</routes>" routes.close() sumoExe = "sumo-gui" if "SUMO" in os.environ: sumoExe = os.path.join(os.environ["SUMO"], "sumo-gui") sumoConfig = "cross.sumo.cfg" sumoProcess = subprocess.Popen("%s -c %s" % (sumoExe, sumoConfig), shell=True, stdout=sys.stdout) initTraCI(PORT) programPointer = len(PROGRAM) - 1 veh = [] step = 0 while not (step > lastVeh and veh == []): veh = cmdSimulationStep(1000) programPointer = min(programPointer + 1, len(PROGRAM) - 1) no = cmdGetInductionLoopVariable_lastStepVehicleNumber("0") if no > 0: programPointer = (0 if programPointer == len(PROGRAM) - 1 else 3) cmdChangeTrafficLightsVariable_stateRYG("0", PROGRAM[programPointer]) step += 1 cmdClose()
vehNr += 1 lastVeh = i print >> routes, "</routes>" routes.close() sumoExe = "sumo-gui" if "SUMO" in os.environ: sumoExe = os.path.join(os.environ["SUMO"], "sumo-gui") sumoConfig = "cross.sumo.cfg" sumoProcess = subprocess.Popen("%s -c %s" % (sumoExe, sumoConfig), shell=True, stdout=sys.stdout) initTraCI(PORT) programPointer = len(PROGRAM)-1 veh = [] step = 0 while not (step > lastVeh and veh == []): veh = cmdSimulationStep(1000) programPointer = min(programPointer+1, len(PROGRAM)-1) no = cmdGetInductionLoopVariable_lastStepVehicleNumber("0") if no > 0: programPointer = (0 if programPointer == len(PROGRAM)-1 else 3) cmdChangeTrafficLightsVariable_stateRYG("0", PROGRAM[programPointer]) step += 1 cmdClose()