def plot_route(self, route_order): x = list([]) y = list([]) plots = list([]) arrow_plots = list([]) arrow_labels = list([]) # Iterate over vertices, retrieving x and y coordinates for vertex in self.vertices: x.append(vertex.x) y.append(vertex.y) # Plot the vertices vertex_plot = plt.scatter(x,y, label="Vertices") plots.append(vertex_plot) # Plot the route for vertex_index in range(len(route_order)-1): arrow_label = "Edge {}->{}".format(route_order[vertex_index], route_order[vertex_index+1]) arrow_plot = plt.arrow(self.vertices[route_order[vertex_index]-1].x, self.vertices[route_order[vertex_index]-1].y, self.vertices[route_order[vertex_index+1]-1].x - self.vertices[route_order[vertex_index]-1].x, self.vertices[route_order[vertex_index+1]-1].y - self.vertices[route_order[vertex_index]-1].y, head_width=1, head_length=1, color='#{}{}{}'.format(Math.color_quantization( self.vertices[route_order[vertex_index]-1].vertex_id, len(self.vertices)), str(hex(int(random.uniform(0.2, 1)*256)))[2:], Math.color_quantization(self.vertices[route_order[vertex_index+1]-1].vertex_id, len(self.vertices))), label=arrow_label) arrow_labels.append(arrow_label) arrow_plots.append(arrow_plot) # Show the graph with a legend plt.legend(arrow_plots, arrow_labels, loc=2, fontsize='small') plt.show()
def plot(self): x = list([]) y = list([]) plots = list([]) arrow_plots = list([]) arrow_labels = list([]) # Iterate over vertices, retrieving x and y coordinates for vertex in self.vertices: x.append(vertex.x) y.append(vertex.y) # Plot the vertices vertex_plot = plt.scatter(x, y, label="Vertices") plots.append(vertex_plot) # Plot the edges for vertex in self.vertices: for adjacent_vertex in vertex.adjacent_vertices: arrow_label = "Edge {}->{}".format(vertex.vertex_id, adjacent_vertex.vertex_id) arrow_plot = plt.arrow(vertex.x, vertex.y, adjacent_vertex.x-vertex.x, adjacent_vertex.y-vertex.y, head_width=1, head_length=1, color='#{}{}{}'.format(Math.color_quantization(vertex.vertex_id, len(self.vertices)), str(hex(int(random.uniform(0.2, 1)*256)))[2:], Math.color_quantization(adjacent_vertex.vertex_id, len(self.vertices))), label=arrow_label) plots.append(arrow_plot) arrow_plots.append(arrow_plot) arrow_labels.append(arrow_label) # Show the graph with a legend plt.legend(arrow_plots, arrow_labels, loc=2, fontsize='small') plt.show()
def plot(self): x = list([]) y = list([]) plots = list([]) arrow_plots = list([]) arrow_labels = list([]) # Iterate over vertices, retrieving x and y coordinates for vertex in self.vertices: x.append(vertex.x) y.append(vertex.y) # Plot the vertices vertex_plot = plt.scatter(x, y, label="Vertices") plots.append(vertex_plot) # Plot the route for edge in self.edges: vertex = edge.vertices[0] adjacent_vertex = edge.vertices[1] arrow_label = "Edge {}->{}".format(vertex.vertex_id, adjacent_vertex.vertex_id) arrow_plot = plt.arrow(vertex.x, vertex.y, adjacent_vertex.x-vertex.x, adjacent_vertex.y-vertex.y, head_width=1, head_length=1, color='#{}{}{}'.format(Math.color_quantization(vertex.vertex_id, len(self.vertices)), Math.color_quantization(vertex.vertex_id % adjacent_vertex.vertex_id + 1, len(self.vertices)), Math.color_quantization(adjacent_vertex.vertex_id, len(self.vertices))), label=arrow_label) arrow_labels.append(arrow_label) arrow_plots.append(arrow_plot) # Show the graph with a legend plt.legend(arrow_plots, arrow_labels, loc=2, fontsize='small') plt.show()
def edge_passes_over_route(self, test_edge): if self.edges is None: return False else: for edge in self.edges: if Math.lines_intersect(edge.get_points(), test_edge.get_points()): return True return False
def generate_heat_map(self, superiority_tolerance=0.3): graph = self.graph x = list([]) y = list([]) plots = list([]) arrow_plots = list([]) arrow_labels = list([]) # Iterate over vertices, retrieving x and y coordinates for vertex in graph.vertices: x.append(vertex.x) y.append(vertex.y) # Plot the vertices vertex_plot = plt.scatter(x, y, label="Vertices") plots.append(vertex_plot) for edge_key, edge_entry in self.edge_dictionary.items(): if edge_entry.edge_count >= (self.max_edge_count * superiority_tolerance): vertices = re.findall(r'\d+', edge_entry.edge_key) vertex_start = graph.get_vertex_by_id(int(vertices[0])) vertex_end = graph.get_vertex_by_id(int(vertices[1])) arrow_label = "Edge {}->{}".format(vertices[0], vertices[1]) arrow_plot = plt.arrow( vertex_start.x, vertex_start.y, vertex_end.x - vertex_start.x, vertex_end.y - vertex_start.y, head_width=1, head_length=1, color='#{}{}{}'.format( Math.normalize_rgb( self.max_edge_count - edge_entry.edge_count, 0, self.max_edge_count), "00", Math.normalize_rgb(edge_entry.edge_count, 0, self.max_edge_count)), label=arrow_label) plots.append(arrow_plot) arrow_plots.append(arrow_plot) arrow_labels.append(arrow_label) # Show the graph with a legend plt.legend(arrow_plots, arrow_labels, loc=2, fontsize='small') plt.show()
def get_shortest_distance_to_route(self, vertex): closest_distance = None closest_item = None if self.edges is not None: for edge in self.edges: distance_to_edge = edge.compute_distance_to_vertex(vertex) if closest_distance is None: closest_distance = distance_to_edge closest_item = edge else: if closest_distance > distance_to_edge: closest_distance = distance_to_edge closest_item = edge else: if self.vertices is not None: closest_distance = Math.calculate_distance_from_point_to_point(self.vertices[-1].get_location(), vertex.get_location()) closest_item = self.vertices[-1] else: return 0, None, None return closest_item, closest_distance
def compute_distance_to_vertex(self, vertex): return Math.calculate_distance_from_line_to_point( self.get_points(), vertex.get_location())
def lasso(self, vertex, closest_item_to_next_vertex): if isinstance(closest_item_to_next_vertex, Edge): # Get v1, v2 edge_vertex1 = closest_item_to_next_vertex.vertices[0] edge_vertex2 = closest_item_to_next_vertex.vertices[1] # use v2's index to get v3 edge_vertex2_index = np.where(self.vertices == edge_vertex2)[0] if edge_vertex2_index < len(self.vertices) - 1: v3 = self.vertices[edge_vertex2_index + 1][0] # Calculate different edge distances. v1_v2 = closest_item_to_next_vertex.distance if edge_vertex2_index < len(self.vertices) - 2: v1_v2_v0_v3 = v1_v2 + Math.calculate_distance_from_point_to_point(edge_vertex2.get_location(), vertex.get_location()) + \ Math.calculate_distance_from_point_to_point(vertex.get_location(), v3.get_location()) v1_v0_v2_v3 = Math.calculate_distance_from_point_to_point(edge_vertex1.get_location(), vertex.get_location()) + \ Math.calculate_distance_from_point_to_point(vertex.get_location(), edge_vertex2.get_location()) + \ Math.calculate_distance_from_point_to_point(edge_vertex2.get_location(), v3.get_location()) else: v1_v2_v0_v3 = v1_v2 + Math.calculate_distance_from_point_to_point( edge_vertex2.get_location(), vertex.get_location()) v1_v0_v2_v3 = Math.calculate_distance_from_point_to_point(edge_vertex1.get_location(), vertex.get_location()) + \ Math.calculate_distance_from_point_to_point(vertex.get_location(), edge_vertex2.get_location()) # Choose the shortest configuration if v1_v2_v0_v3 < v1_v0_v2_v3: # Best to insert it after edge_vertex2 in vertices list # Calculate new vertex location in list and insert it new_vertex_location = np.where( self.vertices == edge_vertex2)[0] + 1 self.vertices = np.insert(self.vertices, new_vertex_location, vertex) # calculate the new edge's location edge_v1_v2 = [ edge for edge in self.edges if edge == closest_item_to_next_vertex ][0] edge_v1_v2_index = np.where(self.edges == edge_v1_v2)[0] new_edge_location = edge_v1_v2_index + 1 # create edge v0_v3 and insert it. Remove edge v2_v3 if new_vertex_location < len(self.vertices) - 1: for edge in self.edges: if edge.vertices[ 0].vertex_id == edge_vertex2.vertex_id and edge.vertices[ 1].vertex_id == v3.vertex_id: edge_v2_v3 = edge self.edges = self.edges[self.edges != edge_v2_v3] self.distance_traveled -= edge_v2_v3.distance edge_v0_v3 = Edge(vertex, v3) self.edges = np.insert(self.edges, new_edge_location, edge_v0_v3) self.distance_traveled += edge_v0_v3.distance # create edge v2_v0 and insert it edge_v2_v0 = Edge(edge_vertex2, vertex) self.edges = np.insert(self.edges, new_edge_location, edge_v2_v0) self.distance_traveled += edge_v2_v0.distance else: # Best to insert it before edge_vertex2 in vertices list # Calculate new vertex location in list and insert it new_vertex_location = np.where( self.vertices == edge_vertex2)[0] self.vertices = np.insert(self.vertices, new_vertex_location, vertex) # Calculate v1_V2 edge index for reference edge_v1_v2 = [ edge for edge in self.edges if edge == closest_item_to_next_vertex ][0] edge_v1_v2_index = np.where(self.edges == edge_v1_v2)[0] # create edges and insert them edge_v1_v0 = Edge(edge_vertex1, vertex) edge_v0_v2 = Edge(vertex, edge_vertex2) self.edges = np.insert(self.edges, edge_v1_v2_index, edge_v0_v2) self.edges = np.insert(self.edges, edge_v1_v2_index, edge_v1_v0) # Remove unnecessary edge self.edges = self.edges[self.edges != edge_v1_v2] # Update distance self.distance_traveled -= edge_v1_v2.distance self.distance_traveled += edge_v1_v0.distance self.distance_traveled += edge_v0_v2.distance elif isinstance(closest_item_to_next_vertex, Vertex): self.goto(vertex) # Set vertex to be true. vertex.visited = True