def process(self, curr_node): """Process the maze by running through the binary heap, until the end node is found""" current_node = self.nodes[curr_node[0]] current_node_distance = curr_node[1] connected_nodes = 0.0 total_nodes = len(self.nodes) while current_node.end == False: connected_nodes += 1 percent = connected_nodes / total_nodes * 100 replace_print( 'Investigating {0:18} Number {1} of {2} nodes ({3:3.2f} %)'. format(current_node.prettify, int(connected_nodes), total_nodes, percent)) # Add min node to visited_nodes and remove it from the priority_que self.visited_nodes.add(current_node.name) self.priority_que.delete_min() # Find the adjacent nodes of curr_node adjacent_nodes = current_node.adjacent_nodes for direction, node_distance in adjacent_nodes.items(): if node_distance and not (node_distance[0].name in self.visited_nodes): self.add_value(node_distance[0].name, node_distance[1] + current_node_distance) node_distance[0].set_prev_node(current_node) self.heapify() current_node = self.nodes[self.priority_que.heap[0][0]] current_node_distance = self.priority_que.heap[0][1] end_node = self.priority_que.delete_min() end_node[0] = self.nodes[end_node[0]] # Final increment and print of "Investigating Node..." connected_nodes += 1 percent = connected_nodes / total_nodes * 100 replace_print( 'Investigating {0:18} Number {1} of {2} nodes ({3:3.2f} %)'.format( current_node.prettify, int(connected_nodes), total_nodes, percent)) print('\nEnd node has been found: {}'.format(end_node[0].prettify)) print('\n') print( 'The path length from start to end node is {} pixels long'.format( end_node[1])) return end_node
def find_nodes(self): """Finds and returns nodes in a maze""" print 'Finding nodes...' maze_copy = self.maze.copy() node_dict = self.make_start_end_node() for key, node in node_dict.items(): maze_copy.putpixel((node.x_pos, node.y_pos), self.RED) found_nodes = 2 number_of_nodes = 2 # Get the rest of the nodes for y in xrange(1, self.height - 1): for x in xrange(1, self.width): pix = self.maze.getpixel((x, y)) if self.check_white(pix): isNode = True directions = self.get_surroundings(x, y) up_and_down = directions['up'] and directions['down'] up_or_down = directions['up'] or directions['down'] left_and_right = directions['left'] and directions['right'] left_or_right = directions['left'] or directions['right'] # Rules for a node (a node is where you can / must change direction while following a path) if up_and_down and not left_or_right: isNode = False elif left_and_right and not up_or_down: isNode = False # Color maze, assign nodes if isNode: node_dict['node_%s_%s' % (x, y)] = self.Node( x, y, surroundings=self.get_surroundings(x, y)) found_nodes += 1 replace_print( 'Number of nodes found: {}'.format(found_nodes)) maze_copy.putpixel((x, y), self.RED) print '\n' filename = self.maze.filename.replace('cropped', 'nodes') maze_copy.save(filename) return node_dict
def make_graph(self): """Connect the nodes""" connected_nodes = 0.0 total_nodes = len(self.node_dict.keys()) direction_sums = { 'up': (0, -1), 'down': (0, 1), 'left': (-1, 0), 'right': (1, 0) } # Loop through the nodes for key, node in self.node_dict.items(): connected_nodes += 1 string = '{} nodes connected out of {} ({:.2f} %)'.format( connected_nodes, total_nodes, connected_nodes / total_nodes * 100) # Pull a given node from the dictionary, get some of its attributes surroundings = node.surroundings x_pos = node.x_pos y_pos = node.y_pos # Loop through its surroundings, find nodes for direction in surroundings: path = surroundings[direction] if path: # Get the adjacent node and its position in tuple form from check_nodes_in_dir, split them up node_and_pos = self.check_nodes_in_dir( x_pos, y_pos, direction_sums[direction]) adj_node = node_and_pos[0] distance = abs((x_pos - node_and_pos[1][0]) + (y_pos - node_and_pos[1][1])) # Set adjacent node in that direction with the distance node.set_adjacent_nodes(direction, (adj_node, distance)) else: node.set_adjacent_nodes(direction, None) replace_print( 'Number of connected nodes: {0:.0f} ({1:.2f} %)'.format( connected_nodes, connected_nodes / total_nodes * 100)) print '\n'