def test_walk_duration(self): node_profile = NodeProfileMultiObjective(dep_times=[10, 5], walk_to_target_duration=27, label_class=LabelTime) self.assertEqual(27, node_profile.get_walk_to_target_duration()) pt2 = LabelTime(departure_time=10, arrival_time_target=35) pt1 = LabelTime(departure_time=5, arrival_time_target=35) node_profile.update([pt2]) node_profile.update([pt1])
def test_pareto_optimality_with_transfers_and_time(self): node_profile = NodeProfileMultiObjective(dep_times=[5, 6, 7]) pt3 = LabelTimeWithBoardingsCount(departure_time=5, arrival_time_target=45, n_boardings=0, first_leg_is_walk=False) pt2 = LabelTimeWithBoardingsCount(departure_time=6, arrival_time_target=40, n_boardings=1, first_leg_is_walk=False) pt1 = LabelTimeWithBoardingsCount(departure_time=7, arrival_time_target=35, n_boardings=2, first_leg_is_walk=False) self.assertTrue(node_profile.update([pt1])) self.assertTrue(node_profile.update([pt2])) self.assertTrue(node_profile.update([pt3])) self.assertEqual(3, len(node_profile.get_labels_for_real_connections()))
def test_evaluate(self): node_profile = NodeProfileMultiObjective(dep_times=[3, 1], label_class=LabelTime) node_profile.update([LabelTime(departure_time=3, arrival_time_target=4)]) self.assertEquals(4, min_arrival_time_target(node_profile.evaluate(3))) node_profile.update([LabelTime(departure_time=1, arrival_time_target=1)]) self.assertEquals(1, min_arrival_time_target(node_profile.evaluate(1)))
def __initialize_node_profiles(self): self._stop_profiles = dict() for node in self._all_nodes: walk_duration_to_target = float('inf') closest_target = None if node in self._targets: walk_duration_to_target = 0 closest_target = node else: for target in self._targets: if self._walk_network.has_edge(target, node): edge_data = self._walk_network.get_edge_data( target, node) walk_duration = int(edge_data["d_walk"] / float(self._walk_speed)) if walk_duration_to_target > walk_duration: walk_duration_to_target = walk_duration closest_target = target self._stop_profiles[node] = NodeProfileMultiObjective( dep_times=self. _stop_departure_times_with_pseudo_connections[node], label_class=self._label_class, walk_to_target_duration=walk_duration_to_target, transit_connection_dep_times=self._stop_departure_times[node], closest_target=closest_target, node_id=node)
def test_finalize(self): node_profile = NodeProfileMultiObjective(label_class=LabelTimeWithBoardingsCount, dep_times=[10]) own_label = LabelTimeWithBoardingsCount(departure_time=10, arrival_time_target=20, n_boardings=0, first_leg_is_walk=False) self.assertTrue(node_profile.update([own_label])) neighbor_label = LabelTimeWithBoardingsCount(departure_time=15, arrival_time_target=18, n_boardings=2, first_leg_is_walk=False) assert(len(node_profile.get_labels_for_real_connections()) == 1) node_profile.finalize([[neighbor_label]], [3]) assert (len(node_profile.get_final_optimal_labels()) == 2) self.assertTrue(any(map(lambda el: el.departure_time == 12, node_profile.get_final_optimal_labels())))
def test_pareto_optimality2(self): node_profile = NodeProfileMultiObjective(dep_times=[5, 10], label_class=LabelTime) pt2 = LabelTime(departure_time=10, arrival_time_target=35, last_leg_is_walk=False) node_profile.update([pt2]) pt1 = LabelTime(departure_time=5, arrival_time_target=35, last_leg_is_walk=False) node_profile.update([pt1]) self.assertEquals(len(node_profile.get_labels_for_real_connections()), 1)
def __compute_profile_stats_from_profiles(profile_data): """ Parameters ---------- profiles: dict mapping from stop_I -> MultiObjectiveNodeProfile Returns ------- observable_name_to_data: dict """ profile_summary_methods, profile_observable_names = NodeProfileAnalyzerTimeAndVehLegs.all_measures_and_names_as_lists( ) profile_summary_data = [[] for _ in range(len(profile_observable_names))] observable_name_to_method = dict( zip(profile_observable_names, profile_summary_methods)) observable_name_to_data = dict( zip(profile_observable_names, profile_summary_data)) nodes = pandas.read_csv(HELSINKI_NODES_FNAME, sep=";") for stop_I in nodes['stop_I'].values: try: profile = profile_data[stop_I] except KeyError: profile = NodeProfileMultiObjective() profile.finalize() profile_analyzer = NodeProfileAnalyzerTimeAndVehLegs( profile, ANALYSIS_START_TIME_DEP, ANALYSIS_END_TIME_DEP) for observable_name in profile_observable_names: method = observable_name_to_method[observable_name] observable_value = method(profile_analyzer) if observable_value is None: print(observable_name, stop_I) _assert_results_are_positive_or_infs_or_nans( numpy.array([observable_value])) observable_name_to_data[observable_name].append(observable_value) return observable_name_to_data
def test_pareto_optimality_with_transfers_only(self): LabelClass = LabelVehLegCount node_profile = NodeProfileMultiObjective(dep_times=[5, 6, 7], label_class=LabelClass) pt3 = LabelClass(departure_time=5, n_vehicle_legs=0, last_leg_is_walk=False) pt2 = LabelClass(departure_time=6, n_vehicle_legs=1, last_leg_is_walk=False) pt1 = LabelClass(departure_time=7, n_vehicle_legs=2, last_leg_is_walk=False) self.assertTrue(node_profile.update([pt1])) self.assertTrue(node_profile.update([pt2])) self.assertTrue(node_profile.update([pt3])) node_profile.finalize() self.assertEqual(1, len(node_profile.get_final_optimal_labels()))
def _get_analyzer(self, labels, start_time, end_time, walk_to_target_duration=float('inf')): dep_times = list(set(map(lambda el: el.departure_time, labels))) p = NodeProfileMultiObjective( dep_times=dep_times, walk_to_target_duration=walk_to_target_duration, label_class=LabelTimeWithBoardingsCount) for label in labels: p.update([label]) p.finalize() analyzer = NodeProfileAnalyzerTimeAndVehLegs.from_profile( p, start_time, end_time) return analyzer
def test_dep_time_skipped_in_update(self): label3 = LabelTimeWithBoardingsCount(departure_time=30, arrival_time_target=20, n_boardings=0, first_leg_is_walk=False) label2 = LabelTimeWithBoardingsCount(departure_time=20, arrival_time_target=20, n_boardings=0, first_leg_is_walk=False) label1 = LabelTimeWithBoardingsCount(departure_time=10, arrival_time_target=20, n_boardings=0, first_leg_is_walk=False) # This should work ok node_profile = NodeProfileMultiObjective(label_class=LabelTimeWithBoardingsCount, dep_times=[10, 20, 30]) node_profile.update([label3]) node_profile.update([label2]) node_profile.update([label2]) node_profile.update([label1]) # This should fail due to dep time 20 missing in between with self.assertRaises(AssertionError): node_profile = NodeProfileMultiObjective(label_class=LabelTimeWithBoardingsCount, dep_times=[10, 20, 30]) node_profile.update([label3]) node_profile.update([label1]) # This should fail due to dep time 30 not being the first to deal with with self.assertRaises(AssertionError): node_profile = NodeProfileMultiObjective(label_class=LabelTimeWithBoardingsCount, dep_times=[10, 20, 30]) node_profile.update([label2])
def test_same_dep_times_fail_in_init(self): with self.assertRaises(AssertionError): node_profile = NodeProfileMultiObjective(label_class=LabelTimeWithBoardingsCount, dep_times=[10, 10, 20, 20])
def test_identity_profile(self): identity_profile = NodeProfileMultiObjective(dep_times=[10]) identity_profile.update([LabelTimeWithBoardingsCount(10, 10, 0, True)]) self.assertEqual(10, min_arrival_time_target(identity_profile.evaluate(10, first_leg_can_be_walk=True)))
def plot_transfer_profile(): alphabetical_labels = list(labels_t_dep_dur_b) alphabetical_labels.sort(key=lambda el: el[-1]) for label in alphabetical_labels: print(label[-1] + " & " + str(label[0]) + " & " + str(label[0] + label[1]) + " & " + str(label[1]) + " & " + str(label[2]) + " \\\\") labels = [ LabelTimeWithBoardingsCount(departure_time=label[0], arrival_time_target=label[0] + label[1], n_boardings=label[2], first_leg_is_walk=False) for label in labels_t_dep_dur_b ] dep_times = list(set(map(lambda el: el.departure_time, labels))) p = NodeProfileMultiObjective(dep_times=dep_times, walk_to_target_duration=10, label_class=LabelTimeWithBoardingsCount) for label in labels: p.update([label]) p.finalize() analyzer = NodeProfileAnalyzerTimeAndVehLegs(p, 0, 20) print(analyzer.mean_n_boardings_on_shortest_paths()) journey_letters = [label[-1] for label in labels_t_dep_dur_b[::-1]] fig = plt.figure(figsize=(5.5, 3.5)) ax1 = plt.subplot(gs[:, :4]) fig = analyzer.plot_new_transfer_temporal_distance_profile( format_string="%S", duration_divider=1, default_lw=4, journey_letters=journey_letters, ax=ax1, ncol_legend=2, legend_font_size=9) ax1.set_xlabel("Departure time $t_{\\text{dep}}$ (min)") ax1.set_ylabel("Temporal distance $\\tau$ (min)") ax2 = plt.subplot(gs[:, 4:]) ax2 = analyzer.plot_temporal_distance_pdf_horizontal(use_minutes=True, duration_divider=1, ax=ax2, legend_font_size=9) ax2.set_ylabel("") ax1.set_ylim(0, 11.5) ax2.set_ylim(0, 11.5) ax2.set_xlim(0, 0.3) ax2.set_yticklabels(["" for _ in ax2.get_yticks()]) ax2.set_xlabel("Probability density $P(\\tau)$") ax2.set_xticks([0.1, 0.2, 0.3]) for _ax, letter in zip([ax1, ax2], "AB"): _ax.text(0.04, 0.98, "\\textbf{" + letter + "}", horizontalalignment="left", verticalalignment="top", transform=_ax.transAxes, fontsize=15, color="black") fig.savefig(settings.FIGS_DIRECTORY + "schematic_transfer_profile.pdf") plt.show()