def process_rows_of_trip(rows): if rows: trip_id = rows[0][trip_id_index] if trip_available_at_date_per_trip_id[trip_id]: connections = [] for i in range(len(rows) - 1): from_row = rows[i] to_row = rows[i + 1] con_dep = from_row[departure_time_index] if departure_time_index else None con_arr = to_row[arrival_time_index] if arrival_time_index else None if con_dep and con_arr: connections += [Connection( trip_id, from_row[stop_id_index], to_row[stop_id_index], hhmmss_to_sec(con_dep), hhmmss_to_sec(con_arr))] else: return # we do not want trips with missing times try: trip_type = TripType(route_type_per_trip_id[trip_id]) except ValueError: trip_type = TripType.UNKNOWN trips_per_id[trip_id] = Trip(trip_id, connections, trip_type)
def check_connection_on_trip(trip_id, connection_index, exp_from_stop_id, exp_to_stop_id, exp_dep_time_hhmmss, exp_arr_time_hhmmss): trip = cs_data.trips_per_id[trip_id] con = trip.connections[connection_index] assert exp_from_stop_id == con.from_stop_id assert exp_to_stop_id == con.to_stop_id assert hhmmss_to_sec(exp_dep_time_hhmmss) == con.dep_time assert hhmmss_to_sec(exp_arr_time_hhmmss) == con.arr_time
def test_unoptimized_earliest_arrival_bern_samedan(): cs_data = create_test_connectionscan_data() cs_core = ConnectionScanCore(cs_data) assert "12:45:00" == seconds_to_hhmmss( cs_core.route_earliest_arrival(bern.id, samedan.id, hhmmss_to_sec("08:30:00"))) assert cs_core.route_earliest_arrival(bern.id, samedan.id, hhmmss_to_sec("21:00:00")) is None
def test_unoptimized_earliest_arrival_bern_zuerich_hb(): cs_data = create_test_connectionscan_data() cs_core = ConnectionScanCore(cs_data) assert "08:58:00" == seconds_to_hhmmss( cs_core.route_earliest_arrival(bern.id, zuerich_hb.id, hhmmss_to_sec("07:35:00"))) assert "08:58:00" == seconds_to_hhmmss( cs_core.route_earliest_arrival(bern.id, zuerich_hb.id, hhmmss_to_sec("08:02:00"))) assert cs_core.route_earliest_arrival(bern.id, zuerich_hb.id, hhmmss_to_sec("23:33:00")) is None
def check_trip(trip_id, exp_nb_connections, exp_first_stop_id, exp_last_stop_id, exp_dep_first_stop, exp_arr_last_stop, exp_trip_type=None): trip = cs_data.trips_per_id[trip_id] assert exp_nb_connections == len(trip.connections) first_con = trip.connections[0] last_con = trip.connections[-1] assert exp_first_stop_id == first_con.from_stop_id assert exp_last_stop_id == last_con.to_stop_id assert hhmmss_to_sec(exp_dep_first_stop) == first_con.dep_time assert hhmmss_to_sec(exp_arr_last_stop) == last_con.arr_time if exp_trip_type is not None: assert exp_trip_type == trip.trip_type
def run_router_with_reconstruction_test(router_type, from_stop, to_stop, des_dep_time_hhmmss, exp_nb_legs, exp_nb_pt_legs, exp_first_stop, exp_last_stop, exp_dep_time_hhmmss, exp_arr_time_hhmmss, exp_pt_in_stops, exp_pt_out_stops): cs_data = create_test_connectionscan_data() cs_core = ConnectionScanCore(cs_data) router = None if router_type == RouterWithReconstructionType.UNOPTIMIZED_EARLIEST_ARRIVAL_WITH_RECONSTRUCTION: router = cs_core.route_earliest_arrival_with_reconstruction elif router_type == RouterWithReconstructionType.OPTIMIZED_EARLIEST_ARRIVAL_WITH_RECONSTRUCTION: router = cs_core.route_optimized_earliest_arrival_with_reconstruction else: ValueError("router not known") journey = router(from_stop.id, to_stop.id, hhmmss_to_sec(des_dep_time_hhmmss)) assert exp_nb_legs == journey.get_nb_journey_legs() assert exp_nb_pt_legs == journey.get_nb_pt_journey_legs() assert (exp_first_stop.id if exp_first_stop is not None else None) == journey.get_first_stop_id() assert (exp_last_stop.id if exp_last_stop is not None else None) == journey.get_last_stop_id() assert (exp_dep_time_hhmmss if exp_dep_time_hhmmss else None) == seconds_to_hhmmss(journey.get_dep_time()) assert (exp_arr_time_hhmmss if exp_arr_time_hhmmss else None) == seconds_to_hhmmss(journey.get_arr_time()) assert [s.id for s in exp_pt_in_stops] == journey.get_pt_in_stop_ids() assert [s.id for s in exp_pt_out_stops] == journey.get_pt_out_stop_ids()
def test_unoptimized_earliest_arrival_bern_duebystrasse_ostermundigen_bahnhof( ): cs_data = create_test_connectionscan_data() cs_core = ConnectionScanCore(cs_data) assert "12:34:00" == seconds_to_hhmmss( cs_core.route_earliest_arrival(bern_duebystrasse.id, ostermundigen_bahnhof.id, hhmmss_to_sec("12:09:46")))
def route_by_name(self, from_stop_name, to_stop_name, desired_dep_time_hhmmss, router): """Wrapper function to execute routing requests based on the name of the source and target stop. Chooses the best fitting id for the source and target stop and forwards the routing request to the specified router. Args: from_stop_name (str): name of the source stop. to_stop_name (str): name of the target stop. desired_dep_time_hhmmss (str): time in format HH:MM:SS. router (Function): router to be called. Returns: The result of the request to the specified router. """ return router( self.connection_scan_data.stops_per_name[from_stop_name].id, self.connection_scan_data.stops_per_name[to_stop_name].id, hhmmss_to_sec(desired_dep_time_hhmmss))
def create_test_connectionscan_data(): stops_per_id = { s.id: s for s in [ fribourg, bern, zuerich_hb, winterthur, st_gallen, interlaken_ost, basel_sbb, chur, thusis, samedan, st_moritz, bern_duebystrasse, koeniz_zentrum, bern_bahnhof, ostermundigen_bahnhof, samedan_bahnhof, samedan_spital, ] } footpaths_per_from_stop_to_stop_id = {(s.id, s.id): Footpath(s.id, s.id, 2 * 60) for s in stops_per_id.values()} footpaths_per_from_stop_to_stop_id[(zuerich_hb.id, zuerich_hb.id)] = Footpath( zuerich_hb.id, zuerich_hb.id, 7 * 60) footpaths_per_from_stop_to_stop_id[(bern.id, bern.id)] = Footpath( bern.id, bern.id, 5 * 60) footpaths_per_from_stop_to_stop_id[(bern_bahnhof.id, bern.id)] = Footpath( bern_bahnhof.id, bern.id, 5 * 60) footpaths_per_from_stop_to_stop_id[(bern.id, bern_bahnhof.id)] = Footpath( bern.id, bern_bahnhof.id, 5 * 60) footpaths_per_from_stop_to_stop_id[(chur.id, chur.id)] = Footpath( chur.id, chur.id, 4 * 60) footpaths_per_from_stop_to_stop_id[(samedan.id, samedan_bahnhof.id)] = Footpath( samedan.id, samedan_bahnhof.id, 3 * 60) footpaths_per_from_stop_to_stop_id[(samedan_bahnhof.id, samedan.id)] = Footpath( samedan_bahnhof.id, samedan.id, 3 * 60) trips = [] trips += get_forth_and_back_trips( [fribourg, bern, zuerich_hb, winterthur, st_gallen], [22 * 60, 56 * 60, 26 * 60, 35 * 60], [6 * 60, 9 * 60, 3 * 60], hhmmss_to_sec("05:34:00"), 32, 30 * 60) trips += get_forth_and_back_trips([interlaken_ost, bern, basel_sbb], [52 * 60, 55 * 60], [12 * 60], hhmmss_to_sec("05:00:00"), 16, 60 * 60) trips += get_forth_and_back_trips([basel_sbb, zuerich_hb, chur], [53 * 60, 75 * 60], [11 * 60], hhmmss_to_sec("05:33:00"), 16, 60 * 60) trips += get_forth_and_back_trips([chur, thusis, samedan, st_moritz], [30 * 60, 75 * 60, 12 * 60], [2 * 60, 6 * 60], hhmmss_to_sec("05:58:00"), 16, 60 * 60) trips += get_forth_and_back_trips([ koeniz_zentrum, bern_duebystrasse, bern_bahnhof, ostermundigen_bahnhof ], [6 * 60, 7 * 60, 15 * 60], [0, 0], hhmmss_to_sec("05:00:00"), 10 * 16, 6 * 60) trips += get_forth_and_back_trips([samedan_bahnhof, samedan_spital], [7 * 60], [], hhmmss_to_sec("15:00:00"), 1, 24 * 60 * 60) return ConnectionScanData(stops_per_id, footpaths_per_from_stop_to_stop_id, {t.id: t for t in trips})
def test_unoptimized_earliest_arrival_bern_bern_bahnhof(): cs_data = create_test_connectionscan_data() cs_core = ConnectionScanCore(cs_data) assert "12:14:46" == seconds_to_hhmmss( cs_core.route_earliest_arrival(bern.id, bern_bahnhof.id, hhmmss_to_sec("12:09:46")))
def test_unoptimized_earliest_arrival_basel_st_gallen(): cs_data = create_test_connectionscan_data() cs_core = ConnectionScanCore(cs_data) assert "09:41:00" == seconds_to_hhmmss( cs_core.route_earliest_arrival(basel_sbb.id, st_gallen.id, hhmmss_to_sec("07:30:00")))
def test_hhmmss_to_sec(): assert 6 * 60 * 60 + 23 * 60 + 5 == hhmmss_to_sec("06:23:05")