def test_create_auto_routing_evacuation_demands(self): # Settings for departure edges. departure_edges = self._net.getEdges() mean = 3 * 60 * 60 std = 0.7 * 60 * 60 time_sampler_parameters = random_traffic_generator.TimeSamplerGammaMeanStd( mean, std) car_per_meter_residential = 0.041666667 # Generate evacuation demands. generator = self._random_traffic_generator zipped_demands = generator.create_evacuation_auto_routing_demands( departure_edges, time_sampler_parameters, car_per_meter_residential) actual_number_car_per_edge = [x.num_cars for x in zipped_demands] target_number_car_per_edge = [ 23, 1, 13, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ] self.assertListEqual(actual_number_car_per_edge[:25], target_number_car_per_edge) # Output the demand xml file. output_file = os.path.join(self._output_dir, 'demands.rou.xml') generator.write_evacuation_vehicle_auto_routing_demands( zipped_demands, 'taz_exit', output_file) # Test for output demands file. with file_util.f_open(output_file, 'r') as f: self.assertLen(f.readlines(), 427)
def test_generate_departure_time(self): np.random.seed(123) mean = 1000 std = 500 number = 200000 time_sampler_parameters = random_traffic_generator.TimeSamplerGammaMeanStd( mean, std) samples = self._random_traffic_generator.generate_departure_time( time_sampler_parameters, number) actual_mean = np.mean(samples) actual_std = np.std(samples) self.assertAlmostEqual(np.abs(mean - actual_mean) / mean, 0, places=2) self.assertAlmostEqual(np.abs(std - actual_std) / std, 0, places=2)
def test_create_shortest_path_evacuation_demands(self): # In the small map, some of the edges are not connected. So if the # evacuation exit is set as one edge, then some of the edges may not have a # path out. The problem can be caught in the warning or info log messages. evacuation_edges = ['-415366780#0'] # Calculate the distance to the evacuation exits. evacuation_path_trees = {} evacuation_path_length = {} for evacuation_edge in evacuation_edges: (evacuation_path_trees[evacuation_edge], evacuation_path_length[evacuation_edge]) = ( self._net.getRestrictedShortestPathsTreeToEdge( evacuation_edge)) # Settings for departure edges. departure_edges = self._net.getEdges() mean = 3 * 60 * 60 std = 0.7 * 60 * 60 time_sampler_parameters = random_traffic_generator.TimeSamplerGammaMeanStd( mean, std) car_per_meter_residential = 0.041666667 # Generate evacuation demands. generator = self._random_traffic_generator zipped_demands = generator.create_evacuation_shortest_path_demands( departure_edges, time_sampler_parameters, car_per_meter_residential, evacuation_edges, evacuation_path_trees, evacuation_path_length) actual_destinations = [x.destination for x in zipped_demands] actual_number_car_per_edge = [x.num_cars for x in zipped_demands] target_destinations = [ None, '-415366780#0', '-415366780#0', '-415366780#0', '-415366780#0', '-415366780#0', '-415366780#0', '-415366780#0', '-415366780#0' ] target_number_car_per_edge = [ 23, 1, 13, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ] self.assertListEqual(actual_destinations[:9], target_destinations) self.assertListEqual(actual_number_car_per_edge[:25], target_number_car_per_edge) # Output the demand xml file. output_file = os.path.join(self._output_dir, 'demands.rou.xml') generator.write_evacuation_vehicle_path_demands( zipped_demands, output_file) # Test for output demands file. with file_util.f_open(output_file, 'r') as f: self.assertLen(f.readlines(), 528)
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)