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 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_assert_raises_wrong_order(self): node_profile = NodeProfileC() pt1 = LabelTime(departure_time=5, arrival_time_target=35) pt2 = LabelTime(departure_time=10, arrival_time_target=35) self.assertTrue(node_profile.update_pareto_optimal_tuples(pt1)) with self.assertRaises(AssertionError): node_profile.update_pareto_optimal_tuples(pt2)
def test_walk_duration(self): node_profile = NodeProfileC(walk_to_target_duration=27) 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_pareto_optimal_tuples(pt2) node_profile.update_pareto_optimal_tuples(pt1) self.assertEqual(1, len(node_profile.get_final_optimal_labels()))
def test_pareto_optimality2(self): node_profile = NodeProfileC() pt2 = LabelTime(departure_time=10, arrival_time_target=35) node_profile.update_pareto_optimal_tuples(pt2) pt1 = LabelTime(departure_time=5, arrival_time_target=35) node_profile.update_pareto_optimal_tuples(pt1) print(node_profile._labels) self.assertEquals(len(node_profile.get_final_optimal_labels()), 1)
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 test_earliest_arrival_time(self): node_profile = NodeProfileC() self.assertEquals(float("inf"), node_profile.evaluate_earliest_arrival_time_at_target(0, 0)) node_profile.update_pareto_optimal_tuples(LabelTime(departure_time=3, arrival_time_target=4)) self.assertEquals(4, node_profile.evaluate_earliest_arrival_time_at_target(2, 0)) node_profile.update_pareto_optimal_tuples(LabelTime(departure_time=1, arrival_time_target=1)) self.assertEquals(1, node_profile.evaluate_earliest_arrival_time_at_target(0, 0))
def test_pareto_optimality(self): node_profile = NodeProfileC() pair3 = LabelTime(departure_time=2, arrival_time_target=3) node_profile.update_pareto_optimal_tuples(pair3) pair2 = LabelTime(departure_time=1, arrival_time_target=2) node_profile.update_pareto_optimal_tuples(pair2) pair1 = LabelTime(departure_time=1, arrival_time_target=1) node_profile.update_pareto_optimal_tuples(pair1) self.assertEqual(2, len(node_profile.get_final_optimal_labels()))
def update_pareto_optimal_tuples(self, new_label): """ Parameters ---------- new_label: LabelTime Returns ------- updated: bool """ assert (isinstance(new_label, LabelTime)) if self._labels: assert (new_label.departure_time <= self._labels[-1].departure_time) best_later_departing_arrival_time = self._labels[ -1].arrival_time_target else: best_later_departing_arrival_time = float('inf') walk_to_target_arrival_time = new_label.departure_time + self._walk_to_target_duration best_arrival_time = min(walk_to_target_arrival_time, best_later_departing_arrival_time, new_label.arrival_time_target) # this should be changed to get constant time insertions / additions # (with time-indexing) if (new_label.arrival_time_target < walk_to_target_arrival_time and new_label.arrival_time_target < best_later_departing_arrival_time): self._labels.append( LabelTime(new_label.departure_time, best_arrival_time)) return True else: return False
def test_simple(self): event_list_raw_data = [ (2, 4, 40, 50, "trip_5", 1), ] transit_connections = list( map(lambda el: Connection(*el), event_list_raw_data)) walk_network = networkx.Graph() walk_network.add_edge(1, 2, d_walk=20) walk_network.add_edge(3, 4, d_walk=15) walk_speed = 1 source_stop = 1 target_stop = 4 transfer_margin = 0 start_time = 0 end_time = 50 labels = [] labels.append(LabelTime(departure_time=20, arrival_time_target=50)) csa_profile = PseudoConnectionScanProfiler(transit_connections, target_stop, start_time, end_time, transfer_margin, walk_network, walk_speed) csa_profile.run() source_stop_profile = csa_profile.stop_profiles[source_stop] source_stop_labels = source_stop_profile.get_final_optimal_labels() self._assert_pareto_tuple_sets_equal(labels, source_stop_labels)
def test_large_numbers_do_not_overflow(self): departure_time = 1475530980 arrival_time = 1475530980 label = LabelTime(departure_time=float(departure_time), arrival_time_target=float(arrival_time), first_leg_is_walk=False) self.assertEqual(departure_time, label.departure_time) self.assertEqual(arrival_time, label.arrival_time_target)
def test_dominates(self): label1 = LabelTime(departure_time=0, arrival_time_target=20) label2 = LabelTime(departure_time=1, arrival_time_target=10) label3 = LabelTime(departure_time=1, arrival_time_target=10) label4 = LabelTime(departure_time=1, arrival_time_target=11) label5 = LabelTime(departure_time=0, arrival_time_target=10) self.assertTrue(label2.dominates(label1)) self.assertTrue(label2.dominates(label3)) self.assertTrue(label2.dominates(label4)) self.assertTrue(label2.dominates(label5))
def test_basics(self): csa_profile = PseudoConnectionScanProfiler( self.transit_connections, self.target_stop, self.start_time, self.end_time, self.transfer_margin, self.walk_network, self.walk_speed) csa_profile.run() stop_3_labels = csa_profile.stop_profiles[3].get_final_optimal_labels() self.assertEqual(len(stop_3_labels), 1) self.assertIn(LabelTime(32, 35), stop_3_labels) stop_2_labels = csa_profile.stop_profiles[2].get_final_optimal_labels() self.assertEqual(len(stop_2_labels), 2) self.assertIn(LabelTime(40, 50), stop_2_labels) self.assertIn(LabelTime(25, 35), stop_2_labels) source_stop_profile = csa_profile.stop_profiles[self.source_stop] source_stop_pareto_optimal_tuples = source_stop_profile.get_final_optimal_labels( ) labels = [] labels.append(LabelTime(departure_time=10, arrival_time_target=35)) labels.append(LabelTime(departure_time=20, arrival_time_target=50)) labels.append(LabelTime(departure_time=32, arrival_time_target=55)) self._assert_pareto_tuple_sets_equal( labels, source_stop_pareto_optimal_tuples)
def _run(self): # if source node in s1: previous_departure_time = float("inf") connections = self._all_connections # list[Connection] n_connections_tot = len(connections) for i, connection in enumerate(connections): # basic checking + printing progress: if self._verbose and i % 1000 == 0: print(i, "/", n_connections_tot) assert (isinstance(connection, Connection)) assert (connection.departure_time <= previous_departure_time) previous_departure_time = connection.departure_time # get all different "accessible" / arrival times (Pareto-optimal sets) arrival_profile = self._stop_profiles[ connection.arrival_stop] # NodeProfileSimple # Three possibilities: # 1. earliest arrival time (Profiles) via transfer earliest_arrival_time_via_transfer = arrival_profile.evaluate_earliest_arrival_time_at_target( connection.arrival_time, self._transfer_margin) # 2. earliest arrival time within same trip (equals float('inf') if not reachable) earliest_arrival_time_via_same_trip = self.__trip_min_arrival_time[ connection.trip_id] # then, take the minimum (or the Pareto-optimal set) of these three alternatives. min_arrival_time = min(earliest_arrival_time_via_same_trip, earliest_arrival_time_via_transfer) # If there are no 'labels' to progress, nothing needs to be done. if min_arrival_time == float("inf"): continue # Update information for the trip if (not connection.is_walk) and ( earliest_arrival_time_via_same_trip > min_arrival_time): self.__trip_min_arrival_time[ connection.trip_id] = earliest_arrival_time_via_transfer # Compute the new "best" pareto_tuple possible (later: merge the sets of pareto-optimal labels) pareto_tuple = LabelTime(connection.departure_time, min_arrival_time) # update departure stop profile (later: with the sets of pareto-optimal labels) self._stop_profiles[ connection.departure_stop].update_pareto_optimal_tuples( pareto_tuple)
def test_last_leg_is_walk(self): event_list_raw_data = [(0, 1, 0, 10, "trip_1", 1)] transit_connections = list( map(lambda el: Connection(*el), event_list_raw_data)) walk_network = networkx.Graph() walk_network.add_edge(1, 2, d_walk=20) walk_speed = 1 source_stop = 0 target_stop = 2 transfer_margin = 0 start_time = 0 end_time = 50 labels = [] labels.append(LabelTime(departure_time=0, arrival_time_target=30)) csa_profile = PseudoConnectionScanProfiler(transit_connections, target_stop, start_time, end_time, transfer_margin, walk_network, walk_speed) csa_profile.run() found_tuples = csa_profile.stop_profiles[ source_stop].get_final_optimal_labels() self._assert_pareto_tuple_sets_equal(found_tuples, labels)
def test_sort(self): l1 = LabelTime(departure_time=1, arrival_time_target=1) l2 = LabelTime(0, 0) self.assertTrue(l1 > l2) self.assertTrue(l1 >= l2) self.assertFalse(l1 < l2) self.assertFalse(l1 <= l2) l1 = LabelTime(0, 0) l2 = LabelTime(0, 0) self.assertTrue(l1 == l2) self.assertTrue(l1 >= l2) self.assertTrue(l1 <= l2) self.assertFalse(l1 != l2) l1 = LabelTime(1, 0) l2 = LabelTime(1, 1) self.assertTrue(l1 > l2) self.assertFalse(l1 < l2) self.assertTrue(sorted([l1, l2])[0] == l2)
def test_basics_no_transfer_tracking(self): csa_profile = MultiObjectivePseudoCSAProfiler(self.transit_connections, self.target_stop, self.start_time, self.end_time, self.transfer_margin, self.walk_network, self.walk_speed, track_vehicle_legs=False) csa_profile.run() stop_3_pareto_tuples = csa_profile.stop_profiles[ 3].get_final_optimal_labels() self.assertEqual(len(stop_3_pareto_tuples), 1) self.assertIn(LabelTime(32., 35.), stop_3_pareto_tuples) stop_2_pareto_tuples = csa_profile.stop_profiles[ 2].get_final_optimal_labels() self.assertEqual(len(stop_2_pareto_tuples), 2) self.assertIn(LabelTime(40., 50.), stop_2_pareto_tuples) self.assertIn(LabelTime(25., 35.), stop_2_pareto_tuples) source_stop_profile = csa_profile.stop_profiles[1] source_stop_pareto_optimal_tuples = source_stop_profile.get_final_optimal_labels( ) pareto_tuples = list() pareto_tuples.append( LabelTime(departure_time=10, arrival_time_target=35)) pareto_tuples.append( LabelTime(departure_time=20, arrival_time_target=50)) pareto_tuples.append( LabelTime(departure_time=32, arrival_time_target=55)) self._assert_label_sets_equal(pareto_tuples, source_stop_pareto_optimal_tuples)
def test_equal(self): label1 = LabelTime(departure_time=0, arrival_time_target=20) label2 = LabelTime(departure_time=0, arrival_time_target=20) label3 = LabelTime(departure_time=10, arrival_time_target=20) self.assertEqual(label1, label2) self.assertNotEqual(label1, label3)
def test_duration(self): label1 = LabelTime(departure_time=0, arrival_time_target=20) self.assertEqual(20, label1.duration())
def test_identity_profile(self): identity_profile = NodeProfileC(0) self.assertFalse(identity_profile.update_pareto_optimal_tuples(LabelTime(10, 10))) self.assertEqual(10, identity_profile.evaluate_earliest_arrival_time_at_target(10, 0))