def carrinheiro(id_user, date): user_carrinheiro = User.get_user(id_user, DATABASE_DIRECTORY) path = Path.Path(user_carrinheiro, date, DATABASE_DIRECTORY) stop_points = scenario_stop_points(path) # download the osm file (scenario) file_name_osm = OpenSteetMap.file_osm(MAPS_DIRECTORY, stop_points) # download the GeoTiff file (scenario) geotiff_name = GeoTiff.geotiff(MAPS_DIRECTORY, stop_points) G, nodes_coordinates, nodes_mass_increment = graph_scenario( stop_points, geotiff_name, path.material_weights, file_name_osm) H = Graph_Collect.create_graph_route(nodes_coordinates, nodes_mass_increment) index_coordinate_start = list(nodes_coordinates.values()).index( path.start_point) node_source = list(nodes_coordinates.keys())[index_coordinate_start] index_coordinate_end = list(nodes_coordinates.values()).index( path.end_point) node_target = list(nodes_coordinates.keys())[index_coordinate_end] cost_total, paths = closest_insertion_path(G, H, node_source, node_target) for i in paths: fig, ax = ox.plot_graph_route(G, i, route_linewidth=6, node_size=0, bgcolor='w') return paths
def genetic_algorithm(G, H, node_source, node_target, nodes_coordinates, impedance): nodes_h = list(nodes_coordinates.keys()) route = GA.GA(G, H, node_source, node_target, nodes_h, impedance) cost_total, paths = Graph_Collect.sum_costs_route(G, H, route, VEHICLE_MASS, impedance) return cost_total, paths
def further_insertion_path(G, H, node_source, node_target, impedance): # order the visit of the stop points route = Heuristics.further_insertion(G, H, node_source, node_target, impedance) # create the path to visit all stop points cost_total, paths = Graph_Collect.sum_costs_route(G, H, route, VEHICLE_MASS, impedance) return cost_total, paths
def nearest_neighbor_path(G, H, node_source, node_target, impedance): #start = time.time() route = Heuristics.nearest_neighbor(G, H, node_source, node_target, VEHICLE_MASS, impedance) cost_total, paths = Graph_Collect.sum_costs_route(G, H, route, VEHICLE_MASS, impedance) #end = time.time() #print("Total cost route nearest", route, cost_total) #print("time nearest (s)", end - start) return cost_total, paths
def genetic_algorithm(G, H, node_source, node_target, nodes_coordinates, impedance): nodes_h = list(nodes_coordinates.keys()) start = time.time() route = GA.GA(G, H, node_source, node_target, nodes_h, impedance) end = time.time() cost_total, paths = Graph_Collect.sum_costs_route(G, H, route, VEHICLE_MASS, impedance) print("Total cost route GA", route, cost_total) print("Time GA", end - start) return cost_total, paths
def further_insertion_path(G, H, node_source, node_target, impedance): #start = time.time() # order the visit of the stop points route = Heuristics.further_insertion(G, H, node_source, node_target, impedance) # create the path to visit all stop points cost_total, paths = Graph_Collect.sum_costs_route(G, H, route, VEHICLE_MASS, impedance) #end = time.time() #print("Total cost route closest insertion", route, cost_total) #print("time closest insertion (s)", end - start) return cost_total, paths
def exact_method(G, H, source, target): nodes_graph = list(H.nodes) nodes_graph.remove(source) if source != target: nodes_graph.remove(target) permutations = list( more_itertools.distinct_permutations(nodes_graph, len(nodes_graph))) paths = [] costs = [] all_permutations = [] for i in permutations: i = list(i) i.insert(0, source) i.append(target) all_permutations.append(i) sum_costs, paths = Graph_Collect.sum_costs_route(G, H, i, VEHICLE_MASS) costs.append(sum_costs) minimum = min(costs) index_minimum = costs.index(minimum) return minimum, all_permutations[index_minimum], paths
def create_route(stop_points, material_weights, json_files, n = None, n_points=10): # the desired geotiff name to the mosaic mosaic_geotiff_name = 'out.tif' geotiff_name_out = MAPS_DIRECTORY + mosaic_geotiff_name # creates the geographic rectangular area based on stop points coordinates # downloads the Open Street Map (osm) file of the area osm_file = OpenSteetMap.file_osm(MAPS_DIRECTORY, stop_points) # downloads the GeoTiff file(s) of the area from the Topodata site # it returns the geotiff name of the Topodata database geotiff_name = GeoTiff.geotiff(MAPS_DIRECTORY, stop_points) geotiff_name = MAPS_DIRECTORY + geotiff_name + '.tif' # gdal translate of the geotiff file geotiff_transformation(geotiff_name, geotiff_name_out) # creates a rectangular area based on stop points coordinates max_lat, min_lat, max_lon, min_lon = Coordinates.create_osmnx(stop_points) # G_file is the name of the graph file (.graphml extension) G_file = Saves.def_file_name(MAPS_DIRECTORY, stop_points, '.graphml') # defines the json file name of the simulation results file_name_json = Saves.def_file_name(RESULTS_DIRECTORY, stop_points, '') + '_' + str(n) + '_' + str(n_points) json_files.append(file_name_json) # the coordinates of the area is used to verify if the graph exists coordinates_list = Coordinates.coordinates_list_bbox(stop_points) """ # if the graph exists, it is not necessary to do all configurations on the graph if verify_graph_exists(G_file, stop_points, coordinates_list) is True: # creates the .net.xml file (SUMO simulator file) netconvert_geotiff(osm_file, geotiff_name_out, NET) # configure the elevation and edge weights # creates dictionaries with coordinates and mass increment data G, nodes_coordinates, nodes_mass_increment = nodes_data(G_file, stop_points, material_weights, osm_file, geotiff_name) else: """ # delete collect points added before in the osm file Map_Simulation.delete_osm_items(osm_file) # if there are ways that can be bidirectional to 'carrinheiro' if BIDIRECTIONAL is True: Map_Simulation.edit_map(osm_file) # creates the osmnx graph based on the geographic area # Scenario graph (paths are edges and junctions are nodes) G = ox.graph_from_bbox(max_lat, min_lat, max_lon, min_lon, network_type='all') # configure the graph G, nodes_coordinates, nodes_mass_increment = Graph.configure_graph_simulation(G, geotiff_name, stop_points, material_weights, osm_file, G_file) ########### # the starting point is the first collect point of the vector index_source = list(nodes_coordinates.values()).index(stop_points[0]) node_source = list(nodes_coordinates.keys())[index_source] # the arrival point is the last collect point of the vector index_target = list(nodes_coordinates.values()).index(stop_points[-1]) node_target = list(nodes_coordinates.keys())[index_target] # creates the ordering graph # it is a complete graph with the number of nodes equivalent to number of collect points H = Graph_Collect.create_graph_route(nodes_coordinates, nodes_mass_increment) # dictionary with adjacent edges information dict_edges_net = Map_Simulation.edges_net(NET) # it is necessary configure the edges on simulator to allow the carrinheiro's type of vehicle Map_Simulation.allow_vehicle(NET) politics = ['weight', 'length'] heuristics = ['SPFA', 'dijkstra', 'astar'] """ # Nearest heuristic_combination(nn, G, H, node_source, node_target, politics[0], heuristics[0], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(nn, G, H, node_source, node_target, politics[0], heuristics[1], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(nn, G, H, node_source, node_target, politics[0], heuristics[2], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(nn, G, H, node_source, node_target, politics[1], heuristics[0], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(nn, G, H, node_source, node_target, politics[1], heuristics[1], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(nn, G, H, node_source, node_target, politics[1], heuristics[2], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) # Closest heuristic_combination(ci, G, H, node_source, node_target, politics[0], heuristics[0], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(ci, G, H, node_source, node_target, politics[0], heuristics[1], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(ci, G, H, node_source, node_target, politics[0], heuristics[2], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(ci, G, H, node_source, node_target, politics[1], heuristics[0], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(ci, G, H, node_source, node_target, politics[1], heuristics[1], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(ci, G, H, node_source, node_target, politics[1], heuristics[2], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) """ # Further heuristic_combination(fi, G, H, node_source, node_target, politics[0], heuristics[0], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(fi, G, H, node_source, node_target, politics[0], heuristics[1], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(fi, G, H, node_source, node_target, politics[0], heuristics[2], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(fi, G, H, node_source, node_target, politics[1], heuristics[0], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(fi, G, H, node_source, node_target, politics[1], heuristics[1], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) heuristic_combination(fi, G, H, node_source, node_target, politics[1], heuristics[2], stop_points, dict_edges_net, nodes_mass_increment, file_name_json) return json_files
def nearest_neighbor(G, H, source, target, impedance): """ :param G: NetworkX graph. input graph :param source: Float Id of the start node :param target: Float Id of the goal node :param weight: Function :return: List List with all nodes of the shortest path """ if source not in H: print("Error") return False open = [source] closed = [] current_vehicle_mass = VEHICLE_MASS nodes_graph = list(H.nodes) nodes_graph.remove(target) route = [] cost_total = 0 edges_update_mass = [] while len(open) > 0: dist = {} node = open.pop(0) closed.append(node) missing = verifying_nodes(closed, nodes_graph) # if current node is the target (objective) and # there is not nodes missing to be visited if node == target and missing is False: return cost_total, route, edges_update_mass else: # checks nodes that have not yet been added in closed possibilities = set(H.adj[node]) - set(closed) for u in possibilities: # checks the edge weight according to the vehicle's mass + # mass increase at the current vertex edge_cost, path = Graph_Collect.cost_path( G, node, u, current_vehicle_mass, impedance) dist.update([(u, [edge_cost, path])]) # sorting the dict according to edge weights dist = dict(sorted(dist.items(), key=lambda item: item[1][0])) # if starting and arrival point is the same node if len(dist) < 1 and source == target: new_node = target else: new_node = list(dist.keys())[0] # if there are more than one not visited node # and the nearest node is the arrival point if len(dist) > 1 and new_node == target: new_node = list(dist.keys())[1] route.extend(list(dist.values())[1][1][:-1]) cost_total += float(list(dist.values())[1][0]) edges_update_mass.append(list(dist.values())[0][1][:2]) else: route.extend(list(dist.values())[0][1][:-1]) cost_total += float(list(dist.values())[0][0]) edges_update_mass.append(list(dist.values())[0][1][:2]) open.append(new_node) current_vehicle_mass += H.nodes[new_node]['mass']
def further_insertion(G, H, source, target, impedance, heuristic): current_vehicle_mass = VEHICLE_MASS path = [source] costs_to_source = {} # create a dictionary with the nodes and respective mass increments of the vehicle mass = {} for i in H.nodes: mass.update([(i, H.nodes[i]['mass'])]) # verify the cost of the source to the nodes for u in H.adj[source]: edge_cost, _ = Graph_Collect.cost_path(G, source, u, current_vehicle_mass, impedance, heuristic) costs_to_source.update([(u, edge_cost)]) # sorting the dict according to edge weights costs_to_source = dict( sorted(costs_to_source.items(), key=lambda item: item[1], reverse=True)) # add the closest node of the source path.append(list(costs_to_source.keys())[0]) # updates the vehicle mass according to current path current_vehicle_mass = updates_vehicle_mass(path, mass) nodes = list(H.nodes) nodes.remove(target) possibilities = set(nodes) - set(path) # all nodes must be visited while len(possibilities) > 0: # len(path) < len(nodes): # get the closest node of any node inside the path max_cost = float('-inf') k_node = float('inf') for a in path: for b in possibilities: cost, _ = Graph_Collect.cost_path(G, a, b, current_vehicle_mass, impedance, heuristic) if cost > max_cost: max_cost = cost k_node = b # the k node must be inserted in a position of the path # where the cost (cost_IK + cost_KJ - cost_IJ) is minimum max_cost = float('-inf') position = 0 for i in range(len(path) - 1): current_vehicle_mass = updates_vehicle_mass(path[:i], mass) cost_IK, _ = Graph_Collect.cost_path(G, path[i], k_node, current_vehicle_mass, impedance, heuristic) cost_KJ, _ = Graph_Collect.cost_path(G, k_node, path[i + 1], current_vehicle_mass, impedance, heuristic) cost_IJ, _ = Graph_Collect.cost_path(G, path[i], path[i + 1], current_vehicle_mass, impedance, heuristic) total_cost = cost_IK + cost_KJ - cost_IJ # print('costs', cost_IK, cost_KJ, cost_IJ, total_cost) if total_cost > max_cost: a_1 = path[i] a_2 = path[i + 1] max_cost = total_cost position = i + 1 path.insert(position, k_node) current_vehicle_mass = updates_vehicle_mass(path, mass) # nodes not yet visited possibilities = set(nodes) - set(path) path.append(target) # get all paths cost_total, paths, edges_update = Graph_Collect.sum_costs_route( G, H, path, VEHICLE_MASS, impedance, heuristic) if impedance == 'weight': G = Graph.update_weight(G, VEHICLE_MASS) return cost_total, paths, edges_update
def nearest_neighbor_path(G, H, node_source, node_target, impedance, heuristic): route, paths = Heuristics.nearest_neighbor(G, H, node_source, node_target, VEHICLE_MASS, impedance, heuristic) cost_total, paths = Graph_Collect.sum_costs_route(G, H, route, VEHICLE_MASS, impedance, heuristic) return cost_total, paths
def fitness_individual(G, H, individual, impedance): total_work_individual, _ = Graph_Collect.sum_costs_route( G, H, individual, VEHICLE_MASS, impedance) return total_work_individual