-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_planning_implementation.py
180 lines (148 loc) · 3.47 KB
/
path_planning_implementation.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
176
177
178
179
180
# %%
import numpy as np
from graphics import *
import time
# %%
R, C = 5, 7
m = [['S', '.', '.', '#', '.', '.', '.'],
['.', '#', '.', '.', '.', '#', '.'],
['.', '#', '.', '#', '.', '.', '#'],
['.', '.', '#', '.', '#', '.', '.'],
['#', '.', '#', 'E', '.', '.', '#']]
# %%
class queue:
def __init__(self):
self.queue = []
def enqueue(self, data):
self.queue.append(data)
def dequeue(self):
return self.queue.pop(0)
def size(self):
return len(self.queue)
# %%
sr, sc = 0, 0
s = (sr, sc)
e = (0,0)
rq, cq = queue(), queue()
# %%
move_count = 0
nodes_left_in_layer = 1
nodes_in_next_layer = 0
# %%
reach_end = False
# %%
visited = np.zeros((5, 7))
# prev = np.empty((5, 7))
# prev.fill(0)
prev = [[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]
# %%
dr = [-1, +1, 0, 0]
dc = [0, 0, +1, -1]
#%%
def rectangle(rr,cc):
rr *=100
cc *=100
rect = Rectangle(Point(cc,rr), Point(100+cc, 100+rr))
rect.draw(win)
def circle(rr,cc):
rr *=100
cc *=100
c = Circle(Point(cc+50,rr+50), 25)
c.draw(win)
def text(rr,cc,text_input):
rr *=100
cc *=100
text = Text(Point(cc+50,rr+50),text_input)
text.setSize(30)
text.draw(win)
class agent:
def __init__(self,cr,cc):
self.cr=cr*100
self.cc=cc*100
self.text = Text(Point(self.cc+50,self.cr+50),'X')
self.text.draw(win)
def movement(self,mr,mc):
mr *=100
mc *=100
self.text.move((mc-self.cc),(mr-self.cr))
self.cc= mc
self.cr= mr
# %%
def draw_grid(R,C):
for i in range(R):
for j in range(C):
rectangle(i,j)
#%%
def explore_neighbours(r, c):
global nodes_in_next_layer, prev
for i in range(0, 4):
rr = r + dr[i]
cc = c + dc[i]
if rr < 0 or cc < 0:
continue
if rr >= R or cc >= C:
continue
if visited[rr][cc]:
continue
if m[rr][cc] == '#':
circle(rr,cc)
continue
rq.enqueue(rr)
cq.enqueue(cc)
visited[rr][cc] = True
prev[rr][cc] = (r, c)
nodes_in_next_layer += 1
# %%
def solve():
global reach_end, e,move_count, nodes_left_in_layer, nodes_in_next_layer
rq.enqueue(sr)
cq.enqueue(sc)
visited[sr][sc] = True
while rq.size() > 0:
r = rq.dequeue()
c = cq.dequeue()
if m[r][c] == 'E':
e = (r, c)
text(r,c,"E")
reach_end = True
break
explore_neighbours(r, c)
nodes_left_in_layer -= 1
if nodes_left_in_layer == 0:
nodes_left_in_layer = nodes_in_next_layer
nodes_in_next_layer = 0
move_count += 1
if reach_end:
return move_count
return -1
# %%
def reconstructPath(prev):
global e
path = []
at = e
while(at != 0.0):
path.append(at)
at = prev[at[0]][at[1]]
path.reverse()
if path[0] == s:
return path
return []
# %%
win = GraphWin("My Grid", 1000, 600)
draw_grid(R,C)
text(sc,sr,"S")
print(solve())
agent_0 = agent(sc,sr)
path_0 = reconstructPath(prev)
for i in path_0:
agent_0.movement(i[0],i[1])
time.sleep(1)
line = Line(Point(agent_0.cc+100,agent_0.cr+100),Point((i[1])*100,(i[0])*100))
line.draw(win)
time.sleep(1)
win.getMouse() # Pause to view result
win.close()