-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijsktra.py
146 lines (120 loc) · 3.63 KB
/
dijsktra.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
import pygame
import sys
import random
import math
import priority_dict
from queue import PriorityQueue
from init import priority_dict
pygame.display.set_caption("Dijstra Path Planning Algorithm")
width=600
height=600
rows=30
column=30
grid=[]
obs=[]
queue=[]
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.neighbour=[]
# self.dist=dist
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])
def hv(self):
gx=25
gy=28
self.dist=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[15][28] #intialising end point
goal_box.goal = True
queue.append(start_box)
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
while True:
searching=True
begin_search=True
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if begin_search:
if len(queue)>0 and searching:
print(3)
current=queue.pop(0)
current.visited=True
if (current==goal_box):
begin_search=False
searching=False
while current.prior != start_box:
path.append(current.prior)
current = current.prior
else:
for nb in current.neighbour:
print(1)
if not nb.queued and not nb.wall:
nb.queued=True
nb.prior=current
queue.append(nb)
if (nb==goal_box):
begin_search=False
searching=False
# else:
# print("Goal not found")
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,(80,80,80))
pygame.display.flip()