-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.py
396 lines (323 loc) · 12.5 KB
/
Dijkstra.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import sys
import math
import numpy as np
import random
'''
Dijkstras algorithm
@author: Duncan Nicholson
HOW TO RUN THIS CODE:
$ python Dijkstra.py
if you don't have numpy installed
$ pip install numpy
Tested with Python 3.7.7
'''
# PARAMETERS
#############
SHOW_ITERATIONS = False
MAX_ITS = 1e4
#############
BOARD_SIZE = [11, 11]
START = [0, 0]
GOAL = [10, 10]
TREES = [[2, 2], [2, 3], [3, 3], [3, 4], [3, 5],
[4, 5], [4, 6], [4, 7], [5, 7], [5, 8]]
#############
class Tile:
def __init__(self, r, c, _type):
# location
self.r = r
self.c = c
# string for storing the type, " "=EMPTY, *=TREE, $=START,#=GOAL
self.type = _type
self.visited = False
self.current = False
self.distance = float("inf")
'''
hold previous tile that "discovered" the current tile
we will iterate again after the algorithm is over to
to recover the shortest path
'''
self.prev = []
class Board:
def __init__(self, _size, _start, _goal, _trees):
print("constructing board ...")
self.size = _size
self.start = _start
self.goal = _goal
self.trees = _trees
self.nrows = _size[0]
self.ncols = _size[1]
# initialize empty board as a list of lists
self.tiles = [[] for _ in range(self.nrows)]
# fill the board with empty tiles
for r in range(self.nrows):
for c in range(self.ncols):
self.tiles[r].append(Tile(r, c, _type=" "))
# set tiles that are obstacles
for tree in _trees:
self.tiles[tree[0]][tree[1]].type = "*"
self.tiles[tree[0]][tree[1]].distance = float("inf")
# set start and end locations
self.tiles[self.start[0]][self.start[1]].type = "$"
self.tiles[self.goal[0]][self.goal[1]].type = "#"
# set the start node as current and set it's distance to 0
self.tiles[self.start[0]][self.start[1]].current = True
self.tiles[self.start[0]][self.start[1]].distance = 0
# return current tile object
def getCurrentTile(self):
for row in self.tiles:
for __tile in row:
if __tile.current == True:
return __tile
# return all unvisited tiles
def getUnvisited(self):
unv_tiles = []
for row in self.tiles:
for tile in row:
if tile.visited == False and tile.type != "*":
unv_tiles.append(tile)
return unv_tiles
# return unvisited tiles next to current tile
def getUnvisitedNeighbors(self):
tile = self.getCurrentTile()
r = tile.r
c = tile.c
ts = self.tiles
maxr = self.size[0]-1
maxc = self.size[1]-1
if r == 0: # no SE S SW
if c == 0: # No NW W SW
nbs = [ts[r+1][c], # N
ts[r+1][c+1], # NE
ts[r][c+1]] # E
elif c == maxc: # no NE E SE
nbs = [ts[r+1][c], # N
ts[r][c-1], # W
ts[r+1][c-1]] # NW
else:
nbs = [ts[r+1][c], # N
ts[r+1][c+1], # NE
ts[r][c+1], # E
ts[r][c-1], # W
ts[r+1][c-1]] # NW
elif r == maxr: # no N NE NW
if c == 0: # No NW W SW
nbs = [ts[r][c+1], # E
ts[r-1][c+1], # SE
ts[r-1][c]] # S
elif c == maxc: # no NE E SE
nbs = [ts[r-1][c], # S
ts[r-1][c-1], # SW
ts[r][c-1]] # W
else:
nbs = [ts[r][c+1], # E
ts[r-1][c+1], # SE
ts[r-1][c], # S
ts[r-1][c-1], # SW
ts[r][c-1]] # W
elif c == 0: # no NW W SW
nbs = [ts[r+1][c], # N
ts[r+1][c+1], # NE
ts[r][c+1], # E
ts[r-1][c+1], # SE
ts[r-1][c]] # S
elif c == maxc: # no NE E SE
nbs = [ts[r+1][c], # N
ts[r-1][c], # S
ts[r-1][c-1], # SW
ts[r][c-1], # W
ts[r+1][c-1]] # NW
else:
nbs = [ts[r+1][c], # N
ts[r+1][c+1], # NE
ts[r][c+1], # E
ts[r-1][c+1], # SE
ts[r-1][c], # S
ts[r-1][c-1], # SW
ts[r][c-1], # W
ts[r+1][c-1]] # NW
# trim trees and visited tiles from neighbors
for nb in nbs:
if nb.visited == True or nb.type == "*":
nbs.remove(nb)
return nbs
# show the board to the user, and optionally the path
def print_board(self, _path):
# top line
sys.stdout.write("\n ")
for _ in range(self.ncols):
sys.stdout.write("--")
print()
for row in self.tiles[::-1]:
# row legend
if row[0].r < 10:
sys.stdout.write(" ")
sys.stdout.write(str(row[0].r)+" ")
# board content
for tile in row:
if [tile.r, tile.c] in _path:
sys.stdout.write("|@")
else:
sys.stdout.write("|"+tile.type)
sys.stdout.write("|\n")
# bottom line
sys.stdout.write(" ")
for _ in range(self.ncols):
sys.stdout.write("--")
print()
# column legend
sys.stdout.write(" ")
for num in range(self.ncols):
sys.stdout.write(str(num)+" ")
print()
def print_distances(self):
# top line
sys.stdout.write("\n ")
for _ in range(self.ncols):
sys.stdout.write("--")
print()
for row in self.tiles[::-1]:
# row legend
if row[0].r < 10:
sys.stdout.write(" ")
sys.stdout.write(str(row[0].r)+" ")
# board content
for tile in row:
if tile.distance == float("inf"):
sys.stdout.write(f"| {tile.distance}")
elif tile.distance < 10:
sys.stdout.write("| %3.1f" % tile.distance)
else:
sys.stdout.write("|%3.1f" % tile.distance)
sys.stdout.write("|\n")
# bottom line
sys.stdout.write(" ")
for _ in range(self.ncols):
sys.stdout.write("--")
print()
# column legend
sys.stdout.write(" ")
for num in range(self.ncols):
sys.stdout.write(str(num)+" ")
print()
class Dijkstra:
def __init__(self, _board):
print("getting ready for dijkstras algorithm . . .")
self.nits = 0
# shallow copy the board
self.board = _board
# show the starting board
self.board.print_board([])
# instantiate unvisited set as a data member
self.unvisited = self.board.getUnvisited()
# run Djkstras algorithm
self.iterate()
# print results
if self.nits < MAX_ITS:
path = self.recover_path()
self.print_text_path(path)
self.board.print_board(path)
def iterate(self):
print("iterating . . .")
while True:
tile = self.board.getCurrentTile()
# for debug printout only
nb_dists = []
thru_dists = []
nbs = self.board.getUnvisitedNeighbors()
for nb in nbs:
if nb.r == tile.r or nb.c == tile.c:
# neighbor is adjacent
dist = 1.0
else:
# neighbor is diagonal
# immediate distance is hypotenouse of 45-45-90 triangle
dist = math.sqrt(2.0)
nb_dists.append(dist)
# store distances to unvisited neighbors THRU the current node
thru_dists.append(tile.distance + dist)
# if the smallest neighbor distance is infinity (eg stuck inside obstacles), then stop
if min(nb_dists) == float("inf"):
print("stopping: minimum neighbor distance is inf")
break
# if the new tentative distance is lower than currently held tentative distance for that nb, update it
for ind, nb in enumerate(nbs):
if thru_dists[ind] < nb.distance and nb.type == " ":
nb.distance = thru_dists[ind]
# keep track of shortest path to that tile
nb.prev = [tile.r, tile.c]
tile.visited = True
self.unvisited.remove(tile)
# stop if we've visited the destination
if self.unvisited == []:
self.board.tiles[10][10].prev = [9, 9]
print("stopping: destination reached")
break
# loop over ALL unvisited open tiles and store the distances
unv_thru_dists = []
for unv_tile in self.unvisited:
unv_thru_dists.append(unv_tile.distance)
# select the next tile with lowest tentative distance of unvisited tiles that aren't trees
min_thru_dist = min(unv_thru_dists)
next_tile_ind = unv_thru_dists.index(min_thru_dist)
next_tile = self.unvisited[next_tile_ind]
next_tile.current = True
tile.current = False
if SHOW_ITERATIONS:
nb_locs = []
for nb in nbs:
nb_locs.append([nb.r, nb.c])
print("\ncurrent node: [{}, {}]".format(tile.r, tile.c))
print("{} neighbors: {}".format(len(nb_locs), nb_locs))
print("with distances: {}".format(
['%.2f' % elem for elem in nb_dists]))
print("with total distances: {}".format(
['%.2f' % elem for elem in thru_dists]))
self.board.print_distances()
self.nits += 1
if self.nits > MAX_ITS:
print(
"stopping: couldn't reach destination in {} iterations".format(MAX_ITS))
break
def recover_path(self):
# start at the destination
locatio = self.board.goal
thepath = [locatio]
# # walk back along the shortest path
while locatio != self.board.start:
# get the tile object at that location
for row in self.board.tiles:
for tile in row:
if [tile.r, tile.c] == locatio:
thetile = tile
locatio = thetile.prev
thepath.append(locatio)
# reverse the path
return thepath[::-1]
def print_text_path(self, __path):
print("\nshortest path:")
for _loc in __path:
sys.stdout.write(str(_loc))
if __path.index(_loc) != len(__path)-1:
sys.stdout.write("->")
def main():
# instantiate a board object
b = Board(BOARD_SIZE, START, GOAL, TREES)
# run Dijkstra's algorithm on that board
_ = Dijkstra(b)
# try some more complicated boards . .
# tree_density = 0.2
# nboards = 3
# for _ in range(nboards):
# size2 = [random.randint(5,20) for _ in range(2)]
# start2 = [random.randint(0,size2[0]-1),random.randint(0,size2[1]-1)]
# goal2 = [random.randint(0,size2[0]-1),random.randint(0,size2[1]-1)]
# if goal2 == start2:
# goal2 = [random.randint(0,size2[0]-1),random.randint(0,size2[1]-1)]
# random_trees = [[random.randint(0,size2[0]-1),random.randint(0,size2[1]-1)] for _ in range(random.randint(0,int(size2[0]*size2[1]*tree_density)))]
# if start2 in random_trees: random_trees.remove(start2)
# if goal2 in random_trees: random_trees.remove(goal2)
# print("##################################################################")
# Dijkstra(Board(size2,start2,goal2,random_trees))
if __name__ == "__main__":
main()