-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_generate_paths.py
2790 lines (2125 loc) · 254 KB
/
pipeline_generate_paths.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
import pandas as pd
import pickle
import seaborn as sns
import numpy as np
import cv2
import matplotlib.pylab as plt
import math
import copy
import time
import decimal
import random
import os
import sys
import string
from pandas.plotting import table
import matplotlib.gridspec as gridspec
from collections import defaultdict
from shapely.geometry import LineString
import scipy.interpolate as interpolate
import pprint
import matplotlib.patheffects as path_effects
import utility_environ_descrip as resto
import utility_path_segmentation as chunkify
import utility_legibility as legib
FLAG_SAVE = True
FLAG_VIS_GRID = False
FLAG_EXPORT_HARDCODED = False
FLAG_REDO_PATH_CREATION = False #True #False #True #False
FLAG_REDO_ENVIR_CACHE = False
FLAG_MIN_MODE = False
FLAG_EXPORT_LATEX_MAXES = False
FLAG_LINE_OUTLINE = False
FLAG_EXPORT_JUST_TEASER = True
FLAG_EXPORT_MOCON = True
VISIBILITY_TYPES = resto.VIS_CHECKLIST
NUM_CONTROL_PTS = 3
NUMBER_STEPS = 30
PATH_TIMESTEPS = 15
resto_pickle = 'pickle_vis'
vis_pickle = 'pickle_resto'
FILENAME_PATH_ASSESS = 'path_assessment/'
FLAG_PROB_HEADING = False
FLAG_PROB_PATH = True
FLAG_EXPORT_SPLINE_DEBUG = False
# EXP SETTINGS VARIABLES
SETTING_EXPORT_TITLE = 'title'
SETTING_RESOLUTION = 'resolution'
SETTING_SAMPLING_TYPE = 'sampling_type'
SETTING_LAYOUT_TYPE = 'exp_layout'
SETTING_IS_RANDOM = 'is_random'
SETTING_RANDOM_KEY = 'random_key_string'
SETTING_F_VIS_LABEL = 'f_vis_label'
SETTING_EPSILON = 'epsilon'
SETTING_LAMBDA = 'lambda'
SETTING_NUM_CHUNKS = 'num_chunks'
SETTING_CHUNK_BY_WHAT = 'chunk-by-what'
SETTING_CHUNK_METHOD = 'chunk_type' # method for chunking paths for navigation, ie CHUNKIFY_LINEAR, CHUNKIFY_TRIANGULAR, CHUNKIFY_MINJERK
SETTING_ANGLE_STRENGTH = 'angle_strength' # How strongly the final approach is enforced
SETTING_MIN_PATH_LENGTH = 'min_path_length'
SETTING_IS_DENOMINATOR = 'is_denominator'
SETTING_F_VISIBILITY_FUNCTION = 'f_vis'
SETTING_ANGLE_CUTOFF = 'angle_cutoff'
SETTING_FOV = 'fov' # field of view of observers
SETTING_PROB_OG = 'prob_og'
SETTING_RIGHT_BOUND = 'right-bound'
SETTING_WAYPOINT_OFFSET = 'waypoint_offset'
SETTING_LEGIBILITY_METHOD = 'l_method'
# PATH_COLORS = [(138,43,226), (0,255,255), (255,64,64), (0,201,87)]
SAMPLE_TYPE_CENTRAL = 'central-new'
SAMPLE_TYPE_CENTRAL_SPARSE = 'central-sprs-new'
SAMPLE_TYPE_DEMO = 'demo2'
SAMPLE_TYPE_CURVE_TEST = 'ctest'
SAMPLE_TYPE_NEXUS_POINTS = 'nn_fin7'
SAMPLE_TYPE_NEXUS_POINTS_ONLY = 'nn_only'
SAMPLE_TYPE_SPARSE = 'sparse'
SAMPLE_TYPE_SYSTEMATIC = 'systematic'
SAMPLE_TYPE_HARDCODED = 'hardcoded'
SAMPLE_TYPE_VISIBLE = 'visible'
SAMPLE_TYPE_INZONE = 'in_zone'
SAMPLE_TYPE_SHORTEST = 'minpaths'
SAMPLE_TYPE_FUSION = 'fusion'
ENV_START_TO_HERE = 'start_to_here'
ENV_HERE_TO_GOALS = 'here_to_goals'
ENV_VISIBILITY_PER_OBS = 'vis_per_obs'
ENV_PROB_G_HERE = 'prob_to_here'
premade_path_sampling_types = [SAMPLE_TYPE_DEMO, SAMPLE_TYPE_SHORTEST, SAMPLE_TYPE_CURVE_TEST]
non_metric_columns = ["path", "goal", 'path_length', 'path_cost', 'sample_points']
bug_counter = defaultdict(int)
curvatures = []
max_curvatures = []
def tests_prob_heading():
p1 = (0,0)
p2 = (0,1)
goals = [(1,1), (-1,1), (0, -1)]
correct = [ 0.5, 0.5, -0.]
result = prob_goals_given_heading(p1, p2, goals)
if np.array_equal(correct, result):
pass
else:
print("Error in heading probabilities")
goals = [(4,0), (5,0)]
correct = [ 0.5, 0.5]
result = prob_goals_given_heading(p1, p2, goals)
print(goals)
print(result)
goals = [(4,1), (4,1)]
correct = [ 0.5, 0.5]
result = prob_goals_given_heading(p1, p2, goals)
print(goals)
print(result)
print("ERR")
goals = [(1,1), (-1,1), (-1,-1), (1,-1)]
correct = [ 0.5, 0.5, 0, 0]
result = prob_goals_given_heading(p1, p2, goals)
print(goals)
print(result)
goals = [(2,0), (0,2), (-2,0), (0,-2)]
correct = [ 0.25, 0.5, 0.25, 0]
result = prob_goals_given_heading(p1, p2, goals)
print(goals)
print(result)
def get_min_viable_path(r, goal, exp_settings):
path_option = construct_single_path_with_angles_spline(exp_settings, r.get_start(), goal, [], fn_export_from_exp_settings(exp_settings))
path_option = chunkify.chunkify_path(r, exp_settings, path_option)
return path_option
def get_min_viable_path_length(r, goal, exp_settings):
path_option = get_min_viable_path(r, goal, exp_settings)
return legib.get_path_length(path_option)[1]
def get_min_direct_path(r, p0, p1, exp_settings):
path_option = [p0, p1]
path_option = chunkify.chunkify_path(r, exp_settings, path_option)
return path_option
def get_legibilities(resto, path, target, goals, obs_sets, f_vis, exp_settings):
vals = {}
f_vis = exp_settings[SETTING_F_VISIBILITY_FUNCTION]
# print("manually: naked")
naked_prob = legib.f_legibility(resto, target, goals, path, [], legib.f_naked, exp_settings)
vals['naked'] = naked_prob
for key in obs_sets.keys():
aud = obs_sets[key]
new_leg = legib.f_legibility(resto, target, goals, path, aud, None, exp_settings)
print(new_leg)
count, new_env, x = legib.f_env(resto, target, goals, path, aud, f_vis, exp_settings)
# count, env_readiness, len(aug_path)
vals[key] = new_leg
vals[key + "-env"] = new_env
return vals
# https://medium.com/@jaems33/understanding-robot-motion-path-smoothing-5970c8363bc4
def smooth_slow(path, weight_data=0.5, weight_smooth=0.1, tolerance=1):
"""
Creates a smooth path for a n-dimensional series of coordinates.
Arguments:
path: List containing coordinates of a path
weight_data: Float, how much weight to update the data (alpha)
weight_smooth: Float, how much weight to smooth the coordinates (beta).
tolerance: Float, how much change per iteration is necessary to keep iterating.
Output:
new: List containing smoothed coordinates.
"""
dims = len(path[0])
new = [[0, 0]] * len(path)
# print(new)
change = tolerance
while change >= tolerance:
change = 0.0
prev_change = change
for i in range(1, len(new) - 1):
for j in range(dims):
x_i = path[i][j]
y_i, y_prev, y_next = new[i][j], new[i - 1][j], new[i + 1][j]
y_i_saved = y_i
y_i += weight_data * (x_i - y_i) + weight_smooth * (y_next + y_prev - (2 * y_i))
new[i][j] = y_i
change += abs(y_i - y_i_saved)
# print(change)
if prev_change == change:
return new
return new
def smoothed(blocky_path, r):
points = []
xys = blocky_path
ts = [t/NUMBER_STEPS for t in range(NUMBER_STEPS + 1)]
bezier = resto.make_bezier(xys)
points = bezier(ts)
points = [(int(px), int(py)) for px, py in points]
return points
return smooth(blocky_path)
return blocky_path
def generate_single_path_grid(restaurant, target, vis_type, n_control):
sample_pts = restaurant.sample_points(n_control, target, vis_type)
path = construct_single_path(restaurant.get_start(), target, sample_pts)
path = smoothed(path, restaurant)
return path
def generate_single_path(restaurant, target, vis_type, n_control):
valid_path = False
while (not valid_path):
sample_pts = restaurant.sample_points(n_control, target, vis_type)
path = construct_single_path_bezier(restaurant.get_start(), target, sample_pts)
valid_path = is_valid_path(restaurant, path)
if (not valid_path):
# print("regenerating")
pass
return path
def at_pt(a, b, tol):
return (abs(a - b) < tol)
def construct_single_path(start, end, sample_pts):
points = [start]
GRID_SIZE = 10
# NUMBER_STEPS should be the final length
# randomly walk there
cx, cy, ctheta = start
targets = sample_pts + [end]
# print(sample_pts)
for target in targets:
tx, ty = resto.to_xy(target)
x_sign, y_sign = 1, 1
if tx < cx:
x_sign = -1
if ty < cy:
y_sign = -1
counter = 0
# print("cx:" + str(cx) + " tx:" + str(tx))
# print("cy:" + str(cy) + " ty:" + str(ty))
# print(not at_pt(cx, tx, GRID_SIZE))
# print(not at_pt(cy, ty, GRID_SIZE))
# Abs status
while not at_pt(cx, tx, GRID_SIZE) or not at_pt(cy, ty, GRID_SIZE):
# print("in loop")
counter = counter + 1
axis = random.randint(0, 1)
if axis == 0 and not at_pt(cx, tx, GRID_SIZE):
cx = cx + (x_sign * GRID_SIZE)
elif not at_pt(cy, ty, GRID_SIZE):
cy = cy + (y_sign * GRID_SIZE)
new_pt = (cx, cy)
points.append(new_pt)
points.append(target)
return points
def get_max_turn_along_path(path):
angle_list = []
is_counting = False
for i in range(len(path) - 4):
p1, p2, p3 = path[i], path[i + 2], path[i + 4]
angle = angle_of_turn([p1, p2], [p2, p3])
print(str((p1, p2, p3)) + "->" + str(angle))
if resto.dist(p1,p2) > 2 or resto.dist(p2, p3) > 2:
angle_list.append(abs(angle))
curvatures.append(angle)
# else:
# print("too short, rejected")
# print(angle_list)
max_curvature = max(angle_list)
# min_curvature = min(angle_list)
# print(max_curvature)
max_curvatures.append(max_curvature)
# print(angle_list.index(max_curvature))
return max_curvature
# def check_curvature(path):
# lx = [x for x,y in path]
# ly = [y for x,y in path]
# #first derivatives
# dx= np.gradient(lx)
# dy = np.gradient(ly)
# #second derivatives
# d2x = np.gradient(dx)
# d2y = np.gradient(dy)
# #calculation of curvature from the typical formula
# curvature = np.abs(dx * d2y - d2x * dy) / (dx * dx + dy * dy)**1.5
# # curvature = curvature[~np.isnan(curvature)]
# curvature = curvature[2:-2]
# print(curvature)
# max_curvature = max(curvature)
# print(max_curvature)
# # curvatures.append(max_curvature)
# return max_curvature
def get_hi_low_of_pts(r):
pt_list = copy.copy(r.get_goals_all())
pt_list.append(r.get_start())
first = pt_list[0]
px, py, ptheta = first
low_x, hi_x = px, px
low_y, hi_y = py, py
for pt in pt_list:
px, py = pt[0], pt[1]
if low_x > px:
low_x = px
if low_y > py:
low_y = py
if hi_x < px:
hi_x = px
if hi_y < py:
hi_y = py
return low_x, hi_x, low_y, hi_y
def is_valid_path(r, path, exp_settings):
return True
tables = r.get_tables()
# print(len(tables))
start = r.get_start()
sx, sy, stheta = start
gx0, gy0, gt0 = r.get_goals_all()[0]
gx1, gy1, gt1 = r.get_goals_all()[1]
# print("sampling central")
low_x, hi_x, low_y, hi_y = get_hi_low_of_pts(r)
for p in path:
if p[0] < start[0] - 2:
# print(p)
return False
line = LineString(path)
if not line.is_simple:
return False
# max_turn = get_max_turn_along_path(path)
# if max_turn >= exp_settings['angle_cutoff']:
# return False
# Checks for table intersection
for t in tables:
if t.intersects_path(path):
return False
BOUND_CHECK_RIGHT = True
right_buffer = exp_settings[SETTING_RIGHT_BOUND]
# Checks for remaining in bounds
for i in range(len(path) - 1):
pt1 = path[i]
pt2 = path[i + 1]
# print((pt1, pt2))
px, py = pt1[0], pt1[1]
if BOUND_CHECK_RIGHT:
if px > hi_x + right_buffer:
return False
if px < 0:
return False
if py < 0:
return False
if px > 1350:
return False
if py > 1000:
return False
return True
def as_tangent(start_angle):
# start_angle is assumed to be in degrees
a = np.deg2rad(start_angle)
dx = np.cos(a)
dy = np.sin(a)
return [dx, dy]
def as_tangent_test(sa):
sa = 90
print(sa)
print(as_tangent(sa))
sa = 0
print(sa)
print(as_tangent(sa))
def path_formatted(xs, ys):
# print(ys)
xs = [int(x) for x in xs]
ys = [int(y) for y in ys]
return list(zip(xs, ys))
def get_pre_goal_pt(goal, exp_settings):
x, y, theta = goal
k = exp_settings[SETTING_ANGLE_STRENGTH]
# print(k)
if theta == resto.DIR_NORTH:
y = y - k
if theta == resto.DIR_SOUTH:
y = y + k
if theta == resto.DIR_EAST:
x = x + k
if theta == resto.DIR_WEST:
x = x - k
return (x, y, theta)
# https://hal.archives-ouvertes.fr/hal-03017566/document
def construct_single_path_with_angles_bspline(exp_settings, start, goal, sample_pts, fn, is_weak=False):
if len(sample_pts) == 0:
return [start, goal]
# return construct_single_path_with_angles_spline(exp_settings, start, goal, sample_pts, fn, is_weak=False)
x, y = [], []
xy_0 = start
xy_n = goal
xy_mid = sample_pts
xy_pre_n = get_pre_goal_pt(goal, exp_settings)
x.append(xy_0[0])
y.append(xy_0[1])
for i in range(len(sample_pts)):
spt = sample_pts[i]
sx = spt[0]
sy = spt[1]
x.append(sx)
y.append(sy)
x.append(xy_pre_n[0])
y.append(xy_pre_n[1])
x.append(xy_n[0])
y.append(xy_n[1])
# Subtract 90 to turn path angle into tangent
start_angle = xy_0[2] - 90
# Do the reverse for the ending point
end_angle = xy_n[2] + 90
# Strength of how much we're enforcing the exit angle
k = exp_settings[SETTING_ANGLE_STRENGTH]
x = np.array(x)
y = np.array(y)
# print(path_formatted(x, y))
tck,u = interpolate.splprep([x,y],s=0)
unew = np.arange(0,1.01,0.01)
out = interpolate.splev(unew,tck)
path = path_formatted(out[0], out[1])
return path
def get_pre_goal_pt_xy(pt, exp_settings):
new_x = pt[0]
new_y = pt[1]
offset = exp_settings[SETTING_WAYPOINT_OFFSET]
if pt[2] == resto.DIR_NORTH:
new_y -= offset
elif pt[2] == resto.DIR_SOUTH:
new_y += offset
return (new_x, new_y)
# https://hal.archives-ouvertes.fr/hal-03017566/document
def construct_single_path_with_angles_spline(exp_settings, start, goal, sample_pts, fn, is_weak=False):
# print("WITH ANGLE")
xy_0 = start
xy_n = goal
xy_mid = sample_pts
x = []
y = []
x.append(xy_0[0])
y.append(xy_0[1])
for i in range(len(sample_pts)):
spt = sample_pts[i]
sx = spt[0]
sy = spt[1]
x.append(sx)
y.append(sy)
# xy_pre_goal = get_pre_goal_pt_xy(goal, exp_settings)
# x.append(xy_pre_goal[0])
# y.append(xy_pre_goal[1])
x.append(xy_n[0])
y.append(xy_n[1])
# print(x)
# print(y)
# exit()
# Subtract 90 to turn path angle into tangent
start_angle = xy_0[2] - 90
# Do the reverse for the ending point
end_angle = xy_n[2] + 90
# Strength of how much we're enforcing the exit angle
k = exp_settings[SETTING_ANGLE_STRENGTH]
if is_weak:
t1 = np.array(as_tangent(start_angle)) * k * .001
else:
t1 = np.array(as_tangent(start_angle)) * k
tn = np.array(as_tangent(end_angle)) * k
# print(type(t1))
# tangent vectors
# print("Tangents")
# print(t1)
# print(tn)
Px=np.concatenate(([t1[0]],x,[tn[0]]))
Py=np.concatenate(([t1[1]],y,[tn[1]]))
# interpolation equations
n = len(x)
phi = np.zeros((n+2,n+2))
for i in range(n):
phi[i+1,i]=1
phi[i+1,i+1]=4
phi[i+1,i+2]=1
# end condition constraints
phi=np.zeros((n+2,n+2))
for i in range(n):
phi[i+1,i] = 1
phi[i+1,i+1] = 4
phi[i+1,i+2] = 1
phi[0,0] = -3
phi[0,2] = 3
phi[n+1,n-1] = -3
phi[n+1,n+1] = 3
# passage matrix
phi_inv = np.linalg.inv(phi)
# control points
Qx=6*phi_inv.dot(Px)
Qy=6*phi_inv.dot(Py)
# figure plot
# plt.figure(figsize=(12, 5))
t=np.linspace(0,1,num=101)
length = 1000
width = 1375
plt.xlim([0, width])
plt.ylim([0, length])
x_all = []
y_all = []
for k in range(0,n-1):
x_t = 1.0/6.0*(((1-t)**3)*Qx[k]+(3*t**3-6*t**2+4)*Qx[k+1]+(-3*t**3+3*t**2+3*t+1)*Qx[k+2]+(t**3)*Qx[k+3])
y_t = 1.0/6.0*(((1-t)**3)*Qy[k]+(3*t**3-6*t**2+4)*Qy[k+1]+(-3*t**3+3*t**2+3*t+1)*Qy[k+2]+(t**3)*Qy[k+3])
x_all.extend(x_t)
y_all.extend(y_t)
if FLAG_EXPORT_SPLINE_DEBUG:
plt.plot(x_t,y_t,'k',linewidth=2.0,color='orange')
print("Saving the path I made lalalala")
plt.plot(x, y, 'ko', label='fit knots',markersize=15.0)
plt.plot(Qx, Qy, 'o--', label='control points',markersize=15.0)
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='upper left', ncol=2)
fn_spline = fn_pathpickle_from_exp_settings(exp_settings) + 'sample-cubic_spline_imposed_tangent_direction.png'
plt.savefig(fn_spline)
# plt.show()
plt.clf()
return path_formatted(x_all, y_all)
def construct_single_path_bezier(start, end, sample_pts):
points = []
xys = [start] + sample_pts + [end]
ts = [t/NUMBER_STEPS for t in range(NUMBER_STEPS + 1)]
bezier = resto.make_bezier(xys)
points = bezier(ts)
points = [(int(px), int(py)) for px, py in points]
return points
def create_path_options(num_paths, target, restaurant, vis_type):
path_list = []
for i in range(num_paths):
path_option = generate_single_path(restaurant, target, vis_type)
path_list.append(path_option)
return path_list
def generate_paths(num_paths, restaurant, vis_types):
path_options = {}
for target in restaurant.get_goals_all():
for vis_type in vis_types:
path_options[target][vis_type] = create_path_options(num_paths, target, restaurant, vis_type)
return path_options
def get_vis_labels():
vis_labels, dummy = get_visibilities([], [], [], [])
return vis_labels
def minMax(x):
return pd.Series(index=['min','max'],data=[x.min(),x.max()])
def determine_lambda(r):
start = r.get_start()
goals = r.get_goals_all()
lambda_val = 0
costs = []
for g in goals:
p = generate_single_path(r, g, None, 0)
p_cost = f_path_cost(p)
costs.append(p_cost)
final_cost = max(costs)
pass
def inspect_heatmap(df):
# print(df)
length = df['x'].max()
width = df['y'].max()
max_multi = df['VIS_MULTI'].max()
max_a = df['VIS_A'].max()
max_b = df['VIS_B'].max()
max_omni = df['VIS_OMNI'].max()
# print((length, width))
print((max_omni, max_multi))
img = np.zeros((length,width), np.uint8)
# df = df.transpose()
for x in range(width):
for y in range(length):
val = df[(df['x'] == x) & (df['y'] == y) ]
v = val['VIS_MULTI']
fill = int(255.0 * (v / max_multi) )
img[x, y] = fill
print(x)
cv2.imwrite('multi_heatmap'+ '.png', img)
# df.at[i,COL_PATHING] = get_pm_label(row)
def inspect_visibility(options, restaurant, ti, fn):
options = options[0]
for pkey in options.keys():
print(pkey)
path = options[pkey][0]
# print('saving fig')
t = range(len(path))
v = get_vis_graph_info(path, restaurant)
# vo, va, vb, vm = v
fig = plt.figure()
ax1 = fig.add_subplot(111)
for key in v.keys():
ax1.scatter(t, v[key], s=10, c='r', marker="o", label=key)
# ax1.scatter(t, va, s=10, c='b', marker="o", label="Vis A")
# ax1.scatter(t, vb, s=10, c='y', marker="o", label="Vis B")
# ax1.scatter(t, vm, s=10, c='g', marker="o", label="Vis Multi")
ax1.set_title('visibility of ' + str(pkey))
plt.legend(loc='upper left');
plt.savefig(fn + "-" + str(ti) + "-" + pkey + '-vis' + '.png')
plt.clf()
# f1 = f_convolved(v1, f_og)
# f2 = f_convolved(v2, f_og)
# f3 = f_convolved(v3, f_og)
# f4 = f_convolved(v4, f_og)
# f5 = f_convolved(v5, f_og)
# fig = plt.figure()
# ax1 = fig.add_subplot(111)
# ax1.scatter(x, f1, s=10, c='b', marker="o", label=vl1)
# ax1.scatter(x, f2, s=10, c='r', marker="o", label=vl2)
# ax1.scatter(x, f3, s=10, c='g', marker="o", label=vl3)
# ax1.scatter(x, f4, s=10, c='y', marker="o", label=vl4)
# ax1.scatter(x, f5, s=10, c='grey', marker="o", label=vl5)
# ax1.set_title('f_remix for best path to goal ' + goal)
# plt.legend(loc='upper left');
def get_vis_graph_info(path, restaurant, exp_settings):
vals_dict = {}
obs_sets = restaurant.get_obs_sets()
for aud_i in obs_sets.keys():
vals = []
for t in range(len(path)):
# goal, goals, path, df_obs
new_val = legib.f_legibility(t, path[t], obs_sets[aud_i], path, exp_settings)
# print(new_val)
# exit()
vals.append(new_val)
vals_dict[aud_i] = vals
return vals_dict
# return vo, va, vb, vm
def inspect_details(detail_dict, fn):
if FLAG_PROB_HEADING:
return
return
vis_labels = get_vis_labels()
vl1 = vis_labels[0]
vl2 = vis_labels[1]
vl3 = vis_labels[2]
vl4 = vis_labels[3]
vl5 = vis_labels[4]
for pkey in detail_dict.keys():
# print('saving fig')
paths_details = detail_dict[pkey]
for detail in paths_details:
v1 = detail[vl1]
v2 = detail[vl2]
v3 = detail[vl3]
v4 = detail[vl4]
v5 = detail[vl5]
goal_index = detail['target_index']
goal = resto.UNITY_GOAL_NAMES[goal_index]
x = range(len(v1))
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(x, v1, s=10, c='b', marker="o", label=vl1)
ax1.scatter(x, v2, s=10, c='r', marker="o", label=vl2)
ax1.scatter(x, v3, s=10, c='g', marker="o", label=vl3)
ax1.scatter(x, v4, s=10, c='y', marker="o", label=vl4)
ax1.scatter(x, v5, s=10, c='grey', marker="o", label=vl5)
ax1.set_title('visibility of best path to goal ' + goal)
plt.legend(loc='upper left');
plt.savefig(fn + 'vis' + '.png')
plt.clf()
f1 = f_convolved(v1, f_og)
f2 = f_convolved(v2, f_og)
f3 = f_convolved(v3, f_og)
f4 = f_convolved(v4, f_og)
f5 = f_convolved(v5, f_og)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(x, f1, s=10, c='b', marker="o", label=vl1)
ax1.scatter(x, f2, s=10, c='r', marker="o", label=vl2)
ax1.scatter(x, f3, s=10, c='g', marker="o", label=vl3)
ax1.scatter(x, f4, s=10, c='y', marker="o", label=vl4)
ax1.scatter(x, f5, s=10, c='grey', marker="o", label=vl5)
ax1.set_title('f_remix for best path to goal ' + goal)
plt.legend(loc='upper left');
plt.savefig(fn + goal + '-' + pkey + '-convolved' + '.png')
plt.clf()
def combine_list_of_dicts(all_options):
new_dict = {}
keys = {}
for option in all_options:
keys = option.keys() | keys
for key in keys:
new_dict[key] = []
for option in all_options:
for key in keys:
new_dict[key].append(option[key])
return new_dict
def get_hardcoded():
start = (104, 477)
end = (1035, 567)
l1 = construct_single_path_bezier(start, end, [(894, 265)])
labels = ['max-lo-fcombo', 'max-la-fcombo', 'max-lb-fcombo', 'max-lm-fcombo']
p1 = [(104, 477), (141, 459), (178, 444), (215, 430), (251, 417), (287, 405), (322, 395), (357, 386), (391, 379), (425, 373), (459, 368), (492, 365), (525, 363), (557, 363), (588, 364), (620, 366), (651, 370), (681, 375), (711, 381), (740, 389), (769, 398), (798, 409), (826, 421), (854, 434), (881, 449), (908, 465), (934, 483), (960, 502), (985, 522), (1010, 543), (1035, 567)]
p2 = [(104, 477), (147, 447), (190, 419), (231, 394), (272, 371), (312, 350), (351, 331), (390, 315), (427, 301), (464, 289), (499, 280), (534, 273), (568, 268), (601, 265), (634, 265), (665, 267), (696, 271), (726, 277), (755, 286), (783, 297), (810, 310), (836, 325), (862, 343), (886, 363), (910, 385), (933, 410), (955, 437), (976, 466), (996, 497), (1016, 531), (1035, 567)]
p3 = [(104, 477), (124, 447), (145, 419), (167, 394), (190, 371), (213, 350), (237, 332), (262, 315), (288, 301), (314, 290), (341, 280), (369, 273), (397, 268), (427, 266), (457, 265), (487, 267), (519, 271), (551, 278), (584, 286), (617, 297), (652, 310), (687, 326), (722, 343), (759, 363), (796, 386), (834, 410), (873, 437), (912, 466), (952, 497), (993, 531), (1035, 567)]
p4 = [(104, 477), (146, 446), (187, 418), (228, 392), (268, 369), (307, 348), (345, 329), (383, 313), (420, 298), (456, 286), (491, 277), (525, 269), (559, 264), (592, 262), (624, 261), (656, 263), (686, 267), (716, 274), (745, 282), (774, 293), (801, 307), (828, 322), (854, 340), (879, 361), (904, 383), (928, 408), (950, 435), (973, 464), (994, 496), (1015, 530), (1035, 567)]
p5 = [(104, 477), (98, 509), (95, 540), (95, 569), (97, 596), (101, 620), (108, 643), (118, 663), (130, 682), (145, 698), (162, 712), (182, 725), (204, 735), (229, 743), (256, 749), (286, 753), (318, 755), (353, 755), (390, 753), (430, 749), (472, 742), (517, 734), (565, 724), (615, 711), (667, 697), (722, 680), (779, 662), (839, 641), (902, 618), (967, 593), (1035, 567)]
# options = {}
# options[labels[0]] = [p5]
# options[labels[1]] = [p1]
# options[labels[2]] = [p2]
# options[labels[3]] = [p3]
# RSS Workshop paper points
options = {}
options[labels[0]] = [p5] # RED
options[labels[1]] = [p3] # YELLOW
options[labels[2]] = [p2] # BLUE
options[labels[3]] = [l1] # GREEN
return options
# remove invalid paths
def trim_paths(r, all_paths_dict, goal, exp_settings, reverse=False):
trimmed_paths = []
trimmed_sp = []
removed_paths = []
all_paths = all_paths_dict['path']
sp = all_paths_dict['sp']
print(len(all_paths))
print(len(sp))
for pi in range(len(all_paths)):
p = all_paths[pi]
is_valid = is_valid_path(r, p, exp_settings)
if is_valid and reverse == False:
trimmed_paths.append(p)
trimmed_sp.append(sp[pi])
elif not is_valid and reverse == True:
trimmed_paths.append(p)
trimmed_sp.append(sp[pi])
if not is_valid and reverse == False:
removed_paths.append(p)
elif is_valid and reverse == True:
removed_paths.append(p)
if reverse == False:
print("Paths trimmed: " + str(len(all_paths)) + " -> " + str(len(trimmed_paths)))
return trimmed_paths, removed_paths, trimmed_sp
def get_mirrored(r, sample_sets):
start = r.get_start()
sx, sy, st = start
mirror_sets = []
# print(path)
for ss in sample_sets:
new_path = []
for p in ss:
new_y_offset = (sy - p[1])
new_y = sy + new_y_offset
new_pt = (p[0], new_y)
new_path.append(new_pt)
mirror_sets.append(new_path)
return mirror_sets
def get_mirrored_path(r, path):
start = r.get_start()
sx, sy, st = start
new_path = []
for p in path:
new_y_offset = (sy - p[1])
new_y = sy + new_y_offset
new_pt = (p[0], new_y)
new_path.append(new_pt)
return new_path
def get_sample_points_sets(r, start, goal, exp_settings):
sample_sets = []
SAMPLE_BUFFER = 150
sampling_type = exp_settings[SETTING_SAMPLING_TYPE]
print(sampling_type)
if sampling_type == SAMPLE_TYPE_SYSTEMATIC or sampling_type == SAMPLE_TYPE_FUSION:
width = r.get_width()
length = r.get_length()
xi_range = range(int(width / resolution))
yi_range = range(int(length / resolution))
for xi in xi_range:
for yi in yi_range:
x = int(resolution * xi)
y = int(resolution * yi)