-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAstar.py
175 lines (147 loc) · 4.78 KB
/
Astar.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import pygame
import sys
# from tkinter import messagebox, Tk
import random
import math
# import priority_dict
# from queue import PriorityQueue
# from init import priority_dict
import heapdict
pygame.display.set_caption("A* Path Planning Algorithm")
width=600
height=600
rows=30
column=30
grid=[]
obs=[]
# queue=[]
queue = heapdict.heapdict()
path=[]
box_width=width//rows
box_height=height//column
goal_found=False
window = pygame.display.set_mode((width,height))
background_colour = (255,255,255)
window.fill(background_colour)
class Box:
def __init__(self,i,j):
self.x=i
self.y=j
self.start=False
self.wall=False
self.goal=False
self.queued=False
self.visited=False
self.diagonal=False
self.neighbour=[]
self.dist=1
self.cost={}
def draw(self,window,color):
pygame.draw.rect(window,color,(self.x*box_width, self.y*box_height,box_width -2,box_height-2))
def set_neighbours(self):
if i>0:
self.neighbour.append(grid[i-1][j])
if i<rows-1:
self.neighbour.append(grid[i+1][j])
if j>0:
self.neighbour.append(grid[i][j-1])
if j<column-1:
self.neighbour.append(grid[i][j+1])
if self.x+1<=column-1 and self.y+1<=rows-1:
grid[self.x+1][self.y+1].diagonal=True
self.neighbour.append(grid[self.x+1][self.y+1])
if self.x+1<=column-1 and self.y-1>0:
grid[self.x+1][self.y-1].diagonal=True
self.neighbour.append(grid[self.x+1][self.y-1])
if self.x-1>0 and self.y+1<=rows-1:
grid[self.x-1][self.y+1].diagonal=True
self.neighbour.append(grid[self.x-1][self.y+1])
if self.x-1>0 and self.y-1>0:
grid[self.x-1][self.y-1].diagonal=True
self.neighbour.append(grid[self.x-1][self.y-1])
def hv(self):
gx=25
gy=16
if not self.diagonal:
self.dist=math.dist([self.x,self.y],[gx,gy])
else:
self.dist=0.5+math.dist([self.x,self.y],[gx,gy])
#create grid
for i in range(column):
arr=[]
for j in range(rows):
# grid[i][j].neighbours()
arr.append(Box(i,j))
grid.append(arr)
for i in range(0,rows):
for j in range(0,column):
grid[i][j].set_neighbours()
start_box=grid[0][0] #intialising start point
start_box.start = True
goal_box=grid[25][16] #intialising end point
goal_box.goal = True
queue[start_box]=0
for i in range (0,100): #generating obstacles using random function
m=random.randint(2,26)
n=random.randint(2,26)
block=grid[m][n]
# obs.append(m,n)
block.wall=True
# main
flag=0
while True:
searching=True
begin_search=True
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for i in range(0,rows):
for j in range(0,column):
grid[i][j].hv()
if begin_search:
print()
if len(queue.items())>0 and searching:
print("1")
(current, dist) = queue.peekitem()
queue.popitem()
current.visited=True
if (current==goal_box):
searching=False
begin_search=False
print("2")
while current.prior != start_box:
path.append(current.prior)
current = current.prior
else:
for nb in current.neighbour:
if begin_search:
print("3")
if not nb.queued and not nb.wall and not flag:
nb.queued=True
nb.prior=current
queue[nb]= nb.dist
print("4")
if (nb==goal_box):
begin_search=False
print("5")
flag=1
window.fill((0,0,0))
for i in range (column):
for j in range(rows):
goal_box.wall=False
box=grid[i][j]
box.draw(window,(50,50,50))
if box.queued:
box.draw(window, (200, 0, 0))
if box.visited:
box.draw(window, (0, 200, 0))
if box in path:
box.draw(window, (0, 0, 200))
if box.start:
box.draw(window,(0,200,200))
if box.goal:
box.draw(window,(200,200,0))
if box.wall:
box.draw(window,(70,80,80))
pygame.display.flip()