def generate_locations(cities_fname): with open(cities_fname, 'r') as cities_file: address_list = [line.strip() for line in cities_file.readlines()] locs_list = [locm.Location(addr) for addr in address_list] moscow = locm.Location(address='Moscow') nodes_coords_list = [tver_coords] + [loc.coords for loc in locs_list ] + [ryazan_coords] # nodes_coords_list = [moscow.coords] + [loc.coords for loc in locs_list] + [moscow.coords] return locs_list, nodes_coords_list
def write_plotting_data_to_file(dropbox_filename = '/othodi/cities_few.txt',out_fname = 'data4plotting.txt'): m1=mapm.Map() m1.add_locations_from_file(fname=dropbox_filename,dropbox=True) print(len(m1.locations)) origin = locm.Location("Moscow",name='Moscow') raw_routes = [] for location in [locn for locn in m1.locations if locn.name != origin.name]: if 'lat' in location.coords and 'lng' in location.coords: try: raw_routes.append(gmaps.get_route(origin.coords, location.coords)) except Exception as e: print('Warning: Error in get_route for location %s: no data from gmaps?\n%s' % (location.address,e)) from operator import itemgetter raw_routes.sort(key=itemgetter(-1)) # sort by duration # for i,point in enumerate(m1.locations): # print("%i: %s" % (i,str(point.coords))) add_points_coords_list = [(point.coords['lat'],point.coords['lng']) for point in m1.locations] add_points_annotes_list = [point.name for point in m1.locations] with open(out_fname,'w') as f: f.write(str(raw_routes[0][0])) f.write('\n') f.write(str(raw_routes[0][1])) f.write('\n') f.write(str(add_points_coords_list)) f.write('\n') f.write(str(add_points_annotes_list)) return raw_routes[0][0],raw_routes[0][1],add_points_coords_list,add_points_annotes_list
def generate_locations(): cities_fname = 'test_city_names_list_100.txt' # cities_fname = 'test_city_names_list_21.txt' # cities_fname = 'test_city_names_list.txt' # cities_fname = 'cities_from_dropbox.txt' with open(cities_fname, 'r') as cities_file: address_list = [line.strip() for line in cities_file.readlines()] locs_list = [locm.Location(addr) for addr in address_list] moscow = locm.Location(address='Moscow') # routes_list = [routem.Route(moscow.coords,dest.coords) for dest in locs_list] # for route,loc in zip(routes_list,locs_list): # print(loc.address) # print(route.to_str()) nodes_coords_list = [tver_coords] + [loc.coords for loc in locs_list ] + [ryazan_coords] # nodes_coords_list = [moscow.coords] + [loc.coords for loc in locs_list] + [moscow.coords] return locs_list, nodes_coords_list
def add_locations_from_file(self, fname=None,dropbox=False): if dropbox: import dropboxm dc = dropboxm.DropboxConnection() f = dc.open_dropbox_file(fname) elif not fname: f = open('test_city_names_list.txt','r') else: raise ValueError("wrong arguments given to Map#add_locations_from_file()") for line in f: if '#' in line: # scip commented lines continue time.sleep(0.5) # 10 requests per second - google api try: new_loc = locm.Location(address=line.strip(),name=line.strip()) self.locations.append(new_loc) except Exception as e: print('Can`t create a location. Error:\n%s' % e) return True
# -*- coding: utf-8 -*- import shutil import locm, routem, mapm, dropboxm, gmaps, tools if __name__ == "__main__": # dr_fname = '/othodi/cities_few.txt' # dc = dropboxm.DropboxConnection() # with dc.open_dropbox_file(dr_fname) as dropbox_file: # address_list = [line.strip() for line in dropbox_file] locs_list = [locm.Location(addr) for addr in address_list] moscow = locm.Location('Moscow') routes_list = [ routem.Route(moscow.coords, dest.coords) for dest in locs_list ] for route, loc in zip(routes_list, locs_list): print(loc.address) print(route.to_str())
def try_to_guess_routes(): cities_fname = 'test_city_names_list_100.txt' # cities_fname = 'test_city_names_list_21.txt' # cities_fname = 'test_city_names_list.txt' # cities_fname = 'cities_from_dropbox.txt' FILE_WITH_COORDS_PAIRS_NAME = "cities_coords.txt" moscow = locm.Location(address='Moscow') ## run this when need to update cities coords / change cities list # locs_list, nodes_coords_list = generate_locations() # put_locs_to_file(nodes_coords_list,fname = FILE_WITH_COORDS_PAIRS_NAME) ## routes_list = [routem.Route(moscow.coords,dest.coords) for dest in locs_list] ## for route,loc in zip(routes_list,locs_list): ## print(loc.address) ## print(route.to_str()) # run this to only prepare the tsp test nodes_coords_list = read_coords_from_file( FILE_WITH_COORDS_PAIRS_NAME) # only variable nodes` coords here tsp_params_list = get_tsp_params_list(FILE_WITH_COORDS_PAIRS_NAME) import sys logger.error("max_itterations = %d" % (tsp_params_list[4])) sys.argv = tsp_params_list result_tuple = tspm.main() locs_coords_list = [nodes_coords_list[index] for index in result_tuple[-1]] plot_fname = 'othodi_app_test_%d_%f.html' % (len(nodes_coords_list), result_tuple[1]) fig_on_gmap = bokehm.Figure(output_fname=plot_fname, use_gmap=True, center_coords=nodes_coords_list[0]) # fig_on_gmap.add_line(locs_coords_list,circle_size=1, circles_color='red',alpha=1.) # fig_on_gmap.add_line([nodes_coords_list[0]],circle_size=35, circles_color='green',alpha=0.5) # fig_on_gmap.save2html() # fig_on_gmap.show() cars_num = 5 # # cities_num=100 only_var_nodes = locs_coords_list[1:-1] cities_num = (len(only_var_nodes)) cities_per_car = cities_num // cars_num print("cities_per_car=%d" % cities_per_car) parts = [ only_var_nodes[car_i * cities_per_car:(car_i + 1) * cities_per_car] for car_i in range(cars_num) ] parts_indeces = [ range(car_i * cities_per_car, (car_i + 1) * cities_per_car, 1) for car_i in range(cars_num) ] # print(parts_indeces) # print(parts) best_scores_list = [] best_routes_list = [] colors_list = ["red", "green", "blue", "orange", "pink"] # parts_with_start_finish = [[nodes_coords_list[0]] + part + [nodes_coords_list[-1]] for part in parts] put_locs_to_file(locs_coords_list[1:-1], fname="cities_coords_all_in_order.txt") for i, part in enumerate(parts): part_coords_file_name = "cities_coords_part_%d.txt" % (i) put_locs_to_file(part, fname=part_coords_file_name) logger.info( "reading var nodes` coords from file - one car route evaluation") nodes_coords_list = read_coords_from_file( part_coords_file_name) # only variable nodes` coords here tsp_params_list = get_tsp_params_list(part_coords_file_name) import sys sys.argv = tsp_params_list logger.info("starting part route evaluation") result_tuple = tspm.main() logger.info("preparing list of dicts of coords for plotting") locs_coords_list = [tver_coords] + [ nodes_coords_list[index] for index in result_tuple[-1] ] + [ryazan_coords] # locs_coords_list = [moscow.coords]+[nodes_coords_list[index] for index in result_tuple[-1]]+[moscow.coords] # fig_on_gmap = bokehm.Figure(output_fname='o_part_%d_ncities_%d_%f.html' % (i,len(part),result_tuple[1]),use_gmap=True, center_coords=nodes_coords_list[0]) circle_sizes = [(i * 3) for index in locs_coords_list] fig_on_gmap.add_line(locs_coords_list, circle_size=circle_sizes, circles_color=colors_list[i], alpha=0.5) # fig_on_gmap.add_line([nodes_coords_list[0]],circle_size=35, circles_color=colors_list[i],alpha=0.5) logger.error("a car route: part %d ncities=%d length=%f" % (i, len(part), result_tuple[1])) best_scores_list.append(result_tuple[1]) best_routes_list.append(result_tuple[-1]) fig_on_gmap.save2html() return best_scores_list, best_routes_list, plot_fname
print(len(car_routes[1])) print(len(car_routes[2])) print(len(car_routes[3])) print(len(car_routes[4])) # print(car_routes['coords'][0][:,0]) # print(car_routes['coords'][0][:,1]) # print(r(tver_coords,ryazan_coords)) # print(r(tver_coords,car_routes['coords'][0][0])) # print(car_routes['rs']) for car_route in car_routes: car_route.sort(order='rs') # print(car_routes['rs']) routes_coords_dicts_lists = create_coords_dicts_lists(car_routes) moscow = locm.Location(address='Moscow') fig_on_gmap = bokehm.Figure(output_fname='threads_pot_sorted_nearest.html', use_gmap=True, center_coords=moscow.coords) circle_sizes = 10 colors_list = ['red', 'green', 'blue', 'orange', 'yellow'] for car_number in range(n_cars): fig_on_gmap.add_line(routes_coords_dicts_lists[car_number], circle_size=circle_sizes, circles_color=colors_list[car_number], alpha=1.) fig_on_gmap.show() import sys sys.exit(0) # t_start = time.time()