def run_marouter(options, first_depart, last_depart, trip_file, weight_file): aggregation = 1800 begin = (int(first_depart) / aggregation) * aggregation marouter_dir = os.path.join(options.iteration_dir, 'marouter') if not os.path.exists(marouter_dir): os.makedirs(marouter_dir) marocfg = abspath_in_dir(marouter_dir, 'tapas.marocfg') ma_routes = abspath_in_dir(marouter_dir, 'ma_flows.rou.xml') ma_weights = abspath_in_dir(marouter_dir, 'ma_weights.xml') with open(marocfg, 'w') as f: f.write("""<configuration> <net-file value="%s"/> <route-files value="%s"/> <additional-files value="%s"/> <taz-param value="taz_id_start,taz_id_end"/> <output value="%s"/> <netload-output value="%s"/> <log-file value="marouter.log"/> <ignore-errors value="true"/> <routing-algorithm value="astar"/> <routing-threads value="16"/> <max-iterations value="5"/> <begin value="%s"/> <end value="%s"/> </configuration>""" % (options.net_file, trip_file, options.taz_file, ma_routes, ma_weights, begin, last_depart + TAPAS_EXTRA_TIME)) with working_dir(marouter_dir): subprocess.check_call( [sumolib.checkBinary('marouter'), "-c", marocfg, "-v"]) return ma_routes, ma_weights
def run_pedestrian_sumo(options, routefile): personfile = abspath_in_dir(options.iteration_dir, 'persons.xml') sumocfg = abspath_in_dir(options.iteration_dir, 'persons.sumocfg') sim_start, sim_end = create_personfile(options.mapped_trips, routefile, personfile) with open(sumocfg, 'w') as f: f.write( #<no-duration-log value="true"/> """<configuration> <net-file value="%s"/> <route-files value="%s"/> <additional-files value="%s"/> <tripinfo-output value="person_tripinfo.xml"/> <no-step-log value="true"/> <log-file value="persons.sumo.log"/> <begin value="%s"/> <end value="%s"/> <phemlight-path value="%s"/> </configuration>""" % (options.net_file, personfile, options.vtype_file, sim_start, sim_end + TAPAS_EXTRA_TIME, options.phemlight_path)) return call([sumolib.checkBinary("sumo"), "-c", sumocfg])
def run_sumo(options, first_depart, last_depart, trip_file, weight_file, meso=True): output_dir = options.iteration_dir suffix = "sim_meso" if meso else "sim_micro" sim_routes = abspath_in_dir(output_dir, 'vehroutes_%s.rou.xml' % suffix) sim_weights = abspath_in_dir(output_dir, 'aggregated_%s.xml' % suffix) aggregation = 1800 begin = (int(first_depart) / aggregation) * aggregation additional = [options.vtype_file, 'dump_' + suffix + '.xml'] offsetFile = os.path.join(os.path.dirname(options.vtype_file), 'tlsOffsets.add.xml') if os.path.exists(offsetFile): additional.append(offsetFile) sumocfg = abspath_in_dir(output_dir, '%s.sumocfg' % suffix) with open(sumocfg, 'w') as f: f.write("""<configuration> <net-file value="%s"/> <route-files value="%s"/> <additional-files value="%s"/> <vehroute-output value="%s"/> <vehroute-output.sorted value="true"/> <vehroute-output.last-route value="true"/> <vehroute-output.intended-depart value="true"/> <vehroute-output.route-length value="true"/> <no-step-log value="true"/> <log-file value="%s.sumo.log"/> <ignore-route-errors value="true"/> <begin value="%s"/> <end value="%s"/> <time-to-teleport value="60"/> <mesosim value="%s"/> <meso-recheck value="10"/> <meso-multi-queue value="true"/> <meso-junction-control.limited value="true"/> <meso-minor-penalty value="0.5"/> <meso-tls-penalty value="1.0"/> </configuration>""" % (options.net_file, trip_file, ','.join(additional), sim_routes, suffix, begin, last_depart + TAPAS_EXTRA_TIME, meso)) with working_dir(output_dir): with open(additional[1], 'w') as f: f.write( '<a>\n <edgeData id="dump" freq="%s" file="%s" excludeEmpty="true" minSamples="1"/>\n</a>' % (aggregation, sim_weights)) subprocess.check_call( [sumolib.checkBinary('sumo'), "-c", sumocfg, "-v"]) return sim_routes, sim_weights
def run_emission_sumo(options, params, conn, routefile, emission_model="HBEFA3", meso=True): if conn is None: return None car_types = emissions.get_car_types(params, conn, emission_model) vtype, vtypes_file = emissions.modify_vtypes(options, car_types) mod_routes_file = abspath_in_dir(options.iteration_dir, 'emission_routes.xml') with open(mod_routes_file, 'w') as f: f.write( '<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">\n' ) for vehicle in sumolib.output.parse(routefile, "vehicle"): if vehicle.id in vtype: # this will skip the clones vehicle.type = vtype[vehicle.id] f.write(vehicle.toXML(" ")) f.write('</routes>\n') sumocfg = abspath_in_dir(options.iteration_dir, 'emissions.sumocfg') with open(sumocfg, 'w') as f: f.write("""<configuration> <net-file value="%s"/> <route-files value="%s"/> <additional-files value="%s"/> <no-step-log value="true"/> <log-file value="emissions.sumo.log"/> <mesosim value="%s"/> <meso-recheck value="10"/> <meso-multi-queue value="true"/> <meso-junction-control.limited value="true"/> </configuration>""" % (options.net_file, mod_routes_file, vtypes_file, meso)) return call([sumolib.checkBinary("sumo"), "-c", sumocfg])
def run_trajectory_sumo(options, net_file, route_file): sumocfg = abspath_in_dir(options.iteration_dir, 'trajectories.sumocfg') with open(sumocfg, 'w') as f: f.write( """<configuration> <net-file value="%s"/> <route-files value="%s"/> <additional-files value="%s"/> <fcd-output value="trajectories.xml"/> <no-step-log value="true"/> <log-file value="trajectories.sumo.log"/> <phemlight-path value="%s"/> </configuration>""" % (net_file, route_file, options.vtype_file, options.phemlight_path)) return call([sumolib.checkBinary("sumo"), "-c", sumocfg])
def modify_vtypes(options, car_types, suffix=""): vtype = {} orig_types = {} unknown_types = set() emission_vtypes = defaultdict(set) for row in csv.DictReader(open(options.mapped_trips)): sumo_type = row[TH.vtype] ct = int(row[TH.car_type]) emission_class = car_types.get(ct) if emission_class is None: if ct not in unknown_types: print("Unknown car_type %s in %s" % (ct, row)) unknown_types.add(ct) vtype[build_uid(row)] = sumo_type else: vtype[build_uid(row)] = "%s_%s%s" % (sumo_type, emission_class, suffix) emission_vtypes[sumo_type].add(emission_class) for vt in sumolib.output.parse(options.vtype_file, "vType"): orig_types[vt.id] = vt vtypes_file = abspath_in_dir(os.path.dirname(options.mapped_trips), 'emission_types.xml') with open(vtypes_file, 'w') as f: f.write( '<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">\n' ) for t, s in emission_vtypes.items(): vt = orig_types[t] for e_class in sorted(s): if e_class is None: if vt.hasAttribute("emissionClass"): del vt.emissionClass else: vt.id = "%s_%s%s" % (t, e_class, suffix) vt.setAttribute("emissionClass", e_class) f.write(vt.toXML(" ")) f.write('</routes>\n') return vtype, vtypes_file
def run_duaiterate(options, first_depart, last_depart, trip_file, weight_file, meso=True): dua_dir = os.path.join(options.iteration_dir, 'dua') if not os.path.exists(dua_dir): os.makedirs(dua_dir) duaIterate_params = abspath_in_dir(dua_dir, 'duaIterate.params') aggregation = 1800 begin = (int(first_depart) / aggregation) * aggregation additional = [options.vtype_file] if options.bidi_taz_file: additional.append(options.bidi_taz_file) trips = [trip_file] if os.path.exists(options.background_trips): trips.append(options.background_trips) params = [ '--router-verbose', '--net-file', options.net_file, '--trips', ",".join(trips), '--additional', ','.join(additional), '--begin', str(begin), '--end', str(last_depart + TAPAS_EXTRA_TIME), '--nointernal-link', # compromise between cheating and solving deadlocks '--time-to-teleport', '60', # '--route-steps','200', # work around a bug where not enough vehicles are emitted '--last-step', str(options.last_step), '--disable-summary', '--routing-algorithm', options.routing_algorithm, # 3600 may be to imprecise for convergence, compromise weight # aggregation since CH is static '--aggregation', str(aggregation), # to many alts cost space/time and may hinder convergence '--max-alternatives', '3', '--continue-on-unbuild', '--inc-base', '100', # '--incrementation', '1', # '--inc-start', '0.40', # # '--inc-max', str(scale), # we scale on trip level now '--router-verbose', # '--zip', # '--disable-tripinfos', '--vehroute-file', 'routesonly', # gawrons a (default 0.5): higher values increase route probability # volatility '--gA', '1', # '--binary', # do we dare use binary mode? # '--time-inc', # reduce running time on initial runs... '--weight-memory', ] if meso: params += [ '--mesosim', '--meso-recheck', '10', '--meso-multiqueue', # now come the positional args (needed when passing negative values '--', 'sumo--meso-tauff', '1.4', 'sumo--meso-taufj', '1.4', 'sumo--meso-taujf', '2.0', 'sumo--meso-taujj', '2.0', 'sumo--meso-jam-threshold', '-1', # edge speed specific threshold 'sumo--meso-junction-control.limited', ] params += [ 'sumo--phemlight-path', options.phemlight_path, # city traffic has shorter headways than highway traffic... 'sumo--max-depart-delay', '300', 'duarouter--phemlight-path', options.phemlight_path, 'duarouter--additional-files', ','.join(additional), 'duarouter--vtype-output', '/dev/null', 'duarouter--routing-threads', '16', ] with open(duaIterate_params, 'w') as f: print(os.linesep.join(params), file=f) with working_dir(dua_dir): duaIterate.main(params) routes = abspath_in_dir(dua_dir, "vehroute_%03i.xml" % (options.last_step - 1)) weights = abspath_in_dir( dua_dir, "dump_%03i_%s.xml" % (options.last_step - 1, aggregation)) return routes, weights
def run_oneshot(options, first_depart, last_depart, trip_file, weight_file, meso=True, addOpt=""): oneshot_dir = os.path.join(options.iteration_dir, 'oneshot') if not os.path.exists(oneshot_dir): os.makedirs(oneshot_dir) suffix = "oneshot_meso" if meso else "oneshot_micro" oneshot_routes = abspath_in_dir(oneshot_dir, 'vehroutes_%s.rou.xml' % suffix) oneshot_weights = abspath_in_dir(oneshot_dir, 'aggregated_%s.xml' % suffix) if os.path.exists(oneshot_routes): print("Route file", oneshot_routes, "exists! Skipping assignment.") return oneshot_routes, oneshot_weights aggregation = 1800 begin = (int(first_depart) / aggregation) * aggregation additional = [options.vtype_file, 'dump_' + suffix + '.xml'] if options.bidi_taz_file: additional.append(options.bidi_taz_file) base_dir = os.path.dirname(options.net_file) if not meso: for add in (os.path.join(base_dir, 'tlsOffsets.add.xml'), os.path.join(oneshot_dir, 'vehroutes_%s_tls.add.xml' % suffix)): if os.path.exists(add): additional.append(add) extra_opt = addOpt.split() extra_cfg = os.path.join(base_dir, 'extra.params') if os.path.exists(extra_cfg): extra_opt += open(extra_cfg).read().split() trips = [trip_file] if os.path.exists(options.background_trips): trips.append(options.background_trips) additional.append(os.path.join(base_dir, 'suburb.taz.xml')) additional += sorted(glob.glob(os.path.join(base_dir, 'public*.xml'))) tempcfg = abspath_in_dir(oneshot_dir, '%s_temp.sumocfg' % suffix) addOpt = "" if meso: addOpt += """ <mesosim value="true"/> <meso-recheck value="10"/> <meso-multi-queue value="true"/> <meso-jam-threshold value="-0.5"/> <meso-junction-control.limited value="true"/> <meso-minor-penalty value="0.5"/> <meso-tls-penalty value="0.5"/>""" if meso and os.path.exists(os.path.join(base_dir, "landmarks.csv")): addOpt += """ <astar.landmark-distances value="%s"/>""" % os.path.join( base_dir, "landmarks.csv") with open(tempcfg, 'w') as f: f.write("""<configuration> <net-file value="%s"/> <route-files value="%s"/> <additional-files value="%s"/> <vehroute-output value="%s"/> <vehroute-output.sorted value="true"/> <vehroute-output.last-route value="true"/> <vehroute-output.intended-depart value="true"/> <vehroute-output.route-length value="true"/> <vehroute-output.skip-ptlines value="true"/> <pedestrian.model value="nonInteracting"/> <routing-algorithm value="astar"/> <device.rerouting.probability value="1"/> <device.rerouting.threads value="16"/> <device.rerouting.adaptation-interval value="10"/> <device.rerouting.adaptation-weight value="0.5"/> <device.rerouting.period value="300"/> <device.rerouting.pre-period value="10"/> <save-state.period value="3600"/> <save-state.suffix value=".xml.gz"/> <summary-output value="summary.xml.gz"/> <no-step-log value="true"/> <log-file value="%s.sumo.log"/> <ignore-route-errors value="true"/> <begin value="%s"/> <end value="%s"/> %s <phemlight-path value="%s"/> </configuration>""" % (options.net_file, ",".join(trips), ','.join(additional), oneshot_routes, suffix, begin, last_depart + TAPAS_EXTRA_TIME, addOpt, options.phemlight_path)) oneshotcfg = abspath_in_dir(oneshot_dir, '%s.sumocfg' % suffix) oneshot_emissions = abspath_in_dir(oneshot_dir, 'emissions_%s.xml' % suffix) with working_dir(oneshot_dir): with open(additional[1], 'w') as f: f.write( '<additional>\n <edgeData id="dump" freq="%s" file="%s" excludeEmpty="true" minSamples="1"/>\n' % (aggregation, oneshot_weights)) # f.write(' <edgeData type="emissions" id="dump_emission" freq="%s" file="%s" excludeEmpty="true" minSamples="1"/>\n' % # (aggregation, oneshot_emissions)) f.write('</additional>\n') subprocess.check_call([ sumolib.checkBinary('sumo'), "-c", tempcfg, "--save-configuration", oneshotcfg ] + extra_opt) os.remove(tempcfg) subprocess.check_call( [sumolib.checkBinary('sumo'), "-c", oneshotcfg, "-v"]) return oneshot_routes, oneshot_weights
def checkOptions(options): if not hasattr(options, "net"): assert options.net_file is not None, "scenario net file is not given" options.net_file = os.path.abspath(options.net_file) assert os.path.isfile( options.net_file), "the given net file %s does not exist" % ( options.net_file) if options.domap or options.rectify: options.net = sumolib.net.readNet(options.net_file, withFoes=False, withConnections=False) if options.vtype_file is None: options.vtype_file = abspath_in_dir(os.path.dirname(options.net_file), "vtypes.xml") else: options.vtype_file = os.path.abspath(options.vtype_file) if options.taz_file is None: options.taz_file = abspath_in_dir(os.path.dirname(options.net_file), "districts.taz.xml") else: options.taz_file = os.path.abspath(options.taz_file) if options.bidi_taz_file: options.bidi_taz_file = os.path.abspath(options.bidi_taz_file) assert options.tapas_trips is not None, "tripfile is not given" options.tapas_trips = os.path.abspath(options.tapas_trips) assert os.path.isfile( options.tapas_trips), "the given tripfile %s does not exist" % ( options.tapas_trips) base = os.path.basename(options.tapas_trips)[:-4] if options.trips_dir is None: assert options.iteration_dir is not None, "iteration or trips directory need to be given" assert os.path.isdir( options.iteration_dir ), "the given iteration directory %s is not accessible" % options.iteration_dir options.trips_dir = os.path.join(options.iteration_dir, 'trips') options.rectified = abspath_in_dir(options.trips_dir, 'rectified_%s.csv' % base) options.rectified_log = abspath_in_dir(options.trips_dir, 't2s_rectify_%s.log' % base) options.mapped_trips = abspath_in_dir(options.trips_dir, 'mapped_%s.csv' % base) options.mapped_log = abspath_in_dir(options.trips_dir, 't2s_map_%s.log' % base) options.trips_for_dua = getSumoTripfileName(options.trips_dir, options.tapas_trips) if not hasattr(options, "script_module"): options.script_module = None if not hasattr(options, "background_trips"): options.background_trips = "" if options.max_spatial_diffusion > 0: assert options.spatial_diffusion > 0, "initial standard deviation needed for spatial diffusion" if type(options.spatial_diffusion_bounds) is str: options.spatial_diffusion_bounds = [ int(e) for e in options.spatial_diffusion_bounds.split(",") ]
def getSumoTripfileName(trips_dir, tapas_trips): base = os.path.basename(tapas_trips)[:-4] return abspath_in_dir(trips_dir, 'miv_%s.trips.xml' % base)