forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
A star algorithm.py
139 lines (92 loc) · 3.3 KB
/
A star algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Node:
def __init__(self, position: (), parent: ()):
self.position = position
self.parent = parent
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position
def __lt__(self, other):
return self.f < other.f
def __repr__(self):
return ('({0},{1})'.format(self.position, self.f))
def draw_grid(map, width, height, spacing=2, **kwargs):
for y in range(height):
for x in range(width):
print('%%-%ds' % spacing % draw_tile(map, (x, y), kwargs), end='')
print()
def draw_tile(map, position, kwargs):
value = map.get(position)
if 'path' in kwargs and position in kwargs['path']: value = '+'
if 'start' in kwargs and position == kwargs['start']: value = '@'
if 'goal' in kwargs and position == kwargs['goal']: value = '$'
return value
def astar_search(map, start, end):
open = []
closed = []
start_node = Node(start, None)
goal_node = Node(end, None)
open.append(start_node)
while len(open) > 0:
open.sort()
current_node = open.pop(0)
closed.append(current_node)
if current_node == goal_node:
path = []
while current_node != start_node:
path.append(current_node.position)
current_node = current_node.parent
return path[::-1]
(x, y) = current_node.position
neighbors = [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
for next in neighbors:
map_value = map.get(next)
if (map_value == '#'):
continue
neighbor = Node(next, current_node)
if (neighbor in closed):
continue
neighbor.g = abs(neighbor.position[0] - start_node.position[0]) + abs(
neighbor.position[1] - start_node.position[1])
neighbor.h = abs(neighbor.position[0] - goal_node.position[0]) + abs(
neighbor.position[1] - goal_node.position[1])
neighbor.f = neighbor.g + neighbor.h
if (add_to_open(open, neighbor) == True):
open.append(neighbor)
return None
def add_to_open(open, neighbor):
for node in open:
if (neighbor == node and neighbor.f >= node.f):
return False
return True
def main():
map = {}
chars = ['c']
start = None
end = None
width = 0
height = 0
fp = open('data\\maze.in', 'r')
while len(chars) > 0:
chars = [str(i) for i in fp.readline().strip()]
width = len(chars) if width == 0 else width
for x in range(len(chars)):
map[(x, height)] = chars[x]
if (chars[x] == '@'):
start = (x, height)
elif (chars[x] == '$'):
end = (x, height)
if (len(chars) > 0):
height += 1
fp.close()
path = astar_search(map, start, end)
print()
print(path)
print()
draw_grid(map, width, height, spacing=1, path=path, start=start, goal=end)
print()
print('Steps to goal: {0}'.format(len(path)))
print()
if __name__ == "__main__":
main()