-
Notifications
You must be signed in to change notification settings - Fork 28
/
all_functions.py
1207 lines (930 loc) · 38 KB
/
all_functions.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division, print_function
import heapq
import time
from math import sqrt, ceil, floor, isinf, pi, isnan
import random
import itertools
import collections
import numpy as np
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import config_user as gl
from sys import getsizeof, stderr
from itertools import chain
from collections import deque
try:
from reprlib import repr
except ImportError:
pass
# Creating local copies of constants
sizeX, sizeY, sizeZ = gl.sizeX, gl.sizeY, gl.sizeZ
cX, cY, cZ, zMove = gl.cX, gl.cY, gl.cZ, sizeX*sizeY*sizeZ
searchRadius,searchRadiusSquared = gl.searchRadius, gl.searchRadius**2
sr = searchRadius
numNodes = sizeX*sizeY*sizeZ
heuristicScale = gl.heuristicScale
makeFigure = gl.makeFigure
makeMovie = gl.makeMovie
restrictVerticalMovement = gl.restrictVerticalMovement
distancerequirement = gl.distancerequirement
refinementDistance, refinementDistanceSquared = gl.refinementDistance, gl.refinementDistance**2
safetymargin = gl.safetymargin
alpha, splinePoints = gl.alpha, gl.splinePoints
pathComputationLimit = gl.t_max / 1000 # convert to seconds
zf1, zf2 = gl.zf1, gl.zf2
#maxTurnAngle = gl.maxTurnAngle
def succ(s):
""" Find which nodes can be moved to next from node s. Used to randomly move any moving goals """
x, y, z = s
# Define successor states
sDel = []
succNode = [
(x, y+1, z-1),
(x+1, y+1, z-1),
(x+1, y, z-1),
(x+1, y-1, z-1),
(x, y-1, z-1),
(x-1, y-1, z-1),
(x-1, y, z-1),
(x-1, y+1, z-1),
(x, y, z-1),
(x, y+1, z),
(x+1, y+1, z),
(x+1, y, z),
(x+1, y-1, z),
(x, y-1, z),
(x-1, y-1, z),
(x-1, y, z),
(x-1, y+1, z),
(x, y+1, z+1),
(x+1, y+1, z+1),
(x+1, y, z+1),
(x+1, y-1, z+1),
(x, y-1, z+1),
(x-1, y-1, z+1),
(x-1, y, z+1),
(x-1, y+1, z+1),
(x, y, z+1),
]
# Nodes to delete when on a boundary
if x == sizeX:
sDel += 1,2,3,10,11,12,18,19,20
elif x == 1:
sDel +=5,6,7,14,15,16,22,23,24
if y == sizeY:
sDel += 0,1,7,9,10,16,17,18,24
elif y == 1:
sDel += 3,4,5,12,13,14,20,21,22
if z == sizeZ:
sDel += 17,18,19,20,21,22,23,24,25
elif z == 1:
sDel += 0,1,2,3,4,5,6,7,8
if sDel:
sDel = set(sDel)
for i in sorted(sDel, reverse=True):
del succNode[i]
return succNode
def cantor(x,y,z):
"""
:param x, y, z: node coordinates
:return: single unique integer of the 3 coordinates using cantor pairing function
"""
x = 0.5 * (x + y) * (x + y + 1) + y
x = 0.5 * (x + z) * (x + z + 1) + z
return x
def rectObs(locX, locY, locZ, dimX, dimY, dimZ):
"""
Generate rectangular obstacles
:param dimX, dimY, dimZ: specifies the dimensions of the obstacle
:param locX, locY, locZ: defines the bottom left corner of the obstacle
:return: array of the individual nodes which compose the larger cuboid
"""
obsLocX, obsLocY, obsLocZ = [],[],[]
appX, appY, appZ = obsLocX.append, obsLocY.append, obsLocZ.append
obsLocs = []
obs_append = obsLocs.append
for dx in xrange(0, dimX):
for dy in xrange(0, dimY):
for dz in xrange(0, dimZ):
appX(locX + dx), appY(locY + dy), appZ(locZ + dz)
obs_append((locX + dx, locY + dy, locZ + dz))
return obsLocs
def plotRectObs(x, y, z, xd, yd, zd, alpha, axisname):
"""
:param x, y, z: coordinates of the obstacle
:param xd, yd, zd: width of the obstacle
:param axisname: figure axis on which to plot
:return: add the obstacle to plot
"""
# Define each pair of x,y,z coordinates. Each column is a vertex
xvec = [x, x+xd, x+xd, x, x, x+xd, x+xd, x]
yvec = [y, y, y+yd, y+yd, y, y, y+yd, y+yd]
zvec = [z, z, z, z, z+zd, z+zd, z+zd, z+zd]
# Specify order in which to connect each vertex to make the 6 faces
vertlist = [[0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7], [0, 1, 2, 3], [4, 5, 6, 7]]
tupleList = zip(xvec, yvec, zvec)
# Add polygon to axis
poly3d = [[tupleList[vertlist[ix][iy]] for iy in xrange(len(vertlist[0]))] for ix in xrange(len(vertlist))]
collection = Poly3DCollection(poly3d, linewidth=1, alpha=alpha)
collection.set_color([0, 0, 0, alpha])
axisname.add_collection3d(collection)
def heuristic(us,ut):
"""
:param us: source node
:param ut: target node
:return: Euclidean distance between them
"""
sx, sy, sz = us
tx, ty, tz = ut
dx, dy, dz = sx-tx, sy-ty, sz-tz
return heuristicScale * sqrt(dx*dx + dy*dy + dz*dz)
def lineOfSight(*args):
"""
:param us OR x1,y1,z1: source node, given as node number or coordinates
:param ut OR x2,y2,z2: target node, given as node number or coordinates
:return: boolean, whether or not line of sight exists between the two nodes
"""
x1, y1, z1 = args[0]
x2, y2 ,z2 = args[1]
dx, dy, dz = x2 - x1, y2 - y1, z2 - z1
ax, ay, az = abs(dx)*2, abs(dy)*2, abs(dz)*2
sx, sy, sz = cmp(dx,0), cmp(dy,0), cmp(dz,0)
if ax >= max(ay,az):
yD = ay - ax/2
zD = az - ax/2
while x1 != x2:
if yD >= 0:
y1 += sy
yD -= ax
if zD >= 0:
z1 += sz
zD -= ax
x1 += sx; yD += ay; zD += az
if isinf(gl.costMatrix[(x1,y1,z1)]):
return False
elif ay >= max(ax,az):
xD = ax - ay/2
zD = az - ay/2
while y1 != y2:
if xD >= 0:
x1 += sx
xD -= ay
if zD >= 0:
z1 += sz
zD -= ay
y1 += sy; xD += ax; zD += az
if isinf(gl.costMatrix[(x1,y1,z1)]):
return False
elif az > max(ax,ay):
xD = ax - az/2
yD = ay - az/2
while z1 != z2:
if xD >= 0:
x1 += sx
xD -= az
if yD >= 0:
y1 += sy
yD -= az
z1 += sz; xD += ax; yD += ay
if isinf(gl.costMatrix[(x1,y1,z1)]):
return False
return True
def lineOfSight4SAU(*args):
"""
This function is only called by searchAndUpdate. It checks for new obstacles, where as the other lineOfSight
function only checks for known obstacles
:param us OR x1,y1,z1: source node, given as node number or coordinates
:param ut OR x2,y2,z2: target node, given as node number or coordinates
:return: boolean, whether or not line of sight exists between the two nodes
"""
x1, y1, z1 = args[0]
x2, y2 ,z2 = args[1]
dx, dy, dz = x2 - x1, y2 - y1, z2 - z1
ax, ay, az = abs(dx)*2, abs(dy)*2, abs(dz)*2
sx, sy, sz = cmp(dx,0), cmp(dy,0), cmp(dz,0)
if ax >= max(ay,az):
yD = ay - ax/2
zD = az - ax/2
while x1 != x2:
if yD >= 0:
y1 += sy
yD -= ax
if zD >= 0:
z1 += sz
zD -= ax
x1 += sx; yD += ay; zD += az
if gl.map_[(x1,y1,z1)] == - 2 or gl.map_[(x1,y1,z1)] == -1:
return False, (x1,y1,z1)
elif ay >= max(ax,az):
xD = ax - ay/2
zD = az - ay/2
while y1 != y2:
if xD >= 0:
x1 += sx
xD -= ay
if zD >= 0:
z1 += sz
zD -= ay
y1 += sy; xD += ax; zD += az
if gl.map_[(x1,y1,z1)] == - 2 or gl.map_[(x1,y1,z1)] == -1:
return False, (x1,y1,z1)
elif az > max(ax,ay):
xD = ax - az/2
yD = ay - az/2
while z1 != z2:
if xD >= 0:
x1 += sx
xD -= az
if yD >= 0:
y1 += sy
yD -= az
z1 += sz; xD += ax; yD += ay
if gl.map_[(x1,y1,z1)] == - 2 or gl.map_[(x1,y1,z1)] == -1:
return False, (x1,y1,z1)
return True, None
def postSmoothPath(pathArray):
"""
:param pathArray: current path stored as a series of nodes
:return: Path smoothed in directions of uniform cost
"""
k, t = 0, [pathArray[0]]
for i in xrange(1,len(pathArray)-1):
x1,y1,z1 = t[k]
x2,y2,z2 = pathArray[i+1]
if (abs(z1-z2)>0.01 and cZ > 1) or not lineOfSight(t[k],pathArray[i+1]):
k += 1
t.append(pathArray[i])
k += 1
t.append(pathArray[-1])
return t
def genRandObs(minObs, maxObs, maxPercent, seed):
"""
Generates random 1x1 obstacles during traversal
:param minObs: min mumber number of randomly generated obstacles
:param maxObs: max mumber number of randomly generated obstacles
:param maxPercent: max percent of map that can contain obstacles
:param seed: used to create deterministic results
:return: updates UAV map and plots obstacles
"""
np.random.seed(seed + gl.stepCount)
num2gen = np.random.random_integers(minObs,maxObs)
randomint = np.random.random_integers
for i in xrange(num2gen):
# Stop generating obstacles if limit is reached
obsFraction = len(gl.map_ ) / numNodes
if obsFraction*100 > maxPercent:
break
# Generate obstacle location
newX, newY, newZ = randomint(1,sizeX), randomint(1,sizeY), randomint(1,sizeZ)
s_obs = (newX,newY,newZ)
# Don't place obstacle at start, goal, other obstacles, or within searchRadius
if s_obs == gl.start:
continue
if s_obs in gl.goals:
continue
curX, curY, curZ = gl.start
if max([abs(curX - newX),abs(curY - newY),abs(curZ - newZ)]) < searchRadius:
continue
# Update map
gl.map_[s_obs] = -2 # -2 indicated undetected obstacle
gl.number_of_obstacles += 1
# Add new obstacle to plot
if makeFigure:
plotRectObs(newX, newY, newZ, 1, 1, 1, 0.2, gl.ax1)
def movingGoal(initX, initY, initZ, T):
"""
Generate and execute moving goals
:param initX, initY, initZ: initial location of goal vertex
:param T: movement frequency, don't move every T iterations
:return: 1. Moves the goal specified by input parameters; 2. boolean, whether or not current goal has moved
"""
goalMoved = False
if gl.stepCount % T == 0: # move every T iterations
q = cantor(initX, initY, initZ) # find unique cantor id
if q in gl.goalsVisited: # break if we've visited this goal already
return
idx = np.where(gl.goals[:, 3] == q)[0][0] # get current location of goal
mgs_old = (gl.goals[idx, 0], gl.goals[idx, 1], gl.goals[idx, 2]) # get current node number of that goal
random.seed(q+3)
mgs = random.choice(succ(mgs_old)) # pick random successor to move to
newseed = q + 4
while mgs in gl.obstacles:
# pick another random successor if we end up in an obstacle
random.seed(newseed)
mgs = random.choice(succ(mgs_old))
newseed += 1
mgx, mgy, mgz = mgs # get coordinates of that location
gl.goals[idx, 0:3] = mgx, mgy, mgz # update location of node in goals array
if mgs_old == gl.goal: # if old goal was current goal, update current goal
gl.goal = mgs
goalMoved = True
if makeFigure:
gl.goalhandles[q].remove() # remove scatter point and add new one
gl.goalhandles[q] = gl.ax1.scatter(mgx, mgy, mgz, c='r')
return goalMoved
def setupLevels():
"""
:return: Dictionary of all hierarchical levels
"""
L = {}
for level in xrange(gl.numlevels,-1,-1):
# Get dimensions for each level
if level == 0:
lsizeX, lsizeY, lsizeZ = sizeX, sizeY, sizeZ
L[level] = CL(level, int(lsizeX), int(lsizeY), int(lsizeZ))
else:
sf = 2+(level-1) # scale factor
lsize = max(sizeX/(2**sf), sizeY/(2**sf), sizeZ/(2**sf))
lsizeX, lsizeY, lsizeZ = lsize, lsize, lsize
if lsizeX > sizeX/gl.minclustersize: lsizeX = sizeX/gl.minclustersize
if lsizeY > sizeY/gl.minclustersize: lsizeY = sizeY/gl.minclustersize
if lsizeZ > sizeZ/gl.minclustersize: lsizeZ = sizeZ/gl.minclustersize
L[level] = CL(level, int(lsizeX), int(lsizeY), int(lsizeZ))
return L
def searchAndUpdate(xNew,yNew,zNew,*args):
"""
New method for faster searching and updating of nodes. Uses line-of-sight checks instead of searching all nodes
that are within the bounding search cube (original "searchAndUpdate" function is now called "searchAndUpdate_old")
This and markSafetyMargin are the major bottlenecks
Modifications to speed them up would be very useful
:param xNew, yNew, zNew: current location of UAV
:param args: checks node of pathToFollow to ensure they still have line-of-sight
:return: boolean, whether or not new obstacles exist nearby
"""
cellsToUpdate = []
cellappend = cellsToUpdate.append
validPath = True
""" Get endpoints of bounding search cube """
searchRange = []
sr_append = searchRange.append
x,y,z = int(round(xNew)), int(round(yNew)), int(round(zNew))
xmin, xmax = max(x-sr, 1), min(x+sr, sizeX)
ymin, ymax = max(y-sr, 1), min(y+sr, sizeY)
zmin, zmax = max(z-sr, 1), min(z+sr, sizeZ)
""" Get nodes that make up the 6 faces """
# Face 1: vary x,y at zmin
[sr_append((dx,dy,zmin)) for dx in xrange(xmin, xmax+1) for dy in xrange(ymin, ymax+1)]
# Face 2: vary x,y at zmax
[sr_append((dx,dy,zmax)) for dx in xrange(xmin, xmax+1) for dy in xrange(ymin, ymax+1)]
# Face 3: vary x,z at ymin
[sr_append((dx,ymin,dz)) for dx in xrange(xmin, xmax+1) for dz in xrange(zmin, zmax+1)]
# Face 4: vary x,z at ymax
[sr_append((dx,ymax,dz)) for dx in xrange(xmin, xmax+1) for dz in xrange(zmin, zmax+1)]
# Face 5: vary y,z at xmin
[sr_append((xmin,dy,dz)) for dy in xrange(ymin, ymax+1) for dz in xrange(zmin, zmax+1)]
# Face 6: vary y,z at xmax
[sr_append((xmax,dy,dz)) for dy in xrange(ymin, ymax+1) for dz in xrange(zmin, zmax+1)]
""" Run line-of-sight checks """
for node in searchRange:
los, blkdNode = lineOfSight4SAU((x,y,z), node)
if not los:
cellappend(blkdNode)
gl.costMatrix[blkdNode] = float('inf')
if cellsToUpdate:
markSafetyMargin(cellsToUpdate,safetymargin)
del cellsToUpdate, searchRange # free up memory
if args:
path = args[0]
path = [(round(pt[0]), round(pt[1]), round(pt[2])) for pt in reversed(path)]
# Check line of sight between nodes in path
if len(path) > 0:
# Extract portion within search radius
path_section = []
x1,y1,z1 = gl.start
x2,y2,z2 = path[0]
while max([abs(x1-x2), abs(y1-y2), abs(z1-z2)]) <= max(refinementDistance,searchRadius):
path_section.append(path.pop(0))
if len(path) < 1:
break
x2,y2,z2 = path[0]
# For each node in path_section:
for idx in xrange(len(path_section)-1):
if not lineOfSight(path_section[idx],path_section[idx+1]):
validPath = False
break
del path, path_section # free up memory
return validPath
def searchAndUpdate_old(xNew,yNew,zNew,*args):
"""
This function is no longer used but is left for posterity
It has been replaced by the more realistic and efficient "searchAndUpdate"
:param xNew, yNew, zNew: current location of UAV
:param args: checks node of pathToFollow to ensure they still have line-of-sight
:return: boolean, whether or not new obstacles exist nearby
"""
cellsToUpdate = []
cellappend = cellsToUpdate.append
validPath = True
# Generate list of points to search
searchRange = []
sr_append = searchRange.append
x,y,z = int(round(xNew)), int(round(yNew)), int(round(zNew))
xmin, xmax = max(x-sr, 1), min(x+sr, sizeX)
ymin, ymax = max(y-sr, 1), min(y+sr, sizeY)
zmin, zmax = max(z-sr, 1), min(z+sr, sizeZ)
[sr_append((dx,dy,dz)) for dx in xrange(xmin, xmax+1) for dy in xrange(ymin, ymax+1) for dz in xrange(zmin, zmax+1)]
# Search them
for obsLoc in searchRange:
if gl.map_[obsLoc] == - 2 or gl.map_[obsLoc] == -1:
# -1 = Known obstacle
# -2 = Newly detected/undetected obstacle
cellappend(obsLoc) # Marking obstacles within search radius
gl.map_[obsLoc] = -1
gl.costMatrix[obsLoc] = float('inf')
if cellsToUpdate:
markSafetyMargin(cellsToUpdate,safetymargin)
del cellsToUpdate, searchRange # free up memory
if args:
path = args[0]
path = [(round(pt[0]), round(pt[1]), round(pt[2])) for pt in reversed(path)]
# Check line of sight between nodes in path
if len(path) > 0:
# Extract portion within search radius
path_section = []
x1,y1,z1 = gl.start
x2,y2,z2 = path[0]
while max([abs(x1-x2), abs(y1-y2), abs(z1-z2)]) <= max(refinementDistance,searchRadius):
path_section.append(path.pop(0))
if len(path) < 1:
break
x2,y2,z2 = path[0]
# For each node in path_section:
for idx in xrange(len(path_section)-1):
if not lineOfSight(path_section[idx],path_section[idx+1]):
validPath = False
break
del path, path_section # free up memory
return validPath
def simulateUAVmovement(pathToFollow):
"""
:param pathToFollow: series of nodes for UAV to follow
:return: list of coordinates to move to
"""
nextcoords = []
for k in xrange(len(pathToFollow)-1):
prevS, nextS = pathToFollow[k], pathToFollow[k+1]
prevX, prevY, prevZ = prevS
nextX, nextY, nextZ = nextS
dX, dY, dZ = nextX-prevX, nextY-prevY, nextZ-prevZ
maxDist = max(abs(dist) for dist in [dX, dY, dZ])
if maxDist <=1:
nextcoords.append((nextX, nextY, nextZ))
else:
for jj in xrange(1, int(maxDist+1)):
xFrac, yFrac, zFrac = dX/maxDist, dY/maxDist, dZ/maxDist
newX, newY, newZ = prevX + jj*xFrac, prevY + jj*yFrac, prevZ + jj*zFrac
nextcoords.append((newX, newY, newZ))
# Next node to go to is last node in array
nextcoords.reverse()
return nextcoords
def euclideanDistance(us,ut):
"""
:param us: source node
:param ut: target node
:return: Euclidean distance between the nodes
"""
xs,ys,zs = us
xt,yt,zt = ut
dx,dy,dz = xs-xt, ys-yt, zs-zt
return sqrt(dx*dx + dy*dy + dz*dz)
def findPath(L):
startTime = time.clock()
distance = euclideanDistance(gl.start,gl.goal)
path = [gl.start, gl.goal]
first_path = True
if distance <= distancerequirement*4: # path distance too short
path = L[0].computeShortestPath(path,False)
return path
# Get most coarse path first
for level in xrange(gl.numlevels,-1,-1):
if distance >= distancerequirement*L[level].maxlength:
try:
path = L[level].computeShortestPath(path,first_path)
first_path = False
break
except (KeyError, IndexError):
# if there's no path at that level, go a level smaller
continue
if level != 0 and time.clock()-startTime < pathComputationLimit:
refined_path = []
x1,y1,z1 = gl.start
x2,y2,z2 = path[0]
while max([abs(x1-x2), abs(y1-y2), abs(z1-z2)]) <= refinementDistance:
refined_path.append(path.pop(0))
if len(path) == 1:
break
x2,y2,z2 = path[0]
refined_path.append(path.pop(0))
# "path" now only contains nodes outside of the search radius
for newlevel in xrange(level-1,-1,-1):
if time.clock()-startTime < pathComputationLimit:
refined_path = L[newlevel].computeShortestPath(refined_path, first_path)
else:
break
path[:0] = refined_path # insert refined path into the beginning of the original path
else:
return path
return path
def parameterValues(ti, p1, p2):
"""
Used to get parametric value t for Catmull-Rom spline
"""
x1, y1, z1 = p1
x2, y2, z2 = p2
return (((x2-x1)**2 + (y2-y1)**2 + (z2-z1)**2 )**0.5)**alpha + ti
def CatmullRomPoints(p0, p1, p2, p3, numPts):
"""
:param p0, p1, p2, p3: (x,y,z) coordinates
:param numPts: number of points to include in this curve segment.
:return: [p1, generated intermediary points, p2]
"""
# Convert points to numpy for array multiplication
p0, p1, p2, p3 = map(np.array, [p0, p1, p2, p3])
# Calculate t0 to t3
t0 = 0
t1 = parameterValues(t0, p0, p1)
t2 = parameterValues(t1, p1, p2)
t3 = parameterValues(t2, p2, p3)
if t1==t0:
t1 = 1e-8
if t3==t2:
t3 = t2+1e-8
# Only find points between p1 and p2
t = np.linspace(t1, t2, numPts)
# Get point for each t-value using equation in: http://faculty.cs.tamu.edu/schaefer/research/catmull_rom.pdf
t = t.reshape(len(t),1)
L01 = (t1-t)/(t1-t0) * p0 + (t - t0) / (t1 - t0) * p1
L12 = (t2-t)/(t2-t1) * p1 + (t - t1) / (t2 - t1) * p2
L23 = (t3-t)/(t3-t2) * p2 + (t - t2) / (t3 - t2) * p3
L012 = (t2-t)/(t2-t0)*L01 + (t-t0)/(t2-t0)*L12
L123 = (t3-t)/(t3-t1)*L12 + (t-t1)/(t3-t1)*L23
C = (t2-t)/(t2-t1)*L012 + (t-t1)/(t2-t1)*L123
return C
def CatmullRomSpline(pts):
"""
Generate Catmull-Rom splines for a path and return the new path
"""
if len(pts) > 2:
# Duplicate first and last nodes to get their splines
pts.insert(0, pts[0])
pts.append(pts[-1])
sz = len(pts)
# Create list of (x,y,z) coordinates
C = []
for i in range(sz-3):
c = CatmullRomPoints(pts[i], pts[i+1], pts[i+2], pts[i+3], splinePoints)
C.extend(c.tolist())
return C
else:
# Cannot generate spline through two nodes
return pts
def plotResultingWaypoints(waypoints,color,size,delete):
"""
Useful for debugging, plots nodes used to form path, then removes them and shows the new ones
for subsequent paths
:param waypoints: vector of nodes
:return: updates plot with scatter points of each waypoint
"""
if makeFigure:
X,Y,Z = [], [], []
if gl.stepCount > 1 and delete==True:
gl.hdl.remove()
for node in waypoints:
x,y,z = node
X.append(x), Y.append(y), Z.append(z)
X.pop(0), Y.pop(0), Z.pop(0) # don't plot start node
gl.hdl = gl.ax1.scatter(X,Y,Z, c=color, s=size)
def succ6(s):
"""
Find which nodes can be moved to next from node s, excluding diagonals
Used to mark nodes within safety margin since its faster than using all 26 successors
"""
x, y, z = s
# Define successor states, one down in z-direction
sDel = []
succNode = [
(x, y, z-1), # Keep - 8 0
(x, y+1, z), # Keep - 9 1
(x+1, y, z), # Keep - 11 2
(x, y-1, z), # Keep - 13 3
(x-1, y, z), # Keep - 15 4
(x, y, z+1), # Keep - 25 5
]
# Nodes to delete when on a boundary
if x == sizeX:
sDel.append(2)
elif x == 1:
sDel.append(4)
if y == sizeY:
sDel.append(1)
elif y == 1:
sDel.append(3)
if z == sizeZ:
sDel.append(5)
elif z == 1:
sDel.append(0)
if sDel:
sDel = set(sDel)
for i in sorted(sDel, reverse=True):
del succNode[i]
return succNode
def markSafetyMargin(cellsToUpdate,sm):
"""
New method for marking nodes within the safety margin, a bit faster than the old one
This and searchAndUpdate are the major bottlenecks
Modifications to speed them up would be very useful
:param cellsToUpdate: list of nodes containing obstacles
:param sm: safe distance to remain from obstacles
:return: recursively mark successors of nodes as obstacles until safety margin is met
"""
if sm == 0:
return
else:
# First, get a list of the immediate successors
allsucc = set()
asa = allsucc.update
for node in cellsToUpdate:
succS = succ6(node)
asa(succS)
# Now repeat for remaining successors
if sm > 1:
for i in xrange(sm-1):
cellsToAdd = []
ce = cellsToAdd.extend
for node in allsucc:
succS = succ6(node)
ce(succS)
allsucc.update(cellsToAdd)
for node in list(allsucc):
#gl.map_[node] = -1
gl.costMatrix[node] = float('inf')
del allsucc
def markSafetyMargin_old(cellsToUpdate,sm):
"""
This function is no longer used but is left for posterity
It has been replaced by the more realistic and efficient "markSafetyMargin"
:param cellsToUpdate: list of nodes containing obstacles
:param sm: safe distance to remain from obstacles
:return: recursively mark successors of nodes as obstacles until safety margin is met
"""
if sm == 0:
return
else:
# First, get a list of the immediate successors
allsucc = []
asa = allsucc.extend
for node in cellsToUpdate:
succS = succ6(node)
asa(succS)
# Remove duplicates
allsucc = list(set(allsucc))
# Now repeat for remaining successors
if sm > 1:
for i in xrange(sm-1):
cellsToAdd = []
ce = cellsToAdd.extend
for node in allsucc:
succS = succ6(node)
ce(succS)
allsucc.extend(list(set(cellsToAdd)))
allsucc = list(set(allsucc))
for node in allsucc:
#gl.map_[node] = -1
gl.costMatrix[node] = float('inf')
del allsucc
class CL: # Create level
""" Creates a class containing the map properties for a given level """
def __init__(self, levelnumber, lsizeX, lsizeY, lsizeZ):
self.levelnumber = levelnumber
self.lengthX = int(sizeX/lsizeX) # distance of successor nodes
self.lengthY = int(sizeY/lsizeY)
self.lengthZ = int(sizeZ/lsizeZ)
self.maxlength = max(self.lengthX, self.lengthY, self.lengthZ)
self.minlength = min(self.lengthX, self.lengthY, self.lengthZ)
def initialize(self, startnode, goalnode):
"""
:param startnode: start location
:param goalnode: goal location
:return: initialization to perform computeShortestPath
"""
CL.U = []
CL.entry_finder = {} # mapping of tasks to entries
CL.km = 0
CL.g = collections.defaultdict(lambda: float('inf'))
CL.rhs = collections.defaultdict(lambda: float('inf'))
CL.bptr = collections.defaultdict(None)
CL.g[goalnode] = float('inf')
CL.rhs[goalnode] = 0
self.add_node(heuristic(startnode, goalnode), 0, goalnode) # add goal to queue
def calcKey(self,s, startnode):
""" Calculates the key values for vertex s based on a given startnode"""
key1 = min(CL.g[s], CL.rhs[s]) + CL.km + heuristic(startnode, s)
key2 = min(CL.g[s], CL.rhs[s])
return key1, key2
def add_node(self, key1, key2, u):
""" Add new node or update priority of existing node """
if u in CL.entry_finder:
self.remove_node(u)
CL.entry_finder[u] = [key1, key2, u]
heapq.heappush(CL.U, [key1, key2, u])
def remove_node(self, u):
""" Mark an existing node as removed """
del CL.entry_finder[u]
def pop_node(self):
""" Remove and return lowest priority task. Raise KeyError if empty """
while True:
key1, key2, u = heapq.heappop(CL.U)
if u in CL.entry_finder:
del CL.entry_finder[u]
return key1, key2, u
raise KeyError('Attempted to pop from an empty priority queue')
def updateVertex(self, u, startnode):
""" Inserts, or removes keys/entries of u in U """
if CL.g[u] != CL.rhs[u]:
k1, k2 = self.calcKey(u, startnode)
self.add_node(k1, k2, u)
elif CL.g[u] == CL.rhs[u]:
if u in CL.entry_finder:
self.remove_node(u)
def computeCost(self, us, ut, first_path):
"""
:param us: source node
:param ut: target node
:return: cost of moving from us to ut for level 0
"""
if isinf(gl.costMatrix[ut]):
return float('inf')
if first_path:
# Check line of sight if closest node is within search radius of current location
dx1, dy1, dz1 = gl.start[0]-us[0], gl.start[1]-us[1], gl.start[2]-us[2]
dx2, dy2, dz2 = gl.start[0]-ut[0], gl.start[1]-ut[1], gl.start[2]-ut[2]
if max([abs(dx1),abs(dy1),abs(dz1)]) <= searchRadius or max([abs(dx2),abs(dy2),abs(dz2)]) <= searchRadius:
if not lineOfSight(us,ut):
return float('inf')
dx, dy, dz = us[0]-ut[0], us[1]-ut[1], us[2]-ut[2]
# Modify these lines if cX and cY are not 1
if dz != 0:
sf = cZ # scale factor