|
| 1 | +#Sample node class for a graph |
| 2 | +class Node(): |
| 3 | + |
| 4 | + def __init__(self, parent=None, position=None): |
| 5 | + self.parent = parent |
| 6 | + self.position = position |
| 7 | + |
| 8 | + self.g = 0 |
| 9 | + self.h = 0 |
| 10 | + self.f = 0 |
| 11 | + |
| 12 | + def __eq__(self, other): |
| 13 | + return self.position == other.position |
| 14 | + |
| 15 | + |
| 16 | +def astar(maze, start, end): |
| 17 | + #Returns a list of tuples as a path from the given start to the given end in the given maze |
| 18 | + |
| 19 | + #Create start and end node |
| 20 | + start_node = Node(None, start) |
| 21 | + start_node.g = start_node.h = start_node.f = 0 |
| 22 | + end_node = Node(None, end) |
| 23 | + end_node.g = end_node.h = end_node.f = 0 |
| 24 | + |
| 25 | + #Initialize both open and closed list |
| 26 | + open_list = [] |
| 27 | + closed_list = [] |
| 28 | + |
| 29 | + #Add the start node |
| 30 | + open_list.append(start_node) |
| 31 | + |
| 32 | + #Loop until you find the end |
| 33 | + while len(open_list) > 0: |
| 34 | + |
| 35 | + #Get the current node |
| 36 | + current_node = open_list[0] |
| 37 | + current_index = 0 |
| 38 | + for index, item in enumerate(open_list): |
| 39 | + if item.f < current_node.f: |
| 40 | + current_node = item |
| 41 | + current_index = index |
| 42 | + |
| 43 | + # Pop current off open list, add to closed list |
| 44 | + open_list.pop(current_index) |
| 45 | + closed_list.append(current_node) |
| 46 | + |
| 47 | + #Found the goal |
| 48 | + if current_node == end_node: |
| 49 | + path = [] |
| 50 | + current = current_node |
| 51 | + while current is not None: |
| 52 | + path.append(current.position) |
| 53 | + current = current.parent |
| 54 | + return path[::-1] # Return reversed path |
| 55 | + |
| 56 | + #Generate children |
| 57 | + children = [] |
| 58 | + for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]: # Adjacent squares |
| 59 | + |
| 60 | + # Get node position |
| 61 | + node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1]) |
| 62 | + |
| 63 | + #Make sure within range |
| 64 | + if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze)-1]) -1) or node_position[1] < 0: |
| 65 | + continue |
| 66 | + |
| 67 | + #Make sure walkable terrain |
| 68 | + if maze[node_position[0]][node_position[1]] != 0: |
| 69 | + continue |
| 70 | + |
| 71 | + #Create new node |
| 72 | + new_node = Node(current_node, node_position) |
| 73 | + |
| 74 | + #Append |
| 75 | + children.append(new_node) |
| 76 | + |
| 77 | + #Loop through children |
| 78 | + for child in children: |
| 79 | + |
| 80 | + # Child is on the closed list |
| 81 | + for closed_child in closed_list: |
| 82 | + if child == closed_child: |
| 83 | + continue |
| 84 | + |
| 85 | + #Create the f, g, and h values |
| 86 | + child.g = current_node.g + 1 |
| 87 | + child.h = ((child.position[0] - end_node.position[0]) ** 2) + ((child.position[1] - end_node.position[1]) ** 2) |
| 88 | + child.f = child.g + child.h |
| 89 | + |
| 90 | + #Child is already in the open list |
| 91 | + for open_node in open_list: |
| 92 | + if child == open_node and child.g > open_node.g: |
| 93 | + continue |
| 94 | + |
| 95 | + #Add the child to the open list |
| 96 | + open_list.append(child) |
| 97 | + |
| 98 | + |
| 99 | +def main(): |
| 100 | + #Sample matrix |
| 101 | + maze = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 102 | + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 103 | + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 104 | + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 105 | + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 106 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 107 | + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 108 | + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 109 | + [0, 0, 0, 0, 1, 0, 0, 0, 0, 0], |
| 110 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] |
| 111 | + |
| 112 | + start = (0, 0) |
| 113 | + end = (7, 6) |
| 114 | + |
| 115 | + path = astar(maze, start, end) |
| 116 | + print(path) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + main() |
0 commit comments