def test_bus_estimate_accessor(self): bus_estimation_accessor.reset() bus_estimation_accessor.start() bus_estimation_accessor.add_sample('jalan1', 'jalan2', 90) real_value = bus_estimation_accessor.predict_eta('jalan1', 'jalan2') self.assertEqual(real_value, 90) bus_estimation_accessor.add_sample('jalan1', 'jalan2', 10) real_value = bus_estimation_accessor.predict_eta('jalan1', 'jalan2') self.assertEqual(real_value, 50) bus_estimation_accessor.stop()
def __generate_busway_mode(): """ :rtype list[BuswayMode] :return: """ mode_list = [] bus_route_list = bus_route_accessor.get_all_bus_routes() """:type :list[BusRoute]""" for bus_route in bus_route_list: station_list = bus_route.stations corridor_name = bus_route.corridor_name origin_list = station_list[:-1] destination_list = station_list[1:] for origin, destination in zip(origin_list, destination_list): eta = bus_estimation_accessor.predict_eta(origin, destination) eta = eta if eta else general_config.get_default_eta() bus_mode = BuswayMode() bus_mode.name = corridor_name bus_mode.corridor = corridor_name bus_mode.eta = max(eta / 60, 1) bus_mode.price = general_config.get_default_price() bus_mode.origin = origin bus_mode.destination = destination bus_mode.heading_from = station_list[0] bus_mode.heading_to = station_list[-1] mode_list.append(bus_mode) for destination, origin in zip(origin_list, destination_list): eta = bus_estimation_accessor.predict_eta(origin, destination) eta = eta if eta else general_config.get_default_eta() bus_mode = BuswayMode() bus_mode.name = corridor_name bus_mode.corridor = corridor_name bus_mode.eta = max(eta / 60, 1) bus_mode.price = general_config.get_default_price() bus_mode.origin = origin bus_mode.destination = destination bus_mode.heading_from = station_list[-1] bus_mode.heading_to = station_list[0] mode_list.append(bus_mode) return mode_list
def __update_bus_states_and_bus_queues(buses_data, mapping_threshold, station_locations, bus_routes): """ :type buses_data: dict[str,BusTrackData] :type bus_routes: list[BusRoute] :param buses_data: :return: """ bus_names = buses_data.keys() bus_coordinates = [(float(buses_data[bus_name].latitude), float(buses_data[bus_name].longitude)) for bus_name in bus_names] bus_stops = __locate_bus(bus_coordinates, mapping_threshold, station_locations) bus_name_stops = filter(lambda x: x[1] is not None, zip(bus_names, bus_stops)) # update bus states bus_state_list = bus_state_accessor.get_all_bus_state() for bus_name, bus_stop in bus_name_stops: bus_state = bus_state_accessor.get_bus_state(bus_name) if bus_state is not None: assert isinstance(bus_state, BusState) if not bus_state.last_station == bus_stop: bus_state.name = bus_name bus_state.previous_station = bus_state.last_station bus_state.previous_time_stop = bus_state.last_time_stop bus_state.last_station = bus_stop bus_state.last_time_stop = datetime.datetime.utcnow() bus_state.stop_list.append(bus_stop) bus_state_accessor.upset_bus_state(bus_state) if bus_state.last_time_stop is not None and bus_state.previous_time_stop is not None: origin = bus_state.previous_station destination = bus_state.last_station delta = (bus_state.last_time_stop - bus_state.previous_time_stop).seconds logging.info('Learn from {0} to {1} is {2}'.format( origin, destination, delta)) bus_estimation_accessor.add_sample(origin, destination, delta) else: bus_state = BusState() bus_state.name = bus_name bus_state.last_station = bus_stop bus_state.last_time_stop = datetime.datetime.utcnow() bus_state.stop_list.append(bus_stop) bus_state_accessor.upset_bus_state(bus_state) # update bus next stop next_bus_accessor.reset() refreshed_bus_state_list = bus_state_accessor.get_all_bus_state() for bus_state in refreshed_bus_state_list: for bus_route in bus_routes: forward_route = bus_route.stations backward_route = bus_route.stations[::-1] if __is_in(bus_state.stop_list, forward_route): last_station = bus_state.last_station last_index = forward_route.index(last_station) if last_index + 1 < len(forward_route): next_stop = forward_route[last_index + 1] prediction = bus_estimation_accessor.predict_eta( last_station, next_stop) next_bus = NextBus() next_bus.bus_name = bus_name next_bus.prediction = prediction next_bus.current_station = last_station next_bus_accessor.set_next_bus(forward_route[0], forward_route[-1], next_stop, next_bus) if __is_in(bus_state.stop_list, backward_route): last_station = bus_state.last_station last_index = backward_route.index(last_station) if last_index + 1 < len(backward_route): next_stop = backward_route[last_index + 1] prediction = bus_estimation_accessor.predict_eta( last_station, next_stop) next_bus = NextBus() next_bus.bus_name = bus_name next_bus.prediction = prediction next_bus.current_station = last_station next_bus_accessor.set_next_bus(forward_route[0], forward_route[-1], next_stop, next_bus)
def __update_bus_states_and_bus_queues(buses_data, mapping_threshold, station_locations, bus_routes): """ :type buses_data: dict[str,BusTrackData] :type bus_routes: list[BusRoute] :param buses_data: :return: """ bus_names = buses_data.keys() bus_coordinates = [(float(buses_data[bus_name].latitude), float(buses_data[bus_name].longitude)) for bus_name in bus_names] bus_stops = __locate_bus(bus_coordinates, mapping_threshold, station_locations) bus_name_stops = filter(lambda x: x[1] is not None, zip(bus_names, bus_stops)) # update bus states bus_state_list = bus_state_accessor.get_all_bus_state() for bus_name, bus_stop in bus_name_stops: bus_state = bus_state_accessor.get_bus_state(bus_name) if bus_state is not None: assert isinstance(bus_state, BusState) if not bus_state.last_station == bus_stop: bus_state.name = bus_name bus_state.previous_station = bus_state.last_station bus_state.previous_time_stop = bus_state.last_time_stop bus_state.last_station = bus_stop bus_state.last_time_stop = datetime.datetime.utcnow() bus_state.stop_list.append(bus_stop) bus_state_accessor.upset_bus_state(bus_state) if bus_state.last_time_stop is not None and bus_state.previous_time_stop is not None: origin = bus_state.previous_station destination = bus_state.last_station delta = (bus_state.last_time_stop - bus_state.previous_time_stop).seconds logging.info('Learn from {0} to {1} is {2}'.format(origin, destination, delta)) bus_estimation_accessor.add_sample(origin, destination, delta) else: bus_state = BusState() bus_state.name = bus_name bus_state.last_station = bus_stop bus_state.last_time_stop = datetime.datetime.utcnow() bus_state.stop_list.append(bus_stop) bus_state_accessor.upset_bus_state(bus_state) # update bus next stop next_bus_accessor.reset() refreshed_bus_state_list = bus_state_accessor.get_all_bus_state() for bus_state in refreshed_bus_state_list: for bus_route in bus_routes: forward_route = bus_route.stations backward_route = bus_route.stations[::-1] if __is_in(bus_state.stop_list, forward_route): last_station = bus_state.last_station last_index = forward_route.index(last_station) if last_index + 1 < len(forward_route): next_stop = forward_route[last_index + 1] prediction = bus_estimation_accessor.predict_eta(last_station, next_stop) next_bus = NextBus() next_bus.bus_name = bus_name next_bus.prediction = prediction next_bus.current_station = last_station next_bus_accessor.set_next_bus(forward_route[0], forward_route[-1], next_stop, next_bus) if __is_in(bus_state.stop_list, backward_route): last_station = bus_state.last_station last_index = backward_route.index(last_station) if last_index + 1 < len(backward_route): next_stop = backward_route[last_index + 1] prediction = bus_estimation_accessor.predict_eta(last_station, next_stop) next_bus = NextBus() next_bus.bus_name = bus_name next_bus.prediction = prediction next_bus.current_station = last_station next_bus_accessor.set_next_bus(forward_route[0], forward_route[-1], next_stop, next_bus)