|
2 | 2 | import numpy as np
|
3 | 3 | import heapq
|
4 | 4 |
|
5 |
| -#no of rows and columns in grid |
6 |
| -rows=10 |
7 |
| -columns=10 |
| 5 | +# no of rows and columns in grid |
| 6 | +rows = 10 |
| 7 | +columns = 10 |
| 8 | + |
8 | 9 |
|
9 | 10 | def grid_map(img):
|
10 |
| - #create a 2d array |
11 |
| - grid=np.zeros((rows,columns)) |
| 11 | + # create a 2d array |
| 12 | + grid = np.zeros((rows, columns)) |
12 | 13 | for i in range(rows):
|
13 | 14 | for j in range(columns):
|
14 |
| - #white blocks |
15 |
| - if (np.array_equal(img[20+(i*40),20+(j*40)],[255,255,255])): |
| 15 | + # white blocks |
| 16 | + if (np.array_equal(img[20+(i*40), 20+(j*40)], [255, 255, 255])): |
16 | 17 | grid[i][j] = 0
|
17 |
| - #start -> orange block |
18 |
| - elif (np.array_equal(img[20+(i*40),20+(j*40)],[39,127,255])): |
| 18 | + # start -> orange block |
| 19 | + elif (np.array_equal(img[20+(i*40), 20+(j*40)], [39, 127, 255])): |
19 | 20 | grid[i][j] = 2
|
20 |
| - #end -> pink block |
21 |
| - elif (np.array_equal(img[20+(i*40),20+(j*40)],[201,174,255])): |
| 21 | + # end -> pink block |
| 22 | + elif (np.array_equal(img[20+(i*40), 20+(j*40)], [201, 174, 255])): |
22 | 23 | grid[i][j] = 3
|
23 |
| - #obstacles ->black blocks |
| 24 | + # obstacles ->black blocks |
24 | 25 | else:
|
25 | 26 | grid[i][j] = 1
|
26 | 27 | return grid
|
27 | 28 |
|
| 29 | + |
28 | 30 | class Cell(object):
|
29 |
| - def __init__(self,x,y,reachable): |
30 |
| - #setting some parameters for each cell |
31 |
| - self.reachable=reachable |
32 |
| - self.x=x |
33 |
| - self.y=y |
34 |
| - self.parent= None |
35 |
| - self.cost=0 |
36 |
| - self.heuristic=0 |
37 |
| - #net_cost=cost+heuristic |
38 |
| - self.net_cost=0 |
| 31 | + def __init__(self, x, y, reachable): |
| 32 | + # setting some parameters for each cell |
| 33 | + self.reachable = reachable |
| 34 | + self.x = x |
| 35 | + self.y = y |
| 36 | + self.parent = None |
| 37 | + self.cost = 0 |
| 38 | + self.heuristic = 0 |
| 39 | + # net_cost=cost+heuristic |
| 40 | + self.net_cost = 0 |
| 41 | + |
39 | 42 |
|
40 | 43 | class Astar(object):
|
41 | 44 | def __init__(self):
|
42 |
| - #list of unchecked neighbour cells |
| 45 | + # list of unchecked neighbour cells |
43 | 46 | self.open = []
|
44 |
| - #keeps cells with lowest total_cost at top |
| 47 | + # keeps cells with lowest total_cost at top |
45 | 48 | heapq.heapify(self.open)
|
46 |
| - #list of already checked cells |
47 |
| - self.closed= set() |
48 |
| - #list of neighbour cells |
49 |
| - self.cells=[] |
50 |
| - |
51 |
| - def init_grid(self,grid): |
| 49 | + # list of already checked cells |
| 50 | + self.closed = set() |
| 51 | + # list of neighbour cells |
| 52 | + self.cells = [] |
| 53 | + |
| 54 | + def init_grid(self, grid): |
52 | 55 | for i in range(rows):
|
53 | 56 | for j in range(columns):
|
54 |
| - #detecting the obstacles |
55 |
| - if grid[i][j]==1: |
56 |
| - reachable= False |
| 57 | + # detecting the obstacles |
| 58 | + if grid[i][j] == 1: |
| 59 | + reachable = False |
57 | 60 | else:
|
58 |
| - reachable= True |
59 |
| - self.cells.append(Cell(i,j,reachable)) |
60 |
| - #detecting the start and end |
61 |
| - if(grid[i][j]==2): |
62 |
| - self.start=self.cell(i,j) |
63 |
| - if(grid[i][j]==3): |
64 |
| - self.end=self.cell(i,j) |
65 |
| - |
66 |
| - def cell(self,x,y): |
67 |
| - #returns the location to identify each cell |
| 61 | + reachable = True |
| 62 | + self.cells.append(Cell(i, j, reachable)) |
| 63 | + # detecting the start and end |
| 64 | + if(grid[i][j] == 2): |
| 65 | + self.start = self.cell(i, j) |
| 66 | + if(grid[i][j] == 3): |
| 67 | + self.end = self.cell(i, j) |
| 68 | + |
| 69 | + def cell(self, x, y): |
| 70 | + # returns the location to identify each cell |
68 | 71 | return self.cells[x*columns+y]
|
69 | 72 |
|
70 |
| - def cell_heuristic(self,cell): |
71 |
| - #returns the heuristic for astar algo |
| 73 | + def cell_heuristic(self, cell): |
| 74 | + # returns the heuristic for astar algo |
72 | 75 | return abs(cell.x-self.end.x)+abs(cell.y-self.end.y)
|
73 | 76 |
|
74 |
| - def neighbour(self,cell): |
75 |
| - cells=[] |
76 |
| - #returns a list of neigbours of a cell |
77 |
| - if cell.x<columns-1: |
78 |
| - cells.append(self.cell(cell.x+1,cell.y)) |
79 |
| - if cell.x>0: |
80 |
| - cells.append(self.cell(cell.x-1,cell.y)) |
81 |
| - if cell.y<rows-1: |
82 |
| - cells.append(self.cell(cell.x,cell.y+1)) |
83 |
| - if cell.y>0: |
84 |
| - cells.append(self.cell(cell.x,cell.y-1)) |
| 77 | + def neighbour(self, cell): |
| 78 | + cells = [] |
| 79 | + # returns a list of neigbours of a cell |
| 80 | + if cell.x < columns - 1: |
| 81 | + cells.append(self.cell(cell.x+1, cell.y)) |
| 82 | + if cell.x > 0: |
| 83 | + cells.append(self.cell(cell.x-1, cell.y)) |
| 84 | + if cell.y < rows-1: |
| 85 | + cells.append(self.cell(cell.x, cell.y+1)) |
| 86 | + if cell.y > 0: |
| 87 | + cells.append(self.cell(cell.x, cell.y-1)) |
85 | 88 | return cells
|
86 | 89 |
|
87 |
| - def update_cell(self,adj,cell): |
88 |
| - #update the details about the selected neigbour cell |
89 |
| - adj.cost=cell.cost+1 |
90 |
| - adj.heuristic=self.cell_heuristic(adj) |
91 |
| - adj.parent=cell |
92 |
| - adj.net_cost= adj.cost + adj.heuristic |
| 90 | + def update_cell(self, adj, cell): |
| 91 | + # update the details about the selected neigbour cell |
| 92 | + adj.cost = cell.cost + 1 |
| 93 | + adj.heuristic = self.cell_heuristic(adj) |
| 94 | + adj.parent = cell |
| 95 | + adj.net_cost = adj.cost + adj.heuristic |
93 | 96 |
|
94 |
| - |
95 | 97 | def display_path(self):
|
96 |
| - #list for storing the path |
97 |
| - route_path =[] |
98 |
| - #flag to determine length of path |
99 |
| - count=0 |
100 |
| - cell=self.end |
| 98 | + # list for storing the path |
| 99 | + route_path = [] |
| 100 | + # flag to determine length of path |
| 101 | + count = 0 |
| 102 | + cell = self.end |
101 | 103 | while cell.parent is not None:
|
102 |
| - #storing the parents in list from end to start |
103 |
| - route_path.append([(cell.y)+1,(cell.x)+1]) |
104 |
| - cell=cell.parent |
105 |
| - count+=1 |
106 |
| - return route_path,count |
| 104 | + # storing the parents in list from end to start |
| 105 | + route_path.append([(cell.y)+1, (cell.x)+1]) |
| 106 | + cell = cell.parent |
| 107 | + count += 1 |
| 108 | + return route_path, count |
107 | 109 |
|
108 | 110 | def search(self):
|
109 |
| - #pushing the first element in open queue |
110 |
| - heapq.heappush(self.open,(self.start.net_cost,self.start)) |
| 111 | + # pushing the first element in open queue |
| 112 | + heapq.heappush(self.open, (self.start.net_cost, self.start)) |
111 | 113 | while(len(self.open)):
|
112 |
| - net_cost,cell=heapq.heappop(self.open) |
113 |
| - #adding the checked cell to closed list |
| 114 | + net_cost, cell = heapq.heappop(self.open) |
| 115 | + # adding the checked cell to closed list |
114 | 116 | self.closed.add(cell)
|
115 | 117 | if cell is self.end:
|
116 |
| - #store path and path legth |
| 118 | + # store path and path legth |
117 | 119 | route_path, route_length = self.display_path()
|
118 | 120 | route_path.reverse()
|
119 | 121 | break
|
120 |
| - #getting the adjoint cells |
121 |
| - neighbours=self.neighbour(cell) |
| 122 | + # getting the adjoint cells |
| 123 | + neighbours = self.neighbour(cell) |
122 | 124 | for path in neighbours:
|
123 |
| - if path.reachable and path not in self.closed: #if cell is not an obstacle and has not been already checked |
124 |
| - if (path.net_cost,path) in self.open: |
125 |
| - if path.cost > cell.cost + 1: #selecting the cell with least cost |
126 |
| - self.update_cell(path,cell) |
| 125 | + # if cell is not an obstacle and has not been already checked |
| 126 | + if path.reachable and path not in self.closed: |
| 127 | + if (path.net_cost, path) in self.open: |
| 128 | + # selecting the cell with least cost |
| 129 | + if path.cost > cell.cost + 1: |
| 130 | + self.update_cell(path, cell) |
127 | 131 | else:
|
128 |
| - self.update_cell(path,cell) |
129 |
| - heapq.heappush(self.open,(path.net_cost,path)) |
| 132 | + self.update_cell(path, cell) |
| 133 | + heapq.heappush(self.open, (path.net_cost, path)) |
130 | 134 | return route_path, route_length
|
131 | 135 |
|
132 | 136 |
|
133 | 137 | def play(img):
|
134 |
| - #map the grid in an array |
| 138 | + # map the grid in an array |
135 | 139 | grid = grid_map(img)
|
136 | 140 |
|
137 |
| - #executing A* |
138 |
| - solution=Astar() |
| 141 | + # executing A* |
| 142 | + solution = Astar() |
139 | 143 | solution.init_grid(grid)
|
140 |
| - route_path,route_length = solution.search() |
141 |
| - |
| 144 | + route_path, route_length = solution.search() |
| 145 | + |
142 | 146 | return route_length, route_path
|
143 | 147 |
|
144 | 148 |
|
145 | 149 | if __name__ == "__main__":
|
146 |
| - #code for checking output for single image |
147 |
| - img = cv2.imread('test_images/test_image1.png') |
| 150 | + # code for checking output for single image |
| 151 | + img = cv2.imread('SampleImages/test_image1.png') |
148 | 152 | route_length, route_path = play(img)
|
149 | 153 | print "OUTPUT FOR SINGLE IMAGE (IMAGE 1)..."
|
150 | 154 | print "route length = ", route_length
|
151 | 155 | print "route path = ", route_path
|
152 |
| - #code for checking output for all images |
| 156 | + # code for checking output for all images |
153 | 157 | route_length_list = []
|
154 |
| - route_path_list = [] |
155 |
| - for file_number in range(1,6): |
156 |
| - file_name = "test_images/test_image"+str(file_number)+".png" |
| 158 | + route_path_list = [] |
| 159 | + for file_number in range(1, 6): |
| 160 | + file_name = "SampleImages/test_image"+str(file_number)+".png" |
157 | 161 | pic = cv2.imread(file_name)
|
158 | 162 | route_length, route_path = play(pic)
|
159 | 163 | route_length_list.append(route_length)
|
160 | 164 | route_path_list.append(route_path)
|
161 |
| - print "OUTPUT FOR ALL IMAGES..." |
162 |
| - for i in range(5): |
163 |
| - print "route length for image ",i+1," = " ,route_length_list[i] |
164 |
| - print "route path for image ",i+1," = ", route_path_list[i] |
| 165 | + print "OUTPUT FOR ALL IMAGES..." |
| 166 | + for i in range(5): |
| 167 | + print "route length for image ", i+1, " = ", route_length_list[i] |
| 168 | + print "route path for image ", i+1, " = ", route_path_list[i] |
0 commit comments