def get_visit_path_by_id(stat_id_list, visit_path, visit_time): #print("get_visit_path_by_id",visit_path) global station_info_dic road_network_dic, station_info_dic = sp.preprocess() station_name_list = [] visit_name_path = [] visit_name_time = visit_time.copy() for stat_id in stat_id_list: station_name_list.append(str(station_info_dic[stat_id]['name'])) for stat_id in visit_path: visit_name_path.append(station_info_dic[stat_id]['name']) #original_path = visit_path.copy() #oritinal_time = visit_time.copy() oritinal_len = len(visit_path) #visited_path, visited_time = get_visit_path_by_name(station_name_list, visit_name_path, visit_time) get_visit_path_by_name(station_name_list, visit_name_path, visit_name_time) #print("After get_visit_path_by_name", visit_name_path) tmp_id_path = [] stat_name_dic = sp.create_stat_name_id_mapping(station_info_dic) for stat in visit_name_path[oritinal_len:len(visit_name_path)]: if stat in stat_name_dic: tmp_id_path.append(stat_name_dic[stat]) else: print("stat not found in name-id mapping") ''' id_path = [] stat_name_dic = sp.create_stat_name_id_mapping(station_info_dic) for stat in visit_path: if stat in stat_name_dic id_path.append(stat_name_dic[stat]) ''' #print("tmp_id_path", tmp_id_path) #print(visited_time) return tmp_id_path, visit_name_time[oritinal_len:len(visit_name_path)]
def computeDisMetrix(): staion_dic = get_station_dic() id_name = {} id = 0 for station in staion_dic: id_name[staion_dic[station]['name']] = id id += 1 dis_metrix = [([0] * len(id_name)) for i in range(len(id_name))] road_network_dic, station_info_dic = sp.preprocess() stations_shortest_path_dic = sp.get_all_stations_spt_dic_from_file() for station in id_name: for station1 in id_name: if station is not station1: distance = 0 print(station + "#" + station1) distance, path = sp.get_shortest_path( station, station1, station_info_dic, stations_shortest_path_dic) if distance == None: _, distance = internal_get_spt_from_stat_name( station, station1) dis_metrix[id_name[station]][id_name[station1]] = distance dis_metrix[id_name[station1]][id_name[station]] = distance return dis_metrix
def preprocess(): global station_info_dic global stat_name_dic if station_info_dic is None: road_network_dic, station_info_dic = sp.preprocess() if stat_name_dic is None: stat_name_dic = sp.create_stat_name_id_mapping(station_info_dic)
def get_visit_path(stat_id_list, visit, visit_time): #print(str(dt.timedelta(seconds = time.time()))) # Initialize the station dict road_network_dic, station_info_dic = sp.preprocess() stations_shortest_path_dic = sp.get_all_stations_spt_dic_from_file() station_list = [] for stat_id in stat_id_list: station_list.append(str(station_info_dic[stat_id]['name'])) permutation_list = get_permutation_with_mini_time(station_list) visit_path, visit_time = simulate_visit_station(permutation_list, visit, visit_time) #print(str(dt.timedelta(seconds = time.time()))) # id_path = [] # stat_name_dic = sp.create_stat_name_id_mapping(station_info_dic) # print(visit_path) # for stat in visit_path: # id_path.append(stat_name_dic[stat]) # visit_path = id_path return visit_path, visit_time
def route_with_sequence(clusters_list, cluster_info_dic): #print(cluster_info_dic) road_network_dic, station_info_dic = sp.preprocess() stations_shortest_path_dic = sp.get_all_stations_spt_dic_from_file() stat_name_dic = sp.create_stat_name_id_mapping(station_info_dic) total_time = 0 average_speed = 3 # m/s visit_path = [] visit_time = [] total_time_list = [] for cluster in clusters_list: print("***** cluster", cluster, " *****") path, time = sr.get_visit_path(cluster_info_dic[cluster]['stations'], visit_path, visit_time) if cluster != clusters_list[0]: dist_to_next_cluster, _ = sp.get_shortest_path_from_stat_id( prev_stat, stat_name_dic[path[0]], station_info_dic, stations_shortest_path_dic) print("dist_to_next_cluster", dist_to_next_cluster) total_time = total_time + (dist_to_next_cluster / average_speed) print(time) print(path) prev_stat = stat_name_dic[path[-1]] total_time = time[-1] print("total_time", total_time, str(datetime.timedelta(seconds=total_time))) total_time_list.append(total_time) return total_time_list
def main(): print("main") global cluster_info_dic global station_info_dic cluster_info_dic = cr.get_cluster_info_dic() road_network_dic, station_info_dic = sp.preprocess() print(cluster_info_dic) #print(station_info_dic) get_all_stats_travel_time() '''
def main(): road_network_dic, station_info_dic = sp.preprocess() stations_shortest_path_dic = sp.get_all_stations_spt_dic_from_file() stat1 = 'B-14' stat2 = 'B-15' if stations_shortest_path_dic is not None: distance, path = sp.get_shortest_path(stat1, stat2, station_info_dic, stations_shortest_path_dic) print('Shortest distance (meter)', distance) print('Shortest path', end = ": ") for road in path: print(road_network_dic[road]['coordinates'], end =", ") else: print('Please run shortest_path.py to generate the file')
import shortest_path as sp import cluster_routing as cr import station_routing as sr import json import data_readin_conversion as drc import pandas as pd import geopandas as gpd import os.path road_network_dic, station_info_dic = sp.preprocess() cluster_info_dic = cr.get_cluster_info_dic() local_station_status_csv = '../data/20190829_stn_status.csv' df_stations = pd.read_csv(local_station_status_csv) gdf_stations = drc.gdf_constructor(df_stations, 'Easting', 'Northing') gdf_stations = drc.app_coordinate_converter(gdf_stations) stat_perm_file = 'stat_perm_cache.json' stat_perm_with_start_file = 'stat_perm_with_start_cache.json' def main(): #print(station_info_dic) #print(gdf_stations.loc[0]['geometry'].y) stats = [ "CSE1", "RE11", "RE34", "RE31", "RE32", "CS19", "CSE5", "RE24", "CS18", "RE25" ] add_min_perm_with_start_to_cache(stats)
def begin_routing(stations_everyday): road_network_dic, station_info_dic = sp.preprocess() name_to_id = {} for id in station_info_dic: name_to_id[station_info_dic[id]['name']] = id print('Initial path:') for day in stations_everyday: each_day = stations_everyday[day] dat_str = [] for clusters in each_day: dat_str = dat_str + clusters print('day' + str(day) + ' path: ' + str(dat_str)) while (True): day = input("Input your current day: ") day = int(day) clusters = stations_everyday[day] dat_str = [] for cluster in clusters: dat_str = dat_str + cluster print('Today\'s path: ' + str(dat_str)) begin_time = input("Input your begin time: ") being_time = get_sec(begin_time) del stations_everyday[day] last_time_repeat = 0 current_station_index = 0 current_cluster_index = 0 stations = clusters[current_cluster_index] visit_path = [] visit_time = [] visit_current_time = [] Need_to_repeat = None while (True): station_name = input("Input your current station name: ") current_time = input("Input your current time: ") visit_current_time.append(current_time) current_time = get_sec(current_time) current_time = current_time - being_time visit_time.append(current_time) if station_name in visit_path: last_time_repeat = current_time visit_path.append(station_name) target = stations[0] if station_name in stations: # If on the right station if Need_to_repeat != None: stations.remove(Need_to_repeat) Need_to_repeat = None try: stations.remove(station_name) except ValueError: pass if current_time - last_time_repeat > N: if len(visit_path) != 0: # Get travel time to visited stations station_travel_time = {} for i in range(0, len(visit_path)): if station_name == visit_path[i]: continue travel_time = getTravelTime( station_name, visit_path[i]) if current_time - visit_time[ i] + travel_time > N and travel_time < M: station_travel_time[ visit_path[i]] = travel_time # Choose the station with minimum travel time if len(station_travel_time) > 0: sorted_s = sorted(station_travel_time.items(), key=operator.itemgetter(1)) sorted_station = collections.OrderedDict(sorted_s) first_station = next(iter(sorted_station)) Need_to_repeat = first_station # Update the new order new_list = [] new_list.append(first_station) for station in stations: new_list.append(station) new_list = list(set(new_list)) stations = get_permutation_start_with_station( new_list, first_station) else: print( "Timt to visit repeat station, but no satisified stations!" ) elif station_name != target: # Update the new order stations = list(set(stations)) stations = get_permutation_with_mini_time(stations) if len(stations) == 0: del clusters[current_cluster_index] current_cluster_index += 1 if current_cluster_index >= len(clusters): print( "You have visited all stations for today, good job!" ) break stations = clusters[current_cluster_index] travel_time = getTravelTime(station_name, stations[0]) back_time = getTravelTime(stations[0], 'CS25') if current_time + travel_time + back_time > 3600 * 8: if Need_to_repeat != None: try: stations.remove(Need_to_repeat) except ValueError: pass # Cut the current cluster left_clusters = {} id = 1 for cluster in clusters: left_clusters[str(id)] = {} left_clusters[str(id)]['visited'] = False left_clusters[str(id)]['stations'] = [] for stat in cluster: left_clusters[str(id)]['stations'].append( name_to_id[stat]) left_clusters[str(id)]['start'] = name_to_id[cluster[0]] id += 1 for left_day in stations_everyday: for cluster in stations_everyday[left_day]: left_clusters[str(id)] = {} left_clusters[str(id)]['visited'] = False left_clusters[str(id)]['stations'] = [] for stat in cluster: left_clusters[str(id)]['stations'].append( name_to_id[stat]) left_clusters[str(id)]['start'] = name_to_id[ cluster[0]] id += 1 print(left_clusters) new_stations_everyday = get_next_day_station_seq(left_clusters) new_day = day + 1 stations_everyday = {} for days in new_stations_everyday: stations_everyday[new_day] = [] for clu in new_stations_everyday[days]: temp = [] for sta in clu: if sta not in temp: temp.append(sta) stations_everyday[new_day].append(temp) new_day += 1 temp = {} print(stations_everyday) print( "You have reached 8 hours limit, now you can go back to CS25" ) break print("Next station is: " + stations[0]) if len(stations_everyday) == 0: print("You have visited all stations, good job!") return