From b194d4f349f7509c636428894430315b79599c6f Mon Sep 17 00:00:00 2001 From: Michael Behrisch Date: Fri, 7 Apr 2023 22:16:02 +0200 Subject: [PATCH] moving network cache to options #12995 --- tools/randomTrips.py | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/tools/randomTrips.py b/tools/randomTrips.py index 0fa87a347ac..ae0a61893e7 100755 --- a/tools/randomTrips.py +++ b/tools/randomTrips.py @@ -41,18 +41,9 @@ DEST_SUFFIX = ".dst.xml" VIA_SUFFIX = ".via.xml" -NET = None # Used as a cache for the net throughout the whole script. - MAXIMIZE_FACTOR = "max" -def get_network(options): - global NET - if NET is None: - NET = sumolib.net.readNet(options.netfile) - return NET - - def get_options(args=None): op = sumolib.options.ArgumentParser(description="Generate trips between random locations", allowed_programs=['duarouter']) @@ -215,11 +206,11 @@ def get_options(args=None): if options.period is None and options.insertionRate is None and options.insertionDensity is None: options.period = [1.] + options.net = sumolib.net.readNet(options.netfile) if options.insertionDensity: # Compute length of the network - net = get_network(options) length = 0. # In meters - for edge in net.getEdges(): + for edge in options.net.getEdges(): if edge.allows(options.vclass): length += edge.getLaneNumber() * edge.getLength() options.insertionRate = [density * (length / 1000.0) for density in options.insertionDensity] @@ -374,15 +365,14 @@ def get_trip(self, min_distance, max_distance, maxtries=100, junctionTaz=False, def get_prob_fun(options, fringe_bonus, fringe_forbidden, max_length): # fringe_bonus None generates intermediate way points - net = get_network(options) randomProbs = defaultdict(lambda: 1) if options.randomFactor != 1: - for edge in net.getEdges(): + for edge in options.net.getEdges(): randomProbs[edge.getID()] = random.uniform(1, options.randomFactor) roundabouts = set() if not options.allowRoundabouts: - for roundabout in net.getRoundabouts(): + for roundabout in options.net.getRoundabouts(): roundabouts.update(roundabout.getEdges()) def edge_probability(edge): @@ -590,18 +580,17 @@ def main(options): if not options.random: random.seed(options.seed) - net = get_network(options) - if options.min_distance > net.getBBoxDiameter() * (options.intermediate + 1): - options.intermediate = int(math.ceil(options.min_distance / net.getBBoxDiameter())) - 1 + if options.min_distance > options.net.getBBoxDiameter() * (options.intermediate + 1): + options.intermediate = int(math.ceil(options.min_distance / options.net.getBBoxDiameter())) - 1 print(("Warning: Using %s intermediate waypoints to achieve a minimum trip length of %s in a network " - "with diameter %.2f.") % (options.intermediate, options.min_distance, net.getBBoxDiameter()), + "with diameter %.2f.") % (options.intermediate, options.min_distance, options.net.getBBoxDiameter()), file=sys.stderr) if options.angle_weight != 1: - xmin, ymin, xmax, ymax = net.getBoundary() + xmin, ymin, xmax, ymax = options.net.getBoundary() options.angle_center = (xmin + xmax) / 2, (ymin + ymax) / 2 - trip_generator = buildTripGenerator(net, options) + trip_generator = buildTripGenerator(options.net, options) idx = 0 vtypeattrs, options.tripattrs, personattrs, otherattrs = split_trip_attributes(