def get_neighbours_nodes(this_node, a=1): # [i,j] x, y = this_node.coordinates[0], this_node.coordinates[1] operations = range(-a, (a + 1)) allX, allY = [], [] for values_individual in operations: if -1 < (x + int(values_individual)) < 395: allX.append(x + int(values_individual)) if -1 < (y + int(values_individual)) < 500: allY.append(y + int(values_individual)) result = [] for xx in allX: for yy in allY: newNode = Node([xx, yy]) newNode.H = heuristic([xx, yy]) newNode.G = path_cost(this_node.coordinates, [xx, yy]) if not newNode.coordinates == this_node.coordinates: result.append(((newNode.H + newNode.G), newNode)) return result
def do_a_star_search(): start_node = Node(start) start_node.G = 0 start_node.H = heuristic(start_node.coordinates) open_set = [((start_node.G + start_node.H), start_node) ] # has (fCost, Node) end_node = Node(end) end_node.H = 0 closed_set = set() # has (x,y) while len(open_set) != 0: current_tup = get_lowest(open_set) open_set.remove(current_tup) current_node = current_tup[1] if current_node.coordinates == end_node.coordinates: distance = get_path(current_node) return distance temp_tuple = (current_node.coordinates[0], current_node.coordinates[1]) closed_set.add(temp_tuple) neighbours = get_neighbours_nodes(current_node) for n in neighbours: if current_tup[1].coordinates == n[1].coordinates: neighbours.remove(n) continue cost = current_node.G + n[1].G if set_compare(n, open_set) and (cost < n[1].G): open_set = delete_node(n, open_set) if ((n[1].coordinates[0], n[1].coordinates[1]) in closed_set) and (cost < n[1].G): closed_set.remove((n[1].coordinates[0], n[1].coordinates[1])) if (not set_compare(n, open_set)) and ( (n[1].coordinates[0], n[1].coordinates[1]) not in closed_set): n[1].G = cost n[1].parent = current_node open_set.append(n)