if random.random() < prob: idx = generate_one(idx) depart += 1 fouttrips.write("</trips>\n") if options.routefile: args = [DUAROUTER, '-n', options.netfile, '-t', options.tripfile, '-o', options.routefile, '--ignore-errors', '--begin', str(options.begin), '--end', str(options.end), '--no-step-log', '--no-warnings'] if options.additional is not None: args += ['--additional-files', options.additional] print("calling ", " ".join(args)) subprocess.call(args) if options.validate: print("calling route2trips") route2trips.main([options.routefile], outfile=options.tripfile) if options.weights_outprefix: trip_generator.source_generator.write_weights( options.weights_outprefix + SOURCE_SUFFIX) trip_generator.sink_generator.write_weights( options.weights_outprefix + SINK_SUFFIX) trip_generator.via_generator.write_weights( options.weights_outprefix + VIA_SUFFIX) # return wether trips could be genreated as requested return trip_generator is not None if __name__ == "__main__": if not main(get_options()): sys.exit(1)
def main(options): if options.seed: random.seed(options.seed) net = sumolib.net.readNet(options.netfile) if options.min_distance > net.getBBoxDiameter() * (options.intermediate + 1): options.intermediate = int( math.ceil(options.min_distance / net.getBBoxDiameter())) - 1 print(("Warning: setting number of intermediate waypoints to %s to achieve a minimum trip length of " + "%s in a network with diameter %.2f.") % ( options.intermediate, options.min_distance, net.getBBoxDiameter())) trip_generator = buildTripGenerator(net, options) idx = 0 vtypeattrs, options.tripattrs, personattrs, otherattrs = split_trip_attributes( options.tripattrs, options.pedestrians, options.vehicle_class) vias = {} def generate_one(idx): label = "%s%s" % (options.tripprefix, idx) try: source_edge, sink_edge, intermediate = trip_generator.get_trip( options.min_distance, options.max_distance, options.maxtries) via = "" if len(intermediate) > 0: via = ' via="%s" ' % ' '.join( [e.getID() for e in intermediate]) if options.validate: vias[label] = via if options.pedestrians: fouttrips.write( ' <person id="%s" depart="%.2f"%s>\n' % (label, depart, personattrs)) if options.persontrips: fouttrips.write( ' <personTrip from="%s" to="%s"%s/>\n' % ( source_edge.getID(), sink_edge.getID(), otherattrs)) else: fouttrips.write( ' <walk from="%s" to="%s"%s/>\n' % (source_edge.getID(), sink_edge.getID(), otherattrs)) fouttrips.write(' </person>\n') elif options.flows > 0: if options.binomial: for j in range(options.binomial): fouttrips.write((' <flow id="%s#%s" begin="%s" end="%s" probability="%s" ' + 'from="%s" to="%s"%s%s/>\n') % ( label, j, options.begin, options.end, 1.0 / options.period / options.binomial, source_edge.getID(), sink_edge.getID(), via, options.tripattrs)) else: fouttrips.write((' <flow id="%s" begin="%s" end="%s" period="%s" from="%s" ' + 'to="%s"%s%s/>\n') % ( label, options.begin, options.end, options.period * options.flows, source_edge.getID(), sink_edge.getID(), via, options.tripattrs)) else: fouttrips.write(' <trip id="%s" depart="%.2f" from="%s" to="%s"%s%s/>\n' % ( label, depart, source_edge.getID(), sink_edge.getID(), via, options.tripattrs)) except Exception as exc: print(exc, file=sys.stderr) return idx + 1 with open(options.tripfile, 'w') as fouttrips: sumolib.writeXMLHeader( fouttrips, "$Id$", "routes") if options.vehicle_class: fouttrips.write(' <vType id="%s" vClass="%s"%s/>\n' % (options.vtypeID, options.vehicle_class, vtypeattrs)) options.tripattrs += ' type="%s"' % options.vtypeID personattrs += ' type="%s"' % options.vtypeID depart = options.begin if trip_generator: if options.flows == 0: while depart < options.end: if options.binomial is None: # generate with constant spacing idx = generate_one(idx) depart += options.period else: # draw n times from a Bernoulli distribution # for an average arrival rate of 1 / period prob = 1.0 / options.period / options.binomial for i in range(options.binomial): if random.random() < prob: idx = generate_one(idx) depart += 1 else: for i in range(options.flows): idx = generate_one(idx) fouttrips.write("</routes>\n") if options.routefile: args = [DUAROUTER, '-n', options.netfile, '-r', options.tripfile, '-o', options.routefile, '--ignore-errors', '--begin', str(options.begin), '--end', str(options.end), '--no-step-log', '--no-warnings'] if options.additional is not None: args += ['--additional-files', options.additional] if options.carWalkMode is not None: args += ['--persontrip.transfer.car-walk', options.carWalkMode] if options.walkfactor is not None: args += ['--persontrip.walkfactor', options.walkfactor] print("calling ", " ".join(args)) subprocess.call(args) if options.validate: print("calling route2trips") route2trips.main([options.routefile], outfile=options.tripfile, vias=vias, calledBy=" via randomTrips.py") if options.weights_outprefix: trip_generator.source_generator.write_weights( options.weights_outprefix + SOURCE_SUFFIX) trip_generator.sink_generator.write_weights( options.weights_outprefix + SINK_SUFFIX) trip_generator.via_generator.write_weights( options.weights_outprefix + VIA_SUFFIX) # return wether trips could be generated as requested return trip_generator is not None
def main(options): if options.seed: random.seed(options.seed) net = sumolib.net.readNet(options.netfile) if options.min_distance > net.getBBoxDiameter() * (options.intermediate + 1): options.intermediate = int( math.ceil(options.min_distance / net.getBBoxDiameter())) - 1 print("Warning: setting number of intermediate waypoints to %s to achieve a minimum trip length of %s in a network with diameter %s." % ( options.intermediate, options.min_distance, net.getBBoxDiameter())) trip_generator = buildTripGenerator(net, options) idx = 0 if options.pedestrians: # figure out which of the tripattrs belong to the <person> and which # belong to the <walk> walkattrs = ' '.join( [a for a in options.tripattrs.split() if is_walk_attribute(a)]) personattrs = ' '.join( [a for a in options.tripattrs.split() if not is_walk_attribute(a)]) def generate_one(idx): label = "%s%s" % (options.tripprefix, idx) try: source_edge, sink_edge, intermediate = trip_generator.get_trip( options.min_distance, options.max_distance, options.maxtries) via = "" if len(intermediate) > 0: via = 'via="%s" ' % ' '.join( [e.getID() for e in intermediate]) if options.pedestrians: fouttrips.write( ' <person id="%s" depart="%.2f" %s>\n' % (label, depart, personattrs)) fouttrips.write( ' <walk from="%s" to="%s" %s/>\n' % (source_edge.getID(), sink_edge.getID(), walkattrs)) fouttrips.write(' </person>\n') else: fouttrips.write(' <trip id="%s" depart="%.2f" from="%s" to="%s" %s%s/>\n' % ( label, depart, source_edge.getID(), sink_edge.getID(), via, options.tripattrs)) except Exception as exc: print(exc, file=sys.stderr) return idx + 1 with open(options.tripfile, 'w') as fouttrips: fouttrips.write("""<?xml version="1.0"?> <!-- generated on %s by $Id$ options: %s --> <trips> """ % (datetime.datetime.now(), (' '.join(sys.argv[1:]).replace('--', '<doubleminus>')))) if options.vehicle_class: fouttrips.write(' <vType id="%s" vClass="%s" />\n' % (options.vehicle_class, options.vehicle_class)) options.tripattrs += ' type="%s"' % options.vehicle_class depart = options.begin if trip_generator: while depart < options.end: if options.binomial is None: # generate with constant spacing idx = generate_one(idx) depart += options.period else: # draw n times from a bernouli distribution # for an average arrival rate of 1 / period prob = 1.0 / options.period / options.binomial for i in range(options.binomial): if random.random() < prob: idx = generate_one(idx) depart += 1 fouttrips.write("</trips>\n") if options.routefile: args = [DUAROUTER, '-n', options.netfile, '-t', options.tripfile, '-o', options.routefile, '--ignore-errors', '--begin', str(options.begin), '--end', str(options.end), '--no-step-log', '--no-warnings'] if options.additional is not None: args += ['--additional-files', options.additional] print("calling ", " ".join(args)) subprocess.call(args) if options.validate: print("calling route2trips") route2trips.main([options.routefile], outfile=options.tripfile) if options.weights_outprefix: trip_generator.source_generator.write_weights( options.weights_outprefix + SOURCE_SUFFIX) trip_generator.sink_generator.write_weights( options.weights_outprefix + SINK_SUFFIX) trip_generator.via_generator.write_weights( options.weights_outprefix + VIA_SUFFIX) # return wether trips could be genreated as requested return trip_generator is not None
def build(handler, prefix, bbox=False): sumo = sumolib.checkBinary('sumo') if bbox: sumogui = sumolib.checkBinary('sumo-gui') else: # offline test mode sumogui = sumo def callSumo(extraopts): guisettingsname = prefix + ".view.xml" print "Writing gui settings file:", guisettingsname with open(guisettingsname, 'w') as f: f.write(""" <viewsettings> <scheme name="real world"/> <delay value="20"/> </viewsettings> """) configname = prefix + ".sumocfg" print "Writing config file:", configname opts = [sumo, "-n", netname, "-a", polyname, "--gui-settings-file", guisettingsname, "-v", "--no-step-log", "--save-configuration", configname] opts += extraopts subprocess.call(opts) print "Calling SUMO GUI" try: subprocess.call([sumogui, "-c", configname]) except: print "SUMO GUI canceled" if bbox: # get the coordinates and cast them to float size = map(float, bbox.split(",")) # calculates the area size = (size[0] - size[2]) * (size[3] - size[1]) areaFactor = abs(size) * 5000 # to adjust period by the area print "Calling osmGet" osmGet.get(["-b", bbox, "-p", prefix]) else: # offline test mode areaFactor = 1 print "Calling osmBuild" # output name for the osm file, will be used by osmBuild, can be after the # process deleted osmname = prefix + "_bbox.osm.xml" # output name for the net file, will be used by osmBuild, randomTrips and # sumo-gui netname = prefix + ".net.xml" # output name for the poly file, will be used by osmBuild and sumo-gui polyname = prefix + ".poly.xml" options = ["-f", osmname, "-p", prefix, "-m", polyfile] typefiles = [] netconvertOptions = osmBuild.DEFAULT_NETCONVERT_OPTS + ",--junctions.corner-detail,5" if handler.pedestrians.enable: # drop? # sidewalks are already included via typefile netconvertOptions += ",--crossings.guess" typefiles.append(pedestrianstypefile) else: typefiles.append(typefile) if handler.ships.enable: typefiles.append(shipstypefile) options += ["--netconvert-typemap", ','.join(typefiles)] options += ["--netconvert-options", netconvertOptions] osmBuild.build(options) if handler.vehicles.enable or handler.bicycles.enable or handler.pedestrians.enable or handler.rails.enable or handler.ships.enable: print "Calling randomTrips" # routenames stores all routefiles and will join the items later, will # be used by sumo-gui routenames = [] if handler.vehicles.enable: routename = prefix + ".vehicles.rou.xml" tripname = prefix + ".vehicles.trips.xml" routenames.append(tripname) randomTrips.main(randomTrips.get_options( handler.vehicles.parseTripOpts(netname, routename, areaFactor))) route2trips.main([routename], outfile=tripname) if handler.bicycles.enable: routename = prefix + ".bicycles.rou.xml" tripname = prefix + ".bicycles.trips.xml" routenames.append(tripname) randomTrips.main(randomTrips.get_options( handler.bicycles.parseTripOpts(netname, routename, areaFactor))) route2trips.main([routename], outfile=tripname) if handler.pedestrians.enable: routename = prefix + ".pedestrians.rou.xml" routenames.append(routename) randomTrips.main(randomTrips.get_options( handler.pedestrians.parseTripOpts(netname, routename, areaFactor))) if handler.rails.enable: routename = prefix + ".rails.rou.xml" tripname = prefix + ".rails.trips.xml" routenames.append(tripname) randomTrips.main(randomTrips.get_options( handler.rails.parseTripOpts(netname, routename, areaFactor))) route2trips.main([routename], outfile=tripname) if handler.ships.enable: routename = prefix + ".ships.rou.xml" tripname = prefix + ".ships.trips.xml" routenames.append(tripname) randomTrips.main(randomTrips.get_options( handler.ships.parseTripOpts(netname, routename, areaFactor))) route2trips.main([routename], outfile=tripname) callSumo(["-r", ",".join(routenames), "--ignore-route-errors"]) else: callSumo([]) print "Done."
def main(options): if options.seed: random.seed(options.seed) net = sumolib.net.readNet(options.netfile) if options.min_distance > net.getBBoxDiameter() * (options.intermediate + 1): options.intermediate = int( math.ceil(options.min_distance / net.getBBoxDiameter())) - 1 print( "Warning: setting number of intermediate waypoints to %s to achieve a minimum trip length of %s in a network with diameter %.2f." % (options.intermediate, options.min_distance, net.getBBoxDiameter())) trip_generator = buildTripGenerator(net, options) idx = 0 vtypeattrs, options.tripattrs, personattrs, otherattrs = split_trip_attributes( options.tripattrs, options.pedestrians, options.vehicle_class) vias = {} def generate_one(idx): label = "%s%s" % (options.tripprefix, idx) try: source_edge, sink_edge, intermediate = trip_generator.get_trip( options.min_distance, options.max_distance, options.maxtries) via = "" if len(intermediate) > 0: via = ' via="%s" ' % ' '.join( [e.getID() for e in intermediate]) if options.validate: vias[label] = via if options.pedestrians: fouttrips.write(' <person id="%s" depart="%.2f"%s>\n' % (label, depart, personattrs)) if options.persontrips: fouttrips.write( ' <personTrip from="%s" to="%s"%s/>\n' % (source_edge.getID(), sink_edge.getID(), otherattrs)) else: fouttrips.write( ' <walk from="%s" to="%s"%s/>\n' % (source_edge.getID(), sink_edge.getID(), otherattrs)) fouttrips.write(' </person>\n') elif options.flows > 0: if options.binomial: for j in range(options.binomial): fouttrips.write( ' <flow id="%s#%s" begin="%s" end="%s" probability="%s" from="%s" to="%s"%s%s/>\n' % (label, j, options.begin, options.end, 1.0 / options.period / options.binomial, source_edge.getID(), sink_edge.getID(), via, options.tripattrs)) else: fouttrips.write( ' <flow id="%s" begin="%s" end="%s" period="%s" from="%s" to="%s"%s%s/>\n' % (label, options.begin, options.end, options.period * options.flows, source_edge.getID(), sink_edge.getID(), via, options.tripattrs)) else: fouttrips.write( ' <trip id="%s" depart="%.2f" from="%s" to="%s"%s%s/>\n' % (label, depart, source_edge.getID(), sink_edge.getID(), via, options.tripattrs)) except Exception as exc: print(exc, file=sys.stderr) return idx + 1 with open(options.tripfile, 'w') as fouttrips: sumolib.writeXMLHeader(fouttrips, "$Id$", "routes") if options.vehicle_class: fouttrips.write( ' <vType id="%s" vClass="%s"%s/>\n' % (options.vtypeID, options.vehicle_class, vtypeattrs)) options.tripattrs += ' type="%s"' % options.vtypeID personattrs += ' type="%s"' % options.vtypeID depart = options.begin if trip_generator: if options.flows == 0: while depart < options.end: if options.binomial is None: # generate with constant spacing idx = generate_one(idx) depart += options.period else: # draw n times from a Bernoulli distribution # for an average arrival rate of 1 / period prob = 1.0 / options.period / options.binomial for i in range(options.binomial): if random.random() < prob: idx = generate_one(idx) depart += 1 else: for i in range(options.flows): idx = generate_one(idx) fouttrips.write("</routes>\n") if options.routefile: args = [ DUAROUTER, '-n', options.netfile, '-r', options.tripfile, '-o', options.routefile, '--ignore-errors', '--begin', str(options.begin), '--end', str(options.end), '--no-step-log', '--no-warnings' ] if options.additional is not None: args += ['--additional-files', options.additional] if options.carWalkMode is not None: args += ['--persontrip.transfer.car-walk', options.carWalkMode] if options.walkfactor is not None: args += ['--persontrip.walkfactor', options.walkfactor] print("calling ", " ".join(args)) subprocess.call(args) if options.validate: print("calling route2trips") route2trips.main([options.routefile], outfile=options.tripfile, vias=vias, calledBy=" via randomTrips.py") if options.weights_outprefix: trip_generator.source_generator.write_weights( options.weights_outprefix + SOURCE_SUFFIX) trip_generator.sink_generator.write_weights(options.weights_outprefix + SINK_SUFFIX) trip_generator.via_generator.write_weights(options.weights_outprefix + VIA_SUFFIX) # return wether trips could be generated as requested return trip_generator is not None
if options.routefile: args = [ DUAROUTER, '-n', options.netfile, '-t', options.tripfile, '-o', options.routefile, '--ignore-errors', '--begin', str(options.begin), '--end', str(options.end), '--no-step-log' ] if options.additional is not None: args += ['--additional-files', options.additional] print("calling ", " ".join(args)) subprocess.call(args) if options.validate: print("calling route2trips") route2trips.main([options.routefile], outfile=options.tripfile) if options.weights_outprefix: trip_generator.source_generator.write_weights( options.weights_outprefix + SOURCE_SUFFIX) trip_generator.sink_generator.write_weights(options.weights_outprefix + SINK_SUFFIX) trip_generator.via_generator.write_weights(options.weights_outprefix + VIA_SUFFIX) # return wether trips could be genreated as requested return trip_generator is not None if __name__ == "__main__": if not main(get_options()):