-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS.py
157 lines (132 loc) · 4.74 KB
/
DFS.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
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import time
# 0 = Empty
# 1 = Wall
# 2 = Start
# 3 = End
# 4 = Queued
# 5 = Visited
# 6 = Found Path
def DFS(maze, s):
startTime = time.time()
queue = [s] # Queue the start
maze[s[0],s[1]] = 4
depthCounter = 0 # number of nodes checked
modifiedNodes = [] # Used in plotting
modifiedNodeStatus = []
pathEdges = {}
while queue != []:
depthCounter += 1
currNode = queue.pop() # FIFO queue
# Check surrounding 8 (or 4) nodes
# for i in [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]:
for i in [[0,-1],[-1,0],[1,0],[0,1]]:
adjNode = [currNode[0]+i[0],currNode[1]+i[1]]
# end node: quit
if maze[adjNode[0],adjNode[1]] == 3:
queue = []
pathEdges[tuple([adjNode[0],adjNode[1]])] = tuple([currNode[0],currNode[1]])
break
# open node: add to queue
elif maze[adjNode[0],adjNode[1]] == 0:
maze[adjNode[0],adjNode[1]] = 4
queue.append(adjNode)
pathEdges[tuple([adjNode[0],adjNode[1]])] = tuple([currNode[0],currNode[1]])
modifiedNodes.append([adjNode[0],adjNode[1]]) # Plotting stuff
modifiedNodeStatus.append(4)
maze[currNode[0],currNode[1]] = 5 # mark current as Visitied
modifiedNodes.append([currNode[0],currNode[1]]) # Plotting stuff
modifiedNodeStatus.append(5)
if depthCounter%100 == 0:
Plot_Search_Path(modifiedNodes, modifiedNodeStatus)
#Draw_Maze_Innit(maze)
modifiedNodes = []
modifiedNodeStatus = []
shortestPath = [tuple([adjNode[0],adjNode[1]])]
while True:
if shortestPath[-1] in pathEdges:
shortestPath.append(pathEdges[shortestPath[-1]])
else:
break
calcTime = time.time()-startTime
print('Search: {} seconds'.format(calcTime))
print('Nodes Visited: {}'.format(depthCounter))
Plot_Search_Path(modifiedNodes, modifiedNodeStatus)
#Draw_Maze_Innit(maze)
#for point in pathEdges:
# ax.plot(point[1],height-point[0],'c.')
# plt.pause(1e-10)
#drawTime = time.time()-calcTime-startTime
#print('Draw: {} seconds'.format(drawTime))
return shortestPath
def Draw_Maze_Innit(mazelist):
# Loop over all points in maze
ax.cla()
nodeWallx = []
nodeWally = []
rowCounter = 0
entryCounter = 0
for row in mazelist:
for entry in row:
# Plotting maze outline and POI
if entry == 1: # Obstacle
nodeWallx.append(entryCounter)
nodeWally.append(height-rowCounter)
#ax.plot(entryCounter, height-rowCounter, 'ks')
elif entry == 2: # Start
ax.plot(entryCounter, height-rowCounter, 'bo')
elif entry == 3: # End
ax.plot(entryCounter, height-rowCounter, 'go')
elif entry == 4: # Queued
ax.plot(entryCounter, height-rowCounter, 'm.')
elif entry == 5: # Visited
ax.plot(entryCounter, height-rowCounter, 'c.')
entryCounter += 1
rowCounter += 1
entryCounter = 0
plt.scatter(nodeWallx,nodeWally,c='k',marker=',')
ax.axis('equal')
plt.pause(1e-10)
return
def Plot_Search_Path(points, stati):
nodeQx = []
nodeQy = []
nodeVx = []
nodeVy = []
for nodeNum in range(len(points)):
if stati[nodeNum] == 4: # Queued
nodeQx.append(points[nodeNum][1])
nodeQy.append(height-points[nodeNum][0])
elif stati[nodeNum] == 5: # Visited
nodeVx.append(points[nodeNum][1])
nodeVy.append(height-points[nodeNum][0])
plt.scatter(nodeQx,nodeQy,c='m',marker='.')
plt.scatter(nodeVx,nodeVy,c='c',marker='.')
plt.pause(1e-10)
return
def PlotPath(path):
xcoords = []
ycoords = []
for node in path:
ycoords.append(height-node[0])
xcoords.append(node[1])
plt.plot(xcoords,ycoords,'r-')
return
if __name__ == '__main__':
mazeList = pd.read_csv("Homework 1\Map3.csv", header=None).to_numpy()
height, width = mazeList.shape
start = np.where(mazeList==2)
startLoc = np.array([start[0][0],start[1][0]])
# Start interactive plot
plt.ion()
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)
Draw_Maze_Innit(mazeList)
time.sleep(1)
shortestPath = DFS(mazeList, startLoc)
PlotPath(shortestPath)
# Keeping the updating plot open
while True:
plt.pause(10)