-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
213 lines (151 loc) · 6.99 KB
/
grid.py
File metadata and controls
213 lines (151 loc) · 6.99 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import numpy as np
class Grid():
grid = distanceValue = totalDistance = gridWidth = gridHeight = obstaclePercent = startNodeX = startNodeY = endNodeX = endNodeY = amountOfObstacles = 0
listOfPathCords = []
emptyNode = np.array([255, 255, 255])
obstacleNode = np.array([0, 0, 0])
startNode = np.array([0, 100, 0])
endNode = np.array([214, 71, 49])
path = np.array([0, 167, 189])
def isNodeEmpty(arrayValues):
return (np.array_equal(arrayValues, Grid.emptyNode))
def isNodeObstacle(arrayValues):
return (np.array_equal(arrayValues, Grid.obstacleNode))
def isNodeStart(arrayValues):
return (np.array_equal(arrayValues, Grid.startNode))
def isNodeEnd(arrayValues):
return (np.array_equal(arrayValues, Grid.endNode))
def isNodePath(arrayValues):
return (np.array_equal(arrayValues, Grid.path))
def randomGridValueX():
return np.random.randint(0, Grid.gridWidth)
def randomGridValueY():
return np.random.randint(0, Grid.gridHeight)
def calculateAmountOfObstacles():
Grid.amountOfObstacles = round((Grid.gridHeight * Grid.gridWidth) * Grid.obstaclePercent)
def calculateObstacles():
while (Grid.amountOfObstacles != 0):
obsX = Grid.randomGridValueX()
obsY = Grid.randomGridValueY()
if (Grid.isNodeEmpty(Grid.grid[obsX, obsY])):
Grid.grid[obsX, obsY] = Grid.obstacleNode
Grid.amountOfObstacles -= 1
def calculateStartNode():
Grid.startNodeX = Grid.randomGridValueX()
Grid.startNodeY = Grid.randomGridValueY()
Grid.grid[Grid.startNodeX, Grid.startNodeY] = Grid.startNode
def calculateEndNode():
Grid.endNodeY = Grid.randomGridValueY()
while True:
Grid.endNodeX = Grid.randomGridValueX()
if (Grid.startNodeX != Grid.endNodeX): break
Grid.grid[Grid.endNodeX, Grid.endNodeY] = Grid.endNode
def gridComputation(gridWidth, gridHeight, obstaclePercent):
Grid.grid = np.random.randint(255,256, (gridWidth, gridHeight, 3), dtype=np.uint8)
Grid.gridWidth = gridWidth
Grid.gridHeight = gridHeight
Grid.obstaclePercent = obstaclePercent
Grid.calculateStartNode()
Grid.calculateEndNode()
Grid.calculateAmountOfObstacles()
Grid.calculateObstacles()
return Grid.grid
class GridSlowMode():
def printPathStep():
if not(len(Grid.listOfPathCords) == 0):
x = Grid.listOfPathCords.pop()
Grid.grid[x[0], x[1]] = np.array([0, 167, 189])
Grid.distanceValue += 1
class GridShortestPath():
def findShortestDistanceValue(nodeX, nodeY):
upNodeValue = GridShortestPath.searchNode(nodeX, nodeY - 1)
rightNodeValue = GridShortestPath.searchNode(nodeX + 1, nodeY)
downNodeValue = GridShortestPath.searchNode(nodeX, nodeY + 1)
leftNodeValue = GridShortestPath.searchNode(nodeX - 1, nodeY)
x = [upNodeValue, rightNodeValue, downNodeValue, leftNodeValue]
while 0 in x: x.remove(0)
if (len(x) == 0):
return 0
return min(x)
def findShortestPath(slowMode):
GridSearch.findShortestPath()
node = GridSearch.searchGrid[Grid.endNodeX, Grid.endNodeY]
nodeX = Grid.endNodeX
nodeY = Grid.endNodeY
Grid.distanceValue = GridShortestPath.findShortestDistanceValue(nodeX, nodeY)
counter = Grid.gridWidth * Grid.gridHeight
i = 0
while (i < counter):
upNodeValue = GridShortestPath.searchNode(nodeX, nodeY - 1)
rightNodeValue = GridShortestPath.searchNode(nodeX + 1, nodeY)
downNodeValue = GridShortestPath.searchNode(nodeX, nodeY + 1)
leftNodeValue = GridShortestPath.searchNode(nodeX - 1, nodeY)
x = [upNodeValue, rightNodeValue, downNodeValue, leftNodeValue]
while 0 in x: x.remove(0)
if (len(x) == 0):
break
x = min(x)
if ((upNodeValue == 100) or (rightNodeValue == 100) or (downNodeValue == 100) or (leftNodeValue == 100)): break
if (x == upNodeValue): nodeY = nodeY - 1
elif (x == rightNodeValue): nodeX = nodeX + 1
elif (x == downNodeValue): nodeY = nodeY + 1
else: nodeX = nodeX - 1
if (slowMode == True):
Grid.listOfPathCords.append([nodeX, nodeY])
else:
Grid.grid[nodeX, nodeY] = np.array([0, 167, 189])
Grid.totalDistance += 1
i += 1
GridShortestPath.cleanGrid()
return Grid.grid
def searchNode(x, y):
if (GridShortestPath.validateNode(x, y)):
return (GridSearch.searchGrid[x, y])[1]
return 0
def validateNode(x, y):
if ((x >= Grid.gridWidth) or (x < 0)):
return False
if ((y >= Grid.gridHeight) or (y < 0)):
return False
if (Grid.isNodeObstacle((Grid.grid[x, y]))):
return False
if (Grid.isNodeEnd((Grid.grid[x, y]))):
return False
return True
def cleanGrid():
for i in range(0, Grid.gridWidth, 1):
for j in range(0, Grid.gridHeight, 1):
if ((not(Grid.isNodeObstacle(Grid.grid[i, j]))) and (not(Grid.isNodeStart(Grid.grid[i, j]))) and (not(Grid.isNodeEnd(Grid.grid[i, j]))) and (not(Grid.isNodePath(Grid.grid[i, j])))):
Grid.grid[i,j] = np.array([255, 255, 255])
class GridSearch():
searchGrid = 0
nodes = []
def findShortestPath():
x = Grid.grid
GridSearch.searchGrid = x
GridSearch.searchGrid[Grid.startNodeX, Grid.startNodeY] = np.array([0, 100, 0])
currentNodeCords = [Grid.startNodeX, Grid.startNodeY]
GridSearch.nodes.append(currentNodeCords)
while (len(GridSearch.nodes) != 0):
node = GridSearch.nodes[0]
del GridSearch.nodes[0]
nodeX = node[0]
nodeY = node[1]
nodeDistance = (GridSearch.searchGrid[nodeX, nodeY])[0]
GridSearch.searchNode(nodeX, nodeY - 1, nodeDistance + 1)
GridSearch.searchNode(nodeX + 1, nodeY, nodeDistance + 1)
GridSearch.searchNode(nodeX, nodeY + 1, nodeDistance + 1)
GridSearch.searchNode(nodeX - 1, nodeY, nodeDistance + 1)
return GridSearch.searchGrid
def searchNode(x, y, nodeDistance):
if (GridSearch.validateNode(x, y)):
GridSearch.searchGrid[x, y] = np.array([nodeDistance, nodeDistance, nodeDistance])
GridSearch.nodes.append([x, y])
def validateNode(x, y):
if ((x >= Grid.gridWidth) or (x < 0)):
return False
if ((y >= Grid.gridHeight) or (y < 0)):
return False
if not(Grid.isNodeEmpty((GridSearch.searchGrid[x, y]))):
return False
return True