def main(): options = parse_args() departCounts = defaultdict(lambda: 0) arrivalCounts = defaultdict(lambda: 0) intermediateCounts = defaultdict(lambda: 0) for element in options.elements: for route in parse_fast(options.routefile, element, ['edges']): edges = route.edges.split() if not hasSubpart(edges, options.subparts): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 for e in edges: intermediateCounts[e] += 1 # warn about potentially missing edges fromAttr, toAttr = ('fromTaz', 'toTaz') if options.taz else ('from', 'to') if 'trip' in options.elements: for trip in parse_fast(options.routefile, 'trip', ['id', fromAttr, toAttr]): if options.subparts: sys.stderr.write("Warning: Ignoring trips when using --subpart\n") break departCounts[trip[1]] += 1 arrivalCounts[trip[2]] += 1 if 'walk' in options.elements: for walk in parse_fast(options.routefile, 'walk', ['from', 'to']): if options.subparts: sys.stderr.write("Warning: Ignoring trips when using --subpart\n") break departCounts[walk.attr_from] += 1 arrivalCounts[walk.to] += 1 departStats = Statistics("departEdges") arrivalStats = Statistics("arrivalEdges") intermediateStats = Statistics("intermediateEdges") for e in sorted(departCounts.keys()): departStats.add(departCounts[e], e) for e in sorted(arrivalCounts.keys()): arrivalStats.add(arrivalCounts[e], e) print(departStats) print(arrivalStats) if options.intermediate: for e in sorted(intermediateCounts.keys()): intermediateStats.add(intermediateCounts[e], e) print(intermediateStats) with open(options.outfile, 'w') as outf: outf.write("<edgedata>\n") outf.write(' <interval begin="0" end="10000" id="routeStats">\n') allEdges = set(departCounts.keys()) allEdges.update(arrivalCounts.keys()) if options.intermediate: allEdges.update(intermediateCounts.keys()) for e in sorted(allEdges): intermediate = ' intermediate="%s"' % intermediateCounts[e] if options.intermediate else '' outf.write(' <edge id="%s" departed="%s" arrived="%s" delta="%s"%s/>\n' % (e, departCounts[e], arrivalCounts[e], arrivalCounts[e] - departCounts[e], intermediate)) outf.write(" </interval>\n") outf.write("</edgedata>\n")
def main(): options = parse_args() edges = set() for routefile in options.routefiles: for route in parse_fast(routefile, 'route', ['edges']): edges.update(route.edges.split()) for walk in parse_fast(routefile, 'walk', ['edges']): edges.update(walk.edges.split()) # warn about potentially missing edges for trip in parse(routefile, ['trip', 'flow'], heterogeneous=True): edges.update([trip.attr_from, trip.to]) if trip.via is not None: edges.update(trip.via.split()) print( "Warning: Trip %s is not guaranteed to be connected within the extracted edges." % trip.id) for walk in parse_fast(routefile, 'walk', ['from', 'to']): edges.update([walk.attr_from, walk.to]) print("Warning: Walk from %s to %s is not guaranteed to be connected within the extracted edges." % ( walk.attr_from, walk.to)) with open(options.outfile, 'w') as outf: for e in sorted(list(edges)): outf.write('edge:%s\n' % e)
def main(): options = parse_args() edges = set() for routefile in options.routefiles: for route in parse_fast(routefile, 'route', ['edges']): edges.update(route.edges.split()) for walk in parse_fast(routefile, 'walk', ['edges']): edges.update(walk.edges.split()) # warn about potentially missing edges for trip in parse(routefile, ['trip', 'flow'], heterogeneous=True): edges.update([trip.attr_from, trip.to]) if trip.via is not None: edges.update(trip.via.split()) print( "Warning: Trip %s is not guaranteed to be connected within the extracted edges." % trip.id) for walk in parse_fast(routefile, 'walk', ['from', 'to']): edges.update([walk.attr_from, walk.to]) print( "Warning: Walk from %s to %s is not guaranteed to be connected within the extracted edges." % (walk.attr_from, walk.to)) with open(options.outfile, 'w') as outf: for e in sorted(list(edges)): outf.write('edge:%s\n' % e)
def main(): options = parse_args() departCounts = defaultdict(lambda: 0) arrivalCounts = defaultdict(lambda: 0) for route in parse_fast(options.routefile, 'route', ['edges']): edges = route.edges.split() if options.subpart is not None and not hasSubpart( edges, options.subpart): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 for walk in parse_fast(options.routefile, 'walk', ['edges']): edges = walk.edges.split() if options.subpart is not None and not hasSubpart( edges, options.subpart): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 # warn about potentially missing edges for trip in parse_fast(options.routefile, 'trip', ['id', 'fromTaz', 'toTaz']): if options.subpart is not None: sys.stderr.print("Warning: Ignoring trips when using --subpart") break departCounts[trip.fromTaz] += 1 arrivalCounts[trip.toTaz] += 1 for walk in parse_fast(options.routefile, 'walk', ['from', 'to']): if options.subpart is not None: sys.stderr.print("Warning: Ignoring trips when using --subpart") break departCounts[walk.attr_from] += 1 arrivalCounts[walk.to] += 1 departStats = Statistics("departEdges") arrivalStats = Statistics("arrivalEdges") for e in departCounts.keys(): departStats.add(departCounts[e], e) for e in arrivalCounts.keys(): arrivalStats.add(arrivalCounts[e], e) print(departStats) print(arrivalStats) with open(options.outfile, 'w') as outf: outf.write("<edgedata>\n") outf.write(' <interval begin="0" end="10000" id="routeStats">\n') allEdges = set(departCounts.keys()) allEdges.update(arrivalCounts.keys()) for e in sorted(list(allEdges)): outf.write( ' <edge id="%s" departed="%s" arrived="%s" delta="%s"/>\n' % (e, departCounts[e], arrivalCounts[e], arrivalCounts[e] - departCounts[e])) outf.write(" </interval>\n") outf.write("</edgedata>\n")
def parseSimple(outf, options): """parse elements without checking time""" departCounts = defaultdict(lambda: 0) arrivalCounts = defaultdict(lambda: 0) intermediateCounts = defaultdict(lambda: 0) for element in options.elements: for route in parse_fast(options.routefile, element, ['edges']): edges = route.edges.split() if not hasSubpart(edges, options.subparts): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 if options.intermediate: for e in edges: intermediateCounts[e] += 1 # warn about potentially missing edges fromAttr, toAttr = ('fromTaz', 'toTaz') if options.taz else ('from', 'to') if 'trip' in options.elements: for trip in parse_fast(options.routefile, 'trip', ['id', fromAttr, toAttr]): if options.subparts: sys.stderr.write( "Warning: Ignoring trips when using --subpart\n") break departCounts[trip[1]] += 1 arrivalCounts[trip[2]] += 1 if 'walk' in options.elements: for walk in parse_fast(options.routefile, 'walk', ['from', 'to']): if options.subparts: sys.stderr.write( "Warning: Ignoring trips when using --subpart\n") break departCounts[walk.attr_from] += 1 arrivalCounts[walk.to] += 1 if options.net_file: net = sumolib.net.readNet(options.net_file) with open(options.poi_file, "w") as pois: sumolib.xml.writeHeader(pois, root="additional") allEdges = set(departCounts.keys()) allEdges.update(arrivalCounts.keys()) for e in sorted(allEdges): if departCounts[e] > options.min_count or arrivalCounts[ e] > options.min_count: lon, lat = net.convertXY2LonLat( *net.getEdge(e).getShape()[0]) pois.write(' <poi id="%s" lon="%.6f" lat="%.6f">\n' % (e, lon, lat)) pois.write(' <param key="departed" value="%s"/>\n' % departCounts[e]) pois.write( ' <param key="arrived" value="%s"/>\n </poi>\n' % arrivalCounts[e]) pois.write("</additional>\n") writeInterval(outf, options, departCounts, arrivalCounts, intermediateCounts)
def main(): options = parse_args() departCounts = defaultdict(lambda: 0) arrivalCounts = defaultdict(lambda: 0) for route in parse_fast(options.routefile, 'route', ['edges']): edges = route.edges.split() if options.subpart is not None and not hasSubpart(edges, options.subpart): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 for walk in parse_fast(options.routefile, 'walk', ['edges']): edges = walk.edges.split() if options.subpart is not None and not hasSubpart(edges, options.subpart): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 # warn about potentially missing edges for trip in parse_fast(options.routefile, 'trip', ['id', 'fromTaz', 'toTaz']): if options.subpart is not None: sys.stderr.print("Warning: Ignoring trips when using --subpart") break departCounts[trip.fromTaz] += 1 arrivalCounts[trip.toTaz] += 1 for walk in parse_fast(options.routefile, 'walk', ['from', 'to']): if options.subpart is not None: sys.stderr.print("Warning: Ignoring trips when using --subpart") break departCounts[walk.attr_from] += 1 arrivalCounts[walk.to] += 1 departStats = Statistics("departEdges") arrivalStats = Statistics("arrivalEdges") for e in departCounts.keys(): departStats.add(departCounts[e], e) for e in arrivalCounts.keys(): arrivalStats.add(arrivalCounts[e], e) print(departStats) print(arrivalStats) with open(options.outfile, 'w') as outf: outf.write("<edgedata>\n") outf.write(' <interval begin="0" end="10000" id="routeStats">\n') allEdges = set(departCounts.keys()) allEdges.update(arrivalCounts.keys()) for e in sorted(list(allEdges)): outf.write(' <edge id="%s" departed="%s" arrived="%s" delta="%s"/>\n' % (e, departCounts[e], arrivalCounts[e], arrivalCounts[e] - departCounts[e])) outf.write(" </interval>\n") outf.write("</edgedata>\n")
def filterLog(log="data/sumo_log.txt", statsOut="data/stats.txt", statsIn="stats.scenario", tripinfos="data/tripinfos.xml"): collisions = 0 timeout = 0 simEnd = -1 for line in open(log): if "collision" in line: collisions += 1 if "waited too long" in line: timeout += 1 if line.startswith("Simulation ended at time: "): simEnd = line.split()[-1] if os.path.exists(tripinfos): durationStats = Statistics(' Traveltimes') for trip in parse_fast(tripinfos, 'tripinfo', ['id', 'duration']): durationStats.add(float(trip.duration), trip.id) durationStats = str(durationStats).replace('"','') else: durationStats = '' statLine = "Collisions: %s Timeouts: %s End: %s%s" % (collisions, timeout, simEnd, durationStats) with open(statsOut, 'w') as o: rootLength = len(os.environ["TEXTTEST_SANDBOX_ROOT"]) + 1 testNameStart = os.environ["TEXTTEST_SANDBOX"].find("/", rootLength) + 1 oldStats = os.path.join(os.environ["TEXTTEST_ROOT"], os.environ["TEXTTEST_SANDBOX"][testNameStart:], statsIn) if os.path.exists(oldStats): for line in open(oldStats): o.write(line) if line.strip() == statLine.strip(): o.close() return sumoVersion = subprocess.check_output(get_app('sumo', 'SUMO_BINARY') + " -V", shell=True).splitlines()[0] print("%s %s\n%s" % (datetime.now(), sumoVersion, statLine), file=o)
def getTLPairs(net, routeFile, speedFactor): # pairs of traffic lights TLPairs = {} # PairKey -> PairData for route in parse_fast(routeFile, 'route', ['edges']): rTLSList = getTLSInRoute(net, route.edges.split()) for oldTL, TLelement in zip(rTLSList[:-1], rTLSList[1:]): key = PairKey(oldTL.edgeID, TLelement.edgeID, oldTL.dist) numVehicles = 0 if key not in TLPairs else TLPairs[key].numVehicles tl = net.getEdge(TLelement.edgeID).getTLS() otl = net.getEdge(oldTL.edgeID).getTLS() edge = net.getEdge(TLelement.edgeID) connection = TLelement.connection oconnection = oldTL.connection ogreen = getFirstGreenOffset(otl, oconnection) green = getFirstGreenOffset(tl, connection) travelTime = TLelement.time / speedFactor betweenOffset = travelTime + ogreen - green startOffset = 0 # relevant data for a pair of traffic lights TLPairs[key] = PairData(otl, oconnection, tl, connection, betweenOffset, startOffset, travelTime, edge.getPriority(), numVehicles + 1, ogreen, green) return TLPairs
def parse_trip_durations(): result = [] for file in sorted(glob.glob("tripinfo_*.xml")): result.append([ float(t.duration) for t in parse_fast(file, 'tripinfo', ['duration']) ]) return result
def accelStats(netstate): lastSpeed = {} stats = Statistics("Accelerations", histogram=True, printMin=True, scale=0.2) for vehicle in parse_fast(netstate, 'vehicle', ['id', 'speed']): speed = float(vehicle.speed) prevSpeed = lastSpeed.get(vehicle.id, speed) stats.add(speed - prevSpeed, (vehicle.id, vehicle.speed)) lastSpeed[vehicle.id] = speed print stats
def parseSimple(outf, options): """parse elements without checking time""" departCounts = defaultdict(lambda: 0) arrivalCounts = defaultdict(lambda: 0) intermediateCounts = defaultdict(lambda: 0) for element in options.elements: for route in parse_fast(options.routefile, element, ['edges']): edges = route.edges.split() if not hasSubpart(edges, options.subparts): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 for e in edges: intermediateCounts[e] += 1 # warn about potentially missing edges fromAttr, toAttr = ('fromTaz', 'toTaz') if options.taz else ('from', 'to') if 'trip' in options.elements: for trip in parse_fast(options.routefile, 'trip', ['id', fromAttr, toAttr]): if options.subparts: sys.stderr.write( "Warning: Ignoring trips when using --subpart\n") break departCounts[trip[1]] += 1 arrivalCounts[trip[2]] += 1 if 'walk' in options.elements: for walk in parse_fast(options.routefile, 'walk', ['from', 'to']): if options.subparts: sys.stderr.write( "Warning: Ignoring trips when using --subpart\n") break departCounts[walk.attr_from] += 1 arrivalCounts[walk.to] += 1 writeInterval(outf, options, departCounts, arrivalCounts, intermediateCounts)
@date 2012-10-11 @version $Id: extract_route_edges.py 16005 2014-03-24 12:46:02Z cschmidt87 $ Extract all used edges from routes and person plans and output a file suitable for pruning edges with netconvert SUMO, Simulation of Urban MObility; see http://sumo-sim.org/ Copyright (C) 2012-2014 DLR (http://www.dlr.de/) and contributors This file is part of SUMO. SUMO is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. """ import os, sys from collections import defaultdict sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sumolib.output import parse, parse_fast route_file, keep_file = sys.argv[1:] edges = set() for route in parse_fast(route_file, 'route', ['edges']): edges.update(route.edges.split()) for walk in parse_fast(route_file, 'walk', ['edges']): edges.update(walk.edges.split()) with open(keep_file, 'w') as outf: outf.write(','.join(edges) + '\n')
def parse_trip_durations(): result = [] for file in sorted(glob.glob("tripinfo_*.xml")): result.append([float(t.duration) for t in parse_fast(file, 'tripinfo', ['duration'])]) return result
def main(): options = parse_args() departCounts = defaultdict(lambda: 0) arrivalCounts = defaultdict(lambda: 0) intermediateCounts = defaultdict(lambda: 0) for route in parse_fast(options.routefile, 'route', ['edges']): edges = route.edges.split() if not hasSubpart(edges, options.subparts): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 for e in edges: intermediateCounts[e] += 1 for walk in parse_fast(options.routefile, 'walk', ['edges']): edges = walk.edges.split() if not hasSubpart(edges, options.subparts): continue departCounts[edges[0]] += 1 arrivalCounts[edges[-1]] += 1 for e in edges: intermediateCounts[e] += 1 # warn about potentially missing edges fromAttr, toAttr = ('fromTaz', 'toTaz') if options.taz else ('from', 'to') for trip in parse_fast(options.routefile, 'trip', ['id', fromAttr, toAttr]): if options.subparts: sys.stderr.write("Warning: Ignoring trips when using --subpart\n") break departCounts[trip[1]] += 1 arrivalCounts[trip[2]] += 1 for walk in parse_fast(options.routefile, 'walk', ['from', 'to']): if options.subparts: sys.stderr.write("Warning: Ignoring trips when using --subpart\n") break departCounts[walk.attr_from] += 1 arrivalCounts[walk.to] += 1 departStats = Statistics("departEdges") arrivalStats = Statistics("arrivalEdges") intermediateStats = Statistics("intermediateEdges") for e in sorted(departCounts.keys()): departStats.add(departCounts[e], e) for e in sorted(arrivalCounts.keys()): arrivalStats.add(arrivalCounts[e], e) print(departStats) print(arrivalStats) if options.intermediate: for e in sorted(intermediateCounts.keys()): intermediateStats.add(intermediateCounts[e], e) print(intermediateStats) with open(options.outfile, 'w') as outf: outf.write("<edgedata>\n") outf.write(' <interval begin="0" end="10000" id="routeStats">\n') allEdges = set(departCounts.keys()) allEdges.update(arrivalCounts.keys()) if options.intermediate: allEdges.update(intermediateCounts.keys()) for e in sorted(allEdges): intermediate = ' intermediate="%s"' % intermediateCounts[e] if options.intermediate else '' outf.write(' <edge id="%s" departed="%s" arrived="%s" delta="%s"%s/>\n' % (e, departCounts[e], arrivalCounts[e], arrivalCounts[e] - departCounts[e], intermediate)) outf.write(" </interval>\n") outf.write("</edgedata>\n")
#!/usr/bin/env python """ @file randomTrips.py @author Jakob Erdmann @date 2012-10-11 @version $Id$ Extract all used edges from routes and person plans and output a file suitable for pruning edges with netconvert SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ Copyright (C) 2010-2013 DLR (http://www.dlr.de/) and contributors All rights reserved """ import os,sys from collections import defaultdict sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sumolib.output import parse, parse_fast route_file, keep_file = sys.argv[1:] edges = set() for route in parse_fast(route_file, 'route', ['edges']): edges.update(route.edges.split()) for walk in parse_fast(route_file, 'walk', ['edges']): edges.update(walk.edges.split()) with open(keep_file, 'w') as outf: outf.write(','.join(edges) + '\n')
This file is part of SUMO. SUMO is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. """ import os import sys from collections import defaultdict sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sumolib.output import parse, parse_fast route_file, keep_file = sys.argv[1:] edges = set() for route in parse_fast(route_file, 'route', ['edges']): edges.update(route.edges.split()) for walk in parse_fast(route_file, 'walk', ['edges']): edges.update(walk.edges.split()) # warn about potentially missing edges for trip in parse_fast(route_file, 'trip', ['id', 'from', 'to']): edges.update([trip.attr_from, trip.to]) print( "Warning: Trip %s is not guaranteed to be connected within the extacted edges." % trip.id) for walk in parse_fast(route_file, 'walk', ['from', 'to']): edges.update([walk.attr_from, walk.to]) print("Warning: Walk from %s to %s is not guaranteed to be connected within the extacted edges." % ( walk.attr_from, walk.to)) with open(keep_file, 'w') as outf:
for i in range(1, len(sys.argv)): with open(sys.argv[i], 'r') as f: l = f.readlines() x = [] y = [] #for line in l[2:]: # s = [float(x) for x in line.split()] # y.append(s[-2]) #x = [i for i in range(1,len(y)+1)] file = sys.argv[i] #y = list(map(float, [e.halting for e in parse_fast(file, 'step',['halting'])])) y = list( map(lambda x: float(x) * 3.6, [e.meanSpeed for e in parse_fast(file, 'step', ['meanSpeed'])])) x = [i for i in range(1, len(y) + 1)] #plt.plot(x, y) y_av = movingaverage(y, 360) ax.plot(x, y_av, "navy", label='alpha = {}'.format(a[i])) #ax.legend() #plt.xticks(np.arange(0, 20001, step=1000)) plt.xlabel("Time Step") plt.ylabel("Mean Speed Km/h") #plt.ylabel("Total Nº Vehicles") #plt.ylabel("Average Nº Stopped Cars per TL") #plt.title("100% Exploração - Troca de Contexto no Timestep 5.000") # plt.title("QL alpha=0.5 gama=0.8 decay=0.90 epsilon=1 - Troca de contexto no Tempo 6.000") plt.grid() plt.show()