-
Notifications
You must be signed in to change notification settings - Fork 0
/
astar_algo.py
117 lines (92 loc) · 3.51 KB
/
astar_algo.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
import numpy as np
def heuristic(a, b):
# return np.sqrt((a.i-b.i)**2 + (a.j-b.j)**2)
return abs(a.i-b.i) + abs(a.j-b.j)
# making a class spot which has f-score, g-score and h-score
class Spot:
# f-Score represents our current best guess as to how cheap a path could be from start to finish if it goes through n.
# g-score is the cost of the cheapest path from start to n currently known.
f = g = h = 0
# 'previous' is the node immediately preceding it on the cheapest path from start
previous = None
def __init__(self, i, j):
self.i = i
self.j = j
self.neighbours = []
def add_neighbours(self, grid): # adding neighbours
i, j = self.i, self.j
if (Spot.is_available(i, j+1)): # bottom
self.neighbours.append(grid[i, j+1])
if (Spot.is_available(i, j-1)): # top
self.neighbours.append(grid[i, j-1])
if (Spot.is_available(i+1, j)): # right
self.neighbours.append(grid[i+1, j])
if (Spot.is_available(i-1, j)): # left
self.neighbours.append(grid[i-1, j])
@staticmethod
def is_available(i, j):
return i >= 0 and i < rows and j >= 0 and j < cols and maze[i, j] == 1
class AStarAlgo:
@staticmethod
def initialize(m, s, e):
global maze, rows, cols, start, end, path_lines, grid
maze = m
path_lines = []
rows, cols = maze.shape
# initializing openset and closedset
grid = np.full(maze.shape, None)
# initializing grid of spots
for i in range(rows):
for j in range(cols):
grid[i, j] = Spot(i, j)
for i in range(rows):
for j in range(cols):
grid[i, j].add_neighbours(grid)
start = grid[s[0], s[1]]
end = grid[e[0], e[1]]
AStarAlgo.open_set = [start]
AStarAlgo.closed_set = []
@staticmethod
def find_path():
global path, start, end, current, grid
winner = 0
for i in range(len(AStarAlgo.open_set)):
if AStarAlgo.open_set[i].f < AStarAlgo.open_set[winner].f:
winner = i
current = AStarAlgo.open_set[winner]
if current == end:
path = []
temp = current
path.append((temp.i, temp.j))
while temp.previous:
path.insert(0, (temp.previous.i, temp.previous.j))
temp = temp.previous
return path
AStarAlgo.open_set.remove(current)
AStarAlgo.closed_set.append(current)
neighbours = current.neighbours
for neighbour in neighbours:
if neighbour in AStarAlgo.closed_set:
continue
# temp_g is the distance from start to the neighbor through current
temp_g = current.g + 1
if neighbour in AStarAlgo.open_set:
if temp_g < neighbour.g:
neighbour.g = temp_g
else:
neighbour.g = temp_g
AStarAlgo.open_set.append(neighbour)
neighbour.previous = current
neighbour.h = heuristic(neighbour, end)
neighbour.f = neighbour.g+neighbour.h
path = []
temp = current
path.append((temp.i, temp.j))
while temp.previous:
path.insert(0, (temp.previous.i, temp.previous.j))
temp = temp.previous
# print(len(path))
return path
@staticmethod
def finished():
return len(AStarAlgo.open_set) == 0 or current == end