-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility_legibility.py
1074 lines (805 loc) · 27 KB
/
utility_legibility.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 copy
import decimal
# import numpy as np
import autograd.numpy as np
import math
import pandas as pd
import matplotlib.pylab as plt
import utility_environ_descrip as resto
import utility_path_segmentation as chunkify
# FUNCTIONS FOR CALCULATING FEATURES OF PATHS
# SUCH AS VISIBLIITY, LEGIBILITY, PATH_LENGTH, and ENVELOPE
F_JDIST = 'JDIST'
F_JHEADING_EXPONENTIAL = 'JHEAD_EXPON'
F_JHEADING_QUADRATIC = 'JHEAD_QUADR'
F_JHEADING = 'JHEAD'
F_SUM_DIST_EXPON = 'SUM_DIST_EXPON'
F_MIN_DIST_EXPON = 'MIN_DIST_EXPON'
LEGIBILITY_METHOD = 'l_method'
PROB_INDEX_DIST = 0
PROB_INDEX_HEADING = 1
def f_cost(t1, t2):
a = resto.dist(t1, t2)
# return a
return np.abs(a * a)
def f_path_length(t1, t2):
a = resto.dist(t1, t2)
return a
# return np.abs(a * a)
def f_path_cost(path):
cost = 0
for i in range(len(path) - 1):
cost = cost + f_cost(path[i], path[i + 1])
return cost
def f_og(t, path):
# len(path)
return NUMBER_STEPS - t
# # Given the observers of a given location, in terms of distance and relative heading
# # Ada final equation TODO verify all correct
# def f_vis_single(p, observers):
# # dist_units = 100
# angle_cone = 120.0 / 2
# distance_cutoff = 2000
# # Given a list of entries in the format
# # ((obsx, obsy), angle, distance)
# if len(observers) == 0:
# return 1
# vis = 0
# for obs in observers:
# if obs == None:
# return 0
# else:
# angle, dist = obs.get_obs_to_pt_relationship(p)
# # print((angle, dist))
# if angle < angle_cone and dist < distance_cutoff:
# vis += np.abs(angle_cone - angle)
# # print(vis)
# return vis
# uniform weighting function
def f_naked(t, pt, aud, path):
return decimal.Decimal(1.0)
# Ada final equation
def f_exp_single(t, pt, aud, path):
# if this is the omniscient case, return the original equation
if len(aud) == 0 and path is not None:
return float(60 - t)
# return float(len(path) - t)
elif len(aud) == 0:
# print('ping')
return 1.0
# if in the (x, y) OR (x, y, t) case we can totally
# still run this equation
val = get_visibility_of_pt_w_observers(pt, aud, normalized=False)
return val
# OBSERVER-AWARE LEGIBILITY PAPER ROMAN VERSION
def f_exp_single_normalized(t, pt, aud, path):
# if this is the omniscient case, return the original equation
if len(aud) == 0 and path is not None:
return float(len(path) - t + 1)
# return float(len(path) - t)
elif len(aud) == 0:
# print('ping')
return 1.0
# if in the (x, y) OR (x, y, t) case we can totally
# still run this equation
val = get_visibility_of_pt_w_observers(pt, aud, normalized=True)
return val
# OBSERVER-AWARE LEGIBILITY PAPER ROMAN VERSION
def f_exp_single_normalized_ilqr(t, pt, aud, path):
# if this is the omniscient case, return the original equation
if len(aud) == 0 and path is not None:
return float(len(path) - t + 1)
# return float(len(path) - t)
elif len(aud) == 0:
# print('ping')
return 1.0
# if in the (x, y) OR (x, y, t) case we can totally
# still run this equation
val = get_visibility_of_pt_w_observers_ilqr(pt, aud, normalized=True)
return val
# ADA MASTER VISIBILITY EQUATION
# ILQR OBSERVER-AWARE EQUATION
def get_visibility_of_pt_w_observers_ilqr(pt, aud, normalized=True, epsilon=.01, angle_fov=120):
observers = []
score = []
reasonable_set_sizes = [0, 1, 5]
if len(aud) not in reasonable_set_sizes:
print(len(aud))
# section for alterating calculculation for a few
# out of the whole set; mainly for different combination techniques
# if len(aud) == 5:
# aud = [aud[2], aud[4]]
MAX_DISTANCE = 500
for observer in aud:
obs_orient = observer.get_orientation() #+ 90
# if obs_orient != 300:
# print(obs_orient)
# exit()
obs_FOV = angle_fov #observer.get_FOV()
angle = angle_between_points(observer.get_center(), pt)
distance = resto.dist(pt, observer.get_center())
# print("~~~")
# print(observer.get_center())
# print(distance)
# print(pt)
# print(ang)
a = angle - obs_orient
signed_angle_diff = (a + 180) % 360 - 180
angle_diff = abs(signed_angle_diff)
# if (pt[0] % 100 == 0) and (pt[1] % 100 == 0):
# print(str(pt) + " -> " + str(observer.get_center()) + " = angle " + str(angle))
# print("observer looking at... " + str(obs_orient))
# print("angle diff = " + str(angle_diff))
# print(angle, distance)
# observation = (pt, angle, distance)
# observers.append(observation)
half_fov = (obs_FOV / 2.0)
# print(half )
if angle_diff < half_fov:
from_center = half_fov - angle_diff
if normalized:
from_center = from_center / (half_fov)
# from_center = from_center * from_center
score.append(from_center)
else:
if normalized:
score.append(0)
else:
score.append(1)
# # full credit at the center of view
# offset_multiplier = np.abs(angle_diff) / obs_FOV
# # # 1 if very close
# # distance_bonus = (MAX_DISTANCE - distance) / MAX_DISTANCE
# # score += (distance_bonus*offset_multiplier)
# score = offset_multiplier
# score = distance
# combination method for multiple viewers: minimum value
if len(score) > 0:
# score = min(score)
score = sum(score)
else:
score = epsilon
return score
# ADA MASTER VISIBILITY EQUATION
# OBSERVER-AWARE EQUATION
def get_visibility_of_pt_w_observers(pt, aud, normalized=True):
observers = []
score = []
reasonable_set_sizes = [0, 1, 5]
if len(aud) not in reasonable_set_sizes:
print(len(aud))
# section for alterating calculculation for a few
# out of the whole set; mainly for different combination techniques
# if len(aud) == 5:
# aud = [aud[2], aud[4]]
MAX_DISTANCE = 500
for observer in aud:
obs_orient = observer.get_orientation() + 90
# if obs_orient != 300:
# print(obs_orient)
# exit()
obs_FOV = observer.get_FOV()
angle = angle_between_points(observer.get_center(), pt)
distance = resto.dist(pt, observer.get_center())
# print("~~~")
# print(observer.get_center())
# print(distance)
# print(pt)
# print(ang)
a = angle - obs_orient
signed_angle_diff = (a + 180) % 360 - 180
angle_diff = abs(signed_angle_diff)
# if (pt[0] % 100 == 0) and (pt[1] % 100 == 0):
# print(str(pt) + " -> " + str(observer.get_center()) + " = angle " + str(angle))
# print("observer looking at... " + str(obs_orient))
# print("angle diff = " + str(angle_diff))
# print(angle, distance)
# observation = (pt, angle, distance)
# observers.append(observation)
half_fov = (obs_FOV / 2.0)
# print(half )
if angle_diff < half_fov:
from_center = half_fov - angle_diff
if normalized:
from_center = from_center / (half_fov)
# from_center = from_center * from_center
score.append(from_center)
else:
if normalized:
score.append(0)
else:
score.append(1)
# # full credit at the center of view
# offset_multiplier = np.abs(angle_diff) / obs_FOV
# # # 1 if very close
# # distance_bonus = (MAX_DISTANCE - distance) / MAX_DISTANCE
# # score += (distance_bonus*offset_multiplier)
# score = offset_multiplier
# score = distance
# combination method for multiple viewers: minimum value
if len(score) > 0:
# score = min(score)
score = sum(score)
else:
score = 0
return score
def prob_overall_fuse_signals(probs_array_goal_given_signals, r, p_n, pt, goal, goals, cost_to_here, exp_settings):
COMPONENT_DIST = decimal.Decimal(probs_array_goal_given_signals[PROB_INDEX_DIST])
COMPONENT_HEADING = decimal.Decimal(probs_array_goal_given_signals[PROB_INDEX_HEADING])
legib_method = get_legib_method_from_exp_settings(exp_settings)
if legib_method in [F_JDIST, F_JHEADING, F_JHEADING_QUADRATIC, F_JHEADING_EXPONENTIAL, F_SUM_DIST_EXPON]:
return COMPONENT_DIST + COMPONENT_HEADING
elif legib_method in [F_SUM_DIST_EXPON]:
return (.5 * COMPONENT_DIST) + (.5 * COMPONENT_HEADING)
elif legib_method in [F_MIN_DIST_EXPON]:
if COMPONENT_HEADING < COMPONENT_DIST:
return COMPONENT_HEADING
else:
return COMPONENT_DIST
else:
print(legib_method)
print("ERROR UNRECOGNIZED F FUSION CHOICE")
exit()
# Ada: Final equation
# TODO Cache this result for a given path so far and set of goals
def prob_goal_given_path(r, p_n1, pt, goal, goals, cost_path_to_here, exp_settings, unnorm_prob_function):
entry = []
start = r.get_start()
g_array = []
g_target = 0
for g in goals:
p_raw = unnorm_prob_function(r, p_n1, pt, g, goals, cost_path_to_here, exp_settings)
g_array.append(p_raw)
if g == goal:
print('target val ' + str(p_raw))
g_target = p_raw
if(sum(g_array) == 0):
print("weird g_array")
return decimal.Decimal(1.0)
print(g_array)
return decimal.Decimal(g_target / (sum(g_array)))
# Ada: Heading-aware version of legibility
def unnormalized_prob_goal_given_path_use_heading(r, p_n1, pt, goal, goals, cost_path_to_here, exp_settings):
prob_val = prob_goal_given_heading(r.get_start(), p_n1, pt, goal, goals, cost_path_to_here, exp_settings)
prob_val = decimal.Decimal(prob_val)
if prob_val.is_nan():
prob_val = decimal.Decimal(1.0)
return prob_val
# decimal.getcontext().prec = 60
# is_og = exp_settings['prob_og']
# start = r.get_start()
# if is_og:
# c1 = decimal.Decimal(cost_path_to_here)
# else:
# c1 = decimal.Decimal(get_min_direct_path_cost_between(r, resto.to_xy(r.get_start()), resto.to_xy(pt), exp_settings))
# c2 = decimal.Decimal(get_min_direct_path_cost_between(r, resto.to_xy(pt), resto.to_xy(goal), exp_settings))
# c3 = decimal.Decimal(get_min_direct_path_cost_between(r, resto.to_xy(start), resto.to_xy(goal), exp_settings))
# # print(c2)
# # print(c3)
# a = np.exp((-c1 + -c2))
# b = np.exp(-c3)
# # print(a)
# # print(b)
# ratio = a / b
# if math.isnan(ratio):
# ratio = 0
# return ratio
# Ada: final equation
def unnormalized_prob_goal_given_path(r, p_n1, pt, goal, goals, cost_path_to_here, exp_settings):
decimal.getcontext().prec = 60
if exp_settings is None:
is_og = True
else:
is_og = exp_settings['prob_og']
start = r.get_start()
if is_og:
c1 = decimal.Decimal(cost_path_to_here)
else:
c1 = decimal.Decimal(get_min_direct_path_cost_between(r, resto.to_xy(r.get_start()), resto.to_xy(pt), exp_settings))
c2 = decimal.Decimal(get_min_direct_path_cost_between(r, resto.to_xy(pt), resto.to_xy(goal), exp_settings))
c3 = decimal.Decimal(get_min_direct_path_cost_between(r, resto.to_xy(start), resto.to_xy(goal), exp_settings))
# print(c2)
# print(c3)
a = np.exp((-c1 + -c2))
b = np.exp(-c3)
# print(a)
# print(b)
ratio = a / b
if math.isnan(ratio):
ratio = 0
return ratio
def get_legib_method_from_exp_settings(exp_settings):
if exp_settings is None:
legib_method = F_JDIST
else:
legib_method = exp_settings[LEGIBILITY_METHOD]
return legib_method
# Ada: Final equation
# TODO Cache this result for a given path so far and set of goals
def prob_array_goal_given_signals(r, p_n1, pt, goal, goals, cost_path_to_here, exp_settings):
val_0, val_1 = 0.0, 0.0
legib_method = get_legib_method_from_exp_settings(exp_settings)
# only add the value to the array if it's going to be relevant
if legib_method in get_set_legibility_method_uses_dist():
val_0 = prob_goal_given_path(r, p_n1, pt, goal, goals, cost_path_to_here, exp_settings, unnormalized_prob_goal_given_path)
if legib_method in get_set_legibility_method_uses_heading():
val_1 = prob_goal_given_heading(r, p_n1, pt, goal, goals, cost_path_to_here, exp_settings)
return [val_0, val_1]
def prob_goal_given_heading(start, pn, pt, goal, goals, cost_path_to_here, exp_settings):
g_probs = prob_goals_given_heading(pn, pt, goals, exp_settings)
g_index = goals.index(goal)
return g_probs[g_index]
def f_angle_prob(heading, goal_theta, exp_settings):
diff = np.abs(1.0 / (heading - goal_theta))
if exp_settings[LEGIBILITY_METHOD] == F_JHEADING_QUADRATIC:
return diff * diff
if exp_settings[LEGIBILITY_METHOD] == F_JHEADING_EXPONENTIAL:
return np.exp(diff)
if exp_settings[LEGIBILITY_METHOD] in [F_JDIST]:
print("ERR, wrong legibility function consulting with angle probability function")
return 0
return diff
def prob_goals_given_heading(p0, p1, goals, exp_settings):
# works with
# diff = (np.abs(np.abs(heading - goal_theta) - 180))
# find the heading from start to pt
# start to pt
# TODO theta
heading = resto.angle_between(p0, p1)
# print("heading: " + str(heading))
# return an array of the normalized prob of each goal from this current heading
# for each goal, find the probability this angle is pointing to it
probs = []
for goal in goals:
# 180 = 0
# 0 or 360 = 1
# divide by other options,
# so if 2 in same dir, 50/50 odds
goal_theta = resto.angle_between(p1, goal[:2])
prob = f_angle_prob(heading, goal_theta, exp_settings)
prob = decimal.Decimal(prob)
if prob == decimal.Decimal('Infinity'):
# very large number
prob = 10000
probs.append(prob)
divisor = sum(probs)
# divisor = 1.0
# print(probs)
return np.true_divide(probs, divisor)
# return ratio
def get_costs_along_path(path):
output = []
ci = 0
csf = 0
for pi in range(len(path)):
# print(pi, ci)
# print(path[ci], path[pi])
cst = f_cost(path[ci], path[pi])
csf = csf + cst
log = (path[pi], csf)
ci = pi
output.append(log)
return output
# returns a list of the path length so far at each point
def get_path_length(path):
total = 0
output = [0]
for i in range(len(path) - 1):
link_length = f_path_length(path[i], path[i + 1])
total = total + link_length
output.append(total)
return output, total
# output = []
# ci = 0
# csf = 0
# total = 0
# for pi in range(len(path)):
# cst = f_path_length(path[ci], path[pi])
# total += cst
# ci = pi
# output.append(total) #log
# return output, total
def get_dist(p0, p1):
p0_x, p0_y = p0
p1_x, p1_y = p1
value = (p0_x-p1_x)**2 + (p0_y-p1_y)**2
if value == 0:
return 0
min_distance = np.sqrt(float(value))
return min_distance
def get_min_direct_path_cost_angle_between(r, p0, p1, exp_settings):
# TODO CURRENT ADA
cost = (num_chunks * cost_chunk) + (leftover*leftover)
return cost
def get_min_direct_path_cost_between(r, p0, p1, exp_settings):
dist = get_dist(p0, p1)
if exp_settings is None:
dt = .025
else:
dt = chunkify.get_dt(exp_settings)
cost_chunk = dt * dt
num_chunks = int()
leftover = dist - (dt*num_chunks)
cost = (num_chunks * cost_chunk) + (leftover*leftover)
return cost
# f_path_cost(path_option)
def get_min_direct_path_length(r, p0, p1, exp_settings):
return get_dist(p0, p1)
# OLD LEGIBILITY CODE MADE FOR ILQR
def f_legibility_ilqr(r, goal, goals, path, aud, f_function=None, exp_settings=None):
print("goal is ")
print(goal)
FLAG_is_denominator = True
f_function = f_exp_single_normalized_ilqr
if path is None or len(path) == 0:
return 0
legibility = decimal.Decimal(0)
divisor = decimal.Decimal(0)
total_dist = decimal.Decimal(0)
if exp_settings is not None and 'lambda' in exp_settings and exp_settings['lambda'] != '':
LAMBDA = decimal.Decimal(exp_settings['lambda'])
epsilon = decimal.Decimal(exp_settings['epsilon'])
else:
# TODO verify this
LAMBDA = 1.0
epsilon = 1.0
start = path[0]
total_cost = decimal.Decimal(0)
aug_path = get_costs_along_path(path)
path_length_list, length_of_total_path = get_path_length(path)
length_of_total_path = decimal.Decimal(length_of_total_path)
delta_x = decimal.Decimal(1.0) #length_of_total_path / len(aug_path)
t = 1
p_n = path[0]
divisor = decimal.Decimal(epsilon)
numerator = decimal.Decimal(0.0)
f_log = []
p_log = []
for pt, cost_to_here in aug_path:
f = decimal.Decimal(f_function(t, pt, aud, path))
# Get this probability from all the available signals
probs_array_goal_given_signals = prob_array_goal_given_signals(r, p_n, pt, goal, goals, cost_to_here, exp_settings)
# print("PROBS ARRAY")
# print(probs_array_goal_given_signals)
# combine them according to the exp settings
prob_goal_signals_fused = prob_overall_fuse_signals(probs_array_goal_given_signals, r, p_n, pt, goal, goals, cost_to_here, exp_settings)
# Then do the normal methods of combining them
f_log.append(float(f))
p_log.append(prob_goal_signals_fused)
if len(aud) == 0: # FLAG_is_denominator or
numerator += (prob_goal_signals_fused * f) # * delta_x)
divisor += f #* delta_x
else:
numerator += (prob_goal_signals_fused * f) # * delta_x)
divisor += decimal.Decimal(1.0) #* delta_x
t = t + 1
total_cost += decimal.Decimal(f_cost(p_n, pt))
p_n = pt
if divisor == 0:
legibility = 0
else:
legibility = (numerator / divisor)
total_cost = - decimal.Decimal(LAMBDA)*total_cost
overall = legibility + total_cost
# if len(aud) == 0:
# print(numerator)
# print(divisor)
# print(f_log)
# print(p_log)
# print(legibility)
# print(overall)
# print()
bug_counter = {}
if legibility > 1.0 or legibility < 0:
print("BAD L ==> " + str(legibility))
# r.get_obs_label(aud)
# goal_index = r.get_goal_index(goal)
# category = r.get_obs_label(aud)
# bug_counter[goal_index, category] += 1
elif (legibility == 1):
goal_index = r.get_goal_index(goal)
category = r.get_obs_label(aud)
bug_counter[goal_index, category] += 1
# print(len(aud))
if exp_settings['kill_1'] == True:
overall = 0.0
return legibility #overall
# Given a
def f_legibility(r, goal, goals, path, aud, f_function=None, exp_settings=None):
if f_function is None:
f_function = f_exp_single_normalized
if exp_settings is not None:
FLAG_is_denominator = exp_settings['is_denominator']
else:
FLAG_is_denominator = True
if f_function is None and FLAG_is_denominator:
f_function = f_exp_single
elif f_function is None:
f_function = f_exp_single_normalized
if path is None or len(path) == 0:
return 0
legibility = decimal.Decimal(0)
divisor = decimal.Decimal(0)
total_dist = decimal.Decimal(0)
if exp_settings is not None and 'lambda' in exp_settings and exp_settings['lambda'] != '':
LAMBDA = decimal.Decimal(exp_settings['lambda'])
epsilon = decimal.Decimal(exp_settings['epsilon'])
else:
# TODO verify this
LAMBDA = 1.0
epsilon = 1.0
start = path[0]
total_cost = decimal.Decimal(0)
aug_path = get_costs_along_path(path)
path_length_list, length_of_total_path = get_path_length(path)
length_of_total_path = decimal.Decimal(length_of_total_path)
delta_x = decimal.Decimal(1.0) #length_of_total_path / len(aug_path)
t = 1
p_n = path[0]
divisor = decimal.Decimal(epsilon)
numerator = decimal.Decimal(0.0)
f_log = []
p_log = []
for pt, cost_to_here in aug_path:
f = decimal.Decimal(f_function(t, pt, aud, path))
# Get this probability from all the available signals
probs_array_goal_given_signals = prob_array_goal_given_signals(r, p_n, pt, goal, goals, cost_to_here, exp_settings)
# combine them according to the exp settings
prob_goal_signals_fused = prob_overall_fuse_signals(probs_array_goal_given_signals, r, p_n, pt, goal, goals, cost_to_here, exp_settings)
# Then do the normal methods of combining them
f_log.append(float(f))
p_log.append(prob_goal_signals_fused)
if len(aud) == 0: # FLAG_is_denominator or
numerator += (prob_goal_signals_fused * f) # * delta_x)
divisor += f #* delta_x
else:
numerator += (prob_goal_signals_fused * f) # * delta_x)
divisor += decimal.Decimal(1.0) #* delta_x
t = t + 1
total_cost += decimal.Decimal(f_cost(p_n, pt))
p_n = pt
if divisor == 0:
legibility = 0
else:
legibility = (numerator / divisor)
total_cost = - decimal.Decimal(LAMBDA)*total_cost
overall = legibility + total_cost
# if len(aud) == 0:
# print(numerator)
# print(divisor)
# print(f_log)
# print(p_log)
# print(legibility)
# print(overall)
# print()
if legibility > 1.0 or legibility < 0:
print("BAD L ==> " + str(legibility))
# r.get_obs_label(aud)
goal_index = r.get_goal_index(goal)
category = r.get_obs_label(aud)
bug_counter[goal_index, category] += 1
elif (legibility == 1):
goal_index = r.get_goal_index(goal)
category = r.get_obs_label(aud)
bug_counter[goal_index, category] += 1
# print(len(aud))
if exp_settings['kill_1'] == True:
overall = 0.0
return legibility #overall
# Old version used for RO-MAN paper 2022
def f_legibility_single_input(r, goal, goals, path, aud, f_function=None, exp_settings=None):
if f_function is None:
f_function = f_exp_single_normalized
if exp_settings is not None:
FLAG_is_denominator = exp_settings['is_denominator']
else:
FLAG_is_denominator = True
if path is None or len(path) == 0:
return 0
legibility = decimal.Decimal(0)
divisor = decimal.Decimal(0)
total_dist = decimal.Decimal(0)
if exp_settings is not None and 'lambda' in exp_settings and exp_settings['lambda'] != '':
LAMBDA = decimal.Decimal(exp_settings['lambda'])
epsilon = decimal.Decimal(exp_settings['epsilon'])
else:
# TODO verify this
LAMBDA = 1.0
epsilon = 1.0
start = path[0]
total_cost = decimal.Decimal(0)
aug_path = get_costs_along_path(path)
path_length_list, length_of_total_path = get_path_length(path)
length_of_total_path = decimal.Decimal(length_of_total_path)
delta_x = decimal.Decimal(1.0) #length_of_total_path / len(aug_path)
t = 1
p_n = path[0]
divisor = epsilon
numerator = decimal.Decimal(0.0)
f_log = []
p_log = []
for pt, cost_to_here in aug_path:
f = decimal.Decimal(f_function(t, pt, aud, path))
prob_goal_given = prob_goal_given_path(r, p_n, pt, goal, goals, cost_to_here, exp_settings)
f_log.append(float(f))
p_log.append(prob_goal_given)
if prob_goal_given > 1 or prob_goal_given < 0:
print(prob_goal_given)
print("!!!")
if len(aud) == 0: # FLAG_is_denominator or
numerator += (prob_goal_given * f) # * delta_x)
divisor += f #* delta_x
else:
numerator += (prob_goal_given * f) # * delta_x)
divisor += decimal.Decimal(1.0) #* delta_x
t = t + 1
total_cost += decimal.Decimal(f_cost(p_n, pt))
p_n = pt
if divisor == 0:
legibility = 0
else:
legibility = (numerator / divisor)
total_cost = - LAMBDA*total_cost
overall = legibility + total_cost
# if len(aud) == 0:
# print(numerator)
# print(divisor)
# print(f_log)
# print(p_log)
# print(legibility)
# print(overall)
# print()
if legibility > 1.0 or legibility < 0:
# print("BAD L ==> " + str(legibility))
# r.get_obs_label(aud)
goal_index = r.get_goal_index(goal)
category = r.get_obs_label(aud)
bug_counter[goal_index, category] += 1
elif (legibility == 1):
goal_index = r.get_goal_index(goal)
category = r.get_obs_label(aud)
bug_counter[goal_index, category] += 1
# print(len(aud))
if exp_settings['kill_1'] == True:
overall = 0.0
return overall
# Given a path, count how long it's in sight
def f_env(r, goal, goals, path, aud, f_function, exp_settings):
fov = exp_settings['fov']
FLAG_is_denominator = exp_settings['is_denominator']
if path is None or len(path) == 0:
return 0, 0, 0
if f_function is None and FLAG_is_denominator:
f_function = f_exp_single
elif f_function is None:
f_function = f_exp_single_normalized
if FLAG_is_denominator:
vis_cutoff = 1
else:
half_fov = fov / 2.0
vis_cutoff = 0
count = 0
aug_path = get_costs_along_path(path)
path_length_list, length_of_total_path = get_path_length(path)
length_of_total_path = decimal.Decimal(length_of_total_path)
epsilon = exp_settings['epsilon']
env_readiness = -1
t = 1
p_n = path[0]
for pt, cost_to_here in aug_path:
f = decimal.Decimal(f_function(t, pt, aud, path))
# if f is greater than 0, this indicates being in-view
if f > vis_cutoff:
count += 1
if env_readiness == -1:
env_readiness = (len(aug_path) - t + 1)
# if it's not at least 0, then out of sight, not part of calc
else:
count = 0.0
t += 1
return count, env_readiness, len(aug_path)
def get_costs(path, target, obs_sets):
vals = []
for aud in obs_sets:
new_val = f_cost()
return vals
def angle_between_points(p1, p2):
x1, y1 = p1[:2]
x2, y2 = p2[:2]
angle = np.arctan2(y2 - y1, x2 - x1)
# ang1 = np.arctan2(*p1[::-1])
# ang2 = np.arctan2(*p2[::-1])
return np.rad2deg(angle)
def angle_between_lines(l1, l2):
p1a, p1b = l1
p2a, p2b = l2
a1 = angle_between_points(p1a, p1b)
a2 = angle_between_points(p2a, p2b)
angle = (a1 - a2)
return angle
def angle_of_turn(l1, l2):
return (angle_between_lines(l1, l2))
def get_scenario_key_label(key):
goal = 'TOP'
if key[0] == (1005, 257, 180):
goal = 'BOT'
observer_label = str(key[1])
return goal + "-o" + observer_label
# TODO ada update
def inspect_legibility_of_paths(options, restaurant, exp_settings, fn):
# options = options[0]
print("Inspecting overall legibility")
print(options.keys())
for pkey in options.keys():
print(pkey)
path = options[pkey]
# print('saving fig')
t = range(len(path))
v = get_legib_graph_info(path, restaurant, exp_settings)
# vo, va, vb, vm = v
fig = plt.figure()
ax1 = fig.add_subplot(111)
for key in v.keys():
print("key combo is")
print(key)
# # print(len(t))
# print(len(v[key]))
t = range(len(v[key]))
observer_group = key[1]
ax1.scatter(t, v[key], s=10, marker="o", label=observer_group)
# plt.savefig(fn + "-" + text_label + '-legib' + '.png')
# plt.clf()
# 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")