def setUp(self): super(RandomTrafficGeneratorTest, self).setUp() testdata_dir = './testdata' self._output_dir = tempfile.mkdtemp( dir=absltest.get_default_test_tmpdir()) sumo_net_file = 'mtv_tiny.net.xml' map_file = _load_file(testdata_dir, sumo_net_file) self._net = sumolib.net.readNet(map_file) traffic_generator = random_traffic_generator.RandomTrafficGenerator( self._net) self._random_traffic_generator = traffic_generator # The traffic generator uses numpy to draw random samples. The numpy random # seed is set here to make the result replicable. np.random.seed(0)
def generate_evacuation_taz_demands(self, residential_car_density, serving_car_density, demand_mean_hours, demand_stddev_hours, population_portion): """Generates evacuation TAZ demands.""" # TODO(yusef): Fix map + total number of cars. # To make the demands consistent, use the default map, paradise_type.net.xml # as the input map instead of the reversed. For Paradise map, an easy way to # check is that the total number of cars is 11072. net = sumolib.net.readNet(self._sumo_net_file) traffic_generator = random_traffic_generator.RandomTrafficGenerator( net) visualizer = map_visualizer.MapVisualizer(net) print('Generating TAZ demands with STD: ', demand_stddev_hours, ' Portion: ', population_portion) # Demands from residential roads. residential_edge_type = ['highway.residential'] residential_edges = net.filterEdges(residential_edge_type) demand_mean_seconds = demand_mean_hours * 60 * 60 demand_stddev_seconds = demand_stddev_hours * 60 * 60 time_sampler_parameters = random_traffic_generator.TimeSamplerGammaMeanStd( demand_mean_seconds, demand_stddev_seconds) car_per_meter_residential = residential_car_density * population_portion np.random.seed(FLAGS.random_seed) residential = traffic_generator.create_evacuation_auto_routing_demands( residential_edges, time_sampler_parameters, car_per_meter_residential) # Demands from parking roads. parking_edge_type = ['highway.service'] parking_edges = net.filterEdges(parking_edge_type) time_sampler_parameters = random_traffic_generator.TimeSamplerGammaMeanStd( demand_mean_seconds, demand_stddev_seconds) car_per_meter_parking = serving_car_density * population_portion parking = traffic_generator.create_evacuation_auto_routing_demands( parking_edges, time_sampler_parameters, car_per_meter_parking) all_demands = residential + parking departure_time_points = [x.time for x in all_demands] cars_per_time_point = [x.num_cars for x in all_demands] departure_time_points = np.array(departure_time_points) / 3600 print('TAZ demands. Total vehicles: ', sum(cars_per_time_point)) # TODO(yusef): reconcile. demands_dir = os.path.join(self._output_dir, _DEMANDS) file_util.f_makedirs(demands_dir) output_hist_figure_path = os.path.join( demands_dir, 'departure_time_histogram_taz_std_%s_portion_%s.pdf' % (demand_stddev_hours, population_portion)) output_cumulative_figure_path = os.path.join( demands_dir, 'departure_time_cumulative_taz_std_%s_portion_%s.pdf' % (demand_stddev_hours, population_portion)) pkl_file = os.path.join( demands_dir, 'demands_taz_tuple_std_%s_portion_%s.pkl' % (demand_stddev_hours, population_portion)) routes_file = os.path.join( demands_dir, 'demands_taz_std_%s_portion_%s.rou.xml' % (demand_stddev_hours, population_portion)) # Output the demand xml file. visualizer.plot_demands_departure_time( departure_time_points, cars_per_time_point, output_hist_figure_path=output_hist_figure_path, output_cumulative_figure_path=output_cumulative_figure_path) file_util.save_variable(pkl_file, all_demands) exit_taz = 'exit_taz' traffic_generator.write_evacuation_vehicle_auto_routing_demands( all_demands, exit_taz, routes_file)
def generate_evacuation_shortest_path_demands( self, residential_car_density, serving_car_density, evacuation_edges, demand_mean_hours, demand_stddev_hours, population_portion): """Generates evacuation demands.""" net = sumolib.net.readNet(self._sumo_net_file) traffic_generator = random_traffic_generator.RandomTrafficGenerator( net) visualizer = map_visualizer.MapVisualizer(net) print('Generating TAZ demands with STD: ', demand_stddev_hours, ' Portion: ', population_portion) # Calculate the distance to the evacuation exits. evacuation_path_trees = {} evacuation_path_length = {} for exit_edge in evacuation_edges: evacuation_path_trees[exit_edge], evacuation_path_length[ exit_edge] = ( net.getRestrictedShortestPathsTreeToEdge(exit_edge)) # Demands from residential roads. residential_edge_type = ['highway.residential'] residential_edges = net.filterEdges(residential_edge_type) demand_mean_seconds = demand_mean_hours * 60 * 60 demand_stddev_seconds = demand_stddev_hours * 60 * 60 time_sampler_parameters = random_traffic_generator.TimeSamplerGammaMeanStd( demand_mean_seconds, demand_stddev_seconds) car_per_meter_residential = residential_car_density * population_portion np.random.seed(FLAGS.random_seed) residential = traffic_generator.create_evacuation_shortest_path_demands( residential_edges, time_sampler_parameters, car_per_meter_residential, evacuation_edges, evacuation_path_trees, evacuation_path_length) # Demands from parking roads. parking_edge_type = ['highway.service'] parking_edges = net.filterEdges(parking_edge_type) time_sampler_parameters = random_traffic_generator.TimeSamplerGammaMeanStd( demand_mean_seconds, demand_stddev_seconds) car_per_meter_parking = serving_car_density * population_portion parking = traffic_generator.create_evacuation_shortest_path_demands( parking_edges, time_sampler_parameters, car_per_meter_parking, evacuation_edges, evacuation_path_trees, evacuation_path_length) all_demands = residential + parking departure_time_points = [x.time for x in all_demands] cars_per_time_point = [x.num_cars for x in all_demands] departure_time_points = np.array(departure_time_points) / 3600 print('Shortest path demands. Total vehicles: ', sum(cars_per_time_point)) # Output the demand xml file. demands_dir = os.path.join(self._output_dir, _DEMANDS) file_util.f_makedirs(demands_dir) output_hist_figure_path = os.path.join( demands_dir, 'departure_time_histogram_shortest_path_std_%s_portion_%s.pdf' % (demand_stddev_hours, population_portion)) output_cumulative_figure_path = os.path.join( demands_dir, 'departure_time_cumulative_shortest_path_std_%s_portion_%s.pdf' % (demand_stddev_hours, population_portion)) pkl_file = os.path.join( demands_dir, 'demands_shortest_path_tuple_std_%s_portion_%s.pkl' % (demand_stddev_hours, population_portion)) routes_file = os.path.join( demands_dir, 'demands_shortest_path_std_%s_portion_%s.rou.xml' % (demand_stddev_hours, population_portion)) visualizer.plot_demands_departure_time( departure_time_points, cars_per_time_point, output_hist_figure_path=output_hist_figure_path, output_cumulative_figure_path=output_cumulative_figure_path) file_util.save_variable(pkl_file, all_demands) traffic_generator.write_evacuation_vehicle_path_demands( all_demands, routes_file)
def generate_arterial_routes_demands_main(_): """This is an example of generating demands only on arterial and freeways. The generated routes do no have the ones only on freeways. """ net = sumolib.net.readNet(FLAGS.sumo_net_file) traffic_generator = random_traffic_generator.RandomTrafficGenerator(net) routes_file = os.path.join(FLAGS.output_dir, 'arterial_routes_demands.xml') token = '<routes>\n' util.append_line_to_file(routes_file, token) token = (' <vType id="Car" accel="0.8" decel="4.5" sigma="0.5" ' 'length="5" minGap="2.5" maxSpeed="38" guiShape="passenger"/>\n') util.append_line_to_file(routes_file, token) # Setup freeway routes. figure_path = os.path.join(FLAGS.output_dir, 'freeway_routes.pdf') input_output = traffic_generator.get_freeway_input_output( figure_path=figure_path) token = ' <!-- freeway routes -->' util.append_line_to_file(routes_file, token) freeway_routes = traffic_generator.setup_shortest_routes( input_output, edge_type_list=random_traffic_generator.FREEWAY_EDGE_TYPES, routes_file=routes_file, figure_folder=None) # Setup arterial roads routes. figure_path = os.path.join(FLAGS.output_dir, 'arterial_routes.pdf') input_output = traffic_generator.get_arterial_input_output( figure_path=figure_path) token = ' <!-- arterial routes -->' util.append_line_to_file(routes_file, token) arterial_routes = traffic_generator.setup_shortest_routes( input_output, edge_type_list=(random_traffic_generator.FREEWAY_EDGE_TYPES + random_traffic_generator.ARTERIAL_EDGE_TYPES), routes_file=routes_file, figure_folder=None) token = ' <!-- freeway + arterial roads demands -->' util.append_line_to_file(routes_file, token) time_step_size = 100 for time_point in range(0, FLAGS.simulation_duration, time_step_size): # Create arbitrary freeway_routes_demands = [(0, 0.5), (1, 0.5), (2, 0.5), (3, 0.5)] traffic_generator.generate_routes_flow(time_point, time_step_size, freeway_routes, freeway_routes_demands, routes_file) arterial_routes_demands = [] # arterial_routes_demands = [(route_id, 0.002) for route_id in # range(len(arterial_routes))] for route_index, route in enumerate(arterial_routes): if (route['edge_from'].getID() == '27628577#0' and route['edge_to'].getID() == '23925644#3'): arterial_routes_demands.append((route_index, 0.3)) elif (route['edge_from'].getID() == '17971093' and route['edge_to'].getID() == '23925644#3'): arterial_routes_demands.append((route_index, 0.3)) elif (route['edge_from'].getID() == '17971093' and route['edge_to'].getID() == '-496364805#0'): arterial_routes_demands.append((route_index, 0.3)) elif (route['edge_from'].getID() == '688239440' and route['edge_to'].getID() == '23925644#3'): arterial_routes_demands.append((route_index, 0.2)) else: arterial_routes_demands.append((route_index, 0.01)) traffic_generator.generate_routes_flow(time_point, time_step_size, arterial_routes, arterial_routes_demands, routes_file) token = '\n</routes>' util.append_line_to_file(routes_file, token)